pry-measure 0.0.2 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fd7ecb4b9be0aa50ba6a782798a7e3ff54f817ef
4
- data.tar.gz: 2b502507be3cf9d891545f7bdf091d4846467d87
3
+ metadata.gz: 959aa87524437b65174dcdf0ff71d8abfee98d84
4
+ data.tar.gz: ccfb73a9008584d6bb65d0b74a9973e9d7c89a44
5
5
  SHA512:
6
- metadata.gz: 8605ad7da9656de67b56754d7941359ce38046b7e779f6f4a6c71919f13c1881d7b7686e5c2c40adc7be050dd6cccdd9c9f60a9666273eb8784a0da42cbbd5f5
7
- data.tar.gz: 1a97ada99ecb026394c4a9128b795acf467d265b66651cdd069030d0477adc6151f4e502adf4637adc3df6dafd214f25be2415887e07e377498c813f7fc62233
6
+ metadata.gz: e72bfb2139336dc712f4a0674f4b8dc0ecc6b98b6cf2ad17f92b2f3c483c9b2b103c08dd3d60e5d7c096a7c985faf4d3bc26ab20fdb4f01c6809d89ef9616351
7
+ data.tar.gz: 6b78f70b00467d499ba4101e647657ace3dde60c23cde7ec89ee3f5dc992135c711276d6626db152b128ccc3245e088ebae9870b08fa4324a88febe86121fbdb
data/CHANGELOG.md ADDED
@@ -0,0 +1,11 @@
1
+ # Version 0.1.0
2
+
3
+ ## 2015.06.13
4
+
5
+ ### New
6
+
7
+ - Add helper methods (remove pry-realtime command)
8
+
9
+ ### Improvements
10
+
11
+ - Refactor pry-measure command, eventually will be removed in favor of only using the helper methods.
data/README.md CHANGED
@@ -14,6 +14,27 @@ or added to the Gemfile:
14
14
  gem 'pry-measure'
