X Tutup
Skip to content

Commit 5d5b1de

Browse files
author
Sebastiano Merlino
committed
Added benchmark example
1 parent 81c46c7 commit 5d5b1de

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

examples/benchmark.cpp

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
This file is part of libhttpserver
3+
Copyright (C) 2011, 2012, 2013, 2014, 2015 Sebastiano Merlino
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18+
USA
19+
*/
20+
21+
#include <httpserver.hpp>
22+
#include <iostream>
23+
24+
#if defined(CPU_COUNT) && (CPU_COUNT+0) < 2
25+
#undef CPU_COUNT
26+
#endif
27+
#if !defined(CPU_COUNT)
28+
#define CPU_COUNT 2
29+
#endif
30+
31+
#define NUMBER_OF_THREADS CPU_COUNT
32+
33+
#define PAGE "<html><head><title>libmicrohttpd demo</title></head><body>libhttpserver demo</body></html>"
34+
35+
using namespace httpserver;
36+
37+
class hello_world_resource : public http_resource {
38+
public:
39+
void render(const http_request&, http_response**);
40+
};
41+
42+
//using the render method you are able to catch each type of request you receive
43+
void hello_world_resource::render(const http_request& req, http_response** res)
44+
{
45+
//it is possible to send a response initializing an http_string_response
46+
//that reads the content to send in response from a string.
47+
*res = new http_response(http_response_builder(PAGE, 200).string_response());
48+
}
49+
50+
int main()
51+
{
52+
//it is possible to create a webserver passing a great number of parameters.
53+
//In this case we are just passing the port and the number of thread running.
54+
webserver ws = create_webserver(8080).start_method(http::http_utils::INTERNAL_SELECT).max_threads(NUMBER_OF_THREADS);
55+
56+
hello_world_resource hwr;
57+
//this way we are registering the hello_world_resource to answer for the endpoint
58+
//"/hello". The requested method is called (if the request is a GET we call the render_GET
59+
//method. In case that the specific render method is not implemented, the generic "render"
60+
//method is called.
61+
ws.register_resource("/", &hwr, true);
62+
63+
//This way we are putting the created webserver in listen. We pass true in order to have
64+
//a blocking call; if we want the call to be non-blocking we can just pass false to the
65+
//method.
66+
ws.start(true);
67+
return 0;
68+
}

0 commit comments

Comments
 (0)
X Tutup