Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / ACE / apps / JAWS / stress_testing / http_tester.cpp
blobdc41907a37ef914c75dd68fcaef209757459f594
1 #include "client.h"
3 int Client_Parameters::tcp_nodelay;
4 int Client_Parameters::sockbufsiz;
5 Stats *Client_Parameters::stats;
7 static void *
8 client_thread(void *data)
10 Client_Parameters *cp = (Client_Parameters *) data;
11 float latency = 0, throughput;
12 URL *u = cp->url;
14 // Check for presence of protocol, hostname and filename.
16 if(!(u->get_protocol() && u->get_hostname() && u->get_filename())) {
17 cerr << "Invalid URL" << endl;
18 return 0;
21 cp->stats->i_have_started(cp->id);
23 // Attempt connection
24 connection webserver;
26 if(webserver.connect(u->get_hostname(), cp->tcp_nodelay, cp->sockbufsiz)) return 0;
27 // Send the request now.
30 char request[BUFSIZ];
32 ACE_Profile_Timer throughput_timer, latency_timer;
33 throughput_timer.start();
34 latency_timer.start();
35 ACE_OS::sprintf(request,"GET /%s HTTP/1.0\r\n\r\n",u->get_filename());
36 webserver.write_n(request, ACE_OS::strlen(request)) ;
38 char buffer[BUFSIZ];
39 ssize_t num_read = 0, total_read = 0;
40 unsigned int first_time = 1;
41 for(;;) {
42 num_read = webserver.read(buffer, sizeof buffer);
43 if(first_time) {
44 ACE_Profile_Timer::ACE_Elapsed_Time et;
45 latency_timer.stop();
46 latency_timer.elapsed_time(et);
47 latency = et.real_time;
48 first_time = 0;
50 if(num_read <= 0)
51 break;
52 total_read += num_read;
54 cp->stats->i_am_done(cp->id);
55 ACE_Profile_Timer::ACE_Elapsed_Time et;
56 throughput_timer.stop();
57 throughput_timer.elapsed_time(et);
58 throughput = (8 * total_read/et.real_time) / (1000 * 1000); //pow(10,6) ;
59 cp->stats->log(cp->id, throughput, latency);
60 webserver.close();
61 return 0;
64 int driver(char *id, int total_num, float requests_sec, char *url1, float p1, char *url2, float p2, char *url3, float p3, int tcp_nodelay, int sockbufsiz) {
65 // construct the client parameters packet
67 Client_Parameters::tcp_nodelay = tcp_nodelay;
68 Client_Parameters::sockbufsiz = sockbufsiz;
70 Client_Parameters::stats = new Stats(total_num);
72 int missed_deadlines = 0;
73 // sleep_time is in microseconds, and requests_sec is per second, hence the pow(10,6)
74 float sleep_time = (1/requests_sec) * (1000.0 * 1000.0); // pow(10,6);
75 float delta = 0;
76 ACE_OS::srand(ACE_OS::time(0));
77 for(int i = 0; i < total_num; i++) { // i is used as a id for threads
78 ACE_Profile_Timer timer;
79 if(sleep_time < delta)
81 // cerr << "Requested rate is too high, sleep_time == " << sleep_time << ", and delta = " << delta << ", after " << i << " iterations! " << endl;
82 missed_deadlines++;
84 else
86 ACE_Time_Value tv(0, (long int) (sleep_time - delta));
87 ACE_OS::sleep(tv);
88 timer.start();
90 Client_Parameters *cp = new Client_Parameters(i);
92 double r = ((double)ACE_OS::rand()/(double)RAND_MAX);
93 // cerr << " choosing between " << url1 << url2 << url3 << " with r == " << r;
94 if(r <= p1) cp->url = new URL(url1);
95 if( (r > p1) && (r <= (p1 + p2))) cp->url = new URL(url2);
96 if( (r > (p1 + p2)) && (r <= p1 + p2 + p3)) cp->url = new URL(url3);
97 // cerr << "The URL being requested is " << cp->url->get_filename() << endl;
100 (ACE_Thread_Manager::instance ())->spawn(client_thread, (void *) cp);
101 timer.stop();
102 ACE_Profile_Timer::ACE_Elapsed_Time et;
103 timer.elapsed_time(et);
104 delta = ( (0.4 * fabs(et.real_time * (1000 * 1000))) + (0.6 * delta) ); // pow(10,6)
107 // Join the other threads..
108 (ACE_Thread_Manager::instance ())->wait();
109 // Now output the data for this test
110 cout << id;
111 Client_Parameters::stats->output();
112 cout << endl;
113 if (missed_deadlines != 0)
115 cout << "missed deadlines " << missed_deadlines << endl;
116 cout << "missed deadlines as a % of total requests: " << (float) missed_deadlines / total_num * 100 << endl;
118 return 0;
122 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
124 // This will set the global scale factor if the ACE_SCALE_FACTOR
125 // environment variable is set.
126 ACE_High_Res_Timer::get_env_global_scale_factor ();
128 if(argc < 3) {
129 cerr << "Usage: " << argv[0] << " infile outfile " << endl;
130 cerr << "The input file contains lines, with the following fields: " << endl;
131 cerr << "experiment_id total_number_of_requests request_rate url1 p1 url2 p2 url3 p3 TCP_NODELAY SOCKET_RECV_BUFSIZ " << endl;
133 return 1;
136 FILE *fp = ACE_OS::fopen(argv[1],"r");
137 if(fp == 0) {
138 ACE_POS::perror("fopen");
139 return 2;
141 ACE_OS::close(1);
142 int fd = ACE_OS::open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0644);
143 if(fd == -1) {
144 ACE_OS::perror("open");
145 return 3;
149 int total_num, tcp, sock;
150 char *id = new char[BUFSIZ];
151 float rate, p1, p2, p3;
152 char *url1 = new char[BUFSIZ];
153 char *url2 = new char[BUFSIZ];
154 char *url3 = new char[BUFSIZ];
157 while(!feof(fp)) {
158 fscanf(fp,"%s %d %f %s %f %s %f %s %f %d %d\n", id, &total_num, &rate, url1, &p1, url2, &p2, url3, &p3, &tcp, &sock);
159 if (id[0] == '#') continue;
160 ACE_OS::fprintf(stderr,"----\n");
161 ACE_OS::fprintf(stderr,"\tNow performing experiment:%s\n\tSending %d requests at %f requests/second\n", id, total_num, rate);
162 driver(id, total_num, rate, url1, p1, url2, p2, url3, p3, tcp, sock);
164 ACE_OS::fclose(fp);
165 ACE_OS::close(fd);
166 return 0;