pry-auto_benching.rb 2.7.0 → 2.8.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3ebc6bc90dcaae0756aca8ffe6156a7a51080df77d30c8b8df0d8a36eccb4a64
4
- data.tar.gz: 88886722771cff694fa7c1703567a3833b6b0e7ebb89ecd9424881bfdb913157
3
+ metadata.gz: 237980bf9d213d1a40df42c9fa3b78a7920ad90f6bf0e2115ea7fc27e47a1c7e
4
+ data.tar.gz: 93206785da40f07e91cead03bd5c75fb90969e65ddce7131ed5f73525fe8c9f3
5
5
  SHA512:
6
- metadata.gz: c672e7e8572c7f1380ee5dc20cf97b15de73fea7ac4c5739a24c7a92108b5b98c91fcae715bc1ade13cdc41c181e70f93efdec699b094d907ee1c50e2ec61321
7
- data.tar.gz: 8154a6a1bf2d71c172839b953b9edce185602dc56806b7587717f15a9460cfdf57c82c723f613826d1b42efdfa7203a645865c37c4fea8f41bbdd155179d7cfb
6
+ metadata.gz: 5484e2012f9ebb124ae4954022f174fc537ead567a31fd71cc29ce04e2abff6b9b820c5992106434c5e7575be53dab5b6e17b58d0ecf0d6532a6901dab645060
7
+ data.tar.gz: de3c42d2facb770932b0cbde3d588ef494135d035ae66cbee97329085041e195760394599d8b87a48311ba7ddb21d97189f2a54930d310a0f33b9d6fd4edb8cf
data/README.md CHANGED
@@ -4,17 +4,17 @@ pry-auto_benching.rb automatically benchmarks input entered into Pry.
4
4
 
5
5
  ## Configuration (optional)
6
6
 
7
- Configuration isn't required unless you want to change a default.
8
- To change a default, open `~/.pryrc` and add code along the lines
9
- of:
7
+ Configuration isn't required unless you want to change a default.<br>
8
+ To change a default, open `~/.pryrc` and add code along the lines of:
10
9
 
