Skip to content

Commit 52f34ca

Browse files
committed
Reduce overheads a bit and show Mops / s
1 parent ebce9f0 commit 52f34ca

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

internals/testing/contention_test.cpp

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
#include "testing_v1/test.hpp"
44

55
#include <algorithm>
6+
#include <chrono>
7+
#include <cstdio>
8+
#include <memory>
69
#include <thread>
710

811
using namespace testing_v1;
@@ -16,23 +19,27 @@ auto contention_test = test([]() {
1619

1720
constexpr size_t n_atoms = 7;
1821

19-
atom<int> atoms[n_atoms];
22+
std::unique_ptr<atom<int>[]> atoms(new atom<int>[n_atoms]);
2023
for (size_t i = 0; i < n_atoms; ++i)
2124
atomically([&]() { atoms[i] = 0; });
2225

26+
auto start = std::chrono::high_resolution_clock::now();
27+
2328
for (size_t t = 0; t < n_threads; ++t) {
24-
std::thread([&]() {
29+
std::thread([&, t]() {
2530
atomically([&]() { n_threads_started.ref() += 1; });
2631
atomically([&]() {
2732
if (n_threads_started != n_threads)
2833
retry();
2934
});
3035

36+
auto s = static_cast<uint32_t>(t);
37+
3138
for (size_t o = 0; o < n_ops; ++o) {
32-
auto i = std::rand() % n_atoms;
39+
auto i = (s = dumpster::ranqd1(s)) % n_atoms;
3340
auto j = i;
3441
while (i == j)
35-
j = std::rand() % n_atoms;
42+
j = (s = dumpster::ranqd1(s)) % n_atoms;
3643

3744
atomically(stack<128>, [&]() {
3845
int &x = atoms[i].ref();
@@ -50,6 +57,15 @@ auto contention_test = test([]() {
5057
retry();
5158
});
5259

60+
std::chrono::duration<double> elapsed =
61+
std::chrono::high_resolution_clock::now() - start;
62+
auto n_total = n_ops * n_threads;
63+
fprintf(stderr,
64+
"%f Mops in %f s = %f Mops/s\n",
65+
n_total / 1000000.0,
66+
elapsed.count(),
67+
n_total / elapsed.count() / 1000000.0);
68+
5369
auto [sum, non_zeroes] = atomically(assume_readonly, [&]() {
5470
int sum = 0;
5571
int non_zeroes = 0;

0 commit comments

Comments
 (0)