-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathchunked_output_benchmark.rb
77 lines (60 loc) · 2.48 KB
/
chunked_output_benchmark.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env ruby
# frozen_string_literal: true
require_relative 'example_helper'
require 'benchmark'
WORDS = File.readlines('/usr/share/dict/words').map(&:chomp).freeze
COUNT = WORDS.count
module Common
def main_loop
if output[:current] < input[:limit]
consumed = yield
output[:current] += consumed
plan_event(nil)
suspend
end
end
def batch
WORDS.drop(output[:current]).take(input[:chunk])
end
end
class Regular < ::Dynflow::Action
include Common
def run(event = nil)
output[:current] ||= 0
output[:words] ||= []
main_loop do
words = batch
output[:words] << words
words.count
end
end
end
class Chunked < ::Dynflow::Action
include Common
def run(event = nil)
output[:current] ||= 0
main_loop do
words = batch
output_chunk(words)
words.count
end
end
end
if $0 == __FILE__
ExampleHelper.world.action_logger.level = 4
ExampleHelper.world.logger.level = 4
Benchmark.bm do |bm|
bm.report('regular 1000 by 100') { ExampleHelper.world.trigger(Regular, limit: 1000, chunk: 100).finished.wait }
bm.report('chunked 1000 by 100') { ExampleHelper.world.trigger(Chunked, limit: 1000, chunk: 100).finished.wait }
bm.report('regular 10_000 by 100') { ExampleHelper.world.trigger(Regular, limit: 10_000, chunk: 100).finished.wait }
bm.report('chunked 10_000 by 100') { ExampleHelper.world.trigger(Chunked, limit: 10_000, chunk: 100).finished.wait }
bm.report('regular 10_000 by 1000') { ExampleHelper.world.trigger(Regular, limit: 10_000, chunk: 1000).finished.wait }
bm.report('chunked 10_000 by 1000') { ExampleHelper.world.trigger(Chunked, limit: 10_000, chunk: 1000).finished.wait }
bm.report('regular 100_000 by 100') { ExampleHelper.world.trigger(Regular, limit: 100_000, chunk: 100).finished.wait }
bm.report('chunked 100_000 by 100') { ExampleHelper.world.trigger(Chunked, limit: 100_000, chunk: 100).finished.wait }
bm.report('regular 100_000 by 1000') { ExampleHelper.world.trigger(Regular, limit: 100_000, chunk: 1000).finished.wait }
bm.report('chunked 100_000 by 1000') { ExampleHelper.world.trigger(Chunked, limit: 100_000, chunk: 1000).finished.wait }
bm.report('regular 100_000 by 10_000') { ExampleHelper.world.trigger(Regular, limit: 100_000, chunk: 10_000).finished.wait }
bm.report('chunked 100_000 by 10_000') { ExampleHelper.world.trigger(Chunked, limit: 100_000, chunk: 10_000).finished.wait }
end
end