红宝石。线程基准
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了红宝石。线程基准相关的知识,希望对你有一定的参考价值。
Connect to three sites using threads and without using them. Fetch the response. Measure the benchmark.
# encoding: utf-8 require 'net/http' require 'benchmark' class StatusFetcher attr_reader :result def initialize @result = [] end def self.start! new end end class ThreadStatusFetcher < StatusFetcher def initialize super threads = [] %w{ stackoverflow.com superuser.com systemfault.com }.each do |page| threads << Thread.new(page) do |url| http = Net::HTTP.new(url, 80) @result << "#{url}: #{http.get('/').message}" end end threads.each(&:join) end end class SimpleStatusFetcher < StatusFetcher def initialize super %w{ stackoverflow.com superuser.com systemfault.com }.each do |url| http = Net::HTTP.new(url, 80) @result << "#{url}: #{http.get('/').message}" end end end Benchmark.bm(8) do |r| r.report('simple') { SimpleStatusFetcher.start!.result } r.report('thread') { ThreadStatusFetcher.start!.result } end
以上是关于红宝石。线程基准的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 omniauth / oauth 对每秒登录次数进行基准测试? (红宝石+rspec)
用 Java 衡量单线程复杂算法的最佳宏基准测试工具/框架是啥? [关闭]
newCacheThreadPool()newFixedThreadPool()newScheduledThreadPool()newSingleThreadExecutor()自定义线程池(代码片段