rbm 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,45 @@
1
+ # Ruby Benchmark
2
+
3
+ rbm is a command line tool for doing quick benchmarks of ruby code.
4
+
5
+ # Installing
6
+
7
+ ## Recommended
8
+
9
+ gem install rbm
10
+
11
+ ## Edge
12
+
13
+ git clone https://github.com/samuelkadolp/rbm.git
14
+ cd rbm && rake install
15
+
16
+ # Usage
17
+
18
+ Using rbm is quite simple. Just pass in each code fragment you want to test as a separate argument.
19
+
20
+ rbm "sleep 1" "sleep 5"
21
+
22
+ You can specify the number of times to loop. You can also provide a code fragment to be run before each time each code fragment is run.
23
+
24
+ rbm --times 100 "sleep 0.01"
25
+ rbm --prerun "n = 5" "sleep n"
26
+
27
+ You may provide a name to each code fragment to be displayed on the benchmark.
28
+
29
+ rbm --name "sleep for 5" "sleep 5" --name "sleep for 2" "sleep 2"
30
+
31
+ You can see the full usage statement at any time with `rbm --help`
32
+
33
+ Usage: rbm [options] [--name name] code [[-N name] code...]
34
+
35
+ Ruby Options:
36
+ -r, --require file[,file] Files to require before benchmarking
37
+ -I, --load-path path[,path] Paths to append to $LOAD_PATH
38
+
39
+ Benchmark Options:
40
+ -n, --times N Number of times to run each code fragment
41
+ -p, --pre code Code to run before each code fragment to be benchmarked
42
+ -N, --name name Names the following code fragment
43
+
44
+ General Options:
45
+ -h, --help Show this message
@@ -0,0 +1,22 @@
1
+ This is free and unencumbered software released into the public domain.
2
+
3
+ Anyone is free to copy, modify, publish, use, compile, sell, or
4
+ distribute this software, either in source code form or as a compiled
5
+ binary, for any purpose, commercial or non-commercial, and by any
6
+ means.
7
+
8
+ In jurisdictions that recognize copyright laws, the author or authors
9
+ of this software dedicate any and all copyright interest in the
10
+ software to the public domain. We make this dedication for the benefit
11
+ of the public at large and to the detriment of our heirs and
12
+ successors. We intend this dedication to be an overt act of
13
+ relinquishment in perpetuity of all present and future rights to this
14
+ software under copyright law.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20
+ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21
+ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
data/bin/rbm ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "rbm"
4
+
5
+ RBM.start
@@ -0,0 +1,78 @@
1
+ require 'optparse'
2
+ require 'rbm/benchmarker'
3
+
4
+ module RBM
5
+ class << self
6
+ def start
7
+ fragments, options = parse_options(ARGV)
8
+
9
+ if load_paths = options.delete(:load_paths)
10
+ $LOAD_PATH.concat(load_paths)
11
+ end
12
+
13
+ begin
14
+ if requires = options.delete(:requires)
15
+ requires.each { |r| require(r) }
16
+ end
17
+ rescue LoadError => e
18
+ puts e
19
+ exit
20
+ end
21
+
22
+ begin
23
+ Benchmarker.new(fragments, options).run
24
+ rescue BenchmarkerSyntaxError => e
25
+ puts e
26
+ exit
27
+ end
28
+ end
29
+
30
+ private
31
+ def parse_options(args)
32
+ fragments, options = [], {}
33
+ current_name = nil
34
+
35
+ op = OptionParser.new do |op|
36
+ op.banner = "Usage: #{op.program_name} [options] [--name name] code [[-N name] code...]"
37
+
38
+ op.separator ''
39
+ op.separator 'Ruby Options:'
40
+
41
+ op.on('-r', '--require file[,file]', Array, 'Files to require before benchmarking') do |list|
42
+ (options[:requires] ||= []).concat(list)
43
+ end
44
+
45
+ op.on('-I', '--load-path path[,path]', Array, 'Paths to append to $LOAD_PATH') do |list|
46
+ (options[:load_paths] ||= []).concat(list)
47
+ end
48
+
49
+ op.separator ''
50
+ op.separator 'Benchmark Options:'
51
+
52
+ op.on('-n', '--times N', Integer, 'Number of times to run each code fragment') { |n| options[:times] = n }
53
+ op.on('-p', '--pre code', String, 'Code to run before each code fragment to be benchmarked') { |p| options[:prerun] = p }
54
+ op.on('-N', '--name name', String, 'Names the following code fragment') { |n| current_name = n }
55
+
56
+ op.separator ''
57
+ op.separator 'General Options:'
58
+
59
+ op.on('-h', '--help', 'Show this message') do
60
+ puts op
61
+ exit
62
+ end
63
+ end
64
+ op.order!(args) do |fragment|
65
+ # block gets run for each non-option argument
66
+ fragments << [current_name || '', fragment]
67
+ current_name = nil
68
+ end
69
+
70
+ if fragments.empty?
71
+ puts op
72
+ exit
73
+ end
74
+
75
+ [fragments, options]
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,73 @@
1
+ require 'benchmark'
2
+
3
+ module RBM
4
+ class Benchmarker
5
+ DEFAULT_OPTIONS = {
6
+ :times => 1
7
+ }
8
+
9
+ attr_reader :options
10
+
11
+ def initialize(fragments, options)
12
+ @options = DEFAULT_OPTIONS.merge(options)
13
+
14
+ compile_prerun(options[:prerun])
15
+ @fragments = fragments.map { |name, fragment| [name, compile_fragment(fragment, options[:prerun], name)] }
16
+ end
17
+
18
+ def run
19
+ width = @fragments.map { |name, method| name.size }.max
20
+ Benchmark.bm(width) do |bm|
21
+ @fragments.each do |name, method|
22
+ bm.report(name) do
23
+ # TODO: wrap errors from fragment execution
24
+ options[:times].times { send(method) }
25
+ end
26
+ end
27
+ end
28
+ end
29
+
30
+ private
31
+ def compile_prerun(prerun)
32
+ # TODO:
33
+ # another way to check prerun syntax outside of compile_fragment so we can
34
+ # give meaningful syntax errors (error in prerun not in a fragment itself)
35
+ wrap_syntax_errors do
36
+ instance_eval <<-EVAL, "prerun", -1
37
+ def bm_prerun
38
+ #{prerun}
39
+ end
40
+ undef bm_prerun
41
+ EVAL
42
+ end
43
+ end
44
+
45
+ def compile_fragment(fragment, prerun = nil, name = nil)
46
+ name = (@fragment_name ||= "fragment_0").succ! unless name && !name.empty?
47
+ fragment_name = "bm_#{name.gsub(/\s+/, '_')}".to_sym
48
+ defined = instance_method(fragment_name) rescue false
49
+ raise "#{fragment_name} is already defined" if defined
50
+
51
+ wrap_syntax_errors do
52
+ # TODO: provide better runtime errors and NameError errors and cleaner stack trace
53
+ instance_eval <<-EVAL, name, -1
54
+ def #{fragment_name}
55
+ #{prerun}
56
+ #{fragment}
57
+ end
58
+ EVAL
59
+ end
60
+
61
+ fragment_name
62
+ end
63
+
64
+ def wrap_syntax_errors
65
+ yield
66
+ rescue SyntaxError => e
67
+ raise(BenchmarkerSyntaxError.new(e.to_s))
68
+ end
69
+ end
70
+
71
+ class BenchmarkerSyntaxError < SyntaxError
72
+ end
73
+ end
@@ -0,0 +1,3 @@
1
+ module RBM
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rbm
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Samuel Kadolph
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-03-14 00:00:00 -04:00
14
+ default_executable:
15
+ dependencies: []
16
+
17
+ description: Command line tool for doing quick ruby benchmarks. Provide each code fragment to run as a separate code fragment to rbm.See rbm --help for more information.
18
+ email:
19
+ - samuel@kadolph.com
20
+ executables:
21
+ - rbm
22
+ extensions: []
23
+
24
+ extra_rdoc_files: []
25
+
26
+ files:
27
+ - bin/rbm
28
+ - lib/rbm/benchmarker.rb
29
+ - lib/rbm/version.rb
30
+ - lib/rbm.rb
31
+ - README.md
32
+ - UNLICENSE
33
+ has_rdoc: true
34
+ homepage: https://github.com/samuelkadolph/rbm
35
+ licenses: []
36
+
37
+ post_install_message:
38
+ rdoc_options: []
39
+
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ requirements: []
55
+
56
+ rubyforge_project:
57
+ rubygems_version: 1.6.2
58
+ signing_key:
59
+ specification_version: 3
60
+ summary: Simple command line tool for quick ruby benchmarks.
61
+ test_files: []
62
+