15
15
  ```
16
16
 
17
+ ## Helper methods
18
+
19
+ A `pry_measure` helper method is added which takes arguments for number of times to execute and number of times to run (defaults to 1 and 10, respectively):
20
+
21
+ ```ruby
22
+ pry(main)> pry_measure(2, 200000) do
23
+ pry(main)* 10.times do |time|; 1000*1000*1000*1000; end
24
+ pry(main)* end
25
+ user system total real
26
+ 0.230000 0.000000 0.230000 ( 0.234847)
27
+ 0.230000 0.000000 0.230000 ( 0.228060)
28
+ => [#<Benchmark::Tms:0x007fd992bd2b30 ...>, #<Benchmark::Tms:0x007fd992bd2220 ...>]
29
+ ```
30
+
31
+ Additionally, `time_method` is added:
32
+
33
+ ```ruby
34
+ pry(main)> time_method String, 'new', 'FooBar'
35
+ Time elapsed 0.004 milliseconds
36
+ ```
37
+
17
38
  ## Usage
18
39
 
19
40
  ```bash
@@ -35,5 +56,3 @@ The above passes the interpolated code to [Benchmark.measure](http://www.ruby-do
35
56
  pry-realtime #{`curl http://google.com`}
36
57
  => 0.6405
37
58
  ```
38
-
39
-
data/lib/pry-measure.rb CHANGED
@@ -6,7 +6,9 @@ module PryMeasure
6
6
  Commands = Pry::CommandSet.new
7
7
  end
8
8
 
9
+ require 'benchmark'
10
+ require "pry-measure/marker"
9
11
  require "pry-measure/measure_command"
10
- require "pry-measure/realtime_command"
12
+ require "pry-measure/helper_methods"
11
13
 
12
14
  Pry.commands.import PryMeasure::Commands
@@ -0,0 +1,22 @@
1
+ # Helper methods
2
+
3
+ # Credit for `time_method`: http://www.skorks.com/2010/03/timing-ruby-code-it-is-easy-with-benchmark/
4
+
5
+ def time_method(object, method, *args)
6
+ beginning_time = Time.now
7
+ object.send(method, *args)
8
+ end_time = Time.now
9
+ puts "Time elapsed #{(end_time - beginning_time)*1000} milliseconds"
10
+ end
11
+
12
+ def pry_measure(test_times = 10, exec_times = 1, &b)
13
+ Benchmark.bm do |x|
14
+ test_times.times do
15
+ x.report do
16
+ exec_times.times do
17
+ yield
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -1,32 +1,13 @@
1
1
  module PryMeasure
2
2
  class Marker
3
- def self.measure(execution_times, arg_string, command, realtime=false, show_output=false, results=[])
4
- execution_times.times do
5
- results << Benchmark.measure do
6
- begin
7
- command.interpolate_string arg_string
8
- rescue Pry::RescuableException => ex
9
- raise Pry::CommandError, ex
3
+ def self.measure(execution_times, test_times, code)
4
+ Benchmark.bm do |x|
5
+ test_times.times do
6
+ x.report do
7
+ execution_times.times { Kernel.eval code }
10
8
  end
11
9
  end
12
10
  end
13
-
14
- if show_output == true
15
- unless realtime
16
- puts " user system total real"
17
- end
18
- puts results
19
- end
20
-
21
- average = average_results(results, execution_times)
22
- realtime ? average[:real] : average
23
- end
24
-
25
- def self.average_results(results, execution_times, response={})
26
- [:real, :utime, :stime, :total].each do |key|
27
- response.merge!(key =>(results.map(&key).inject(&:+) / execution_times.to_i).round(4))
28
- end
29
- response
30
11
  end
31
12
  end
32
- end
13
+ end
@@ -1,12 +1,17 @@
1
- require "pry-measure/marker"
2
-
3
1
  PryMeasure::Commands.create_command "pry-measure" do
4
2
  description "Measure a block of code using Ruby's benchmark module"
5
3
 
4
+ TEST_TIMES_DESC = 'Times to execute test code (default: 10)'
5
+ EXEC_TIMES_DESC = 'Times to run benchmark (n times -t)'
6
+
6
7
  banner <<-BANNER
7
- Usage: measure [ -t <times> -s ] <block>
8
+ Usage: pry-measure [ -t <times> -c <times> ] <test code>
8
9
 
9
- Calls the block for the given amount of times (default 500) and returns benchmark results.
10
+ -t #{TEST_TIMES_DESC}
11
+ -c #{EXEC_TIMES_DESC}
12
+
13
+ Executes the code for the given amount of -t times (or default 10)
14
+ and returns benchmark results.
10
15
  BANNER
11
16
 
12
17
  command_options(
@@ -16,26 +21,33 @@ PryMeasure::Commands.create_command "pry-measure" do
16
21
  )
17
22
 
18
23
  def setup
19
- @times = 500
24
+ @test_times = 10
25
+ @exec_times = 1
20
26
  end
21
27
 
22
28
  def options(opt)
23
- opt.on :s, :show, "Shows default benchmark output"
24
- opt.on :t, :times, "Times to call block (default: 500)", optional: true, argument: true
29
+ opt.on :c, :count, EXEC_TIMES_DESC, optional: true, argument: true
30
+ opt.on :t, :times, TEST_TIMES_DESC, optional: true, argument: true
25
31
  end
26
32
 
27
- def process
28
- if opts[:times]
29
- @times = opts[:times]
30
- raise Pry::CommandError, "-t should be an integer" unless times_is_int?
33
+ def validate(option)
34
+ unless is_int?(option)
35
+ raise Pry::CommandError, "#{option} isn't an integer"
31
36
  end
32
37
 
33
- output = !opts[:show].nil?
38
+ option.to_i
39
+ end
40
+
41
+ def process
42
+ @test_times = validate(opts[:count]) if opts[:count]
43
+ @exec_times = validate(opts[:times]) if opts[:times]
44
+
45
+ code = args.join(' ')
34
46
 
35
- PryMeasure::Marker.measure @times.to_i, arg_string, self, false, output
47
+ PryMeasure::Marker.measure @exec_times, @test_times, code
36
48
  end
37
49
 
38
- def times_is_int?
39
- !!(@times =~ /^[-+]?[0-9]+$/)
50
+ def is_int?(candidate)
51
+ !!(candidate =~ /^[-+]?[0-9]+$/)
40
52
  end
41
- end
53
+ end
@@ -1,3 +1,3 @@
1
1
  module PryMeasure
2
- VERSION = "0.0.2"
2
+ VERSION = "0.1.0"
3
3
  end
metadata CHANGED
@@ -1,71 +1,72 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pry-measure
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthew Dodds
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-13 00:00:00.000000000 Z
11
+ date: 2015-06-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pry
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
19
  version: 0.9.10
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '>='
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: 0.9.10
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rspec
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '0'
33
+ version: '3.3'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '0'
40
+ version: '3.3'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rake
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - '>='
45
+ - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '0'
47
+ version: '10.4'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - '>='
52
+ - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '0'
55
- description:
54
+ version: '10.4'
55
+ description: Provides benchmark helper methods for easier measuring
56
56
  email:
57
57
  - matthewrusselldodds@gmail.com
58
58
  executables: []
59
59
  extensions: []
60
60
  extra_rdoc_files: []
61
61
  files:
62
+ - CHANGELOG.md
63
+ - LICENSE
64
+ - README.md
65
+ - lib/pry-measure.rb
66
+ - lib/pry-measure/helper_methods.rb
62
67
  - lib/pry-measure/marker.rb
63
68
  - lib/pry-measure/measure_command.rb
64
- - lib/pry-measure/realtime_command.rb
65
69
  - lib/pry-measure/version.rb
66
- - lib/pry-measure.rb
67
- - LICENSE
68
- - README.md
69
70
  homepage: http://github.com/matthewrdodds/pry-measure
70
71
  licenses:
71
72
  - MIT
@@ -76,17 +77,17 @@ require_paths:
76
77
  - lib
77
78
  required_ruby_version: !ruby/object:Gem::Requirement
78
79
  requirements:
79
- - - '>='
80
+ - - ">="
80
81
  - !ruby/object:Gem::Version
81
82
  version: '0'
82
83
  required_rubygems_version: !ruby/object:Gem::Requirement
83
84
  requirements:
84
- - - '>='
85
+ - - ">="
85
86
  - !ruby/object:Gem::Version
86
87
  version: 1.3.6
87
88
  requirements: []
88
89
  rubyforge_project:
89
- rubygems_version: 2.0.3
90
+ rubygems_version: 2.4.5
90
91
  signing_key:
91
92
  specification_version: 4
92
93
  summary: Adds simple benchmark tool to pry
@@ -1,21 +0,0 @@
1
- require "pry-measure/marker"
2
-
3
- PryMeasure::Commands.create_command "pry-realtime" do
4
- description "Measure the execution time of code using Ruby's Benchmark.realtime"
5
-
6
- banner <<-BANNER
7
- Usage: pry-realtime <code>
8
-
9
- Evaluates code using Benchmark.realtime
10
- BANNER
11
-
12
- command_options(
13
- shellwords: false,
14
- keep_retval: true,
15
- interpolate: false
16
- )
17
-
18
- def process
19
- PryMeasure::Marker.measure 1, arg_string, self, true
20
- end
21
- end