您的位置:首页 > 其它

三 100万并发连接服务器笔记之测试端就绪

2015-06-08 17:06 501 查看


重新编写测试端程序

测试端程序需要增加绑定本机IP和本地端口的功能,以尽可能的向外发出更多的tcp请求。需要对client1.c重构,增加参数传递。 下面是client2.c的代码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
<div class="line" id="file-client2-c-LC1"><span class="cp" style="color: rgb(153, 153, 153); font-weight: bold;">#include <sys/types.h></span></div><div class="line" id="file-client2-c-LC2"><span class="cp" style="color: rgb(153, 153, 153); font-weight: bold;">#include <sys/time.h></span></div><div class="line" id="file-client2-c-LC3"><span class="cp" style="color: rgb(153, 153, 153); font-weight: bold;">#include <sys/queue.h></span></div><div class="line" id="file-client2-c-LC4"><span class="cp" style="color: rgb(153, 153, 153); font-weight: bold;">#include <stdlib.h></span></div><div class="line" id="file-client2-c-LC5"><span class="cp" style="color: rgb(153, 153, 153); font-weight: bold;">#include <err.h></span></div><div class="line" id="file-client2-c-LC6"><span class="cp" style="color: rgb(153, 153, 153); font-weight: bold;">#include <event.h></span></div><div class="line" id="file-client2-c-LC7"><span class="cp" style="color: rgb(153, 153, 153); font-weight: bold;">#include <evhttp.h></span></div><div class="line" id="file-client2-c-LC8"><span class="cp" style="color: rgb(153, 153, 153); font-weight: bold;">#include <unistd.h></span></div><div class="line" id="file-client2-c-LC9"><span class="cp" style="color: rgb(153, 153, 153); font-weight: bold;">#include <stdio.h></span></div><div class="line" id="file-client2-c-LC10"><span class="cp" style="color: rgb(153, 153, 153); font-weight: bold;">#include <string.h></span></div><div class="line" id="file-client2-c-LC11"><span class="cp" style="color: rgb(153, 153, 153); font-weight: bold;">#include <sys/socket.h></span></div><div class="line" id="file-client2-c-LC12"><span class="cp" style="color: rgb(153, 153, 153); font-weight: bold;">#include <netinet/in.h></span></div><div class="line" id="file-client2-c-LC13"><span class="cp" style="color: rgb(153, 153, 153); font-weight: bold;">#include <time.h></span></div><div class="line" id="file-client2-c-LC14"><span class="cp" style="color: rgb(153, 153, 153); font-weight: bold;">#include <pthread.h></span></div><div class="line" id="file-client2-c-LC15"> </div><div class="line" id="file-client2-c-LC16"><span class="cp" style="color: rgb(153, 153, 153); font-weight: bold;">#define BUFSIZE 4096</span></div><div class="line" id="file-client2-c-LC17"><span class="cp" style="color: rgb(153, 153, 153); font-weight: bold;">#define SLEEP_MS 10</span></div><div class="line" id="file-client2-c-LC18"> </div><div class="line" id="file-client2-c-LC19"><span class="kt" style="color: rgb(68, 85, 136); font-weight: bold;">char</span> <span class="n">buf</span><span class="p">[</span><span class="n">BUFSIZE</span><span class="p">];</span></div><div class="line" id="file-client2-c-LC20"> </div><div class="line" id="file-client2-c-LC21"><span class="kt" style="color: rgb(68, 85, 136); font-weight: bold;">int</span> <span class="n">bytes_recvd</span> <span class="o" style="font-weight: bold;">=</span> <span class="mi" style="color: rgb(0, 153, 153);">0</span><span class="p">;</span></div><div class="line" id="file-client2-c-LC22"><span class="kt" style="color: rgb(68, 85, 136); font-weight: bold;">int</span> <span class="n">chunks_recvd</span> <span class="o" style="font-weight: bold;">=</span> <span class="mi" style="color: rgb(0, 153, 153);">0</span><span class="p">;</span></div><div class="line" id="file-client2-c-LC23"><span class="kt" style="color: rgb(68, 85, 136); font-weight: bold;">int</span> <span class="n">closed</span> <span class="o" style="font-weight: bold;">=</span> <span class="mi" style="color: rgb(0, 153, 153);">0</span><span class="p">;</span></div><div class="line" id="file-client2-c-LC24"><span class="kt" style="color: rgb(68, 85, 136); font-weight: bold;">int</span> <span class="n">connected</span> <span class="o" style="font-weight: bold;">=</span> <span class="mi" style="color: rgb(0, 153, 153);">0</span><span class="p">;</span></div><div class="line" id="file-client2-c-LC25"> </div><div class="line" id="file-client2-c-LC26"><span class="k" style="font-weight: bold;">static</span> <span class="kt" style="color: rgb(68, 85, 136); font-weight: bold;">char</span> <span class="n">ip_array</span><span class="p">[</span><span class="mi" style="color: rgb(0, 153, 153);">300</span><span class="p">]</span> <span class="o" style="font-weight: bold;">=</span>  <span class="s" style="color: rgb(221, 17, 68);">"192.168.190.134,192.168.190.143,192.168.190.144,192.168.190.145,192.168.190.146,192.168.190.147,192.168.190.148,192.168.190.149,192.168.190.151,192.168.190.152"</span><span class="p">;</span></div><div class="line" id="file-client2-c-LC27"><span class="k" style="font-weight: bold;">static</span> <span class="kt" style="color: rgb(68, 85, 136); font-weight: bold;">char</span> <span class="n">server_ip</span><span class="p">[</span><span class="mi" style="color: rgb(0, 153, 153);">16</span><span class="p">]</span> <span class="o" style="font-weight: bold;">=</span>  <span class="s" style="color: rgb(221, 17, 68);">"192.168.190.133"</span><span class="p">;</span></div><div class="line" id="file-client2-c-LC28"><span class="k" style="font-weight: bold;">static</span> <span class="kt" style="color: rgb(68, 85, 136); font-weight: bold;">int</span> <span class="n">server_port</span> <span class="o" style="font-weight: bold;">=</span> <span class="mi" style="color: rgb(0, 153, 153);">8000</span><span class="p">;</span></div><div class="line" id="file-client2-c-LC29"><span class="k" style="font-weight: bold;">static</span> <span class="kt" style="color: rgb(68, 85, 136); font-weight: bold;">int</span> <span class="n">max_conns</span> <span class="o" style="font-weight: bold;">=</span> <span class="mi" style="color: rgb(0, 153, 153);">62000</span><span class="p">;</span></div><div class="line" id="file-client2-c-LC30"> </div><div class="line" id="file-client2-c-LC31"><span class="c1" style="color: rgb(153, 153, 136); font-style: italic;">// called per chunk received</span></div><div class="line" id="file-client2-c-LC32"><span class="kt" style="color: rgb(68, 85, 136); font-weight: bold;">void</span> <span class="nf" style="color: rgb(153, 0, 0); font-weight: bold;">chunkcb</span><span class="p">(</span><span class="k" style="font-weight: bold;">struct</span> <span class="n">evhttp_request</span> <span class="o" style="font-weight: bold;">*</span><span class="n">req</span><span class="p">,</span> <span class="kt" style="color: rgb(68, 85, 136); font-weight: bold;">void</span> <span class="o" style="font-weight: bold;">*</span><span class="n">arg</span><span class="p">)</span> <span class="p">{</span></div><div class="line" id="file-client2-c-LC33">    <span class="kt" style="color: rgb(68, 85, 136); font-weight: bold;">int</span> <span class="n">s</span> <span class="o" style="font-weight: bold;">=</span> <span class="n">evbuffer_remove</span><span class="p">(</span> <span class="n">req</span><span class="o" style="font-weight: bold;">-></span><span class="n">input_buffer</span><span class="p">,</span> <span class="o" style="font-weight: bold;">&</span><span class="n">buf</span><span class="p">,</span> <span class="n">BUFSIZE</span> <span class="p">);</span></div><div class="line" id="file-client2-c-LC34">    <span class="n">bytes_recvd</span> <span class="o" style="font-weight: bold;">+=</span> <span class="n">s</span><span class="p">;</span></div><div class="line" id="file-client2-c-LC35">    <span class="n">chunks_recvd</span><span class="o" style="font-weight: bold;">++</span><span class="p">;</span></div><div class="line" id="file-client2-c-LC36">    <span class="k" style="font-weight: bold;">if</span> <span class="p">(</span><span class="n">connected</span> <span class="o" style="font-weight: bold;">>=</span> <span class="n">max_conns</span> <span class="o" style="font-weight: bold;">&&</span> <span class="n">chunks_recvd</span> <span class="o" style="font-weight: bold;">%</span> <span class="mi" style="color: rgb(0, 153, 153);">10000</span> <span class="o" style="font-weight: bold;">==</span> <span class="mi" style="color: rgb(0, 153, 153);">0</span><span class="p">)</span></div><div class="line" id="file-client2-c-LC37">        <span class="n">printf</span><span class="p">(</span><span class="s" style="color: rgb(221, 17, 68);">">Chunks: %d</span><span class="se" style="color: rgb(221, 17, 68);">\t</span><span class="s" style="color: rgb(221, 17, 68);">Bytes: %d</span><span class="se" style="color: rgb(221, 17, 68);">\t</span><span class="s" style="color: rgb(221, 17, 68);">Closed: %d</span><span class="se" style="color: rgb(221, 17, 68);">\n</span><span class="s" style="color: rgb(221, 17, 68);">"</span><span class="p">,</span> <span class="n">chunks_recvd</span><span class="p">,</span> <span class="n">bytes_recvd</span><span class="p">,</span> <span class="n">closed</span><span class="p">);</span></div><div class="line" id="file-client2-c-LC38"><span class="p">}</span></div><div class="line" id="file-client2-c-LC39"> </div><div class="line" id="file-client2-c-LC40"><span class="c1" style="color: rgb(153, 153, 136); font-style: italic;">// gets called when request completes</span></div><div class="line" id="file-client2-c-LC41"><span class="kt" style="color: rgb(68, 85, 136); font-weight: bold;">void</span> <span class="nf" style="color: rgb(153, 0, 0); font-weight: bold;">reqcb</span><span class="p">(</span><span class="k" style="font-weight: bold;">struct</span> <span class="n">evhttp_request</span> <span class="o" style="font-weight: bold;">*</span><span class="n">req</span><span class="p">,</span> <span class="kt" style="color: rgb(68, 85, 136); font-weight: bold;">void</span> <span class="o" style="font-weight: bold;">*</span><span class="n">arg</span><span class="p">)</span> <span class="p">{</span></div><div class="line" id="file-client2-c-LC42">    <span class="n">closed</span><span class="o" style="font-weight: bold;">++</span><span class="p">;</span></div><div class="line" id="file-client2-c-LC43"><span class="p">}</span></div><div class="line" id="file-client2-c-LC44"> </div><div class="line" id="file-client2-c-LC45"><span class="kt" style="color: rgb(68, 85, 136); font-weight: bold;">int</span> <span class="nf" style="color: rgb(153, 0, 0); font-weight: bold;">main</span><span class="p">(</span><span class="kt" style="color: rgb(68, 85, 136); font-weight: bold;">int</span> <span class="n">argc</span><span class="p">,</span> <span class="kt" style="color: rgb(68, 85, 136); font-weight: bold;">char</span> <span class="o" style="font-weight: bold;">**</span><span class="n">argv</span><span class="p">)</span> <span class="p">{</span></div><div class="line" id="file-client2-c-LC46">    <span class="kt" style="color: rgb(68, 85, 136); font-weight: bold;">int</span> <span class="n">ch</span><span class="p">;</span></div><div class="line" id="file-client2-c-LC47">    <span class="k" style="font-weight: bold;">while</span> <span class="p">((</span><span class="n">ch</span> <span class="o" style="font-weight: bold;">=</span> <span class="n">getopt</span><span class="p">(</span><span class="n">argc</span><span class="p">,</span> <span class="n">argv</span><span class="p">,</span> <span class="s" style="color: rgb(221, 17, 68);">"o:h:p:m:"</span><span class="p">))</span> <span class="o" style="font-weight: bold;">!=</span> <span class="o" style="font-weight: bold;">-</span><span class="mi" style="color: rgb(0, 153, 153);">1</span><span class="p">)</span> <span class="p">{</span></div><div class="line" id="file-client2-c-LC48">        <span class="k" style="font-weight: bold;">switch</span> <span class="p">(</span><span class="n">ch</span><span class="p">)</span> <span class="p">{</span></div><div class="line" id="file-client2-c-LC49">        <span class="k" style="font-weight: bold;">case</span> <span class="sc" style="color: rgb(221, 17, 68);">'h'</span>:</div><div class="line" id="file-client2-c-LC50">            <span class="n">printf</span><span class="p">(</span><span class="s" style="color: rgb(221, 17, 68);">"host is %s</span><span class="se" style="color: rgb(221, 17, 68);">\n</span><span class="s" style="color: rgb(221, 17, 68);">"</span><span class="p">,</span> <span class="n">optarg</span><span class="p">);</span></div><div class="line" id="file-client2-c-LC51">            <span class="n">strncpy</span><span class="p">(</span><span class="n">server_ip</span><span class="p">,</span> <span class="n">optarg</span><span class="p">,</span> <span class="mi" style="color: rgb(0, 153, 153);">15</span><span class="p">);</span></div><div class="line" id="file-client2-c-LC52">            <span class="k" style="font-weight: bold;">break</span><span class="p">;</span></div><div class="line" id="file-client2-c-LC53">        <span class="k" style="font-weight: bold;">case</span> <span class="sc" style="color: rgb(221, 17, 68);">'p'</span>:</div><div class="line" id="file-client2-c-LC54">            <span class="n">printf</span><span class="p">(</span><span class="s" style="color: rgb(221, 17, 68);">"port is %s</span><span class="se" style="color: rgb(221, 17, 68);">\n</span><span class="s" style="color: rgb(221, 17, 68);">"</span><span class="p">,</span> <span class="n">optarg</span><span class="p">);</span></div><div class="line" id="file-client2-c-LC55">            <span class="n">server_port</span> <span class="o" style="font-weight: bold;">=</span> <span class="n">atoi</span><span class="p">(</span><span class="n">optarg</span><span class="p">);</span></div><div class="line" id="file-client2-c-LC56">            <span class="cm" style="color: rgb(153, 153, 136); font-style: italic;">/*strncpy(server_ip, optarg, 15);*/</span></div><div class="line" id="file-client2-c-LC57">            <span class="k" style="font-weight: bold;">break</span><span class="p">;</span></div><div class="line" id="file-client2-c-LC58">        <span class="k" style="font-weight: bold;">case</span> <span class="sc" style="color: rgb(221, 17, 68);">'m'</span>:</div><div class="line" id="file-client2-c-LC59">            <span class="n">printf</span><span class="p">(</span><span class="s" style="color: rgb(221, 17, 68);">"max_conns is %s</span><span class="se" style="color: rgb(221, 17, 68);">\n</span><span class="s" style="color: rgb(221, 17, 68);">"</span><span class="p">,</span> <span class="n">optarg</span><span class="p">);</span></div><div class="line" id="file-client2-c-LC60">            <span class="n">max_conns</span> <span class="o" style="font-weight: bold;">=</span> <span class="n">atoi</span><span class="p">(</span><span class="n">optarg</span><span class="p">);</span></div><div class="line" id="file-client2-c-LC61">            <span class="cm" style="color: rgb(153, 153, 136); font-style: italic;">/*strncpy(server_ip, optarg, 15);*/</span></div><div class="line" id="file-client2-c-LC62">            <span class="k" style="font-weight: bold;">break</span><span class="p">;</span></div><div class="line" id="file-client2-c-LC63">        <span class="k" style="font-weight: bold;">case</span> <span class="sc" style="color: rgb(221, 17, 68);">'o'</span>:</div><div class="line" id="file-client2-c-LC64">            <span class="n">printf</span><span class="p">(</span><span class="s" style="color: rgb(221, 17, 68);">"ori_ips is %s</span><span class="se" style="color: rgb(221, 17, 68);">\n</span><span class="s" style="color: rgb(221, 17, 68);">"</span><span class="p">,</span> <span class="n">optarg</span><span class="p">);</span></div><div class="line" id="file-client2-c-LC65"> </div><div class="line" id="file-client2-c-LC66">            <span class="n">strncpy</span><span class="p">(</span><span class="n">ip_array</span><span class="p">,</span> <span class="n">optarg</span><span class="p">,</span> <span class="mi" style="color: rgb(0, 153, 153);">300</span> <span class="o" style="font-weight: bold;">-</span> <span class="mi" style="color: rgb(0, 153, 153);">1</span><span class="p">);</span></div><div class="line" id="file-client2-c-LC67">            <span class="k" style="font-weight: bold;">break</span><span class="p">;</span></div><div class="line" id="file-client2-c-LC68">        <span class="p">}</span></div><div class="line" id="file-client2-c-LC69">    <span class="p">}</span></div><div class="line" id="file-client2-c-LC70"> </div><div class="line" id="file-client2-c-LC71">    <span class="n">event_init</span><span class="p">();</span></div><div class="line" id="file-client2-c-LC72">    <span class="k" style="font-weight: bold;">struct</span> <span class="n">evhttp</span> <span class="o" style="font-weight: bold;">*</span><span class="n">evhttp_connection</span><span class="p">;</span></div><div class="line" id="file-client2-c-LC73">    <span class="k" style="font-weight: bold;">struct</span> <span class="n">evhttp_request</span> <span class="o" style="font-weight: bold;">*</span><span class="n">evhttp_request</span><span class="p">;</span></div><div class="line" id="file-client2-c-LC74">    <span class="kt" style="color: rgb(68, 85, 136); font-weight: bold;">char</span> <span class="n">path</span><span class="p">[</span><span class="mi" style="color: rgb(0, 153, 153);">32</span><span class="p">];</span></div><div class="line" id="file-client2-c-LC75">    <span class="kt" style="color: rgb(68, 85, 136); font-weight: bold;">int</span> <span class="n">i</span><span class="p">;</span></div><div class="line" id="file-client2-c-LC76"> </div><div class="line" id="file-client2-c-LC77">    <span class="kt" style="color: rgb(68, 85, 136); font-weight: bold;">char</span> <span class="n">delims</span><span class="p">[]</span> <span class="o" style="font-weight: bold;">=</span> <span class="s" style="color: rgb(221, 17, 68);">","</span><span class="p">;</span></div><div class="line" id="file-client2-c-LC78">    <span class="kt" style="color: rgb(68, 85, 136); font-weight: bold;">char</span> <span class="o" style="font-weight: bold;">*</span><span class="n">ori_ip</span> <span class="o" style="font-weight: bold;">=</span> <span class="nb" style="color: rgb(0, 134, 179);">NULL</span><span class="p">;</span></div><div class="line" id="file-client2-c-LC79">    <span class="n">ori_ip</span> <span class="o" style="font-weight: bold;">=</span> <span class="n">strtok</span><span class="p">(</span> <span class="n">ip_array</span><span class="p">,</span> <span class="n">delims</span> <span class="p">);</span></div><div class="line" id="file-client2-c-LC80">    <span class="k" style="font-weight: bold;">while</span> <span class="p">(</span><span class="n">ori_ip</span> <span class="o" style="font-weight: bold;">!=</span> <span class="nb" style="color: rgb(0, 134, 179);">NULL</span><span class="p">)</span> <span class="p">{</span></div><div class="line" id="file-client2-c-LC81">        <span class="k" style="font-weight: bold;">for</span> <span class="p">(</span><span class="n">i</span> <span class="o" style="font-weight: bold;">=</span> <span class="mi" style="color: rgb(0, 153, 153);">1</span><span class="p">;</span> <span class="n">i</span> <span class="o" style="font-weight: bold;"><=</span> <span class="n">max_conns</span><span class="p">;</span> <span class="n">i</span><span class="o" style="font-weight: bold;">++</span><span class="p">)</span> <span class="p">{</span></div><div class="line" id="file-client2-c-LC82">            <span class="n">evhttp_connection</span> <span class="o" style="font-weight: bold;">=</span> <span class="n">evhttp_connection_new</span><span class="p">(</span><span class="n">server_ip</span><span class="p">,</span> <span class="n">server_port</span><span class="p">);</span></div><div class="line" id="file-client2-c-LC83">            <span class="n">evhttp_connection_set_local_address</span><span class="p">(</span><span class="n">evhttp_connection</span><span class="p">,</span> <span class="n">ori_ip</span><span class="p">);</span></div><div class="line" id="file-client2-c-LC84">            <span class="n">evhttp_set_timeout</span><span class="p">(</span><span class="n">evhttp_connection</span><span class="p">,</span> <span class="mi" style="color: rgb(0, 153, 153);">864000</span><span class="p">);</span> <span class="c1" style="color: rgb(153, 153, 136); font-style: italic;">// 10 day timeout</span></div><div class="line" id="file-client2-c-LC85">            <span class="n">evhttp_request</span> <span class="o" style="font-weight: bold;">=</span> <span class="n">evhttp_request_new</span><span class="p">(</span><span class="n">reqcb</span><span class="p">,</span> <span class="nb" style="color: rgb(0, 134, 179);">NULL</span><span class="p">);</span></div><div class="line" id="file-client2-c-LC86">            <span class="n">evhttp_request</span><span class="o" style="font-weight: bold;">-></span><span class="n">chunk_cb</span> <span class="o" style="font-weight: bold;">=</span> <span class="n">chunkcb</span><span class="p">;</span></div><div class="line" id="file-client2-c-LC87">            <span class="n">sprintf</span><span class="p">(</span><span class="o" style="font-weight: bold;">&</span><span class="n">path</span><span class="p">,</span> <span class="s" style="color: rgb(221, 17, 68);">"/test/%d"</span><span class="p">,</span> <span class="o" style="font-weight: bold;">++</span><span class="n">connected</span><span class="p">);</span></div><div class="line" id="file-client2-c-LC88"> </div><div class="line" id="file-client2-c-LC89">            <span class="k" style="font-weight: bold;">if</span> <span class="p">(</span><span class="n">i</span> <span class="o" style="font-weight: bold;">%</span> <span class="mi" style="color: rgb(0, 153, 153);">1000</span> <span class="o" style="font-weight: bold;">==</span> <span class="mi" style="color: rgb(0, 153, 153);">0</span><span class="p">)</span></div><div class="line" id="file-client2-c-LC90">                <span class="n">printf</span><span class="p">(</span><span class="s" style="color: rgb(221, 17, 68);">"Req: %s</span><span class="se" style="color: rgb(221, 17, 68);">\t</span><span class="s" style="color: rgb(221, 17, 68);">-></span><span class="se" style="color: rgb(221, 17, 68);">\t</span><span class="s" style="color: rgb(221, 17, 68);">%s</span><span class="se" style="color: rgb(221, 17, 68);">\n</span><span class="s" style="color: rgb(221, 17, 68);">"</span><span class="p">,</span> <span class="n">ori_ip</span><span class="p">,</span> <span class="o" style="font-weight: bold;">&</span><span class="n">path</span><span class="p">);</span></div><div class="line" id="file-client2-c-LC91"> </div><div class="line" id="file-client2-c-LC92">            <span class="n">evhttp_make_request</span><span class="p">(</span> <span class="n">evhttp_connection</span><span class="p">,</span> <span class="n">evhttp_request</span><span class="p">,</span> <span class="n">EVHTTP_REQ_GET</span><span class="p">,</span> <span class="n">path</span> <span class="p">);</span></div><div class="line" id="file-client2-c-LC93">            <span class="n">evhttp_connection_set_timeout</span><span class="p">(</span><span class="n">evhttp_request</span><span class="o" style="font-weight: bold;">-></span><span class="n">evcon</span><span class="p">,</span> <span class="mi" style="color: rgb(0, 153, 153);">864000</span><span class="p">);</span></div><div class="line" id="file-client2-c-LC94">            <span class="n">event_loop</span><span class="p">(</span> <span class="n">EVLOOP_NONBLOCK</span> <span class="p">);</span></div><div class="line" id="file-client2-c-LC95"> </div><div class="line" id="file-client2-c-LC96">            <span class="k" style="font-weight: bold;">if</span> <span class="p">(</span> <span class="n">connected</span> <span class="o" style="font-weight: bold;">%</span> <span class="mi" style="color: rgb(0, 153, 153);">1000</span> <span class="o" style="font-weight: bold;">==</span> <span class="mi" style="color: rgb(0, 153, 153);">0</span> <span class="p">)</span></div><div class="line" id="file-client2-c-LC97">                <span class="n">printf</span><span class="p">(</span><span class="s" style="color: rgb(221, 17, 68);">"</span><span class="se" style="color: rgb(221, 17, 68);">\n</span><span class="s" style="color: rgb(221, 17, 68);">Chunks: %d</span><span class="se" style="color: rgb(221, 17, 68);">\t</span><span class="s" style="color: rgb(221, 17, 68);">Bytes: %d</span><span class="se" style="color: rgb(221, 17, 68);">\t</span><span class="s" style="color: rgb(221, 17, 68);">Closed: %d</span><span class="se" style="color: rgb(221, 17, 68);">\n</span><span class="s" style="color: rgb(221, 17, 68);">"</span><span class="p">,</span> <span class="n">chunks_recvd</span><span class="p">,</span> <span class="n">bytes_recvd</span><span class="p">,</span> <span class="n">closed</span><span class="p">);</span></div><div class="line" id="file-client2-c-LC98"> </div><div class="line" id="file-client2-c-LC99">            <span class="n">usleep</span><span class="p">(</span><span class="n">SLEEP_MS</span> <span class="o" style="font-weight: bold;">*</span> <span class="mi" style="color: rgb(0, 153, 153);">10</span><span class="p">);</span></div><div class="line" id="file-client2-c-LC100">        <span class="p">}</span></div><div class="line" id="file-client2-c-LC101"> </div><div class="line" id="file-client2-c-LC102">        <span class="n">ori_ip</span> <span class="o" style="font-weight: bold;">=</span> <span class="n">strtok</span><span class="p">(</span> <span class="nb" style="color: rgb(0, 134, 179);">NULL</span><span class="p">,</span> <span class="n">delims</span> <span class="p">);</span></div><div class="line" id="file-client2-c-LC103">    <span class="p">}</span></div><div class="line" id="file-client2-c-LC104"> </div><div class="line" id="file-client2-c-LC105">    <span class="n">event_dispatch</span><span class="p">();</span></div><div class="line" id="file-client2-c-LC106"> </div><div class="line" id="file-client2-c-LC107">    <span class="k" style="font-weight: bold;">return</span> <span class="mi" style="color: rgb(0, 153, 153);">0</span><span class="p">;</span></div><div class="line" id="file-client2-c-LC108"><span class="p">}</span></div>
view rawclient2.c hosted
with ❤ by GitHub

若不指定端口,系统会随机挑选没有使用到的端口,可以节省些心力。

编译:
[code]gcc -o client2 client2.c -levent


参数解释

-h 要连接的服务器IP地址
-p 要连接的服务器端口
-m 本机IP地址需要绑定的随机端口数量
-o 本机所有可用的IP地址列表,注意所有IP地址都应该可用

运行:
[code]./client2 -h 192.168.190.230 -p 8000 -m 64000 -o 192.168.190.134,192.168.190.143,192.168.190.144,192.168.190.145,192.168.190.146,192.168.190.147,192.168.190.148,192.168.190.149,192.168.190.150,192.168.190.151


太长了,每次执行都要粘贴过去,直接放在一个client2.sh文件中,执行就很简单方便多了。
[code]#!/bin/sh
./client2 -h 192.168.190.230 -p 8000 -m 64000 -o 192.168.190.134,192.168.190.143,192.168.190.144,192.168.190.145,192.168.190.146,192.168.190.147,192.168.190.148,192.168.190.149,192.168.190.150,192.168.190.151


保存,赋值可运行:
[code]chmod +x client2.sh


启动测试:
[code]sh client2.sh


第三个遇到的问题:fs.file-max的问题

测试端程序client2.c在发出的数量大于某个值(大概为40万时)是,通过dmesg命令查看会得到大量警告信息:
[code][warn] socket: Too many open files in system


此时,就需要检查/proc/sys/fs/file-max参数了。

查看一下系统对fs.file-max的说明
[code] /proc/sys/fs/file-max
This file defines a system-wide limit on the number of open files for all processes. (See also setrlimit(2), which can be used by a process to set the per-process limit,
RLIMIT_NOFILE, on the number of files it may open.) If you get lots of error messages about running out of file handles, try increasing this value:
echo 100000 > /proc/sys/fs/file-max
The kernel constant NR_OPEN imposes an upper limit on the value that may be placed in file-max.
If you increase /proc/sys/fs/file-max, be sure to increase /proc/sys/fs/inode-max to 3-4 times the new value of /proc/sys/fs/file-max, or you will run out of inodes.


file-max表示系统所有进程最多允许同时打开所有的文件句柄数,系统级硬限制。Linux系统在启动时根据系统硬件资源状况计算出来的最佳的最大同时打开文件数限制,如果没有特殊需要,不用修改,除非打开的文件句柄数超过此值。

在为测试机分配4G内存时,对应的fs.file-max值为386562,很显然打开的文件句柄很受限,38万个左右。 很显然,无论是测试端还是服务端,都应该将此值调大些,一定要大于等于/etc/security/limits.conf送所设置的soft nofile和soft nofile值。

注意ulimit -n,仅仅设置当前shell以及由它启动的进程的资源限制。

备注:以上参数,具有包含和被包含的关系。

当前会话修改,可以这么做:
[code]echo 1048576 > /proc/sys/fs/file-max


但系统重启后消失。

永久修改,要添加到 /etc/sysctl.conf 文件中:
[code]fs.file-max = 1048576


保存并使之生效:
[code]sysctl -p


再测,就不会出现此问题了。

一台6G内存机器测试机,分配7个网卡,可以做到不占用虚拟内存,对外发出64000 * 7 = 448000个对外持久请求。要完成100万的持久连接,还得再想办法。

最终测试端组成如下:

两台物理机器各自一个网卡,每个发出64000个请求
两个6G左右的centos测试端机器(绑定7个桥接或NAT连接)各自发出64000*7 = 448000请求
共使用了16个网卡(物理网卡+虚拟网卡)
1M ≈ 1024K ≈ 1024000 = (64000) + (64000) + (64000*7) + (64000*7)
共耗费16G内存,16个网卡(物理+虚拟),四台测试机

备注:

下面就要完成1M持久连接的目标,但在服务端还会遇到最后一个问题。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: