Skip to content

Commit 191ef4c

Browse files
authored
Merge pull request #91 from jean-roland/fix_thr_test
Use wall time in throughput test
2 parents 7a987d8 + cc3c5ec commit 191ef4c

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

examples/universal/z_sub_thr.cxx

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// ZettaScale Zenoh Team, <[email protected]>
1313
//
1414
#include <stdio.h>
15-
#include <time.h>
15+
#include <chrono>
1616

1717
#include "../getargs.h"
1818
#include "zenoh.hxx"
@@ -23,35 +23,37 @@ using namespace zenoh;
2323
struct Stats {
2424
volatile unsigned long count = 0;
2525
volatile unsigned long finished_rounds = 0;
26-
volatile clock_t start = 0;
27-
volatile clock_t stop = 0;
28-
volatile clock_t end = 0;
29-
volatile clock_t first_start = 0;
26+
std::chrono::steady_clock::time_point start = {};
27+
std::chrono::steady_clock::time_point first_start = {};
28+
std::chrono::steady_clock::time_point end = {};
3029

3130
void operator()(const Sample &) {
3231
if (count == 0) {
33-
start = clock();
34-
if (!first_start) {
32+
start = std::chrono::steady_clock::now();
33+
if (first_start == std::chrono::steady_clock::time_point()) {
3534
first_start = start;
3635
}
3736
count++;
3837
} else if (count < N) {
3938
count++;
4039
} else {
41-
stop = clock();
4240
finished_rounds++;
43-
printf("%f msg/s\n", N * (double)CLOCKS_PER_SEC / (double)(stop - start));
41+
auto elapsed_us =
42+
std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now() - start).count();
43+
std::cout << static_cast<double>(N) * 1000000.0 / static_cast<double>(elapsed_us) << " msg/s\n";
4444
count = 0;
4545
}
4646
}
4747

48-
void operator()() { end = clock(); }
48+
void operator()() { end = std::chrono::steady_clock::now(); }
4949

5050
void print() const {
51-
const double elapsed = (double)(end - first_start) / (double)CLOCKS_PER_SEC;
51+
auto elapsed_s =
52+
static_cast<double>(std::chrono::duration_cast<std::chrono::microseconds>(end - first_start).count()) /
53+
1000000.0;
5254
const unsigned long sent_messages = N * finished_rounds + count;
53-
printf("Sent %ld messages over %f seconds (%f msg/s)\n", sent_messages, elapsed,
54-
(double)sent_messages / elapsed);
55+
std::cout << "Sent " << sent_messages << " messages over " << elapsed_s << " seconds ("
56+
<< static_cast<double>(sent_messages) / elapsed_s << " msg/s)\n";
5557
}
5658
};
5759

0 commit comments

Comments
 (0)