pry-auto_benching.rb 1.0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 0aefc25f86eed7cb38d19ae6128aa04d84e5318892566a69e6a5650e02f02eb3
4
+ data.tar.gz: 47aeb7d378f28fa24eb1d6cce8fef5e5ff56c74aaa35b80d654df45631d56538
5
+ SHA512:
6
+ metadata.gz: 963a145aacc484144627e1f119e571f267505451dd7261c0d7d8fb06c0805cdfda228f63a261ec63cdad98994193d522b7d45a1239ae386a01341cb12cc8bb04
7
+ data.tar.gz: 3cf40ff58c699a02f7c2cee7c98ae2180dbadc3c7c7b876016d74d0eebdc9d94deb30661945a22d40c6958809d39b970c7c6dcdc3c79e7ee8b4cec4e3e3b37dc
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright 2017
4
+ Robert Gleeson
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,82 @@
1
+ # pry-auto_benching.rb
2
+
3
+ pry-auto_benching.rb automatically benchmarks input entered into Pry.
4
+
5
+ ## Configuration (optional)
6
+
7
+ Configuration isn't required unless you want to change a default.
8
+ To change a default, open up `~/.pryrc` and add something like:
9
+
10
+ ```ruby
11
+ Pry.configure do |config|
12
+ # Start benchmarking as soon as Pry starts? default is true.
13
+ config.auto_benching.enabled = false
14
+
15
+ # Benchmark Pry commands? Default is false
16
+ config.auto_benching.benchmark_commands = true
17
+
18
+ # The type of clock to use, default is Process::CLOCK_MONOTONIC.
19
+ # See https://www.rubydoc.info/stdlib/core/Process:clock_gettime
20
+ config.auto_benching.clock_type = Process::CLOCK_REALTIME
21
+
22
+ # Benchmark results are printed depending on the return value
23
+ # of this lambda, by default results are printed if duration > 0.0.
24
+ config.auto_benching.speak_if = ->(pry, duration) {
25
+ duration > 0.5
26
+ }
27
+
28
+ # A lambda for presenting the benchmark results.
29
+ # Default is: "Benchmark: %.2fs".
30
+ config.auto_benching.speaker = ->(pry, duration) {
31
+ pry.pager.page sprintf("Elapsed time: %.2fs", duration)
32
+ }
33
+ end
34
+ ```
35
+
36
+ ## Examples
37
+
38
+ __1.__
39
+
40
+ Show help by running `pry-auto_benching -h`:
41
+
42
+ [1] pry(main)> pry-auto_benching -h
43
+ Usage: pry-auto_benching [enable|disable]
44
+
45
+ Enable or disable benchmarking of input.
46
+
47
+ -h, --help Show this message.
48
+
49
+ __2.__
50
+
51
+ Disable auto benchmarking by running `pry-auto_benching disable`:
52
+
53
+ [1] pry(main)> sleep 2
54
+ pry-auto_benching.rb: 2.00s
55
+ => 2
56
+ [2] pry(main)> pry-auto_benching disable
57
+ pry-auto_benching.rb: stopped benchmarking.
58
+ [3] pry(main)> sleep 2
59
+ => 2
60
+
61
+ __3.__
62
+
63
+ Last example just shows the benchmark of a method that sleeps for a random
64
+ number of seconds:
65
+
66
+ [3] pry(main)> def sleep_rand
67
+ [3] pry(main)* sleep rand(10)
68
+ [3] pry(main)* end
69
+ => :sleep_rand
70
+ [4] pry(main)> sleep_rand
71
+ pry-auto_benching.rb: 7.00s
72
+ => 7
73
+
74
+ ## Install
75
+
76
+ ```ruby
77
+ gem "pry-auto_benching.rb", github: "r-obert/pry-auto_benching.rb"
78
+ ```
79
+
80
+ ## License
81
+
82
+ [MIT](./LICENSE.txt).
@@ -0,0 +1,5 @@
1
+ class Pry
2
+ module AutoBenching
3
+ VERSION = "1.0.0"
4
+ end
5
+ end
@@ -0,0 +1,87 @@
1
+ module Pry::AutoBenching
2
+ MEMORY = Hash.new{|h,k| h[k] = [] }
3
+
4
+ BEFORE_EVAL = ->(_, pry) do
5
+ clock_type = pry.config.auto_benching.clock_type
6
+ MEMORY[pry.hash].push Process.clock_gettime(clock_type)
7
+ end
8
+
9
+ AFTER_EVAL = ->(input, pry) do
10
+ clock_type = pry.config.auto_benching.clock_type
11
+ duration = (sprintf "%.2f", Process.clock_gettime(clock_type) - MEMORY[pry.hash][-1]).to_f
12
+ if input.nil? # Pry command
13
+ pry.config.auto_benching.benchmark_commands and
14
+ pry.config.auto_benching.speak_if.call(pry, duration) and
15
+ pry.config.auto_benching.speaker.call(pry, duration)
16
+ else
17
+ pry.config.auto_benching.speak_if.call(pry, duration) and
18
+ pry.config.auto_benching.speaker.call(pry, duration)
19
+ end
20
+ MEMORY[pry.hash].clear
21
+ end
22
+
23
+ BEFORE_SESSION = ->(_,_, pry) do
24
+ Pry::AutoBenching.enable(pry) if pry.config.auto_benching.enabled
25
+ end
26
+
27
+ AFTER_SESSION = ->(_, _, pry) do
28
+ MEMORY.delete(pry.hash)
29
+ end
30
+
31
+ def self.enable(pry)
32
+ if not pry.config.hooks.hook_exists? :before_eval, BEFORE_EVAL.hash
33
+ pry.config.hooks.add_hook :before_eval, BEFORE_EVAL.hash, BEFORE_EVAL
34
+ pry.config.hooks.add_hook :after_eval, AFTER_EVAL.hash , AFTER_EVAL
35
+ pry.config.hooks.add_hook :after_session, AFTER_SESSION.hash , AFTER_SESSION
36
+ end
37
+ end
38
+
39
+ def self.disable(pry)
40
+ pry.config.hooks.delete_hook :before_eval, BEFORE_EVAL.hash
41
+ pry.config.hooks.delete_hook :after_eval , AFTER_EVAL.hash
42
+ pry.config.hooks.delete_hook :after_session , AFTER_SESSION.hash
43
+ MEMORY[pry.hash].clear
44
+ end
45
+
46
+ require 'pry' if not defined?(Pry::ClassCommand)
47
+ class AutoBenchingCommand < Pry::ClassCommand
48
+ match 'pry-auto_benching'
49
+ command_options argument_required: true
50
+ group 'pry-auto_benching.rb'
51
+ description 'Start or disable benchmarking.'
52
+ banner <<-CMDBANNER
53
+ Usage: pry-auto_benching [enable|disable]
54
+
55
+ Start or disable benchmarking code (or Pry commands).
56
+ CMDBANNER
57
+
58
+ def process(command)
59
+ if command == 'enable'
60
+ Pry::AutoBenching.enable(_pry_)
61
+ _pry_.pager.page('pry-auto_benching.rb: benchmarking.')
62
+ elsif command == 'disable'
63
+ Pry::AutoBenching.disable(_pry_)
64
+ _pry_.pager.page('pry-auto_benching.rb: stopped benchmarking.')
65
+ else
66
+ raise Pry::CommandError, "'#{command}' is not implemented by this command, try -h for help."
67
+ end
68
+ end
69
+ end
70
+
71
+ Pry::Commands.add_command(AutoBenchingCommand)
72
+ Pry.config.hooks.add_hook :before_session, BEFORE_SESSION.hash, BEFORE_SESSION
73
+ Pry.config.auto_benching = Pry::Config.from_hash({
74
+ enabled: true,
75
+ clock_type: Process::CLOCK_MONOTONIC,
76
+ benchmark_commands: false,
77
+ speak_if: ->(pry, duration) { duration > 0.0 },
78
+ speaker: ->(pry, duration) {
79
+ pry.pager.page format("%{AutoBenchingSays} %{WallTime}s", {
80
+ :AutoBenchingSays => pry.color ?
81
+ Pry::Helpers::Text.green("pry-auto_benching.rb:") :
82
+ "pry-auto_benching.rb:",
83
+ :WallTime => sprintf("%.2f", duration)
84
+ })
85
+ }
86
+ }, nil)
87
+ end
@@ -0,0 +1,14 @@
1
+ require './lib/pry-auto_benching/version'
2
+ Gem::Specification.new do |spec|
3
+ spec.name = "pry-auto_benching.rb"
4
+ spec.version = Pry::AutoBenching::VERSION
5
+ spec.authors = ["Robert Gleeson"]
6
+ spec.email = "trebor.g@protonmail.com"
7
+ spec.summary = "Pry plugin for automatically benchmarking input using wall clock time."
8
+ spec.description = spec.summary
9
+ spec.homepage = "https://github.com/r-obert/pry-auto_benching.rb"
10
+ spec.licenses = ["MIT"]
11
+ spec.require_paths = ["lib"]
12
+ spec.files = Dir["*.{md,txt,gemspec}", "lib/**/*.rb"]
13
+ spec.add_runtime_dependency "pry", "~> 0.11"
14
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pry-auto_benching.rb
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Robert Gleeson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-11-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pry
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.11'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.11'
27
+ description: Pry plugin for automatically benchmarking input using wall clock time.
28
+ email: trebor.g@protonmail.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - LICENSE.txt
34
+ - README.md
35
+ - lib/pry-auto_benching.rb
36
+ - lib/pry-auto_benching/version.rb
37
+ - pry-auto_benching.gemspec
38
+ homepage: https://github.com/r-obert/pry-auto_benching.rb
39
+ licenses:
40
+ - MIT
41
+ metadata: {}
42
+ post_install_message:
43
+ rdoc_options: []
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ requirements: []
57
+ rubyforge_project:
58
+ rubygems_version: 2.7.7
59
+ signing_key:
60
+ specification_version: 4
61
+ summary: Pry plugin for automatically benchmarking input using wall clock time.
62
+ test_files: []