11
10
  ```ruby
12
11
  Pry.configure do |config|
13
12
  # Start benchmarking as soon as Pry starts? default is true.
14
13
  config.auto_benching.enabled = false
15
14
 
16
- # Benchmark Pry commands? Default is true
17
- config.auto_benching.benchmark_commands = false
15
+ # The max number of previous benchmark results to store in memory,
16
+ # for use by `auto-benching --past`. The default is 70.
17
+ config.auto_benching.max_history_size = 120
18
18
 
19
19
  # The type of clock to use, default is Process::CLOCK_MONOTONIC.
20
20
  # See https://www.rubydoc.info/stdlib/core/Process:clock_gettime
@@ -47,12 +47,14 @@ __1.__
47
47
 
48
48
  Show help by running `auto-benching -h`:
49
49
 
50
- [3] pry(main)> auto-benching -h-h
51
- Usage: auto-benching [enable|disable]
50
+ [11] pry(main)> auto-benching -h
51
+ Usage: auto-benching [options] [enable|disable]
52
52
 
53
- Enable or disable benchmarking code and Pry commands.
53
+ The auto-benching command can enable, disable, and
54
+ provide other features related to pry-auto_benching.rb
54
55
 
55
56
  -v, --version Display version.
57
+ -p, --past Display past benchmark results.
56
58
  -h, --help Show this message.
57
59
 
58
60
  __2.__
@@ -66,7 +68,6 @@ Disable auto benchmarking by running `auto-benching disable`:
66
68
  __3.__
67
69
 
68
70
  An example showing the prompt being updated with benchmark results.
69
- `_pry_.config.auto_benching.target_display` is set to `:prompt` (the default).
70
71
 
71
72
  [7] pry(main)> sleep 0.2
72
73
  => 0
@@ -92,24 +93,36 @@ An example with `_pry_.config.auto_benching.target_display` set to `:output`.
92
93
  pry-auto_benching.rb: 0.31s
93
94
  => #<Net::HTTPOK 200 OK readbody=true>
94
95
 
96
+ __5.__
97
+
98
+ Display benchmark results from the current Pry session using `auto-benching --past`:
99
+
100
+ [6] 0.5s (main)> auto-benching --past
101
+ 1. Net::HTTP.get_response URI.parse('https://github.com') (0.88s)
102
+ 2. Net::HTTP.get_response URI.parse('https://www.youtube.com') (2.18s)
103
+ 3. Net::HTTP.get_response URI.parse('https://www.github.com/ruby/ruby') (0.59s)
104
+ 4. sleep 0.5 (0.5s)
105
+ [7] pry(main)>
106
+
95
107
  ## Accuracy
96
108
 
97
- pry-auto_benching.rb can measure down to a tenth of a millisecond (0.01 represents 10 milliseconds),
98
- benchmarking `sleep 0.01` shows no overhead in the results:
109
+ pry-auto_benching.rb can measure down to 10 milliseconds (0.01 = 10 milliseconds). <br>
110
+ Benchmarking `sleep 0.01` shows no overhead in the results:
99
111
 
100
112
  [1] pry(main)> sleep 0.01
101
113
  => 0
102
114
  [2] 0.01s (main)>
103
115
 
104
- but it is possible results could be reported inaccurately since the measurement
116
+ But it is possible results can be reported inaccurately since the measurement
105
117
  includes a **very** small number of method calls to Pry internals. The overhead should be
106
118
  minimal and skew results by a margin that could be considered negligible.
107
119
 
108
120
  ## Integration with custom prompts
109
121
 
110
- If you want a custom prompt to integrate with pry-auto_benching.rb, it's easy, simply use
122
+ If you want a custom prompt to integrate with pry-auto_benching.rb, it's easy, just use
111
123
  `pry.config.prompt_name` (by default it is equal to 'pry') somewhere in the prompt and the
112
- duration of a benchmark will be inserted in its place.
124
+ duration of a benchmark will be inserted in its place, as long as `auto_benching.target_display`
125
+ is set to `:prompt`.
113
126
 
114
127
  Example:
115
128
 
@@ -5,27 +5,48 @@ Pry::Commands.add_command Class.new(Pry::ClassCommand) {
5
5
  group 'pry-auto_benching.rb'
6
6
  description 'Enable or disable benchmarking.'
7
7
  banner <<-CMDBANNER
8
- Usage: auto-benching [enable|disable]
8
+ Usage: auto-benching [options] [enable|disable]
9
9
 
10
- Enable or disable benchmarking code and Pry commands.
10
+ The auto-benching command can enable, disable, and
11
+ provide other features related to pry-auto_benching.rb
11
12
  CMDBANNER
12
13
 
13
14
  def options(o)
14
15
  o.on :v, :version, 'Display version.'
16
+ o.on :p, :past, 'Display past benchmark results.'
15
17
  end
16
18
 
17
19
  def process(command)
18
- if opts.v?
19
- return _pry_.output.puts "pry-auto_benching.rb v#{Pry::AutoBenching::VERSION}"
20
+ case
21
+ when opts.version?
22
+ _pry_.output.puts "pry-auto_benching.rb v#{Pry::AutoBenching::VERSION}"
23
+ when opts.past?
24
+ process_past
25
+ else
26
+ if command == 'enable'
27
+ Pry::AutoBenching.enable(_pry_)
28
+ _pry_.pager.page('pry-auto_benching.rb: benchmarking.')
29
+ elsif command == 'disable'
30
+ Pry::AutoBenching.disable(_pry_)
31
+ _pry_.pager.page('pry-auto_benching.rb: stopped benchmarking.')
32
+ else
33
+ raise Pry::CommandError, "'#{command}' is not implemented by this command, try -h for help."
34
+ end
20
35
  end
21
- if command == 'enable'
22
- Pry::AutoBenching.enable(_pry_)
23
- _pry_.pager.page('pry-auto_benching.rb: benchmarking.')
24
- elsif command == 'disable'
25
- Pry::AutoBenching.disable(_pry_)
26
- _pry_.pager.page('pry-auto_benching.rb: stopped benchmarking.')
36
+ end
37
+
38
+ private
39
+ def process_past
40
+ moments = Pry::AutoBenching.moments[_pry_.hash]
41
+ if moments.empty?
42
+ _pry_.pager.page "No benchmark results are currently stored for this session."
27
43
  else
28
- raise Pry::CommandError, "'#{command}' is not implemented by this command, try -h for help."
44
+ _pry_.pager.page moments.map.with_index { |moment, i|
45
+ format "%{index}. %{code} (%{duration})",
46
+ index: blue(i + 1),
47
+ code: moment.input.chomp,
48
+ duration: bold("#{moment.duration}s")
49
+ }.join("\n")
29
50
  end
30
51
  end
31
52
  }
@@ -1,5 +1,5 @@
1
1
  class Pry
2
2
  module AutoBenching
3
- VERSION = "2.7.0"
3
+ VERSION = "2.8.0"
4
4
  end
5
5
  end
@@ -1,6 +1,52 @@
1
1
  module Pry::AutoBenching
2
2
  require 'pry-auto_benching/pry_command'
3
3
 
4
+ #
5
+ # MomentList is an Array-like object who cycles its contents
6
+ # when a max size is reached.
7
+ #
8
+ # @api private
9
+ #
10
+ MomentList = Class.new do
11
+ include Enumerable
12
+
13
+ def initialize(pry)
14
+ @pry = pry
15
+ @ary = []
16
+ end
17
+
18
+ def each
19
+ @ary.each do |moment|
20
+ yield moment
21
+ end
22
+ end
23
+
24
+ def [](index)
25
+ @ary[index]
26
+ end
27
+
28
+ def pop
29
+ @ary.pop
30
+ end
31
+
32
+ def empty?
33
+ @ary.empty?
34
+ end
35
+
36
+ def <<(other)
37
+ @ary.pop while @ary.size >= @pry.config.auto_benching.max_history_size
38
+ @ary << other
39
+ end
40
+ end
41
+
42
+ #
43
+ # @api private
44
+ #
45
+ Moment = Struct.new(:start_timestamp) do
46
+ attr_accessor :input
47
+ attr_accessor :duration
48
+ end
49
+
4
50
  write_duration = -> (pry, duration) {
5
51
  auto_benching = pry.config.auto_benching
6
52
  if auto_benching.target_display == :prompt
@@ -16,64 +62,95 @@ module Pry::AutoBenching
16
62
  end
17
63
  }
18
64
 
19
- MEMORY = Hash.new{|h,k| h[k] = [] }
65
+ @moments = Hash.new do |h, pry|
66
+ h[pry.hash] = MomentList.new(pry)
67
+ end
20
68
 
21
- BEFORE_EVAL = ->(_, pry) do
69
+ @before_eval = ->(_, pry) do
22
70
  clock_type = pry.config.auto_benching.clock_type
23
- MEMORY[pry.hash].push Process.clock_gettime(clock_type)
71
+ k = @moments.key?(pry.hash) ? pry.hash : pry
72
+ @moments[k] << Moment.new(Process.clock_gettime(clock_type))
24
73
  end
25
74
 
26
- AFTER_EVAL = ->(input, pry) do
75
+ @after_eval = ->(_, pry) do
27
76
  auto_benching = pry.config.auto_benching
28
- duration = (sprintf "%.2f", Process.clock_gettime(auto_benching.clock_type) - MEMORY[pry.hash].pop).to_f
29
- if (input != nil or auto_benching.benchmark_commands) and auto_benching.display_duration_if.call(pry, duration)
30
- write_duration.call(pry, duration)
77
+ moment = @moments[pry.hash][-1]
78
+ moment.duration = (sprintf "%.2f", Process.clock_gettime(auto_benching.clock_type) - moment.start_timestamp).to_f
79
+ if auto_benching.display_duration_if.call(pry, moment.duration)
80
+ moment.input = pry.input_ring.to_a[-1]
81
+ write_duration.call(pry, moment.duration)
31
82
  else
83
+ @moments[pry.hash].pop
32
84
  pry.config.forget(:prompt_name)
33
85
  end
34
86
  end
35
87
 
36
- BEFORE_SESSION = ->(_,_, pry) do
88
+ @before_session = ->(_, _, pry) do
37
89
  Pry::AutoBenching.enable(pry) if pry.config.auto_benching.enabled
38
90
  end
39
91
 
40
- AFTER_SESSION = ->(_, _, pry) do
41
- MEMORY.delete(pry.hash)
92
+ @after_session = ->(_, _, pry) do
93
+ @moments.delete(pry.hash)
42
94
  end
43
95
 
96
+ #
97
+ # Enables benchmarking for the param `pry`.
98
+ #
99
+ # @param [Pry] pry
100
+ # An instance of Pry.
101
+ #
102
+ # @return [void]
103
+ #
44
104
  def self.enable(pry)
45
- if not pry.config.hooks.hook_exists? :before_eval, BEFORE_EVAL.hash
46
- pry.config.hooks.add_hook :before_eval, BEFORE_EVAL.hash, BEFORE_EVAL
47
- pry.config.hooks.add_hook :after_eval, AFTER_EVAL.hash , AFTER_EVAL
48
- pry.config.hooks.add_hook :after_session, AFTER_SESSION.hash , AFTER_SESSION
105
+ if not pry.config.hooks.hook_exists? :before_eval, @before_eval.hash
106
+ pry.config.hooks.add_hook :before_eval, @before_eval.hash, @before_eval
107
+ pry.config.hooks.add_hook :after_eval, @after_eval.hash , @after_eval
108
+ pry.config.hooks.add_hook :after_session, @after_session.hash , @after_session
49
109
  end
50
110
  end
51
111
 
112
+ #
113
+ # Disables benchmarking for the param `pry`.
114
+ #
115
+ # @param [Pry] pry
116
+ # An instance of Pry.
117
+ #
118
+ # @return [void]
119
+ #
52
120
  def self.disable(pry)
53
- pry.config.hooks.delete_hook :before_eval, BEFORE_EVAL.hash
54
- pry.config.hooks.delete_hook :after_eval , AFTER_EVAL.hash
55
- pry.config.hooks.delete_hook :after_session , AFTER_SESSION.hash
56
- MEMORY[pry.hash].clear
121
+ pry.config.hooks.delete_hook :before_eval, @before_eval.hash
122
+ pry.config.hooks.delete_hook :after_eval , @after_eval.hash
123
+ pry.config.hooks.delete_hook :after_session , @after_session.hash
124
+ @moments.delete(pry.hash)
57
125
  end
58
- end
59
126
 
60
- Pry.configure do |config|
61
- config.hooks.add_hook :before_session, Pry::AutoBenching::BEFORE_SESSION.hash,
62
- Pry::AutoBenching::BEFORE_SESSION
63
- config.auto_benching = Pry::Config.from_hash({
64
- enabled: true,
65
- target_display: :prompt,
66
- prompt_color: :green,
67
- clock_type: Process::CLOCK_MONOTONIC,
68
- display_duration_if: ->(pry, duration) { duration >= 0.01 },
69
- benchmark_commands: true,
70
- output_string: ->(pry, duration) {
71
- format("%{TextDesc} %{Duration}s", {
72
- :TextDesc => pry.color ?
73
- Pry::Helpers::Text.green("pry-auto_benching.rb:") :
74
- "pry-auto_benching.rb:",
75
- :Duration => sprintf("%.2f", duration)
76
- })
77
- }
78
- })
127
+ #
128
+ # @return [Pry::AutoBenching::MomentList]
129
+ # An Array-like object.
130
+ #
131
+ # @api private
132
+ #
133
+ def self.moments
134
+ @moments
135
+ end
136
+
137
+ Pry.configure do |config|
138
+ config.hooks.add_hook :before_session, @before_session.hash, @before_session
139
+ config.auto_benching = Pry::Config.from_hash({
140
+ max_history_size: 70,
141
+ enabled: true,
142
+ target_display: :prompt,
143
+ prompt_color: :green,
144
+ clock_type: Process::CLOCK_MONOTONIC,
145
+ display_duration_if: ->(pry, duration) { duration >= 0.01 },
146
+ output_string: ->(pry, duration) {
147
+ format("%{TextDesc} %{Duration}s", {
148
+ :TextDesc => pry.color ?
149
+ Pry::Helpers::Text.green("pry-auto_benching.rb:") :
150
+ "pry-auto_benching.rb:",
151
+ :Duration => sprintf("%.2f", duration)
152
+ })
153
+ }
154
+ })
155
+ end
79
156
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pry-auto_benching.rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.7.0
4
+ version: 2.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Gleeson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-11-18 00:00:00.000000000 Z
11
+ date: 2018-11-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pry