ruby写的HTTP的压力测试工具

上一篇 / 下一篇  2014-11-12 12:58:31 / 个人分类:自动化测试

#encoding:utf-8
##liuyb
##20141108
require 'net/http'
require 'thread'
require 'open-uri'
require 'date'
require 'timeout'
require 'optparse'

require 'optparse'

  def parse_args  ##用于做使用说明
    #@threads=
    usage = false

    pts = OptionParser.new
    opts.on("-p THREADSNUM",Integer, "The number of threads.") do |val|
      @threads = val
    end
    
    opts.on("--dir DIRECTORY", "Saved url for presstest.") do |val|
      @dir = val
    end


    opts.on("--time RUNTIME",Integer, "Run time of the presstest.") do |val|
       @runtime = val
    end
    
    opts.on("--rand RandomNumber",Integer, "The random number of the pressurl.") do |val|
      @randnum = val
    end

    opts.on("-?", "--help", "Display this help text.") {usage = true}

    begin
      opts.parse(ARGV)
    rescue StandardError => e
      puts "Error while parsing command line options:\n" + e.message + "\n\n"
      puts opts.to_s
      exit 1
    end
    if usage
      puts opts.to_s
      exit 0
    end

  end

$normal=[]  #定义200请求存放的数组
$fail=[] #定义非200的请求类型

system(">log") ##存储下载的get下来的的HTML文件,最后计算吞吐量
def res(url)  
url=URI.parse(url)
Net::HTTP.start(url.host, url.port) {|http|
#http.read_timeout = 8
#http.open_timeout = 3  
response = http.get(url.path+"?"+url.query)
cod = response.code
f=File.open("log","a")
f.puts(response.body)#将HTML的body写入Log文件

if cod == "200"
$normal<<cod
else
$fail<<cod
end
}
end




def run
        $total_time_begin = Time.now.to_i
parse_args  #执行parse_args
f=open(@dir,"r") #@dir是parse_args传过来的参数,就是url存储文件
all=f.readlines
lon=all.size
queue = Queue.new 
lon.times do |nu| 
queue.push(all[nu])
end

threads = []
threadNums = @threads.to_i   #@threads也是通过opt_parse参数传过来的并发数
len=[]
threadNums.times do
j=0
#$time=$total_time_end - $total_time_begin
#unless queue.empty?
threads<<Thread.new do  #创建多线程
while(1)
$end=Time.now.to_i
$dua=$end - $total_time_begin #计算执行时间
if $dua <= @runtime.to_i  #如果执行时间小于@runtime传过来的数字就继续
url=all[rand(@randnum.to_i)].chomp("\n")#随机从文件中读取一个url
#puts "the #{rand(1022)} line is: #{url} "
begin     
timeout(0.6) do ##600ms    
res(url)
end     
rescue TimeoutError     
$fail<<"501"
#puts "Timed Out"     
end    
len<<"1"
$end=Time.now.to_i
else
break
puts " Dual time is #{$dua}\n"
end
end
$total_time_end=Time.now.to_i

end
j=j+1
end
threads.each{|t| t.join}
puts "\n"*6

datasize=File.size("log")
puts "All request is #{len.size}"
puts "The request status code of 200 :  #{$normal.size}"
puts "The unormal request status code :  #{$fail.size}"
$total_time_ends = Time.now.to_i
avgtime= ($total_time_ends - $total_time_begin)*1000 / len.size
puts "The average response time is : #{avgtime}ms"
puts "Thread number is : " + threadNums.to_s
qps= (len.size) / ($total_time_ends - $total_time_begin)
puts "The transmit datasize is : #{datasize/1024}K"
puts "Transmit data per second is #{datasize/1024/($total_time_ends - $total_time_begin)}K"
puts "Request per second is : #{qps}"
puts "Run time is :" + ($total_time_ends - $total_time_begin).to_s + "s"
end

run ##调起执行多线程


最后,其中的res里边为get请求,如果想做post请求或其它类型的请求,均可自己定义


TAG:

 

评分:0

我来说两句

Open Toolbar