From c61a321f3a9bb235970a1bf40ff6b266a30ab2aa Mon Sep 17 00:00:00 2001 From: Gluzskiy Alexandr Date: Tue, 4 Sep 2012 11:39:52 +0300 Subject: added cpu heavy loader test code --- heavy_loader.cpp | 138 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100644 heavy_loader.cpp (limited to 'heavy_loader.cpp') 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 +#include +#include +#include +#include + + + + +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 > 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 threads; + +void threads_count() +{ + while(true) + { + std::cout<<"Runninng: "<= max_threads) + { + for(std::list::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: "<