diff options
author | Gluzskiy Alexandr <sss@sss.chaoslab.ru> | 2012-01-18 13:33:14 +0200 |
---|---|---|
committer | Gluzskiy Alexandr <sss@sss.chaoslab.ru> | 2012-01-18 13:33:14 +0200 |
commit | 6609abad442dfa871c373fee50445cfb760136f7 (patch) | |
tree | bf87663a93aabd7973d37935b17cdba4ee3a2f09 | |
parent | d6a0738689f7691d85971baed024b78840c3a680 (diff) |
speed test optimization
-rwxr-xr-x | server/server/config.cpp | 14 | ||||
-rwxr-xr-x | server/server/config.h | 10 | ||||
-rw-r--r-- | server/server/speedtest.cpp | 73 |
3 files changed, 59 insertions, 38 deletions
diff --git a/server/server/config.cpp b/server/server/config.cpp index abfa11d..e7cfe98 100755 --- a/server/server/config.cpp +++ b/server/server/config.cpp @@ -20,7 +20,7 @@ void reload_config_thread_func(config *cfg, boost::mutex *m) } } -void speed_test_thread_func(std::list<config::proxy_entry>* proxy_list, std::list<config::static_proxy_entry> *static_proxy_list, const std::string& test_url, boost::mutex *m) +void speed_test_thread_func(int check_interval, std::list<config::proxy_entry>* proxy_list, std::list<config::static_proxy_entry> *static_proxy_list, const std::string& test_url, boost::mutex *m) { while(!boost::this_thread::interruption_requested()) { @@ -31,18 +31,18 @@ void speed_test_thread_func(std::list<config::proxy_entry>* proxy_list, std::lis speedtest t(test_url); //*l<<i->host<<" "<<(int)t.test(&(*i))<<"\n"; t.test(&(*i)); - boost::this_thread::sleep(boost::posix_time::seconds(2)); + boost::this_thread::sleep(boost::posix_time::seconds(1)); } /* for(std::list<config::proxy_entry>::iterator i = proxy_list->begin(), end = proxy_list->end(); i != end; ++i) { speedtest t(test_url); //*l<<i->host<<" "<<(int)t.test(&(*i))<<"\n"; t.test(&(*i)); - boost::this_thread::sleep(boost::posix_time::seconds(2)); + boost::this_thread::sleep(boost::posix_time::seconds(1)); } */ // delete l; m->unlock(); - boost::this_thread::sleep(boost::posix_time::minutes(180)); + boost::this_thread::sleep(boost::posix_time::seconds(check_interval)); } } @@ -114,7 +114,7 @@ config::config(const char *pth): cfg_reload_thr(NULL), speed_test_thr(NULL) load_upload_list(); load_servers_list(); cfg_reload_thr = new boost::thread(boost::bind(reload_config_thread_func, this, &lists_mutex)); - speed_test_thr = new boost::thread(boost::bind(speed_test_thread_func, &proxy_list, &static_proxy_list, vars.speed_test_file_url, &lists_mutex)); + speed_test_thr = new boost::thread(boost::bind(speed_test_thread_func, vars.proxy_check_interval, &proxy_list, &static_proxy_list, vars.speed_test_file_url, &lists_mutex)); } const int config::get_int(const std::string& data, const char* var, int default_) { @@ -473,7 +473,7 @@ void config::load_deleted_list(char* pth) } } -std::list<config::p_proxy_entry> *config::make_p_proxy_list() +/*std::list<config::p_proxy_entry> *config::make_p_proxy_list() { if(!p_proxy_list.empty()) p_proxy_list.clear(); @@ -511,7 +511,7 @@ std::list<config::p_proxy_entry> *config::make_p_proxy_list() } } return &p_proxy_list; -} +}*/ const int config::ban_time() { diff --git a/server/server/config.h b/server/server/config.h index 86c3988..0f6a695 100755 --- a/server/server/config.h +++ b/server/server/config.h @@ -14,6 +14,7 @@ public: std::string host, login, password, country, state, city; int port; size_t speed; + std::list<size_t> speed_list; proxy_type type; proxy_entry(): port(0), speed(0), type(UNKNOWN) {} @@ -29,7 +30,7 @@ public: type = t; } }; - struct p_proxy_entry +/* struct p_proxy_entry { std::string host, login, password; int port, id, options; @@ -64,12 +65,13 @@ public: { id = 100; } - }; + };*/ struct static_proxy_entry { int port, position; size_t speed; + std::list<size_t> speed_list; std::string host, login, password, label; proxy_type type; static_proxy_entry(): port(0), position(0), speed(0), type(UNKNOWN) @@ -178,7 +180,7 @@ public: std::list<std::string> *get_deleted_list(); std::list<upload_entry> *get_upload_list(); std::list<conn_server> *get_server_list(); - std::list<p_proxy_entry> *make_p_proxy_list(); +// std::list<p_proxy_entry> *make_p_proxy_list(); private: struct cfg_data { @@ -222,7 +224,7 @@ private: std::list<std::string> firewall_list, deleted_list; std::list<upload_entry> upload_list; std::list<conn_server> servers_list; - std::list<p_proxy_entry> p_proxy_list; +// std::list<p_proxy_entry> p_proxy_list; boost::thread *cfg_reload_thr, *speed_test_thr; boost::mutex lists_mutex; }; diff --git a/server/server/speedtest.cpp b/server/server/speedtest.cpp index 9b37b84..1cd2ee7 100644 --- a/server/server/speedtest.cpp +++ b/server/server/speedtest.cpp @@ -1,6 +1,5 @@ #include "headers.h" - size_t speedtest::curl_data_callback(void *contents, size_t size, size_t nmemb, void *userp) //code from curl usage examples, may be slow { size_t realsize = size * nmemb; @@ -21,8 +20,8 @@ size_t speedtest::curl_data_callback(void *contents, size_t size, size_t nmemb, size_t speedtest::test(config::proxy_entry *p) { curl = curl_easy_init(); -/* if(!curl) - ;//handle error */ + if(!curl) + return 0; memory_struct chunk; chunk.memory = (char*)malloc(1); chunk.size = 0; @@ -62,29 +61,39 @@ size_t speedtest::test(config::proxy_entry *p) curl_easy_cleanup(curl); free(chunk.memory); - if(error) + size_t speed = 0; + if(!error) { -// const char *szError = curl_easy_strerror(error); - p->speed = 0; - return 0; + if(chunk.size) + { + if(t) + speed = (double)chunk.size/t; + else + speed = chunk.size; + } } - size_t speed = 0; - if(chunk.size) + p->speed_list.push_back(speed); + while(p->speed_list.size() > 10) + p->speed_list.pop_front(); + if(!p->speed_list.empty()) { - if(t) - speed = (double)chunk.size/t; - else - speed = chunk.size; + if(speed) + { + size_t speed_sum = 0; + for(std::list<size_t>::iterator i = p->speed_list.begin(), end = p->speed_list.end(); i != end; ++i) + speed_sum += *i; + speed = speed_sum/p->speed_list.size(); + } + p->speed = speed; } - p->speed = speed; return speed; } size_t speedtest::test(config::static_proxy_entry *p) { curl = curl_easy_init(); -/* if(!curl) - ;//handle error */ + if(!curl) + return 0; memory_struct chunk; chunk.memory = (char*)malloc(1); chunk.size = 0; @@ -124,20 +133,30 @@ size_t speedtest::test(config::static_proxy_entry *p) curl_easy_cleanup(curl); free(chunk.memory); - if(error) + size_t speed = 0; + if(!error) { -// const char *szError = curl_easy_strerror(error); - p->speed = 0; - return 0; + if(chunk.size) + { + if(t) + speed = (double)chunk.size/t; + else + speed = chunk.size; + } } - size_t speed = 0; - if(chunk.size) + p->speed_list.push_back(speed); + while(p->speed_list.size() > 10) + p->speed_list.pop_front(); + if(!p->speed_list.empty()) { - if(t) - speed = (double)chunk.size/t; - else - speed = chunk.size; + if(speed) + { + size_t speed_sum = 0; + for(std::list<size_t>::iterator i = p->speed_list.begin(), end = p->speed_list.end(); i != end; ++i) + speed_sum += *i; + speed = speed_sum/p->speed_list.size(); + } + p->speed = speed; } - p->speed = speed; return speed; } |