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 --- Makefile | 10 ++++ Makefile.mingw32 | 8 ++++ empty | 0 heavy_loader.cpp | 138 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 156 insertions(+) create mode 100644 Makefile create mode 100644 Makefile.mingw32 delete mode 100644 empty create mode 100644 heavy_loader.cpp diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..c1f1b94 --- /dev/null +++ b/Makefile @@ -0,0 +1,10 @@ +all: + g++ -c *.cpp -I/usr/include -O2 -pipe -fomit-frame-pointer -march=core2 + #g++ -c *.cpp -I/usr/include -g3 -ggdb -pipe + g++ -o loader *.o -lboost_thread-mt -lboost_random-mt -lcurl -Wl,-O1 -s + #g++ -o loader *.o -lboost_thread-mt -lboost_random-mt -lcurl +clean: + rm *.o +clean-all: + rm *.o loader + diff --git a/Makefile.mingw32 b/Makefile.mingw32 new file mode 100644 index 0000000..a780243 --- /dev/null +++ b/Makefile.mingw32 @@ -0,0 +1,8 @@ +all: + i686-pc-mingw32-g++ -c *.cpp -I/home/sss/temp/mingw/usr/i686-pc-mingw32/include -O2 -pipe -fomit-frame-pointer -march=i686 -DWIN32 -D_WIN32 -DBOOST_THREAD_USE_LIB + i686-pc-mingw32-g++ -o loader.exe *.o -lssl -lcrypto -lwldap32 -lcurl -lidn -lintl -liconv -lssh2 -lwldap32 -lz -lgnutls -lnettle -lhogweed -lgmp -lz -lssh2 -lgcrypt -lgpg-error -lboost_system-mt -lboost_date_time-mt -lboost_thread_win32-mt -lboost_random-mt -lboost_filesystem-mt -lmswsock -lws2_32 -lgdi32 -lz -Wl,-O1 -s +clean: + rm *.o +clean-all: + rm *.o loader.exe + diff --git a/empty b/empty deleted file mode 100644 index e69de29..0000000 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: "<