From 012e954b472d2e47b03647ac0c8f745416d7db58 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Sun, 2 Jun 2019 13:48:47 +1200 Subject: [PATCH] Improved fiber benchmarks. Increase number of iterations. --- benchmark/fiber_chain.rb | 40 -------------------------------- benchmark/fiber_chain.yml | 36 ++++++++++++++++++++++++++++ benchmark/vm2_fiber_allocate.yml | 8 +++++++ benchmark/vm2_fiber_count.yml | 8 +++++++ benchmark/vm2_fiber_reuse.yml | 14 +++++++++++ benchmark/vm2_fiber_switch.yml | 2 +- bootstraptest/test_fiber.rb | 39 +++++++++++++++++++++++++++++++ bootstraptest/test_thread.rb | 21 ----------------- 8 files changed, 106 insertions(+), 62 deletions(-) delete mode 100755 benchmark/fiber_chain.rb create mode 100755 benchmark/fiber_chain.yml create mode 100644 benchmark/vm2_fiber_allocate.yml create mode 100644 benchmark/vm2_fiber_count.yml create mode 100644 benchmark/vm2_fiber_reuse.yml create mode 100644 bootstraptest/test_fiber.rb diff --git a/benchmark/fiber_chain.rb b/benchmark/fiber_chain.rb deleted file mode 100755 index 7e0a7f9d45..0000000000 --- a/benchmark/fiber_chain.rb +++ /dev/null @@ -1,40 +0,0 @@ -# Check performance of fiber creation and transfer. - -def make_link(previous) - Fiber.new do - while message = previous.resume - Fiber.yield(message) - end - end -end - -def make_chain(length, &block) - chain = Fiber.new(&block) - - (length - 1).times do - chain = make_link(chain) - end - - return chain -end - -def run_benchmark(length, repeats, message = :hello) - chain = nil - - chain = make_chain(length) do - while true - Fiber.yield(message) - end - end - - repeats.times do - abort "invalid result" unless chain.resume == message - end -end - -n = (ARGV[0] || 1000).to_i -m = (ARGV[1] || 1000).to_i - -5.times do - run_benchmark(n, m) -end diff --git a/benchmark/fiber_chain.yml b/benchmark/fiber_chain.yml new file mode 100755 index 0000000000..e526551da4 --- /dev/null +++ b/benchmark/fiber_chain.yml @@ -0,0 +1,36 @@ +prelude: | + def make_link(previous) + Fiber.new do + while message = previous.resume + Fiber.yield(message) + end + end + end + + def make_chain(length = 1000, &block) + chain = Fiber.new(&block) + + (length - 1).times do + chain = make_link(chain) + end + + return chain + end + + message = "Hello World!" + + chain = make_chain do + while true + Fiber.yield(message) + end + end +benchmark: + make_chain: | + make_chain(100) do + while true + Fiber.yield(message) + end + end + resume_chain: | + chain.resume +loop_count: 5000 diff --git a/benchmark/vm2_fiber_allocate.yml b/benchmark/vm2_fiber_allocate.yml new file mode 100644 index 0000000000..f29705f694 --- /dev/null +++ b/benchmark/vm2_fiber_allocate.yml @@ -0,0 +1,8 @@ +prelude: | + # Disable GC to see raw throughput: + GC.disable +benchmark: + vm2_fiber_allocate: | + fiber = Fiber.new{Fiber.yield} + fiber.resume +loop_count: 100000 diff --git a/benchmark/vm2_fiber_count.yml b/benchmark/vm2_fiber_count.yml new file mode 100644 index 0000000000..2005772e84 --- /dev/null +++ b/benchmark/vm2_fiber_count.yml @@ -0,0 +1,8 @@ +prelude: | + fibers = [] +benchmark: + vm2_fiber_count: | + fiber = Fiber.new{Fiber.yield} + fibers << fiber + fiber.resume +loop_count: 100000 diff --git a/benchmark/vm2_fiber_reuse.yml b/benchmark/vm2_fiber_reuse.yml new file mode 100644 index 0000000000..5dbdd5dfc5 --- /dev/null +++ b/benchmark/vm2_fiber_reuse.yml @@ -0,0 +1,14 @@ +prelude: | + GC.disable + fibers = [] +benchmark: + vm2_fiber_reuse: | + 1024.times do + fiber = Fiber.new{Fiber.yield} + fibers << fiber + fiber.resume + end + + fibers.clear + GC.start +loop_count: 200 diff --git a/benchmark/vm2_fiber_switch.yml b/benchmark/vm2_fiber_switch.yml index f3e4c91283..fbf7283c29 100644 --- a/benchmark/vm2_fiber_switch.yml +++ b/benchmark/vm2_fiber_switch.yml @@ -6,4 +6,4 @@ prelude: | benchmark: vm2_fiber_switch: | fib.resume -loop_count: 6000000 +loop_count: 20000000 diff --git a/bootstraptest/test_fiber.rb b/bootstraptest/test_fiber.rb new file mode 100644 index 0000000000..35e1bf6851 --- /dev/null +++ b/bootstraptest/test_fiber.rb @@ -0,0 +1,39 @@ +show_limit %q{ + fibers = [] + begin + fiber = Fiber.new{Fiber.yield} + fiber.resume + fibers << fiber + + raise Exception, "skipping" if fibers.count >= 10_000 + rescue Exception => error + puts "Fiber count: #{fibers.count} (#{error})" + break + end while true +} + +assert_equal %q{ok}, %q{ + Fiber.new{ + }.resume + :ok +} + +assert_equal %q{ok}, %q{ + 10_000.times.collect{Fiber.new{}} + :ok +} + +assert_equal %q{ok}, %q{ + fibers = 100.times.collect{Fiber.new{Fiber.yield}} + fibers.each(&:resume) + fibers.each(&:resume) + :ok +} + +assert_normal_exit %q{ + at_exit { Fiber.new{}.resume } +} + +assert_normal_exit %q{ + Fiber.new(&Object.method(:class_eval)).resume("foo") +}, '[ruby-dev:34128]' diff --git a/bootstraptest/test_thread.rb b/bootstraptest/test_thread.rb index 03fb441441..3ff55bab11 100644 --- a/bootstraptest/test_thread.rb +++ b/bootstraptest/test_thread.rb @@ -9,19 +9,6 @@ show_limit %q{ break end while true } -show_limit %q{ - fibers = [] - begin - fiber = Fiber.new{Fiber.yield} - fiber.resume - fibers << fiber - - raise Exception, "skipping" if fibers.count >= 10_000 - rescue Exception => error - puts "Fiber count: #{fibers.count} (#{error})" - break - end while true -} assert_equal %q{ok}, %q{ Thread.new{ }.join @@ -323,10 +310,6 @@ assert_equal 'ok', %q{ m }, '[ruby-dev:34492]' -assert_normal_exit %q{ - at_exit { Fiber.new{}.resume } -} - assert_normal_exit %q{ g = enum_for(:local_variables) loop { g.next } @@ -352,10 +335,6 @@ assert_normal_exit %q{ loop { g.next } }, '[ruby-dev:34128]' -assert_normal_exit %q{ - Fiber.new(&Object.method(:class_eval)).resume("foo") -}, '[ruby-dev:34128]' - assert_normal_exit %q{ Thread.new("foo", &Object.method(:class_eval)).join }, '[ruby-dev:34128]'