easy_bench 1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/easy_bench.rb +121 -0
  2. metadata +76 -0
data/lib/easy_bench.rb ADDED
@@ -0,0 +1,121 @@
1
+ require 'forwardable'
2
+ require 'sometimes_memoize'
3
+
4
+ class EasyBench
5
+ extend Forwardable
6
+
7
+ VERSION = '1.0'
8
+
9
+ attr_reader :dataset
10
+
11
+ def initialize(title=nil)
12
+ @dataset = DataSet.new(title)
13
+ end
14
+
15
+ def caller
16
+ start_time = Time.now
17
+ res = yield
18
+ elapsed_time = Time.now - start_time
19
+
20
+ @dataset.runs << elapsed_time
21
+ if @dataset.min_run_time.nil? || @dataset.min_run_time > elapsed_time
22
+ @dataset.min_run_time = elapsed_time
23
+ end
24
+ if @dataset.max_run_time.nil? || @dataset.max_run_time < elapsed_time
25
+ @dataset.max_run_time = elapsed_time
26
+ end
27
+
28
+ res
29
+ end
30
+
31
+ def_delegators :dataset, :report, :stats
32
+
33
+ def self.log_times(logger, label)
34
+ start_time = Time.now
35
+ logger.info('Starting ' + label + ' at ' + start_time.to_s)
36
+ res = yield
37
+ end_time = Time.now
38
+ logger.info('Finished ' + label + ' at ' + end_time.to_s + ' (' + (end_time - start_time).to_s + 's)')
39
+ res
40
+ end
41
+
42
+ class DataSet
43
+ include SometimesMemoize
44
+
45
+ attr_accessor :runs, :min_run_time, :max_run_time
46
+ attr_reader :title
47
+
48
+ def initialize(title)
49
+ @title = title
50
+ @runs = []
51
+ end
52
+
53
+ def total_runs
54
+ @runs.size
55
+ end
56
+
57
+ def total_run_time
58
+ @runs.inject(0.0){|sum, run| sum + run}
59
+ end
60
+ sometimes_memoize :total_run_time
61
+
62
+ def ave_run_time
63
+ (self.total_run_time / self.total_runs)
64
+ end
65
+
66
+ def report(bars=false)
67
+ memoizing do
68
+ if @title
69
+ puts @title
70
+ puts
71
+ end
72
+ print_table
73
+ if bars
74
+ puts
75
+ draw_bars
76
+ end
77
+ puts
78
+ end
79
+ end
80
+
81
+ def stats
82
+ memoizing do
83
+ {
84
+ :count => total_runs,
85
+ :total => total_run_time,
86
+ :min => min_run_time,
87
+ :max => max_run_time,
88
+ :mean => ave_run_time
89
+ }
90
+ end
91
+ end
92
+
93
+ def print_table
94
+ puts "num runs : #{total_runs}"
95
+ puts "total run time : #{total_run_time}s"
96
+ puts "min run time : #{min_run_time}s"
97
+ puts "max run time : #{max_run_time}s"
98
+ puts "ave run time : #{ave_run_time}s"
99
+ end
100
+
101
+ def draw_bars(include_values=false)
102
+ if total_runs > 0
103
+ band_height = (max_run_time - min_run_time) / 4.0
104
+ (0..4).to_a.reverse.each do |band|
105
+ bars = []
106
+ @runs.each do |run|
107
+ if run >= min_run_time + (band.to_f * band_height)
108
+ bars << '*'
109
+ else
110
+ bars << ' '
111
+ end
112
+ end
113
+ puts bars.join(' ')
114
+ end
115
+ end
116
+ puts runs.join(' ') if include_values
117
+ end
118
+
119
+ end
120
+
121
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: easy_bench
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 1
7
+ - 0
8
+ version: "1.0"
9
+ platform: ruby
10
+ authors:
11
+ - Ben Lund
12
+ - Academia, Inc
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-05-05 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: sometimes_memoize
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ version: "0"
31
+ type: :runtime
32
+ version_requirements: *id001
33
+ description: A quick and easy benchmarking library for Ruby. Useful for benchmarking only part of an iteration, and accumulating the data to report later in the code.
34
+ email: ben@academia.edu
35
+ executables: []
36
+
37
+ extensions: []
38
+
39
+ extra_rdoc_files: []
40
+
41
+ files:
42
+ - lib/easy_bench.rb
43
+ has_rdoc: true
44
+ homepage: http://github.com/academia-edu/easy_bench
45
+ licenses: []
46
+
47
+ post_install_message:
48
+ rdoc_options: []
49
+
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ segments:
58
+ - 0
59
+ version: "0"
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ segments:
66
+ - 0
67
+ version: "0"
68
+ requirements: []
69
+
70
+ rubyforge_project:
71
+ rubygems_version: 1.3.7
72
+ signing_key:
73
+ specification_version: 3
74
+ summary: Measure the call times of only some of the expressions in your interation -- the timer is explicitly started and stopped for the duration of a block. The result of the operation inside then benchmarked block is returned from the benchmarking call, so an EasyBench call can be wrapped around existing code with no changes. This style allows for interleaving of two or more alternatives being benchmarked within an interaction, which can correct for ramp-up effects you might see if you benchmarked the alternatives sequentially. Simple report output shows minimum, maximum, and mean call times. The report output can also include a simple bar chart to visually inspect the data for outliners (only really useful for small runs of 20 iterations or so)
75
+ test_files: []
76
+