rbm 0.0.3 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/bin/rbm CHANGED
@@ -2,4 +2,4 @@
2
2
 
3
3
  require "rbm"
4
4
 
5
- RBM.start
5
+ RBM.start(ARGV)
data/lib/rbm.rb CHANGED
@@ -1,14 +1,16 @@
1
1
  require "optparse"
2
- require "rbm/benchmarker"
3
- require "rbm/version"
4
2
 
5
3
  module RBM
4
+ require "rbm/benchmarker"
5
+ require "rbm/fragment"
6
+ require "rbm/version"
7
+
6
8
  class << self
7
- def start
8
- fragments, options = parse_options(ARGV)
9
+ def start(args)
10
+ fragments, options = parse_options(args)
9
11
 
10
12
  if load_paths = options.delete(:load_paths)
11
- $LOAD_PATH.concat(load_paths)
13
+ $LOAD_PATH.unshift(load_paths)
12
14
  end
13
15
 
14
16
  begin
@@ -16,8 +18,8 @@ module RBM
16
18
  requires.each { |r| require(r) }
17
19
  end
18
20
  rescue LoadError => e
19
- puts e
20
- exit
21
+ $stderr.puts(e)
22
+ exit 1
21
23
  end
22
24
 
23
25
  Benchmarker.new(fragments, options).run
@@ -25,7 +27,8 @@ module RBM
25
27
 
26
28
  private
27
29
  def parse_options(args)
28
- fragments, options = [], { :requires => [], :load_paths => [] }
30
+ fragments = []
31
+ options = { :requires => [], :load_paths => [] }
29
32
  current_name, current_prerun, current_postrun = nil, nil, nil
30
33
 
31
34
  op = OptionParser.new do |op|
@@ -37,9 +40,6 @@ module RBM
37
40
  op.on("-r", "--require file[,file]", Array, "Files to require before benchmarking") { |a| options[:requires].concat(a) }
38
41
  op.on("-I", "--load-path path[,path]", Array, "Paths to append to $LOAD_PATH") { |a| options[:load_paths].concat(a) }
39
42
 
40
- # op.separator ""
41
- # op.separator "Benchmark Options:"
42
-
43
43
  op.separator ""
44
44
  op.separator "Code Fragment Options:"
45
45
 
@@ -62,15 +62,15 @@ module RBM
62
62
  exit
63
63
  end
64
64
  end
65
- op.order!(args) do |fragment|
65
+ op.order!(args) do |code|
66
66
  # block gets run for each non-option argument
67
- fragments << { :name => current_name, :prerun => current_prerun, :postrun => current_postrun, :fragment => fragment }
67
+ fragments << Fragment.new(code, current_name, current_prerun, current_postrun)
68
68
  current_name, current_prerun, current_postrun = nil, nil, nil
69
69
  end
70
70
 
71
71
  if fragments.empty?
72
72
  puts op
73
- exit
73
+ exit 1
74
74
  end
75
75
 
76
76
  [fragments, options]
@@ -1,4 +1,4 @@
1
- require 'benchmark'
1
+ require "benchmark"
2
2
 
3
3
  module RBM
4
4
  class Benchmarker
@@ -8,31 +8,15 @@ module RBM
8
8
 
9
9
  attr_reader :fragments, :options
10
10
 
11
- def initialize(fragments, options)
12
- @fragments, @options = fragments, DEFAULT_OPTIONS.merge(options)
11
+ def initialize(fragments, options = {})
12
+ @fragments = fragments
13
+ @options = self.class::DEFAULT_OPTIONS.merge(options)
13
14
  end
14
15
 
15
16
  def run
16
- width = fragments.map { |fragment| (fragment[:name] || "").length }.max
17
- Benchmark.bm(width) do |bm|
17
+ Benchmark.bmbm do |bm|
18
18
  fragments.each do |fragment|
19
- name = fragment[:name] || ""
20
- fragment_name = (fragment[:name] || (@unnamed_fragment ||= "fragment_0").succ!).gsub(/\s+/, "_")
21
-
22
- object = Object.new
23
- binding = object.send(:binding)
24
-
25
- bm.report(name) do
26
- eval options[:init], binding, "init", 1 if options[:init]
27
- eval fragment[:prerun], binding, "#{fragment_name}_prerun", 1 if fragment[:prerun]
28
- eval <<-EVAL, binding, fragment_name, 0
29
- #{options[:times]}.times do
30
- #{fragment[:fragment]}
31
- end
32
- EVAL
33
- eval fragment[:postrun], binding, "#{fragment_name}_postrun", 1 if fragment[:postrun]
34
- eval options[:cleanup], binding, "cleanup", 1 if options[:cleanup]
35
- end
19
+ fragment.run(bm, options[:times], options[:init], options[:cleanup])
36
20
  end
37
21
  end
38
22
  end
