clocker 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 241d80d7ab828ad458f5e1f76b583c787c3c392361ecb419d7e1218d08e58816
4
+ data.tar.gz: dce67fae4400fc505d7f8e3716fcc3e66af171e1962b18e6fcac59e498b87317
5
+ SHA512:
6
+ metadata.gz: cf9d4a93c56a0c68556dd7a8f628a1bd5452f2c0ca8b31fc20a5961aa58e64eeda35393fcf3a7c8699280fda684809eef334a8d7759a9eb486e6445ffc3efd39
7
+ data.tar.gz: 48484f36c1e403cf479f5dd429703172ac5ab44c9bd33fd46d91d13ddb7ee3783566cbf55227046309dc64c3f880d6aa6f91e7a0187a39ddb0014ea03bf2ed9f
@@ -0,0 +1,35 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /test/tmp/
9
+ /test/version_tmp/
10
+ /tmp/
11
+
12
+ ## Specific to RubyMotion:
13
+ .dat*
14
+ .repl_history
15
+ build/
16
+
17
+ ## Documentation cache and generated files:
18
+ /.yardoc/
19
+ /_yardoc/
20
+ /doc/
21
+ /rdoc/
22
+
23
+ ## Environment normalisation:
24
+ /.bundle/
25
+ /vendor/bundle
26
+ /lib/bundler/man/
27
+
28
+ # for a library or gem, you might want to ignore these files since the code is
29
+ # intended to run in multiple environments; otherwise, check them in:
30
+ Gemfile.lock
31
+ .ruby-version
32
+ .ruby-gemset
33
+
34
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
35
+ .rvmrc
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.1
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Michael Chadwick
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,95 @@
1
+ # Clocker
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/clocker.svg)](http://badge.fury.io/rb/clocker)
4
+
5
+ A simple RubyGem that reports how long a specific Ruby command or block of code takes to run.
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
38
+ Add this line to your application's Gemfile:
39
+
40
+ ```ruby
41
+ gem 'clocker'
42
+ ```
43
+
44
+ And then execute:
45
+ ```
46
+ $ bundle
47
+ ```
48
+
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
+ ```
61
+
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`.
63
+
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
71
+
72
+ c1c1c1
73
+ {:mins=>0, :secs=>3, :ms=>13}
74
+ ```
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 @@
1
+ require "bundler/gem_tasks"
@@ -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
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'clocker/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'clocker'
8
+ spec.version = Clocker::VERSION
9
+ spec.platform = Gem::Platform::RUBY
10
+ spec.authors = ['Michael Chadwick']
11
+ spec.email = ['mike@codana.me']
12
+ spec.homepage = 'http://rubygems.org/gems/clocker'
13
+ spec.summary = 'Calculate how long a command or block of code takes to run'
14
+ spec.description = 'Give Clocker some code to process, and it will run it and display how long it took to finish.'
15
+
16
+ spec.files = `git ls-files`.split("\n")
17
+ spec.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ spec.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
19
+ spec.require_paths = ['lib']
20
+ spec.license = 'MIT'
21
+
22
+ spec.add_development_dependency "bundler", "~> 2.1"
23
+ spec.add_development_dependency "rake", "~> 12.3"
24
+ end
@@ -0,0 +1,54 @@
1
+ # lib/clocker.rb
2
+ # Clocker
3
+
4
+ class Clocker
5
+ attr_accessor :options
6
+
7
+ def initialize(options = {})
8
+ self.options = options
9
+ end
10
+
11
+ def clock(&block)
12
+ @start = Time.now
13
+
14
+ if options[:show_messages]
15
+ puts "\nstart: #{@start}"
16
+ end
17
+
18
+ begin
19
+ block.call
20
+ rescue
21
+ puts "ERROR: invalid command or block"
22
+ end
23
+
24
+ return stop
25
+ end
26
+
27
+ def stop
28
+ @stop = Time.now
29
+
30
+ if options[:show_messages]
31
+ puts "\nended: #{@stop}"
32
+ end
33
+
34
+ ms = ((@stop - @start) * 1000).to_i
35
+
36
+ if ms > 60000
37
+ mins = ms / 60000
38
+ ms = ms - (60000 * mins)
39
+ if ms > 1000
40
+ secs = ms / 1000
41
+ ms = ms % 1000
42
+ end
43
+
44
+ return { mins: mins, secs: secs, ms: ms }
45
+ elsif ms > 1000
46
+ secs = ms / 1000
47
+ ms = ms % 1000
48
+
49
+ return { mins: 0, secs: secs, ms: ms }
50
+ else
51
+ return { mins: 0, secs: 0, ms: ms }
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,6 @@
1
+ # lib/clocker/version.rb
2
+ # Version of Clocker
3
+
4
+ class Clocker
5
+ VERSION = '1.0.2'
6
+ end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe Clocker do
4
+ it 'has a version number' do
5
+ expect(Clocker::VERSION).not_to be nil
6
+ end
7
+
8
+ it 'does something useful' do
9
+ expect(false).to eq(true)
10
+ end
11
+ end
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'clocker'
@@ -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 ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: clocker
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Michael Chadwick
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-07-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.1'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '12.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '12.3'
41
+ description: Give Clocker some code to process, and it will run it and display how
42
+ long it took to finish.
43
+ email:
44
+ - mike@codana.me
45
+ executables:
46
+ - clocker
47
+ extensions: []
48
+ extra_rdoc_files: []
49
+ files:
50
+ - ".gitignore"
51
+ - ".rspec"
52
+ - ".travis.yml"
53
+ - Gemfile
54
+ - LICENSE.txt
55
+ - README.md
56
+ - Rakefile
57
+ - bin/clocker
58
+ - clocker.gemspec
59
+ - lib/clocker.rb
60
+ - lib/clocker/version.rb
61
+ - spec/clocker_spec.rb
62
+ - spec/spec_helper.rb
63
+ - tests/test-clocker.rb
64
+ homepage: http://rubygems.org/gems/clocker
65
+ licenses:
66
+ - MIT
67
+ metadata: {}
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubygems_version: 3.1.4
84
+ signing_key:
85
+ specification_version: 4
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