Skip to content

Commit 140e5c0

Browse files
committed
Added example/benchmark.cc
1 parent bd089e9 commit 140e5c0

File tree

3 files changed

+43
-7
lines changed

3 files changed

+43
-7
lines changed

example/Makefile

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11

22
CC = clang++
33
CFLAGS = -std=c++14 -I..
4-
OPENSSL_SUPPORT = -DCPPHTTPLIB_OPENSSL_SUPPORT -I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib -lssl -lcrypto
4+
#OPENSSL_SUPPORT = -DCPPHTTPLIB_OPENSSL_SUPPORT -I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib -lssl -lcrypto
55

6-
all: server client hello simplesvr
6+
all: server client hello simplesvr benchmark
77

8-
server : server.cc ../httplib.h
8+
server : server.cc ../httplib.h Makefile
99
$(CC) -o server $(CFLAGS) server.cc $(OPENSSL_SUPPORT)
1010

11-
client : client.cc ../httplib.h
11+
client : client.cc ../httplib.h Makefile
1212
$(CC) -o client $(CFLAGS) client.cc $(OPENSSL_SUPPORT)
1313

14-
hello : hello.cc ../httplib.h
14+
hello : hello.cc ../httplib.h Makefile
1515
$(CC) -o hello $(CFLAGS) hello.cc $(OPENSSL_SUPPORT)
1616

17-
simplesvr : simplesvr.cc ../httplib.h
17+
simplesvr : simplesvr.cc ../httplib.h Makefile
1818
$(CC) -o simplesvr $(CFLAGS) simplesvr.cc $(OPENSSL_SUPPORT)
1919

20+
benchmark : benchmark.cc ../httplib.h Makefile
21+
$(CC) -o benchmark $(CFLAGS) benchmark.cc $(OPENSSL_SUPPORT)
22+
2023
pem:
2124
openssl genrsa 2048 > key.pem
2225
openssl req -new -key key.pem | openssl x509 -days 3650 -req -signkey key.pem > cert.pem

example/benchmark.cc

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <httplib.h>
2+
#include <chrono>
3+
#include <iostream>
4+
5+
using namespace std;
6+
7+
struct StopWatch {
8+
StopWatch(const string& label) : label_(label) {
9+
start_ = chrono::system_clock::now();
10+
}
11+
~StopWatch() {
12+
auto end = chrono::system_clock::now();
13+
auto diff = end - start_;
14+
auto count = chrono::duration_cast<chrono::milliseconds>(diff).count();
15+
cout << label_ << ": " << count << " millisec." << endl;
16+
}
17+
string label_;
18+
chrono::system_clock::time_point start_;
19+
};
20+
21+
int main(int argc, char* argv[]) {
22+
string body(1024 * 5, 'a');
23+
24+
httplib::Client cli("httpbin.org", 80);
25+
26+
for (int i = 0; i < 3; i++) {
27+
StopWatch sw(to_string(i).c_str());
28+
auto res = cli.post("/post", body, "application/octet-stream");
29+
assert(res->status == 200);
30+
}
31+
32+
return 0;
33+
}

test/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ CFLAGS = -std=c++14 -DGTEST_USE_OWN_TR1_TUPLE -I.. -I.
66
all : test
77
./test
88

9-
test : test.cc ../httplib.h
9+
test : test.cc ../httplib.h Makefile
1010
$(CC) -o test $(CFLAGS) test.cc gtest/gtest-all.cc gtest/gtest_main.cc $(OPENSSL_SUPPORT)
1111

1212
pem:

0 commit comments

Comments
 (0)