summaryrefslogtreecommitdiff
path: root/test/fiber/http.rb
diff options
context:
space:
mode:
authorSamuel Williams <samuel.williams@oriontransfer.co.nz>2020-05-15 12:23:42 +1200
committerSamuel Williams <samuel.williams@oriontransfer.co.nz>2020-05-15 13:26:23 +1200
commit6fa8455ebbf457e5d8752295a8d6380146636c0c (patch)
treea5c16f9d9e386b78d16986fc00ed31cbc4bf56a7 /test/fiber/http.rb
parent39365b46e250162f278cb36aa148bc2a92b1b84a (diff)
Move `test/scheduler` -> `test/fiber` [Bug #16892][ruby-core:98366].
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/3110
Diffstat (limited to 'test/fiber/http.rb')
-rwxr-xr-xtest/fiber/http.rb53
1 files changed, 53 insertions, 0 deletions
diff --git a/test/fiber/http.rb b/test/fiber/http.rb
new file mode 100755
index 00000000000..e2a007bc847
--- /dev/null
+++ b/test/fiber/http.rb
@@ -0,0 +1,53 @@
+
+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}")
+ responses[topic] = Net::HTTP.get(uri).scan(topic).size
+ end.resume
+ end
+
+ Thread.scheduler&.run
+
+ return responses
+end
+
+def sweep(repeats: 3, **options)
+ times = (1..8).map do |i|
+ $stderr.puts "Measuring #{i} topic(s)..."
+ topics = TOPICS[0...i]
+
+ Thread.new do
+ Benchmark.realtime do
+ scheduler = Scheduler.new
+ Thread.current.scheduler = scheduler
+
+ 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
+
+sweep(blocking: true)
+sweep(blocking: false)