2020-05-14 06:10:55 -04:00
|
|
|
|
|
|
|
require 'benchmark'
|
|
|
|
|
|
|
|
TOPICS = ["cats", "dogs", "pigs", "skeletons", "zombies", "ocelots", "villagers", "pillagers"]
|
|
|
|
|
|
|
|
require 'net/http'
|
|
|
|
require 'uri'
|
|
|
|
require 'json'
|
|
|
|
|
|
|
|
require_relative 'scheduler'
|
|
|
|
|
|
|
|
def fetch_topics(topics)
|
|
|
|
responses = {}
|
|
|
|
|
|
|
|
topics.each do |topic|
|
|
|
|
Fiber.new(blocking: Fiber.current.blocking?) do
|
|
|
|
uri = URI("https://www.google.com/search?q=#{topic}")
|
2021-02-09 01:39:56 -05:00
|
|
|
response = Net::HTTP.get(uri)
|
|
|
|
responses[topic] = response.scan(topic).size
|
2020-05-14 06:10:55 -04:00
|
|
|
end.resume
|
|
|
|
end
|
|
|
|
|
2021-02-09 01:39:56 -05:00
|
|
|
Fiber.scheduler&.run
|
2020-05-14 06:10:55 -04:00
|
|
|
|
|
|
|
return responses
|
|
|
|
end
|
|
|
|
|
|
|
|
def sweep(repeats: 3, **options)
|
|
|
|
times = (1..8).map do |i|
|
2021-02-09 01:39:56 -05:00
|
|
|
$stderr.puts "Measuring #{i} topic(s) #{options.inspect}..."
|
2020-05-14 06:10:55 -04:00
|
|
|
topics = TOPICS[0...i]
|
|
|
|
|
|
|
|
Thread.new do
|
|
|
|
Benchmark.realtime do
|
|
|
|
scheduler = Scheduler.new
|
2021-02-09 01:39:56 -05:00
|
|
|
Fiber.set_scheduler(scheduler)
|
2020-05-14 06:10:55 -04:00
|
|
|
|
|
|
|
repeats.times do
|
|
|
|
Fiber.new(**options) do
|
|
|
|
pp fetch_topics(topics)
|
|
|
|
end.resume
|
|
|
|
|
|
|
|
scheduler.run
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end.value / repeats
|
|
|
|
end
|
|
|
|
|
|
|
|
puts options.inspect
|
|
|
|
puts JSON.dump(times.map{|value| value.round(3)})
|
|
|
|
end
|
|
|
|
|
2021-02-09 01:39:56 -05:00
|
|
|
# sweep(blocking: true)
|
2020-05-14 06:10:55 -04:00
|
|
|
sweep(blocking: false)
|