-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.cpp
204 lines (165 loc) · 6.09 KB
/
tests.cpp
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include "HttpRouter.hpp"
#include <iostream>
#include <chrono>
static inline uint64_t rdtsc() {
uint32_t lo, hi;
asm volatile ("rdtsc" : "=a" (lo), "=d" (hi));
return ((uint64_t)hi << 32) | lo;
}
static
void calculate_cpu_clock_speed() {
uint64_t start, end;
std::chrono::duration<double> elapsed;
// Get TSC and time at start
start = rdtsc();
auto start_time = std::chrono::high_resolution_clock::now();
// Busy-wait loop (~1 ms)
do {
auto end_time = std::chrono::high_resolution_clock::now();
elapsed = end_time - start_time;
} while (elapsed.count() < 0.001);
end = rdtsc();
double fcpu_hz = (end - start) / elapsed.count();
double fcpu_ghz = fcpu_hz / 1e9;
std::cout << "CPU freq: " << fcpu_ghz << " GHz\n";
}
void benchmark_routes() {
struct user_data {
int routed = 0;
} userData;
http_router<user_data *> r;
// set up a few routes
r.add("GET", "/service/candy/:kind", [](user_data *user,
std::vector<string_view> &args) {
user->routed++;
});
r.add("GET", "/service/shutdown", [](user_data *user,
std::vector<string_view> &args) {
user->routed++;
});
r.add("GET", "/", [](user_data *user,
std::vector<string_view> &args) {
user->routed++;
});
r.add("GET", "/:filename", [](user_data *user,
std::vector<string_view> &args) {
user->routed++;
});
// run benchmark of various urls
std::vector<std::string> test_urls = {
"/service/candy/lollipop",
"/service/candy/gum",
"/service/candy/seg_råtta",
"/service/candy/lakrits",
"/service/shutdown",
"/",
"/some_file.html",
"/another_file.jpeg"
};
for (std::string &test_url : test_urls) {
auto start = std::chrono::high_resolution_clock::now();
for (int i = 0; i < 10000000; i++) {
r.route("GET", 3, test_url.data(), test_url.length(), &userData);
}
auto stop = std::chrono::high_resolution_clock::now();
unsigned int ms = std::chrono::duration_cast<std::chrono::milliseconds>(stop - start).count();
std::cout << "[" << 10000.0 / ms << " million req/sec] for URL: " << test_url << std::endl;
}
std::cout << "Checksum: " << userData.routed << std::endl << std::endl;
}
void demo_routes() {
struct user_data {
// pass whatever you need as user data
} userData;
http_router<user_data *> r;
// set up a few routes
r.add(std::string("GET"), "/service/candy/:kind", [](user_data *user,
std::vector<string_view> &args) {
std::cout << "Now serving candy of kind " << args[0] << std::endl;
});
r.add(std::string("GET"), "/service/shutdown", [](user_data *user,
std::vector<string_view> &args) {
std::cout << "Shutting down now" << std::endl;
});
r.add("GET", "/", [](user_data *user,
std::vector<string_view> &args) {
std::cout << "Serving index now" << std::endl;
});
r.add("GET", "/:filename", [](user_data *user,
std::vector<string_view> &args) {
std::cout << "Serving file: " << args[0] << std::endl;
});
r.add("GET", "/:page/:username", [](user_data *user,
std::vector<string_view> &args) {
std::cout << "Serving page: " << args[0] << " username: " << args[1] << std::endl;
});
r.add("GET", "/service/:kind/dash/:type", [](user_data *user,
std::vector<string_view> &args) {
std::cout << "Serving kind: " << args[0] << " type: " << args[1] << std::endl;
});
r.add("GET", "/service/:name", [](user_data *user,
std::vector<string_view> &args) {
std::cout << "Now serving unknown service name: " << args[0] << std::endl;
});
r.add("GET", "/service/:name/query/:querystr", [](user_data *user,
std::vector<string_view> &args) {
std::cout << "Serving sevice name: " << args[0] << " query: " << args[1] << std::endl;
});
r.add("GET", "/service/*/logs", [](user_data *user,
std::vector<string_view> &args) {
std::cout << "Now serving logs" << std::endl;
});
r.add("GET", "/foo/bar/:arg/baz", [](user_data *user,
std::vector<string_view> &args) {
std::cout << "Serving foobar with baz arg: " << args[0] << std::endl;
});
r.add("GET", "/foo/bar/:arg", [](user_data *user,
std::vector<string_view> &args) {
std::cout << "Serving foobar arg: " << args[0] << std::endl;
});
/* Should be of lower priority */
r.add("GET", "/:name/known", [](user_data *user,
std::vector<string_view> &args) {
std::cout << "Place name: " << args[0] << std::endl;
});
/* Should be of higher priority becuase there is match before variable */
r.add("GET", "/someplace/:name", [](user_data *user,
std::vector<string_view> &args) {
std::cout << "Some place service name: " << args[0] << std::endl;
});
// run benchmark of various urls
std::vector<std::string> test_urls = {
"/service/candy/lollipop",
"/service/candy/gum",
"/service/candy/seg_råtta",
"/service/candy/lakrits",
"/service/shutdown",
"/",
"/some_file.html",
"/another_file.jpeg",
"/1/admin/",
"/service/cheese/dash/mozarella",
"/service/cheese/query/name/",
"/service/mail/logs",
"/service/upkeep/logs/?time=1732666926",
"/foo/bar/111/baz",
"/foo/bar/222",
"/service/unknown",
"/someplace/somewhere/unknown",
"/someplace/known"
};
for (std::string &test_url : test_urls) {
std::cout << "[" << test_url << "]" << std::endl;
r.route("GET", 3, test_url.data(), test_url.length(), &userData);
}
}
int main(int argc, char *argv[]) {
calculate_cpu_clock_speed();
std::cout << "\nDemo\n\n";
demo_routes();
if (argc > 1) {
std::cout << "\nBenchmark\n\n";
benchmark_routes();
}
return 0;
}