gusucode.com > 它是一个c_s网络编程框架源码程序 > st_asio_wrapper/performance_test/test_client/test_client.cpp
#include <boost/tokenizer.hpp> //configuration #define SERVER_PORT 9527 //configuration #include "../../include/st_asio_wrapper_test_client.h" using namespace st_asio_wrapper; #ifdef _MSC_VER #define atoll _atoi64 #endif #define QUIT_COMMAND "quit" class my_test_client : public st_test_client { public: class my_test_socket : public st_test_client::test_socket { public: my_test_socket(st_test_client& st_test_client_) : test_socket(st_test_client_) {} protected: virtual bool check_msg(const std::string& str) { if (str.size() >= sizeof(size_t) && recv_index == *(size_t*) str.data()) return true; else { printf("check msg error: %ld.", recv_index); return false; } } }; protected: virtual boost::shared_ptr<test_socket> create_client() {return boost::shared_ptr<test_socket>(new my_test_socket(*this));} }; int main(int argc, const char* argv[]) { /////////////////////////////////////////////////////////// puts("usage: test_client link_num"); size_t link_num = 16; if (argc > 1) link_num = std::min((size_t) 4096, std::max((size_t) atoll(argv[1]), (size_t) 1)); printf("exec: test_client %lu\n", link_num); /////////////////////////////////////////////////////////// std::string str; my_test_client client; client.set_link_num(link_num); // client.set_server_addr(SERVER_PORT, "::1"); //ipv6 // client.set_server_addr(SERVER_PORT, "127.0.0.1"); //ipv4 client.start_service(); while(client.is_running()) { std::getline(std::cin, str); if (str == QUIT_COMMAND) client.stop_service(); else if (!str.empty()) { size_t msg_num = 1024; size_t msg_len = 1024; //must greater than or equal to sizeof(size_t) char msg_fill = '0'; char model = 0; //0 broadcast, 1 randomly pick one link per msg char_separator<char> sep(" \t"); tokenizer<char_separator<char>> tok(str, sep); auto iter = begin(tok); if (iter != end(tok)) msg_num = std::max((size_t) atoll(iter++->data()), (size_t) 1); if (iter != end(tok)) msg_len = std::min((size_t) (MAX_MSG_LEN - HEAD_LEN), std::max((size_t) atoll(iter++->data()), sizeof(size_t))); //include seq if (iter != end(tok)) msg_fill = *iter++->data(); if (iter != end(tok)) model = *iter++->data() - '0'; unsigned percent = 0; size_t total_msg_bytes; switch (model) { case 0: total_msg_bytes = msg_num * link_num; break; case 1: total_msg_bytes = msg_num; break; default: total_msg_bytes = 0; break; } if (total_msg_bytes > 0) { printf("test parameters after adjustment: %ld %lu %c %d\n", msg_num, msg_len, msg_fill, model); puts("performance test begin, this application will has no response during the test!"); client.reset(); total_msg_bytes *= msg_len; auto begin_time = get_system_time().time_of_day().total_seconds(); auto buff = new char[msg_len]; memset(buff, msg_fill, msg_len); size_t send_bytes = 0; for (size_t i = 0; i < msg_num; ++i) { memcpy(buff, &i, sizeof(size_t)); //seq switch (model) { case 0: client.broadcast_msg(buff, msg_len); send_bytes += link_num * msg_len; break; case 1: client.random_send_msg(buff, msg_len); send_bytes += msg_len; break; default: break; } unsigned new_percent = (unsigned) (100 * send_bytes / total_msg_bytes); if (percent != new_percent) { percent = new_percent; printf("\r%u%%", percent); fflush(stdout); } } delete[] buff; while(client.get_total_recv_bytes() != total_msg_bytes) this_thread::sleep(get_system_time() + posix_time::milliseconds(50)); auto used_time = get_system_time().time_of_day().total_seconds() - begin_time; printf("\r100%%\ntime spent statistics: %d hours, %d minutes, %d seconds.\n", used_time / 60 / 60, (used_time / 60) % 60, used_time % 60); if (used_time > 0) printf("speed: %lu(*2)kB/s.\n", total_msg_bytes / 1024 / used_time); } // if (total_data_num > 0) } } return 0; } //configuration #undef SERVER_PORT //configuration