Created
July 8, 2018 16:34
-
-
Save bibendi/79c09b093cc07644aea2ccea2f7b0bb0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'benchmark/ips' | |
require 'open-uri' | |
require 'bigdecimal' | |
require 'bigdecimal/math' | |
class Contract | |
DIGITS = 10_000 | |
URL = 'http://www.nooooooooooooooo.com/'.freeze | |
def call(parallel_io_only:, cpu_count:, io_count:) | |
threads = [] | |
io_count.times { threads << Thread.new { io_meth } } | |
if parallel_io_only | |
cpu_count.times { cpu_meth } | |
else | |
cpu_count.times { threads << Thread.new { cpu_meth } } | |
end | |
threads.each(&:join) | |
end | |
private | |
def cpu_meth | |
BigMath.PI(DIGITS) | |
end | |
def io_meth | |
open(URL) | |
end | |
end | |
[ | |
{cpu_count: 2, io_count: 6}, | |
{cpu_count: 3, io_count: 3}, | |
{cpu_count: 6, io_count: 2} | |
].each do |options| | |
Benchmark.ips do |x| | |
x.report("parallel_io_only: true, options: #{options}") do | |
::Contract.new.call(parallel_io_only: true, **options) | |
end | |
x.report("parallel_io_only: false, options: #{options}") do | |
::Contract.new.call(parallel_io_only: false, **options) | |
end | |
x.compare! | |
end | |
end |
Author
bibendi
commented
Jul 8, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment