summaryrefslogtreecommitdiff
path: root/heavy_loader.cpp
diff options
context:
space:
mode:
authorGluzskiy Alexandr <sss@sss.chaoslab.ru>2012-09-04 11:39:52 +0300
committerGluzskiy Alexandr <sss@sss.chaoslab.ru>2012-09-04 11:39:52 +0300
commitc61a321f3a9bb235970a1bf40ff6b266a30ab2aa (patch)
treebbb96fb200b246b71fc54409a8da006a62f7c013 /heavy_loader.cpp
parent3ab3d03b8b603f210d878d352b56442eec14f6af (diff)
added cpu heavy loader test code
Diffstat (limited to 'heavy_loader.cpp')
-rw-r--r--heavy_loader.cpp138
1 files changed, 138 insertions, 0 deletions
diff --git a/heavy_loader.cpp b/heavy_loader.cpp
new file mode 100644
index 0000000..323f470
--- /dev/null
+++ b/heavy_loader.cpp
@@ -0,0 +1,138 @@
+#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;
+}