libtopo/test/test.c

104 lines
1.8 KiB
C
Raw Normal View History

2022-06-10 11:29:01 +00:00
#include <stdint.h>
2022-05-26 16:36:03 +00:00
#include <unistd.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include "topo.h"
#define S2NS (1000000000UL)
static inline uint64_t
get_uptime()
{
struct timespec tp;
clock_gettime(CLOCK_MONOTONIC, &tp);
2022-06-10 11:29:01 +00:00
return ((uint64_t)tp.tv_sec * S2NS + (uint64_t)tp.tv_nsec);
2022-05-26 16:36:03 +00:00
}
void test(const char * case_name, struct timespec * ts)
{
uint64_t slow;
uint64_t fast;
slow = get_uptime();
if (nanosleep(ts, NULL) != 0) {
perror("nanosleep() interrupted!");
exit(-1);
}
slow = get_uptime() - slow;
fast = topo_uptime_ns();
if (nanosleep(ts, NULL) != 0) {
perror("nanosleep() interrupted!");
exit(-1);
}
fast = topo_uptime_ns() - fast;
printf("%s: clock_gettime(): %lu, topo_uptime_ns(): %lu, %% diff: %.2f%%\n", case_name, slow, fast, ((float)fast - (float)slow) / (float)slow * 100);
}
int ts_test()
{
struct timespec ts;
// 1s
ts.tv_nsec = 0;
ts.tv_sec = 1;
test("1s", &ts);
// 100ms
ts.tv_nsec = 100000000;
ts.tv_sec = 0;
test("100ms", &ts);
// 10ms
ts.tv_nsec = 10000000;
ts.tv_sec = 0;
test("10ms", &ts);
// 1ms
ts.tv_nsec = 1000000;
ts.tv_sec = 0;
test("1ms", &ts);
// 100us
ts.tv_nsec = 100000;
ts.tv_sec = 0;
test("100us", &ts);
// 10us
ts.tv_nsec = 10000;
ts.tv_sec = 0;
test("10us", &ts);
// 1us
ts.tv_nsec = 1000;
ts.tv_sec = 0;
test("1us", &ts);
// 100ns
ts.tv_nsec = 100;
ts.tv_sec = 0;
test("100ns", &ts);
// 10ns
ts.tv_nsec = 10;
ts.tv_sec = 0;
test("10ns", &ts);
// 1ns
ts.tv_nsec = 1;
ts.tv_sec = 0;
test("1ns", &ts);
return 0;
}
int main()
{
2022-06-10 11:29:01 +00:00
topo_init(1);
topo_init(1);
2022-05-26 16:36:03 +00:00
ts_test();
return 0;
2022-06-10 11:29:01 +00:00
}