Skip to content

Commit

Permalink
fix: use monotonic time for throughput example
Browse files Browse the repository at this point in the history
  • Loading branch information
jean-roland committed Dec 19, 2023
1 parent e772c64 commit a49c9f6
Showing 1 changed file with 19 additions and 20 deletions.
39 changes: 19 additions & 20 deletions examples/unix/c11/z_sub_thr.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@

#include "zenoh-pico.h"

#define N 1000000
#define PACKET_NB 1000000

typedef struct {
volatile unsigned long count;

Check warning

Code scanning / Cppcheck (reported by Codacy)

struct member 'z_stats_t::count' is never used. Warning

struct member 'z_stats_t::count' is never used.
volatile unsigned long finished_rounds;

Check warning

Code scanning / Cppcheck (reported by Codacy)

struct member 'z_stats_t::finished_rounds' is never used. Warning

struct member 'z_stats_t::finished_rounds' is never used.
volatile clock_t start;
volatile clock_t stop;
volatile clock_t first_start;
z_clock_t start;
z_clock_t first_start;
} z_stats_t;

#if Z_FEATURE_SUBSCRIPTION == 1

Check warning

Code scanning / Cppcheck (reported by Codacy)

misra violation 2009 with no text in the supplied rule-texts-file Warning

misra violation 2009 with no text in the supplied rule-texts-file
Expand All @@ -35,36 +34,36 @@ z_stats_t *z_stats_make(void) {
z_stats_t *stats = malloc(sizeof(z_stats_t));

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 11.5 rule Note

MISRA 11.5 rule

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 21.3 rule Note

MISRA 21.3 rule
stats->count = 0;
stats->finished_rounds = 0;
stats->first_start = 0;
stats->first_start.tv_nsec = 0;
return stats;
}

void on_sample(const z_sample_t *sample, void *context) {

Check warning

Code scanning / Cppcheck (reported by Codacy)

misra violation 804 with no text in the supplied rule-texts-file Warning

misra violation 804 with no text in the supplied rule-texts-file
(void)sample;
z_stats_t *stats = (z_stats_t *)context;

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 11.5 rule Note

MISRA 11.5 rule
if (stats->count == 0) {
stats->start = clock();
if (!stats->first_start) {
stats->count++;
// Start set measurement
if (stats->count == 1) {

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 10.4 rule Note

MISRA 10.4 rule
stats->start = z_clock_now();
if (stats->first_start.tv_nsec == 0) {
stats->first_start = stats->start;
}
stats->count++;
} else if (stats->count < N) {
stats->count++;
} else {
stats->stop = clock();
} else if (stats->count >= PACKET_NB) {

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 10.4 rule Note

MISRA 10.4 rule
// Stop set measurement
stats->finished_rounds++;
printf("%f msg/s\n", N * (double)CLOCKS_PER_SEC / (double)(stats->stop - stats->start));
unsigned long elapsed_ms = z_clock_elapsed_ms(&stats->start);
printf("Received %d msg in %lu ms (%.1f msg/s)\n", PACKET_NB, elapsed_ms,

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 17.7 rule Note

MISRA 17.7 rule
(double)(PACKET_NB * 1000 / elapsed_ms));

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 10.4 rule Note

MISRA 10.4 rule
stats->count = 0;
}

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 15.7 rule Note

MISRA 15.7 rule
}

void drop_stats(void *context) {

Check warning

Code scanning / Cppcheck (reported by Codacy)

misra violation 804 with no text in the supplied rule-texts-file Warning

misra violation 804 with no text in the supplied rule-texts-file
const clock_t end = clock();
const z_stats_t *stats = (z_stats_t *)context;
const double elapsed = (double)(end - stats->first_start) / (double)CLOCKS_PER_SEC;
const unsigned long sent_messages = N * stats->finished_rounds + stats->count;
printf("Stats being dropped after unsubscribing: sent %ld messages over %f seconds (%f msg/s)\n", sent_messages,
elapsed, (double)sent_messages / elapsed);
z_stats_t *stats = (z_stats_t *)context;

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 11.5 rule Note

MISRA 11.5 rule
unsigned long elapsed_ms = z_clock_elapsed_ms(&stats->first_start);
const unsigned long sent_messages = PACKET_NB * stats->finished_rounds + stats->count;

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 12.1 rule Note

MISRA 12.1 rule

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 10.4 rule Note

MISRA 10.4 rule
printf("Stats after unsubscribing: received %ld messages over %lu miliseconds (%.1f msg/s)\n", sent_messages,

Check notice

Code scanning / Cppcheck (reported by Codacy)

%ld in format string (no. 1) requires 'long' but the argument type is 'unsigned long'. Note

%ld in format string (no. 1) requires 'long' but the argument type is 'unsigned long'.

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 17.7 rule Note

MISRA 17.7 rule
elapsed_ms, (double)(sent_messages * 1000 / elapsed_ms));

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 10.4 rule Note

MISRA 10.4 rule

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 10.8 rule Note

MISRA 10.8 rule
free(context);

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 21.3 rule Note

MISRA 21.3 rule
}

Expand Down

0 comments on commit a49c9f6

Please sign in to comment.