clocker 0.1.3 → 1.0.2

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
- SHA1:
3
- metadata.gz: 313d465450aad6b302c19b427cd85f1558e842e6
4
- data.tar.gz: 8f5ec6a2e5f7eb59e1bd02827316b1535f0a2489
2
+ SHA256:
3
+ metadata.gz: 241d80d7ab828ad458f5e1f76b583c787c3c392361ecb419d7e1218d08e58816
4
+ data.tar.gz: dce67fae4400fc505d7f8e3716fcc3e66af171e1962b18e6fcac59e498b87317
5
5
  SHA512:
6
- metadata.gz: c3bc670dada1807546c51c61627eae28b3ad2766c3e78783bfeee3bc8518fb5ba97d0e16cb4f45d9ffe341998867005513111147202eb619f950244ac071b9a8
7
- data.tar.gz: 1ffc33bf6fccfc9b228046b5afc10fc8667408445c865bf0e118efc4b8a435e6f6ab0cae3d2eae5e0b90d80de143e21a4e467cebe4740185aac1661d20bdfdb1
6
+ metadata.gz: cf9d4a93c56a0c68556dd7a8f628a1bd5452f2c0ca8b31fc20a5961aa58e64eeda35393fcf3a7c8699280fda684809eef334a8d7759a9eb486e6445ffc3efd39
7
+ data.tar.gz: 48484f36c1e403cf479f5dd429703172ac5ab44c9bd33fd46d91d13ddb7ee3783566cbf55227046309dc64c3f880d6aa6f91e7a0187a39ddb0014ea03bf2ed9f
data/.gitignore CHANGED
@@ -27,9 +27,9 @@ build/
27
27
 
28
28
  # for a library or gem, you might want to ignore these files since the code is
29
29
  # intended to run in multiple environments; otherwise, check them in:
30
- # Gemfile.lock
31
- # .ruby-version
32
- # .ruby-gemset
30
+ Gemfile.lock
31
+ .ruby-version
32
+ .ruby-gemset
33
33
 
34
34
  # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
35
35
  .rvmrc
data/README.md CHANGED
@@ -1,9 +1,40 @@
1
1
  # Clocker
2
2
 
