| 
	一.问题来源 
	   想得到当前互联网公司的web server是哪个类型的?(Apache   Nginx   Lighttpd)所以编写此程序。主要统计baidu  Google  alibaba  tencent(qq)  taobao   renren    sina   京东  凡客  yahoo  盛大  网易  搜狐  畅游  等公司的web server 。当然,也有可能得到的是反向代理服务器。这得具体看公司的后台系统架构了。 
	  
	二.程序 
	1.源码 
	vi ab.sh 
	  
	
	
		
			#!/bin/bash  
			#author         longxibendi   
			#blog           http://blog.csdn.net/longxibendi  
			#function       get the  web servers information of companies'  
			if [ -e  /tmp/web_server.log  ] ;then  
			     rm  -f  /tmp/web_server.log  
			fi  
			if [ -e  /tmp/ab.log ] ; then  
			     rm  -f   /tmp/ab.log  
			fi  
			for company_name in   taobao  360buy   dangdang  vancl  mbaobao alibaba google  baidu  yahoo  sina   sohu   ren  
			ren   changyou wanmei  sdo 126 163 139   
			do  
			   {  
			     ab -n 1 -c 1   http:  
			     if [ $? -eq 0  ] ; then  
			       printf  "%15s,   %20s/n"    "${company_name}.com",   " ` cat  /tmp/ab_${company_name}.log | grep "Server  
			 Software" `  " >> /tmp/web_server.log  
			     fi  
			     sleep  2  
			   }&  
			done  
			wait    
			echo "It is  OK "   
	  
	  
	2.说明&执行程序 
	通过ab  网站主页,然后把得到信息重定向到 一个文件中,然后再从该文件中过滤信息,统一记录到 /tmp/web_server.log中 
	  
	
	
		
			[root@localhost Desktop]# chmod a+x ab.sh  
			[root@localhost Desktop]# ./ab.sh > /dev/null   
	  
	  
	  
	3.执行结果,通过查看 /tmp/web_server.log 得到网站web server信息(有可能是反向代理服务器) 
	  
	
	
		
			[root@localhost Desktop]# cat /tmp/web_server.log  
			    360buy.com,,    Server Software:        jdws    
			    taobao.com,,    Server Software:        nginx    
			   mbaobao.com,,    Server Software:            
			     baidu.com,,    Server Software:        BWS/1.0    
			    renren.com,,    Server Software:        nginx/0.7.67    
			    google.com,,    Server Software:        gws    
			  changyou.com,,    Server Software:        Apache    
			      sina.com,,    Server Software:        Apache/2.0.63    
			   alibaba.com,,    Server Software:        Apache/2.2.15    
			       126.com,,    Server Software:        Apache    
			     vancl.com,,    Server Software:        Microsoft-IIS/6.0    
			    wanmei.com,,    Server Software:        nginx    
			       sdo.com,,    Server Software:        SVS    
			      sohu.com,,    Server Software:        SWS    
			  dangdang.com,,    Server Software:        nginx/0.7.61    
			     yahoo.com,,    Server Software:        YTS/1.20.0    
			       139.com,,    Server Software:        Apache    
			       163.com,,    Server Software:        nginx     
	  
	  
	4.简单分析结果 
	sohu.com       用的 web server 是 SWS  (这个有可能是自己开发的,有可能是用的SWS) 
	google.com    用的 web server 是 gws   
	baidu.com      用的 web server 是 BWS 
	taobao.com   用的 web server 是 nginx 
	还有很多,看3的结果 
	  
	  
	三.伪多线程与单进程脚本对比 
	1.先看单进程的程序 
	vi  1_ab.sh 
	  
	
	
		
			#!/bin/bash  
			#author         longxibendi   
			#blog           http://blog.csdn.net/longxibendi  
			#function       get the  web servers information of companies'  
			if [ -e  /tmp/web_server.log  ] ;then  
			     rm  -f  /tmp/web_server.log  
			fi  
			if [ -e  /tmp/ab.log ] ; then  
			     rm  -f   /tmp/ab.log  
			fi  
			for company_name in   taobao  360buy   dangdang  vancl  mbaobao alibaba google  baidu  yahoo  sina   sohu   renren   changyou wanmei  sdo 126 163 139   
			do  
			 #  {  
			     ab -n 1 -c 1   http:  
			     if [ $? -eq 0  ] ; then  
			       printf  "%10s,   %20s/n"    "${company_name}.com",   " ` cat  /tmp/ab.log | grep "Server Software" `  " >> /tmp/web_server.log  
			     fi  
			#     sleep  3  
			 #  }&  
			done  
			wait    
			echo "It is  OK "   
	  
	  
	2.说明 
	上面的单进程程序,只用一个进程实现,很简单就不解释了。具体统计web server实现与伪多线程程序类似 
	  
	3.两程序执行效率对比 
	  
	
	
		
			[root@localhost Desktop]# time -p ./1_ab.sh > /dev/null  
			real 12.54  
			user 0.03  
			sys 1.74  
			[root@localhost Desktop]# time -p ./ab.sh > /dev/null  
			real 11.20  
			user 0.07  
			sys 2.00   
	  
	  
	4.分析 
	可以看到 伪多线程程序明显比单进程程序快 
	其实,如果在执行 ab.sh的时候可以 ps -ef | grep  ab.sh一下,就会发现不同了 
	  
	
	
		
			[root@localhost ~]# ps -ef | grep  ab.sh  
			root     26846  5761  1 02:15 pts/1    00:00:00 /bin/bash ./ab.sh  
			root     26854 26846  0 02:15 pts/1    00:00:00 /bin/bash ./ab.sh  
			root     26857 26846  0 02:15 pts/1    00:00:00 /bin/bash ./ab.sh  
			root     26859 26846  0 02:15 pts/1    00:00:00 /bin/bash ./ab.sh  
			root     26865 26846  0 02:15 pts/1    00:00:00 /bin/bash ./ab.sh  
			root     26871 26846  0 02:15 pts/1    00:00:00 /bin/bash ./ab.sh  
			root     26874 26846  0 02:15 pts/1    00:00:00 /bin/bash ./ab.sh  
			root     26875 26846  0 02:15 pts/1    00:00:00 /bin/bash ./ab.sh  
			root     26886 26846  0 02:15 pts/1    00:00:00 /bin/bash ./ab.sh  
			root     26889 26846  0 02:15 pts/1    00:00:00 /bin/bash ./ab.sh  
			root     26892 26846  0 02:15 pts/1    00:00:00 /bin/bash ./ab.sh  
			root     26895 26846  0 02:15 pts/1    00:00:00 /bin/bash ./ab.sh  
			root     26898 26846  0 02:15 pts/1    00:00:00 /bin/bash ./ab.sh  
			root     26899 26846  0 02:15 pts/1    00:00:00 /bin/bash ./ab.sh  
			root     26916 26846  0 02:15 pts/1    00:00:00 /bin/bash ./ab.sh  
			root     26952 22657  0 02:15 pts/3    00:00:00 grep ab.sh   
	  
	可以发现: 伪多线程程序 ab.sh 是 由 进程号为  26846 的进程生成一些子进程,这些子进程来执行,完成统计工作。 
	而单进程程序 1_ab.sh  是由 一个进程自己完成所有工作。 
	  
	如果仔细阅读上述程序  ab.sh   和   1_ab.sh   ,会发现另一个不同点,1_ab.sh 只需要写入两个文件 ,而 ab.sh 则需要写入19个文件,这是因为ab.sh每个子进程自己维护一个  ab_$company_name.log文件,也就是说,每一次 ab 一个 网址,比如 baidu 那么这次执行结果就 重定向到 ab_baidu.com.log中。这样 ab.sh 要打开的文件次数比1_ab.sh多。所以下面总结一下这两程序的不同点。 
	5.ab.sh与1_ab.sh不同 
	其一.ab.sh是伪多线程的(一个母进程,多个子进程),而 1_ab.sh是单一进程  所以ab.sh是不是应该比1_ab.sh快呢?是不是可以实现并行处理呢? 
	  
	其二. ab.sh打开的文件多,需要写入的文件多,而1_ab.sh写入的文件少。是不是1_ab.sh连续写的数据多,ab.sh随机写的数据多?那为什么ab.sh比1_ab.sh快呢? 
	  
	声明:本文档可以随意更改,但必须署名原作者 
	作者:凤凰舞者 qq:578989855(责任编辑:IT) |