webbench 源码解析

论坛 期权论坛     
选择匿名的用户   2021-5-30 02:09   132   0
<p><br> </p>
<p><span style="word-wrap:break-word; color:rgb(102,102,102); font-family:宋体,Arial; font-size:16px; line-height:26px"><span style="word-wrap:break-word; color:rgb(51,51,51)">webbench作为一个简单的网站压力测试工具,小巧简单,其源码仅仅600行左右,是一个学习linux下C编程的好例子。<br style="word-wrap:break-word"> </span></span><br style="word-wrap:break-word; color:rgb(102,102,102); font-family:宋体,Arial; font-size:16px; line-height:26px"> <span style="word-wrap:break-word; color:rgb(102,102,102); font-family:宋体,Arial; font-size:16px; line-height:26px"><span style="word-wrap:break-word; color:rgb(51,51,51)">解压后的webbench由下面几个文件组成:</span></span><br style="word-wrap:break-word; color:rgb(102,102,102); font-family:宋体,Arial; font-size:16px; line-height:26px"> <img alt="" height="105" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-6785e546d69a7a1d22cdc80e5fce8747.png" style="word-wrap:break-word; border:0px; color:rgb(102,102,102); font-family:宋体,Arial; font-size:16px; line-height:26px" width="587"><br style="word-wrap:break-word; color:rgb(102,102,102); font-family:宋体,Arial; font-size:16px; line-height:26px"> <span style="word-wrap:break-word; color:rgb(102,102,102); font-family:宋体,Arial; font-size:16px; line-height:26px"><span style="word-wrap:break-word; color:rgb(51,51,51)">2个C源文件,一个是socket.c 另一个是webbench.c</span></span><br style="word-wrap:break-word; color:rgb(102,102,102); font-family:宋体,Arial; font-size:16px; line-height:26px"> <span style="font-family:宋体,Arial; color:#666666"><span style="font-size:16px; line-height:26px; background-color:rgb(229,102,0)">socket.c</span></span></p>
<p><br style="word-wrap:break-word; color:rgb(102,102,102); font-family:宋体,Arial; font-size:16px; line-height:26px"> </p>
<div class="codeText" id="codeText" style="word-wrap:break-word; border:1px solid rgb(221,221,221); width:1041.453125px; overflow:auto; margin:0px 0px 1.1em; padding:0px; word-break:break-all; letter-spacing:0.1px; font-family:Consolas,monospace; color:rgb(102,102,102)">
<ol class="dp-css none_number" start="1" style="word-wrap:break-word; margin:0px 1px 0px 0px; padding:5px 0pt; list-style:none outside none; color:rgb(92,92,92); line-height:1.3"><li style="word-wrap:break-word; margin:0px; padding:0px 0px 0px 10px; list-style:none outside none"> 内部仅包含一个Socket函数,如下:</li><li style="word-wrap:break-word; margin:0px; padding:0px 0px 0px 10px; list-style:none outside none"> int Socket(const char *host, int clientPort)</li><li style="word-wrap:break-word; margin:0px; padding:0px 0px 0px 10px; list-style:none outside none"> {<!-- --></li><li style="word-wrap:break-word; margin:0px; padding:0px 0px 0px 10px; list-style:none outside none">         //以host和clientPort构成一对TCP的套接字(服务器)<br style="word-wrap:break-word"> </li><li style="word-wrap:break-word; margin:0px; padding:0px 0px 0px 10px; list-style:none outside none"> <span style="white-space:pre"></span>//创建sock bind connect</li><li style="word-wrap:break-word; margin:0px; padding:0px 0px 0px 10px; list-style:none outside none">     <span style="white-space:pre"></span>//创建失败返回-1  成功返回一个sockt描述符<br style="word-wrap:break-word"> </li><li style="word-wrap:break-word; margin:0px; padding:0px 0px 0px 10px; list-style:none outside none"> }</li></ol>
</div>
<span style="word-wrap:break-word; color:rgb(102,102,102); font-family:宋体,Arial; font-size:16px; line-height:26px"><span style="word-wrap:break-word; background-color:rgb(229,102,0)">webbench.c</span></span>
<br style="word-wrap:break-word; color:rgb(102,102,102); font-family:宋体,Arial; font-size:16px; line-height:26px">
<div class="codeText" id="codeText" style="word-wrap:break-word; border:1px solid rgb(221,221,221); width:1041.453125px; overflow:auto; margin:0px 0px 1.1em; padding:0px; word-break:break-all; letter-spacing:0.1px; font-family:Consolas,monospace; color:rgb(102,102,102)">
<ol class="dp-css none_number" start="1" style="word-wrap:break-word; margin:0px 1px 0px 0px; padding:5px 0pt; list-style:none outside none; color:rgb(92,92,92); line-height:1.3"><li style="word-wrap:break-word; margin:0px; padding:0px 0px 0px 10px; list-style:none outside none"> 在webbench.c源文件中,包含了下面几个函数:</li><li style="word-wrap:break-word; margin:0px; padding:0px 0px 0px 10px; list-style:none outside none"> static void alarm_handler(int signal)</li><li style="word-wrap:break-word; margin:0px; padding:0px 0px 0px 10px; list-style:none outside none"> static void usage(void)</li><li style="word-wrap:break-word; margin:0px; padding:0px 0px 0px 10px; list-style:none outside none"> void build_request(const char *url)</li><li style="word-wrap:break-word; margin:0px; padding:0px 0px 0px 10px; list-style:none outside none"> static int bench(void)</li><li style="word-wrap:break-word; margin:0px; padding:0px 0px 0px 10px; list-style:none outside none"> void benchcore(const char *host, const int port, const char *req)<span style="line-height:1.3; letter-spacing:0.1px">  </span></li><li style="word-wrap:break-word; margin:0px; padding:0px 0px 0px 10px; list-style:none outside none"> <span style="line-height:1.3; letter-spacing:0.1px">int main(int argc, char *argv[])</span></li></ol>
<div style="word-wrap:break-word">
  <span style="word-wrap:break-word; color:rgb(92,92,92
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP