@@ -42,6 +42,60 @@ static void destroy_gif_writer(void *p)
42
42
free (p );
43
43
}
44
44
45
+ static struct timespec current_precise_monotonic_timespec (void )
46
+ {
47
+ struct timespec now ;
48
+
49
+ if (UNLIKELY (clock_gettime (CLOCK_MONOTONIC , & now ) < 0 )) {
50
+ lwan_status_perror ("clock_gettime" );
51
+ return (struct timespec ){};
52
+ }
53
+
54
+ return now ;
55
+ }
56
+
57
+ static double elapsed_time_ms (const struct timespec then )
58
+ {
59
+ const struct timespec now = current_precise_monotonic_timespec ();
60
+ struct timespec diff = {
61
+ .tv_sec = now .tv_sec - then .tv_sec ,
62
+ .tv_nsec = now .tv_nsec - then .tv_nsec ,
63
+ };
64
+
65
+ if (diff .tv_nsec < 0 ) {
66
+ diff .tv_sec -- ;
67
+ diff .tv_nsec += 1000000000l ;
68
+ }
69
+
70
+ return (double )diff .tv_sec / 1000.0 + (double )diff .tv_nsec / 1000000.0 ;
71
+ }
72
+
73
+ LWAN_HANDLER_ROUTE (benchmark , "/benchmark" )
74
+ {
75
+ struct forth_ctx * f = forth_new ();
76
+ coro_defer (request -> conn -> coro , destroy_forth_ctx , f );
77
+
78
+ if (!forth_parse_string (f , twister ))
79
+ return HTTP_INTERNAL_ERROR ;
80
+
81
+ struct timespec before = current_precise_monotonic_timespec ();
82
+ for (int i = 0 ; i < 100000 ; i ++ ) {
83
+ struct forth_vars vars = {
84
+ .x = i / 64. ,
85
+ .y = i / 64. ,
86
+ .t = 0 ,
87
+ };
88
+ if (!forth_run (f , & vars ))
89
+ return HTTP_INTERNAL_ERROR ;
90
+ }
91
+
92
+ response -> mime_type = "text/plain" ;
93
+ lwan_strbuf_printf (response -> buffer , "elapsed time: %lfms" ,
94
+ elapsed_time_ms (before ));
95
+
96
+ return HTTP_OK ;
97
+ }
98
+
45
99
LWAN_HANDLER_ROUTE (twister , "/" )
46
100
{
47
101
struct forth_ctx * f = forth_new ();
0 commit comments