@@ -0,0 +1,24 @@
1
+ module RBM
2
+ class Fragment
3
+ @@unnamed_fragment = "fragment_0"
4
+
5
+ attr_reader :code, :name, :prerun, :postrun
6
+
7
+ def initialize(code, name = nil, prerun = nil, postrun = nil)
8
+ @code, @name, @prerun, @postrun = code, name, prerun, postrun
9
+ end
10
+
11
+ def run(bm, times, init = nil, cleanup = nil)
12
+ fragment_name = (name || @@unnamed_fragment.succ!).gsub(/\s+/, "_")
13
+ object = Object.new
14
+
15
+ bm.report(name) do
16
+ object.instance_eval(init, "#{fragment_name}_init", 0) if init
17
+ object.instance_eval(prerun, "#{fragment_name}_prerun", 0) if prerun
18
+ object.instance_eval("#{times}.times { #{code} }", "#{fragment_name}", 0)
19
+ object.instance_eval(postrun, "#{fragment_name}_postrun", 0) if postrun
20
+ object.instance_eval(cleanup, "#{fragment_name}_cleanup", 0) if cleanup
21
+ end
22
+ end
23
+ end
24
+ end
@@ -1,3 +1,3 @@
1
1
  module RBM
2
- VERSION = "0.0.3"
2
+ VERSION = "1.0.0"
3
3
  end
metadata CHANGED
@@ -1,62 +1,53 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: rbm
3
- version: !ruby/object:Gem::Version
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
4
5
  prerelease:
5
- version: 0.0.3
6
6
  platform: ruby
7
- authors:
7
+ authors:
8
8
  - Samuel Kadolph
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
-
13
- date: 2011-04-13 00:00:00 -04:00
14
- default_executable:
12
+ date: 2012-06-14 00:00:00.000000000 Z
15
13
  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:
14
+ description: rbm lets you benchmark varies code fragments by running them a specified
15
+ number of times along with code fragments before and after all of the fragments
16
+ or an individual fragment. See rbm --help for more information.
17
+ email:
19
18
  - samuel@kadolph.com
20
- executables:
19
+ executables:
21
20
  - rbm
22
21
  extensions: []
23
-
24
22
  extra_rdoc_files: []
25
-
26
- files:
23
+ files:
27
24
  - bin/rbm
28
25
  - lib/rbm/benchmarker.rb
26
+ - lib/rbm/fragment.rb
29
27
  - lib/rbm/version.rb
30
28
  - lib/rbm.rb
31
- - README.md
32
- - UNLICENSE
33
- has_rdoc: true
34
- homepage: https://github.com/samuelkadolph/rbm
29
+ homepage: http://samuelkadolph.github.com/rbm/
35
30
  licenses: []
36
-
37
31
  post_install_message:
38
32
  rdoc_options: []
39
-
40
- require_paths:
33
+ require_paths:
41
34
  - lib
42
- required_ruby_version: !ruby/object:Gem::Requirement
35
+ required_ruby_version: !ruby/object:Gem::Requirement
43
36
  none: false
44
- requirements:
45
- - - ">="
46
- - !ruby/object:Gem::Version
47
- version: "0"
48
- required_rubygems_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
42
  none: false
50
- requirements:
51
- - - ">="
52
- - !ruby/object:Gem::Version
53
- version: "0"
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
54
47
  requirements: []
55
-
56
48
  rubyforge_project:
57
- rubygems_version: 1.6.2
49
+ rubygems_version: 1.8.21
58
50
  signing_key:
59
51
  specification_version: 3
60
- summary: Simple command line tool for quick ruby benchmarks.
52
+ summary: rbm is a command line tool for doing quick benchmarks of ruby code.
61
53
  test_files: []
62
-
data/README.md DELETED
@@ -1,51 +0,0 @@
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 run each code fragment.
23
- You can also provide a code fragment to be run before all code fragments or before a code fragment.
24
-
25
- rbm --times 1000 "5 / 5"
26
- rbm --times 1000 --init "n = 5" "n / 5"
27
- rbm --times 1000 --init "n = 5" --pre "m = 5" "n / m"
28
-
29
- You may provide a name to each code fragment to be displayed on the benchmark.
30
-
31
- rbm --name "sleep for 5" "sleep 5" --name "sleep for 2" "sleep 2"
32
-
33
- You can see the full usage statement at any time with `rbm --help`
34
-
35
- Usage: rbm [options] [--name name] [--pre code] code [[--name name] [--pre code] code...]
36
-
37
- Ruby Options:
38
- -r, --require file[,file] Files to require before benchmarking
39
- -I, --load-path path[,path] Paths to append to $LOAD_PATH
40
-
41
- Code Fragment Options:
42
- -n, --times n Number of times to run each code fragment
43
- -i, --init code Code to run before every code fragment
44
- -c, --cleanup code Code to run after each code fragment
45
- -N, --name name Names the following code fragment
46
- -p, --pre code Code to run before the follow code fragment
47
- -P, --post code Code to run after the follow code fragment
48
-
49
- General Options:
50
- -v, --version Print the version and exit
51
- -h, --help Print this message and exit
data/UNLICENSE DELETED
@@ -1,22 +0,0 @@
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.