Skip to content

Instantly share code, notes, and snippets.

@we4tech we4tech/Benchmark.txt
Last active Aug 2, 2018

Embed
What would you like to do?
Shows how to make a threaded call
## FYI: Not literally accurate. Because the API is coming from a non-constant setup. (A public API from httpbin)
user system total real
Sequential 10 calls
req:0: 200
req:1: 200
req:2: 200
req:3: 200
req:4: 200
req:5: 200
req:6: 200
req:7: 200
req:8: 200
req:9: 200
0.050000 0.010000 0.060000 ( 3.031101)
Threaded 10 calls with 1 threads
req:0: 200
req:1: 200
req:2: 200
req:3: 200
req:4: 200
req:5: 200
req:6: 200
req:7: 200
req:8: 200
req:9: 200
0.030000 0.010000 0.040000 ( 1.425694)
Threaded 10 calls with 2 threads
req:1: 200
req:0: 200
req:3: 200
req:2: 200
req:4: 200
req:5: 200
req:6: 200
req:7: 200
req:8: 200
req:9: 200
0.040000 0.020000 0.060000 ( 0.619645)
Threaded 10 calls with 3 threads
req:0: 200
req:2: 200
req:1: 200
req:3: 200
req:5: 200
req:4: 200
req:7: 200
req:6: 200
req:8: 200
req:9: 200
0.030000 0.010000 0.040000 ( 0.897210)
Threaded 10 calls with 4 threads
req:0: 200
req:1: 200
req:3: 200
req:2: 200
req:4: 200
req:5: 200
req:7: 200
req:6: 200
req:8: 200
req:9: 200
0.030000 0.010000 0.040000 ( 0.390123)
Threaded 10 calls with 5 threads
req:3: 200
req:4: 200
req:0: 200
req:2: 200
req:5: 200
req:8: 200
req:7: 200
req:6: 200
req:9: 200
req:1: 200
0.030000 0.010000 0.040000 ( 0.580215)
Threaded 10 calls with 6 threads
req:0: 200
req:1: 200
req:2: 200
req:3: 200
req:4: 200
req:5: 200
req:6: 200
req:9: 200
req:7: 200
req:8: 200
0.030000 0.010000 0.040000 ( 0.272985)
Threaded 10 calls with 7 threads
req:3: 200
req:2: 200
req:1: 200
req:5: 200
req:4: 200
req:0: 200
req:6: 200
req:7: 200
req:8: 200
req:9: 200
0.030000 0.010000 0.040000 ( 0.311916)
Threaded 10 calls with 8 threads
req:2: 200
req:5: 200
req:0: 200
req:6: 200
req:7: 200
req:1: 200
req:4: 200
req:3: 200
req:8: 200
req:9: 200
0.030000 0.010000 0.040000 ( 0.245650)
Threaded 10 calls with 9 threads
req:2: 200
req:3: 200
req:1: 200
req:0: 200
req:6: 200
req:8: 200
req:7: 200
req:4: 200
req:5: 200
req:9: 200
0.030000 0.010000 0.040000 ( 0.232136)
Threaded 10 calls with 10 threads
req:1: 200
req:6: 200
req:0: 200
req:9: 200
req:3: 200
req:7: 200
req:5: 200
req:2: 200
req:4: 200
req:8: 200
0.030000 0.020000 0.050000 ( 0.136998)
require 'benchmark'
require 'uri'
require 'net/http'
def get(url)
Net::HTTP.get_response(URI(url))
end
def sequential_call
10.times do |i|
puts "req:#{i}: #{get('https://httpbin.org/get').code}"
end
end
def threaded_call(workers = 1)
url = 'https://httpbin.org/get'
req_queue = Queue.new
result_queue = Queue.new
threads = []
10.times { |i| req_queue << ["req:#{i}", url] }
workers.times { req_queue << :quit }
threads = Array.new(workers) do
Thread.new do
while (req = req_queue.pop) do
break if req == :quit
result_queue << [req.first, get(req.last)]
end
end
end
threads.map(&:join)
while (res = result_queue.pop(true) rescue nil) do
puts "#{res.first}: #{res.last.code}"
end
end
Benchmark.bm do |x|
x.report("Sequential 10 calls\n") { sequential_call }
1.upto(10) do |i|
x.report("Threaded 10 calls with #{i} threads\n") { threaded_call(i) }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.