forked from eunomia-bpf/bpftime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.c
65 lines (56 loc) · 1.39 KB
/
test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <stdint.h>
// The timespec struct holds seconds and nanoseconds
struct timespec start_time, end_time;
void start_timer()
{
clock_gettime(CLOCK_MONOTONIC_RAW, &start_time);
}
void end_timer()
{
clock_gettime(CLOCK_MONOTONIC_RAW, &end_time);
}
__attribute_noinline__ uint64_t __benchmark_test_function3(const char *a, int b,
uint64_t c)
{
return a[b] + c;
}
static double get_elapsed_time()
{
long seconds = end_time.tv_sec - start_time.tv_sec;
long nanoseconds = end_time.tv_nsec - start_time.tv_nsec;
if (start_time.tv_nsec > end_time.tv_nsec) { // clock underflow
--seconds;
nanoseconds += 1000000000;
}
printf("Elapsed time: %ld.%09ld seconds\n", seconds, nanoseconds);
return seconds * 1.0 + nanoseconds / 1000000000.0;
}
static double get_function_time(int iter)
{
start_timer();
// test base line
for (int i = 0; i < iter; i++) {
__benchmark_test_function3("hello", i % 4, i);
}
end_timer();
double time = get_elapsed_time();
return time;
}
void do_benchmark_userspace(int iter)
{
double base_line_time, after_hook_time, total_time;
printf("a[b] + c for %d times\n", iter);
base_line_time = get_function_time(iter);
printf("avg function elapse time: %lf ns\n\n",
(base_line_time) / iter * 1000000000.0);
}
int main()
{
puts("");
int iter = 100 * 1000;
do_benchmark_userspace(iter);
return 0;
}