forked from rickydebojeet/http-load-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload_generator.cpp
244 lines (191 loc) · 6.21 KB
/
load_generator.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#include "http_client.hh"
FILE *log_file;
bool done;
int main(int argc, char *argv[])
{
int test_duration, user_count;
float think_time, throughput = 0, response_time = 0;
if (argc != 4)
{
cerr << "Usage " << argv[0] << " user_count think_time test_duration" << endl;
exit(EXIT_FAILURE);
}
user_count = atoi(argv[1]);
think_time = atof(argv[2]);
test_duration = atoi(argv[3]);
cout << "Host: " << HOST << endl
<< "Port: " << PORT << endl
<< "User Count: " << user_count << endl
<< "Think Time: " << think_time << endl
<< "Test Duration: " << test_duration << endl
<< "Testing ..." << endl;
// Opening log file
log_file = fopen("load_gen.log", "w");
fprintf(log_file, "Host: %s\n", HOST);
fprintf(log_file, "Port: %d\n", PORT);
fprintf(log_file, "User Count: %d\n", user_count);
fprintf(log_file, "Think Time: %f\n", think_time);
fprintf(log_file, "Test Duration: %d\n", test_duration);
pthread_t threads[user_count];
struct user_info *info = (user_info *)malloc(sizeof(struct user_info) * user_count);
struct timeval start_time, end_time;
// Starting timer for the load test
gettimeofday(&start_time, NULL);
done = false;
for (int i = 0; i < user_count; i++)
{
// Initializing user info
info[i].user_id = i;
info[i].hostname = HOST;
info[i].portno = PORT;
info[i].cliport = 5000 + i;
info[i].think_time = think_time;
info[i].total_rtt = 0;
// Creating threads
if (pthread_create(&threads[i], NULL, &user_routine, &info[i]) != 0)
{
cerr << "Error: Could not create thread " << i << endl;
exit(EXIT_FAILURE);
}
fprintf(log_file, "Created Thread %d\n", i);
}
// Waiting for test duration
sleep(test_duration);
fprintf(log_file, "Main Thread Woke up!!\n");
gettimeofday(&end_time, NULL);
done = true;
// Waiting for all threads to finish
for (int i = 0; i < user_count; i++)
{
pthread_join(threads[i], NULL);
}
// Calculating average throughput and response time
for (int i = 0; i < user_count; i++)
{
throughput += info[i].total_count;
response_time += (info[i].total_rtt / info[i].total_count);
}
response_time /= user_count;
throughput /= test_duration;
fprintf(log_file, "\n\nAverage Throughput: %f\n", throughput);
fprintf(log_file, "Average Response Time: %f\n", response_time * 1000);
cout << "\n\nAverage Throughput: " << throughput << endl
<< "Average Response Time: " << response_time << endl;
// Closing log file
fclose(log_file);
return 0;
}
void *user_routine(void *args)
{
struct user_info *info = (struct user_info *)args;
int sockfd, portno, n;
char buffer[256];
struct timeval start, end;
struct hostent *server;
struct sockaddr_in serv_addr, cli_addr;
// Get the server address
portno = info->portno;
server = gethostbyname(info->hostname);
if (server == NULL)
{
cerr << "Error, no such host" << endl;
exit(EXIT_FAILURE);
}
// Ready the socket address for connecting
bzero((char *)&serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length);
serv_addr.sin_port = htons(portno);
// clear the address struct for the client socket
bzero((char *)&cli_addr, sizeof(cli_addr));
while (1)
{
gettimeofday(&start, NULL);
// Create a socket
sockfd = socket(AF_INET, SOCK_STREAM, 0);
int x = 1;
if (sockfd < 0)
{
cerr << "Error opening socket" << endl;
exit(EXIT_FAILURE);
}
cli_addr.sin_family = AF_INET;
cli_addr.sin_addr.s_addr = htonl(INADDR_ANY);
cli_addr.sin_port = htons(info->cliport);
int err = 0;
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEPORT, &x, sizeof(x)) < 0)
cerr << "setsockopt(SO_REUSEADDR) failed" << endl;
struct linger l_opt;
l_opt.l_onoff = 1;
l_opt.l_linger = 1;
setsockopt(sockfd, SOL_SOCKET, SO_LINGER, &l_opt, sizeof l_opt);
err = bind(sockfd, (struct sockaddr *) &cli_addr, sizeof(cli_addr));
if (err < 0)
{
cerr << err << endl;
exit(EXIT_FAILURE);
}
// Connect to the server
if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
cerr << "Error connecting" << endl;
exit(EXIT_FAILURE);
}
// cout << "Please enter the url: ";
// bzero(buffer, 256);
// cin >> buffer;
// string url = buffer;
string url = URL;
string request = send_request(url);
n = write(sockfd, request.c_str(), strlen(request.c_str()));
if (n < 0)
{
#if FAULT_EXIT
cerr << "Error writing to socket" << endl;
exit(EXIT_FAILURE);
#endif
}
string response = "";
while (1)
{
bzero(buffer, 256);
n = read(sockfd, buffer, 255);
if (n < 0)
{
#if FAULT_EXIT
cerr << "Error reading from socket" << endl;
exit(EXIT_FAILURE);
#endif
}
else if (n == 0)
{
#if SANITY_CHECK
cout << "Server closed connection" << endl;
#endif
break;
}
response += buffer;
}
#if OUTPUT
cout << "Response: " << response << endl;
#endif
close(sockfd);
gettimeofday(&end, NULL);
float elapsed_time = time_diff(&start, &end);
if (done)
{
break;
}
// Updating user metrics
info->total_rtt += elapsed_time;
info->total_count++;
sleep(info->think_time);
}
fprintf(log_file, "Thread #%d finished\n", info->user_id);
fflush(log_file);
pthread_exit(NULL);
}
float time_diff(struct timeval *t1, struct timeval *t2)
{
return (t2->tv_sec - t1->tv_sec) + (t2->tv_usec - t1->tv_usec) / 1e6;
}