在进行memcpy操作时,虽然是内存操作,但是仍然是耗一点点CPU的,今天测试了一下单线程中执行memcpy的效率,这个结果对于配置TCP epoll中的work thread
数量有指导意义。如下基于8K的内存快执行memcpy, 1个线程大约1S能够拷贝500M,如果服务器带宽或网卡到上限是1G,那么网络io的work thread 开2个即可,考虑到消息的解析损耗,3个线程足以抗住硬件的最高负载。
在我到测试机器上到测试结果是:
Intel(R) Xeon(R) CPU E5405 @ 2.00GHz
do memcpy speed:12.27 ms/MB
each thread can do memcpy 667.645 MB
1 #include <iostream>
2 #include <sys/time.h>
3 #include <string.h>
4
5 using namespace std;
6
7 int main(int argc, char* argv[])
8 {
9 long len = 8192;
10 int loop = 200;
11 char* p = new char[len];
12 char* q = p;
13 struct timeval start, end;
14 gettimeofday(&start, NULL);
15 for (int i =0; i < loop; ++i)
16 {
17 char* p = new char[len];
18 *p = char(i);
19 memcpy(p, q, len);
20 delete [] p;
21 }
22 gettimeofday(&end, NULL);
23 cout <<"do memcpy speed:" << ((end.tv_sec - start.tv_sec)*1000 + double(end.tv_usec - start.tv_usec) / (len*loop/1000/1000) ) / loop<<" ms/MB\n";
24 cout <<"each thread can do memcpy "<< double(len)*loop/1000/1000 / ((end.tv_sec - start.tv_sec) + double(end.tv_usec - start.tv_usec) / 1000/1000) <<" MB\n";
25
26 }