3
- Find out how long that block of code takes to run.
3
+ [![Gem Version](https://badge.fury.io/rb/clocker.svg)](http://badge.fury.io/rb/clocker)
4
4
 
5
- ## Installation
5
+ A simple RubyGem that reports how long a specific Ruby command or block of code takes to run.
6
6
 
7
+ ## Usage
8
+ ### Command Line
9
+ Simply run `clocker` followed by a command or block of code. Add an `-m` before the command or block to print out start and end times.
10
+
11
+ Ex. 1
12
+ ```
13
+ $ clocker "puts 'hi'"
14
+ hi
15
+ Clocked at 0 mins, 0 secs, 0 ms
16
+ ```
17
+
18
+ Ex. 2
19
+ ```
20
+ $ clocker "2.times { puts 'hello world'; sleep(0.6) }"
21
+ hello world
22
+ hello world
23
+
24
+ Clocked at 0 mins, 1 secs, and 210 ms
25
+ ```
26
+
27
+ Ex. 3
28
+ ```
29
+ $ clocker -m "1.upto(5) { |i| print i+1; sleep(0.4) }"
30
+ start: 2015-08-18 16:16:47 -0700
31
+ 23456
32
+ ended: 2015-08-18 16:16:49 -0700
33
+
34
+ Clocked at 0 mins, 2 secs, and 12 ms
35
+ ```
36
+
37
+ ### Gem Library
7
38
  Add this line to your application's Gemfile:
8
39
 
9
40
  ```ruby
@@ -11,18 +42,54 @@ gem 'clocker'
11
42
  ```
12
43
 
13
44
  And then execute:
14
-
15
- $ bundle
45
+ ```
46
+ $ bundle
47
+ ```
16
48
 
17
49
  Or install it yourself as:
50
+ ```
51
+ $ gem install clocker
52
+ ```
53
+
54
+ Sample code block:
55
+ ```ruby
56
+ >> clocker = Clocker.new
57
+ >> clocker.clock do
58
+ >> # code here
59
+ >> end
60
+ ```
18
61
 
19
- $ gem install clocker
62
+ By default, clocker will not print out the start time before the command|block, or the end time after the command|block, unless you pass the instance `show_messages: true`.
20
63
 
21
- ## Usage
64
+ ```ruby
65
+ >> c1 = Clocker.new
66
+ >> duration = c1.clock do
67
+ >> 3.times { print 'c1'; sleep(1) }
68
+ >> end
69
+ >> puts
70
+ >> puts duration
22
71
 
72
+ c1c1c1
73
+ {:mins=>0, :secs=>3, :ms=>13}
23
74
  ```
24
- clocker = Clocker.new
25
- clocker.clock do
26
- # code here
27
- end
28
- ```
75
+
76
+ ```ruby
77
+ >> c2 = Clocker.new(show_messages: true)
78
+ >> duration = c2.clock do
79
+ >> 3.times { print 'c2'; sleep(1) }
80
+ >> end
81
+ >> puts duration
82
+
83
+ start: 2015-08-18 16:07:17 -0700
84
+ c2c2c2
85
+ ended: 2015-08-18 16:07:20 -0700
86
+ {:mins=>0, :secs=>3, :ms=>14}
87
+ ```
88
+
89
+ ## Contributing
90
+
91
+ Bug reports and pull requests are welcome on GitHub at https://github.com/michaelchadwick/clocker.
92
+
93
+ ## License
94
+
95
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,62 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'optparse'
4
+ require_relative '../lib/clocker'
5
+ require_relative '../lib/clocker/version'
6
+
7
+ BINARY_NAME = $PROGRAM_NAME.split('/').last
8
+
9
+ def parse_options
10
+ options = {
11
+ show_messages: false
12
+ }
13
+
14
+ optparse = OptionParser.new do |opts|
15
+ opts.banner = "Calculate how long a command or block of code takes to run\n"
16
+ opts.banner += "usage: #{BINARY_NAME} COMMAND|BLOCK\n"
17
+
18
+ opts.on('-m', '--message', 'Show start and ended messages') do |m|
19
+ options[:show_messages] = m
20
+ end
21
+
22
+ opts.on('-v', '--version', 'Display version number and exit') do
23
+ puts "#{Clocker::VERSION}"
24
+ exit
25
+ end
26
+
27
+ opts.on('-h', '-?', '--help', 'Display this screen and exit') do
28
+ puts opts
29
+ exit
30
+ end
31
+ end
32
+
33
+ optparse.parse!()
34
+
35
+ return options
36
+ end
37
+
38
+ def print_error(error)
39
+ case error
40
+ when OptionParser::InvalidOption
41
+ puts "#{BINARY_NAME} ERROR: illegal option #{error.args.join(' ')}"
42
+ else
43
+ puts "#{BINARY_NAME} ERROR: #{error}"
44
+ end
45
+ end
46
+
47
+ begin
48
+ options = parse_options
49
+
50
+ if (ARGV.count > 0)
51
+ command = ARGV.join(" ")
52
+ c = Clocker.new(options)
53
+ t = c.clock { eval(command) }
54
+ puts
55
+ puts "Clocked at #{t[:mins]} mins, #{t[:secs]} secs, and #{t[:ms]} ms"
56
+ else
57
+ puts "#{BINARY_NAME} ERROR: missing command or block"
58
+ end
59
+ rescue => error
60
+ print_error(error)
61
+ exit(false)
62
+ end
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
10
10
  spec.authors = ['Michael Chadwick']
11
11
  spec.email = ['mike@codana.me']
12
12
  spec.homepage = 'http://rubygems.org/gems/clocker'
13
- spec.summary = 'Calculate how long a block of code takes to run'
13
+ spec.summary = 'Calculate how long a command or block of code takes to run'
14
14
  spec.description = 'Give Clocker some code to process, and it will run it and display how long it took to finish.'
15
15
 
16
16
  spec.files = `git ls-files`.split("\n")
@@ -19,6 +19,6 @@ Gem::Specification.new do |spec|
19
19
  spec.require_paths = ['lib']
20
20
  spec.license = 'MIT'
21
21
 
22
- spec.add_development_dependency "bundler", "~> 1.9"
23
- spec.add_development_dependency "rake", "~> 10.0"
22
+ spec.add_development_dependency "bundler", "~> 2.1"
23
+ spec.add_development_dependency "rake", "~> 12.3"
24
24
  end
@@ -1,35 +1,38 @@
1
+ # lib/clocker.rb
1
2
  # Clocker
2
3
 
3
- # Allows you to run a process for a time and get stats on how long it took
4
- # If you give it ms bounds on init, it will just run a timer for a random
5
- # time between the two params
6
-
7
4
  class Clocker
5
+ attr_accessor :options
6
+
8
7
  def initialize(options = {})
9
8
  self.options = options
10
9
  end
11
10
 
12
- def clock
11
+ def clock(&block)
13
12
  @start = Time.now
14
- unless options[:show_messages].nil?
15
- puts "start: #{@start}"
13
+
14
+ if options[:show_messages]
15
+ puts "\nstart: #{@start}"
16
16
  end
17
-
17
+
18
18
  begin
19
- yield
20
- rescue StandardError => e
21
- puts e.message
19
+ block.call
20
+ rescue
21
+ puts "ERROR: invalid command or block"
22
22
  end
23
- stop
23
+
24
+ return stop
24
25
  end
25
-
26
+
26
27
  def stop
27
28
  @stop = Time.now
28
- unless options[:show_messages].nil?
29
- puts "ended: #{@stop}"
29
+
30
+ if options[:show_messages]
31
+ puts "\nended: #{@stop}"
30
32
  end
33
+
31
34
  ms = ((@stop - @start) * 1000).to_i
32
-
35
+
33
36
  if ms > 60000
34
37
  mins = ms / 60000
35
38
  ms = ms - (60000 * mins)
@@ -37,15 +40,15 @@ class Clocker
37
40
  secs = ms / 1000
38
41
  ms = ms % 1000
39
42
  end
40
-
41
- { mins: mins, secs: secs, ms: ms }
43
+
44
+ return { mins: mins, secs: secs, ms: ms }
42
45
  elsif ms > 1000
43
46
  secs = ms / 1000
44
47
  ms = ms % 1000
45
-
46
- { mins: 0, secs: secs, ms: ms }
48
+
49
+ return { mins: 0, secs: secs, ms: ms }
47
50
  else
48
- { mins: 0, secs: 0, ms: ms }
51
+ return { mins: 0, secs: 0, ms: ms }
49
52
  end
50
53
  end
51
54
  end
@@ -1,3 +1,6 @@
1
- module Clocker
2
- VERSION = '0.1.3'
1
+ # lib/clocker/version.rb
2
+ # Version of Clocker
3
+
4
+ class Clocker
5
+ VERSION = '1.0.2'
3
6
  end
@@ -0,0 +1,18 @@
1
+ require 'clocker'
2
+
3
+ c1 = Clocker.new
4
+
5
+ duration1 = c1.clock do
6
+ 3.times { print 'c1'; sleep(1) }
7
+ end
8
+
9
+ puts
10
+ puts duration1
11
+
12
+ c2 = Clocker.new(show_messages: true)
13
+
14
+ duration2 = c2.clock do
15
+ 3.times { print 'c2'; sleep(1) }
16
+ end
17
+
18
+ puts duration2
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: clocker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Chadwick
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-08-18 00:00:00.000000000 Z
11
+ date: 2020-07-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -16,35 +16,34 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '1.9'
19
+ version: '2.1'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '1.9'
26
+ version: '2.1'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '10.0'
33
+ version: '12.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: '10.0'
40
+ version: '12.3'
41
41
  description: Give Clocker some code to process, and it will run it and display how
42
42
  long it took to finish.
43
43
  email:
44
44
  - mike@codana.me
45
45
  executables:
46
- - console
47
- - setup
46
+ - clocker
48
47
  extensions: []
49
48
  extra_rdoc_files: []
50
49
  files:
@@ -52,22 +51,21 @@ files:
52
51
  - ".rspec"
53
52
  - ".travis.yml"
54
53
  - Gemfile
55
- - Gemfile.lock
56
54
  - LICENSE.txt
57
55
  - README.md
58
56
  - Rakefile
59
- - bin/console
60
- - bin/setup
57
+ - bin/clocker
61
58
  - clocker.gemspec
62
59
  - lib/clocker.rb
63
60
  - lib/clocker/version.rb
64
61
  - spec/clocker_spec.rb
65
62
  - spec/spec_helper.rb
63
+ - tests/test-clocker.rb
66
64
  homepage: http://rubygems.org/gems/clocker
67
65
  licenses:
68
66
  - MIT
69
67
  metadata: {}
70
- post_install_message:
68
+ post_install_message:
71
69
  rdoc_options: []
72
70
  require_paths:
73
71
  - lib
@@ -82,9 +80,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
82
80
  - !ruby/object:Gem::Version
83
81
  version: '0'
84
82
  requirements: []
85
- rubyforge_project:
86
- rubygems_version: 2.4.8
87
- signing_key:
83
+ rubygems_version: 3.1.4
84
+ signing_key:
88
85
  specification_version: 4
89
- summary: Calculate how long a block of code takes to run
90
- test_files: []
86
+ summary: Calculate how long a command or block of code takes to run
87
+ test_files:
88
+ - spec/clocker_spec.rb
89
+ - spec/spec_helper.rb
@@ -1,21 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- clocker (0.1.0)
5
-
6
- GEM
7
- remote: https://rubygems.org/
8
- specs:
9
- rake (10.4.2)
10
-
11
- PLATFORMS
12
- ruby
13
- x64-mingw32
14
-
15
- DEPENDENCIES
16
- bundler (~> 1.9)
17
- clocker!
18
- rake (~> 10.0)
19
-
20
- BUNDLED WITH
21
- 1.10.5
@@ -1 +0,0 @@
1
- #!/usr/bin/env ruby
data/bin/setup DELETED
@@ -1 +0,0 @@
1
- #!/usr/bin/env ruby