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
|
// Copyright © 2010-2012 sss
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include <boost/thread.hpp>
#include <boost/random.hpp>
#include <boost/nondet_random.hpp>
#include <list>
#include <curl/curl.h>
size_t failed_threads = 0, success_threads = 0;
size_t curl_data_callback(void *contents, size_t size, size_t nmemb, void *userp)
{
return size * nmemb;
}
std::string get_random(int length)
{
std::string chars("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890");
std::string data;
try{
boost::random_device rng;
boost::variate_generator<boost::random_device&, boost::uniform_int<> > gen(rng, boost::uniform_int<>(0, chars.length()-1));
for(int i = 0; i < length; ++i)
data += chars[gen()];
}
catch(const std::exception &e)
{
data.clear();
for(int i = 0; i < length; ++i)
data += "a";
return data;
}
return data;
}
class curl_test
{
public:
curl_test(const char* d): s(d), finished(false), thr(NULL){}
void start()
{
thr = new boost::thread(boost::bind(curl_test::test, s, this));
}
~curl_test()
{
delete thr;
}
static void test(const std::string &s, curl_test *c)
{
CURL *curl = NULL;
curl = curl_easy_init();
if(!curl)
return;
std::string url = s;
url += "/";
url += get_random(5);
url += "/";
url += get_random(3);
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_USERAGENT, "tuk-tuk/0.1");
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 2);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 2);
//curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 1);
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 0);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &curl_data_callback);
time_t start = time(0);
CURLcode error = curl_easy_perform(curl);
if(!error)
success_threads++;
double t = time(0) - start;
curl_easy_cleanup(curl);
c->finished = true;
}
const std::string s;
bool finished;
private:
boost::thread *thr;
};
std::list<curl_test> threads;
void threads_count()
{
while(true)
{
std::cout<<"Runninng: "<<threads.size()<<" threads"<<std::endl;
if(failed_threads)
std::cout<<"Failed to start: "<<failed_threads<<" threads"<<std::endl;
std::cout<<"Correct server reply recieved in "<<success_threads<<" threads"<<std::endl;
boost::this_thread::sleep(boost::posix_time::seconds(3));
}
}
int main(int argc, char *argv[])
{
if(argc == 3)
{
new boost::thread(boost::bind(threads_count));
int max_threads = atoi(argv[2]);
for(;;)
{
while(threads.size() >= max_threads)
{
for(std::list<curl_test>::iterator i = threads.begin(), end = threads.end(); i != end; ++i)
{
if(i->finished)
{
threads.erase(i);
i = threads.begin(), end = threads.end();
}
}
boost::this_thread::sleep(boost::posix_time::milliseconds(10));
}
try{
threads.push_back(curl_test(argv[1]));
threads.back().start();
}
catch(const std::exception &e)
{
failed_threads++;
}
}
}
else
std::cout<<"usage: "<<argv[0]<<" base_url threads_count"<<std::endl;
return 0;
}
|