-
Notifications
You must be signed in to change notification settings - Fork 352
Expand file tree
/
Copy pathhttpbench.cpp
More file actions
153 lines (130 loc) · 4.14 KB
/
Copy pathhttpbench.cpp
File metadata and controls
153 lines (130 loc) · 4.14 KB
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
#include "icy/http/parser.h"
#include "icy/http/request.h"
#include "icy/http/response.h"
#include "benchutil.h"
#include <cstdint>
#include <cstdlib>
#include <iostream>
#include <string>
using namespace icy;
namespace {
http::Request makeRequest()
{
http::Request request(http::Method::Post, "/api/v1/items?limit=10");
request.setHost("127.0.0.1", 8080);
request.setKeepAlive(true);
request.setContentType("application/json");
request.setContentLength(26);
request.add("User-Agent", "icey-bench");
request.add("Accept", "application/json");
return request;
}
http::Response makeResponse()
{
http::Response response(http::StatusCode::OK);
response.setContentType("application/json");
response.setContentLength(27);
response.setKeepAlive(true);
response.add("Server", "icey-bench");
response.add("X-Bench", "1");
return response;
}
const std::string& requestWire()
{
static const std::string wire =
"POST /api/v1/items?limit=10 HTTP/1.1\r\n"
"Host: 127.0.0.1:8080\r\n"
"User-Agent: icey-bench\r\n"
"Accept: application/json\r\n"
"Content-Type: application/json\r\n"
"Content-Length: 26\r\n"
"Connection: keep-alive\r\n"
"\r\n"
"{\"name\":\"icey\",\"count\":42}";
return wire;
}
void benchRequestWrite(bench::Reporter& reporter)
{
const http::Request request = makeRequest();
Buffer buffer;
buffer.reserve(512);
constexpr uint64_t iterations = 200000;
uint64_t bytes = 0;
const auto measurement = bench::measure(reporter.options(), iterations, [&] {
bytes = 0;
for (uint64_t index = 0; index < iterations; ++index) {
buffer.clear();
request.write(buffer);
bytes += buffer.size();
}
});
reporter.add({
.name = "http request write",
.measurement = measurement,
.metrics = {bench::metric("bytes", bytes / iterations)},
});
}
void benchResponseWrite(bench::Reporter& reporter)
{
const http::Response response = makeResponse();
Buffer buffer;
buffer.reserve(512);
constexpr uint64_t iterations = 200000;
uint64_t bytes = 0;
const auto measurement = bench::measure(reporter.options(), iterations, [&] {
bytes = 0;
for (uint64_t index = 0; index < iterations; ++index) {
buffer.clear();
response.write(buffer);
bytes += buffer.size();
}
});
reporter.add({
.name = "http response write",
.measurement = measurement,
.metrics = {bench::metric("bytes", bytes / iterations)},
});
}
void benchRequestResponseCycle(bench::Reporter& reporter)
{
const std::string& wire = requestWire();
const http::Response response = makeResponse();
Buffer responseBuffer;
responseBuffer.reserve(512);
constexpr uint64_t iterations = 100000;
uint64_t bytes = 0;
uint64_t checksum = 0;
const auto measurement = bench::measure(reporter.options(), iterations, [&] {
bytes = 0;
checksum = 0;
for (uint64_t index = 0; index < iterations; ++index) {
http::Request request;
http::Parser parser(&request);
const auto result = parser.parse(wire.data(), wire.size());
if (!result.ok() || !result.messageComplete) {
std::cerr << "httpbench: cycle parse failed\n";
std::exit(1);
}
responseBuffer.clear();
response.write(responseBuffer);
bytes += result.bytesConsumed + responseBuffer.size();
checksum += request.getMethod().size() + request.getURI().size()
+ responseBuffer.size();
}
});
reporter.add({
.name = "http request-response cycle",
.measurement = measurement,
.metrics = {bench::metric("bytes", bytes / iterations),
bench::metric("checksum", checksum)},
});
}
} // namespace
int main(int argc, char** argv)
{
bench::Reporter reporter(argc, argv);
benchRequestWrite(reporter);
benchResponseWrite(reporter);
benchRequestResponseCycle(reporter);
return reporter.finish();
}