Currently, I am trying to explore more about libevent server library.
I came across this example†, which I ran with main as:
† Program to run after downloading libevent library
main.cpp
#include <winsock2.h>
#include <windows.h>
#include<stdio.h>
#include <csignal>
#include <condition_variable>
#include "HttpServer.h"
#pragma comment(lib,"WS2_32")
std::mutex m;
std::condition_variable cv;
void signalHandler(int signum)
{
cv.notify_all();
}
void onHello(CHttpRequest *req, void *arg)
{
char *data = "<html><body><center><h1>Hello World </h1></center></body></html>";
req->AddBufferOut(data, strlen(data));
req->SendReply(HTTP_OK);
}
int main(int argc, char** argv)
{
WORD wVersionRequested;
WSADATA wsaData;
wVersionRequested = MAKEWORD(2, 2);
WSAStartup(wVersionRequested, &wsaData);
signal(SIGINT, signalHandler);
signal(SIGTERM, signalHandler);
CHttpServer server("127.0.0.1", 5555, 5); //Create five threads
server.SetCallback("/hello", onHello, NULL);
server.Start();
std::unique_lock<std::mutex> lk(m);
cv.wait(lk);
return 0;
}
5 threads get created in this by passing in CHttpServer server("127.0.0.1", 5555, 5);
. I added a Sleep in void CHttpRequest::AddBufferOut(const char* data, size_t len)
of 5 seconds, as shown below:
evbuffer_add(evhttp_request_get_output_buffer(this->req), data, len);
std::this_thread::sleep_for(std::chrono::milliseconds(5000)); // In place of "evthread_use_pthreads" replaced with "evthread_use_windows_threads"
console text
Thread-id created in Lambda Expression -----> 1696
Thread-id created in Lambda Expression -----> 3008
Thread-id created in Lambda Expression -----> 4464
Thread-id created in Lambda Expression -----> 5516
Thread-id created in Lambda Expression -----> 1652
Sending Reply by Thread-id------> 3008
Sending Reply by Thread-id------> 5516
Sending Reply by Thread-id------> 1696
Sending Reply by Thread-id------> 4464
Sending Reply by Thread-id------> 1652
Sending Reply by Thread-id------> 3008
Sending Reply by Thread-id------> 3008
Sending Reply by Thread-id------> 3008
Sending Reply by Thread-id------> 3008
Sending Reply by Thread-id------> 3008
The first five requests run parallel, but after that all requests get assigned to same thread. This case occurs when the sleep is more than 1 second, and doesn't when it is less one second. Any help would be appreciated. I am using windows 7 VS 2015.
I am passing 10 requests using JMeter all request reach at once. It only prints "Hello world" on web http://127.0.0.1:5555/hello
so time taken is not much. You can share your experience with a similar bug even if it was encountered in different Programs and measures you had taken.