timmy 0.1.0 → 0.2.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
  SHA1:
3
- metadata.gz: 378e5c010407e6580faf572a2978188f53ec39f0
4
- data.tar.gz: 8c3dd12f56e273ee41a833245624921798ef8c0d
3
+ metadata.gz: ab3e84bce8d95e5a76d488fb1abb5f8505c6c90a
4
+ data.tar.gz: 10ccacef3de073ba665185003850a0e83c4334e3
5
5
  SHA512:
6
- metadata.gz: b2b47383177bbc8504e59536032cd2403dcf4c4677612017c9adecdb014d7bd9142e86bcd02e13cc308cac5356f9587301a9b0d5a426d44f59e8484991d6998a
7
- data.tar.gz: 1fcdaff54247ee41a9fff01f76e27ec77f8fe7b34dd87a1b04204d316632bd0fa08fcfdfc0aca05656effc127b94fe79db47837dd491952e41f289997dad016a
6
+ metadata.gz: 26175e0bf819a6f13c5b9f29f28159c130865463837727ab23beb6d0d1e40f5101e5623025458719d3065a151a1f4f96c8f5c44b43ec53a686f1e2eb2d04550c
7
+ data.tar.gz: d0e2ee57f092eea87d35ea718cdc76704568cc09b89e443cfd79b3cfb5589d33816ae7099c913bf2da298bbe739a433c796b69a2145f386b520e82c3e673534d
data/bin/timmy ADDED
@@ -0,0 +1,34 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'optparse'
4
+ require 'timmy'
5
+
6
+ options = {
7
+ replay: nil,
8
+ profile: false
9
+ }
10
+
11
+ OptionParser.new do |parser|
12
+ parser.banner = "Usage: [COMMAND] | timmy"
13
+ parser.separator ""
14
+ parser.separator "Options:"
15
+ parser.on("-r LOG", "--replay LOG", "Replay specific log file") do |replay|
16
+ options[:replay] = replay
17
+ end
18
+ parser.on("-p", "--profile", "Profile targeted timers ") do |profile|
19
+ options[:profile] = profile
20
+ end
21
+ end.parse!
22
+
23
+ Timmy::ConfigLoader.load
24
+ Timmy::Logger.clear
25
+
26
+ if replay = options[:replay]
27
+ Timmy::Runner.replay_log(replay)
28
+ else
29
+ Timmy::Runner.consume_stdin
30
+ end
31
+
32
+ if options[:profile]
33
+ Timmy::TargetedTimer.put_stopped_profiles
34
+ end
@@ -0,0 +1,59 @@
1
+ module Timmy
2
+ class Logger
3
+ class << self
4
+ def clear
5
+ @output = ''
6
+ end
7
+
8
+ def set_output_directory(dir)
9
+ @output_directory = dir
10
+ end
11
+
12
+ def set_precision(precision)
13
+ @precision = precision
14
+ end
15
+
16
+ def put(line, internal: false, since: 0)
17
+ duration = format_duration(MasterTimer.get - since)
18
+ if internal
19
+ @output += "#{duration} > #{line}\n"
20
+ puts bold("#{duration} #{line}")
21
+ else
22
+ @output += "#{duration} | #{line}\n"
23
+ puts feint(duration) + " " + line
24
+ end
25
+ end
26
+
27
+ def finalize
28
+ suffix = "#{MasterTimer.start.to_i}+#{(MasterTimer.get).to_i}"
29
+ filename = File.join(output_directory, "timmy-#{suffix}.log")
30
+
31
+ put("EOF > #{filename}", internal: true)
32
+ File.write(filename, @output)
33
+ end
34
+
35
+ def format_duration(duration)
36
+ format = precision > 0 ? "%d:%0#{3 + precision}.#{precision}f" : "%d:%02d"
37
+ sprintf(format, duration / 60, duration % 60)
38
+ end
39
+
40
+ private
41
+
42
+ def bold(string)
43
+ "\e[0m\e[1m#{string}\e[0m"
44
+ end
45
+
46
+ def feint(string)
47
+ "\e[0m\e[2m#{string}\e[0m"
48
+ end
49
+
50
+ def output_directory
51
+ @output_directory ||= "/tmp"
52
+ end
53
+
54
+ def precision
55
+ @precision ||= 0
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,20 @@
1
+ module Timmy
2
+ class MasterTimer
3
+ class << self
4
+ def start(time = nil)
5
+ @start_time = time if time
6
+ @start_time ||= Time.now.to_f
7
+ end
8
+
9
+ def get
10
+ return @duration if @duration
11
+ Time.now.to_f - @start_time
12
+ end
13
+
14
+ def set(duration)
15
+ @duration = duration
16
+ end
17
+ end
18
+ end
19
+ end
20
+
@@ -0,0 +1,46 @@
1
+ module Timmy
2
+ class Runner
3
+ class << self
4
+ def consume_stdin
5
+ MasterTimer.start
6
+
7
+ STDIN.each_line do |line|
8
+ run_line(line.rstrip)
9
+ end
10
+
11
+ TargetedTimer.stop_all
12
+ Logger.finalize
13
+ end
14
+
15
+ def replay_log(log)
16
+ basename = File.basename(log)
17
+ match = log.match(/timmy-(?<time>\d+)\+\d+.log/)
18
+ time = match[:time].to_i
19
+
20
+ MasterTimer.start(time)
21
+
22
+ lines = File.readlines(log)
23
+ lines.each do |line|
24
+ if match = line.match(/^(?<m>\d+):(?<s>\d+(\.\d+)?) \| (?<content>.*)/)
25
+ content = match[:content]
26
+ time = match[:m].to_i * 60 + match[:s].to_f
27
+
28
+ MasterTimer.set(time)
29
+ run_line(content)
30
+ end
31
+ end
32
+
33
+ TargetedTimer.stop_all
34
+ Logger.finalize
35
+ end
36
+
37
+ private
38
+
39
+ def run_line(line)
40
+ TargetedTimer.start_for_line(line)
41
+ Logger.put(line)
42
+ TargetedTimer.stop_for_line(line)
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,85 @@
1
+ module Timmy
2
+ class TargetedTimer
3
+ class << self
4
+ def start_for_line(line)
5
+ TargetedTimerDefinition.all.each do |definition|
6
+ if match = line.match(definition.start_regex)
7
+ stop_by_id(definition.id)
8
+ label = match.send(:[], :label) rescue nil
9
+ started.push(self.new(definition, label: label))
10
+ end
11
+ end
12
+ end
13
+
14
+ def stop_for_line(line)
15
+ started.each do |timer|
16
+ if (stop_regex = timer.definition.stop_regex) && line.match?(stop_regex)
17
+ stop(timer)
18
+ end
19
+ end
20
+ end
21
+
22
+ def stop(timer)
23
+ timer.stop
24
+ started.delete(timer)
25
+ stopped.push(timer)
26
+ end
27
+
28
+ def stop_by_id(id)
29
+ matches = started.select { |timer| timer.definition.id == id }
30
+ matches.each { |timer| stop(timer) }
31
+ end
32
+
33
+ def stop_all
34
+ started.each { |timer| stop(timer) }
35
+ end
36
+
37
+ def started
38
+ @started ||= []
39
+ end
40
+
41
+ def stopped
42
+ @stopped ||= []
43
+ end
44
+
45
+ def put_stopped_profiles
46
+ puts ""
47
+ puts "Slowest targeted timers:"
48
+ TargetedTimer.stopped.sort_by { |timer| -timer.duration }[0..9].each do |timer|
49
+ timer.put_profile
50
+ end
51
+ end
52
+ end
53
+
54
+ attr_reader :definition, :label, :duration
55
+
56
+ def initialize(definition, label: nil)
57
+ @definition = definition
58
+ @start_time = MasterTimer.get
59
+ @label = label
60
+ end
61
+
62
+ def stop
63
+ put
64
+ @duration = MasterTimer.get - @start_time
65
+ end
66
+
67
+ def put_profile
68
+ duration = Logger.format_duration(@duration)
69
+ puts " - #{duration} - #{formatted_id} - #{@label}"
70
+ end
71
+
72
+ private
73
+
74
+ def put
75
+ log_line = formatted_id
76
+ log_line += " #{@label}" if @label
77
+
78
+ Logger.put(log_line, since: @start_time, internal: true)
79
+ end
80
+
81
+ def formatted_id
82
+ @definition.id.to_s.split('_').collect(&:capitalize).join
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,26 @@
1
+ module Timmy
2
+ class TargetedTimerDefinition
3
+ class << self
4
+ def add(id, start_regex:, stop_regex: nil)
5
+ all.push(self.new(id, start_regex: start_regex, stop_regex: stop_regex))
6
+ end
7
+
8
+ def all
9
+ @all ||= [
10
+ self.new(:docker_build,
11
+ start_regex: /Step \d+\/\d+ : (?<label>.*)$/,
12
+ stop_regex: / ---> [0-9a-f]{12}$/)
13
+ ]
14
+ end
15
+ end
16
+
17
+ attr_reader :id, :start_regex, :stop_regex
18
+
19
+ def initialize(id, start_regex:, stop_regex: nil)
20
+ @id = id
21
+
22
+ @start_regex = start_regex
23
+ @stop_regex = stop_regex
24
+ end
25
+ end
26
+ end
data/lib/timmy/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Timmy
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/timmy.rb CHANGED
@@ -1,4 +1,7 @@
1
1
  require "timmy/config_loader"
2
- require "timmy/meters"
3
- require "timmy/timer"
2
+ require "timmy/logger"
3
+ require "timmy/master_timer"
4
+ require "timmy/runner"
5
+ require "timmy/targeted_timer"
6
+ require "timmy/targeted_timer_definition"
4
7
  require "timmy/version"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: timmy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Karol Słuszniak
8
8
  autorequire:
9
- bindir: exe
9
+ bindir: bin
10
10
  cert_chain: []
11
- date: 2019-10-28 00:00:00.000000000 Z
11
+ date: 2019-10-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,7 +52,9 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '5.0'
55
- description:
55
+ description: A simple command line tool that allows to measure how much time an arbitrary
56
+ command spends on each stage of execution by annotating the command output with
57
+ timestamps as well as running command-specific targeted timers.
56
58
  email:
57
59
  - ksluszniak@gmail.com
58
60
  executables:
@@ -60,21 +62,15 @@ executables:
60
62
  extensions: []
61
63
  extra_rdoc_files: []
62
64
  files:
63
- - ".gitignore"
64
- - ".travis.yml"
65
- - Gemfile
66
- - LICENSE.md
67
- - README.md
68
- - Rakefile
69
- - bin/console
70
- - bin/setup
71
- - exe/timmy
65
+ - bin/timmy
72
66
  - lib/timmy.rb
73
67
  - lib/timmy/config_loader.rb
74
- - lib/timmy/meters.rb
75
- - lib/timmy/timer.rb
68
+ - lib/timmy/logger.rb
69
+ - lib/timmy/master_timer.rb
70
+ - lib/timmy/runner.rb
71
+ - lib/timmy/targeted_timer.rb
72
+ - lib/timmy/targeted_timer_definition.rb
76
73
  - lib/timmy/version.rb
77
- - timmy.gemspec
78
74
  homepage: https://github.com/karolsluszniak/timmy
79
75
  licenses:
80
76
  - MIT
@@ -98,5 +94,5 @@ rubyforge_project:
98
94
  rubygems_version: 2.6.14
99
95
  signing_key:
100
96
  specification_version: 4
101
- summary: Time execution of command and its stages marked by console output
97
+ summary: Time execution of commands and their stages based on console output
102
98
  test_files: []
data/.gitignore DELETED
@@ -1,8 +0,0 @@
1
- /.bundle/
2
- /.yardoc
3
- /_yardoc/
4
- /coverage/
5
- /doc/
6
- /pkg/
7
- /spec/reports/
8
- /tmp/
data/.travis.yml DELETED
@@ -1,7 +0,0 @@
1
- ---
2
- sudo: false
3
- language: ruby
4
- cache: bundler
5
- rvm:
6
- - 2.4.1
7
- before_install: gem install bundler -v 1.16.5
data/Gemfile DELETED
@@ -1,6 +0,0 @@
1
- source "https://rubygems.org"
2
-
3
- git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
-
5
- # Specify your gem's dependencies in timmy.gemspec
6
- gemspec
data/LICENSE.md DELETED
@@ -1,21 +0,0 @@
1
- The MIT License (MIT)
2
-
3
- Copyright (c) 2019 Karol Słuszniak
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.
data/README.md DELETED
@@ -1,38 +0,0 @@
1
- # Timmy
2
-
3
- **Time execution of command and its stages marked by console output.**
4
-
5
- This tool allows to measure how much an arbitrary command line tool spends on each stage of its execution as long as it prints lines to standard output to indicate when these stages start and end.
6
-
7
- ## Installation
8
-
9
- Install the gem globally:
10
-
11
- gem install timmy
12
-
13
- ## Usage
14
-
15
- Pipe the output from any command to it, e.g. from `docker build`:
16
-
17
- docker build . | timmy
18
-
19
- As the most basic means for timing, each line of command output will be prefixed by total time elapsed since `timmy` has started.
20
-
21
- In addition, `timmy` is capable of capturing and timing specific stages of command execution by matching the output against its meter definitions. For example, there's a built-in support provided for `docker build` which means that:
22
-
23
- - meter is started every time Docker outputs something like `Step 16/22 : RUN bundle`
24
- - meter is stopped when Docker outputs something like ` ---> 8912f93fa8a5` and `timmy` prints the duration of a Docker build step that has just finished
25
-
26
- In the end, `timmy` will also write the entire output to a log file in `/tmp` in order to make it easier to run a series of benchmarks and refer back to their results.
27
-
28
- ## Configuration
29
-
30
- You can add your own meters by creating `~/.timmy.rb` like below:
31
-
32
- ```ruby
33
- Timmy::Meters.add(:docker_build_step,
34
- start_regex: /Step \d+\/\d+ : (?<title>.*)$/,
35
- end_regex: / ---> [0-9a-f]{12}$/)
36
- ```
37
-
38
- Note that `end_regex` is optional - if absent, previous meter instance will be stopped every time a new is started.
data/Rakefile DELETED
@@ -1,10 +0,0 @@
1
- require "bundler/gem_tasks"
2
- require "rake/testtask"
3
-
4
- Rake::TestTask.new(:test) do |t|
5
- t.libs << "test"
6
- t.libs << "lib"
7
- t.test_files = FileList["test/**/*_test.rb"]
8
- end
9
-
10
- task :default => :test
data/bin/console DELETED
@@ -1,14 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require "bundler/setup"
4
- require "timmy"
5
-
6
- # You can add fixtures and/or initialization code here to make experimenting
7
- # with your gem easier. You can also use a different console, if you like.
8
-
9
- # (If you use this, don't forget to add pry to your Gemfile!)
10
- # require "pry"
11
- # Pry.start
12
-
13
- require "irb"
14
- IRB.start(__FILE__)
data/bin/setup DELETED
@@ -1,8 +0,0 @@
1
- #!/usr/bin/env bash
2
- set -euo pipefail
3
- IFS=$'\n\t'
4
- set -vx
5
-
6
- bundle install
7
-
8
- # Do any other automated setup that you need to do here
data/exe/timmy DELETED
@@ -1,11 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require 'optparse'
4
- require 'timmy'
5
-
6
- OptionParser.new do |opts|
7
- opts.banner = "Usage: [COMMAND] | timmy"
8
- end.parse!
9
-
10
- Timmy::ConfigLoader.load()
11
- Timmy::Timer.new().consume_stdin()
data/lib/timmy/meters.rb DELETED
@@ -1,24 +0,0 @@
1
- module Timmy
2
- class Meters
3
- BUILTIN = {
4
- docker_build_step: {
5
- begin_regex: /Step \d+\/\d+ : (?<title>.*)$/,
6
- end_regex: / ---> [0-9a-f]{12}$/
7
- }
8
- }
9
-
10
- class << self
11
- def add(key, begin_regex:, end_regex: nil)
12
- meters[key] = { begin_regex: begin_regex, end_regex: end_regex }
13
- end
14
-
15
- def clone_all
16
- Marshal.load(Marshal.dump(meters))
17
- end
18
-
19
- def meters
20
- @meters ||= BUILTIN
21
- end
22
- end
23
- end
24
- end
data/lib/timmy/timer.rb DELETED
@@ -1,115 +0,0 @@
1
- module Timmy
2
- class Timer
3
- def initialize
4
- @start_time = current_time()
5
- @output = ""
6
- @meters = Meters.clone_all
7
- end
8
-
9
- def consume_stdin
10
- STDIN.each_line do |line|
11
- line.rstrip!
12
-
13
- try_start_meters(line)
14
- put_line(line)
15
- try_end_meters(line)
16
- end
17
-
18
- end_all_meters()
19
- finalize()
20
- end
21
-
22
- private
23
-
24
- def try_start_meters(line)
25
- @meters.each do |key, meter|
26
- begin_regex = meter.fetch(:begin_regex)
27
-
28
- if meter_start = line.match(begin_regex)
29
- end_meter(key) if meter.has_key?(:start_time)
30
-
31
- title = begin
32
- meter_start[:title]
33
- rescue IndexError
34
- nil
35
- end
36
-
37
- meter.merge!({
38
- start_time: current_time(),
39
- title: title
40
- })
41
- end
42
- end
43
- end
44
-
45
- def try_end_meters(line)
46
- started_meters.each do |key, meter|
47
- if (end_regex = meter[:end_regex]) && line.match?(end_regex)
48
- end_meter(key)
49
- end
50
- end
51
- end
52
-
53
- def end_all_meters
54
- started_meters.each do |key, _|
55
- end_meter(key)
56
- end
57
- end
58
-
59
- def end_meter(key)
60
- meter = @meters.fetch(key)
61
-
62
- start_time = meter.fetch(:start_time)
63
- title = meter.fetch(:title)
64
-
65
- log_line = key.to_s.upcase
66
- log_line += " #{title}" if title
67
-
68
- put_line(log_line, since: start_time, internal: true)
69
-
70
- meter.delete(:start_time)
71
- meter.delete(:title)
72
- end
73
-
74
- def put_line(line, since: @start_time, internal: false)
75
- current_time_cached = current_time()
76
-
77
- if internal
78
- @output += "#{format_duration(current_time_cached - since)} #{line}\n"
79
- puts "#{bold(format_duration(current_time_cached - since))} #{bold(line)}"
80
- else
81
- @output += "#{format_duration(current_time_cached - since)} | #{line}\n"
82
- puts "#{feint(format_duration(current_time_cached - since))} #{line}"
83
- end
84
- end
85
-
86
- def finalize
87
- total_duration = current_time() - @start_time
88
- suffix = "#{@start_time}+#{total_duration.round}"
89
- log_filename = "/tmp/timmy-#{suffix}.log"
90
-
91
- put_line("EOF > #{log_filename}", internal: true)
92
- File.write(log_filename, @output)
93
- end
94
-
95
- def format_duration(duration)
96
- sprintf("%d:%02d", duration / 60, duration % 60)
97
- end
98
-
99
- def bold(string)
100
- "\e[0m\e[1m#{string}\e[0m"
101
- end
102
-
103
- def feint(string)
104
- "\e[0m\e[2m#{string}\e[0m"
105
- end
106
-
107
- def current_time
108
- Time.now().to_i()
109
- end
110
-
111
- def started_meters
112
- @meters.select { |_, meter| meter.has_key?(:start_time) }
113
- end
114
- end
115
- end
data/timmy.gemspec DELETED
@@ -1,25 +0,0 @@
1
- lib = File.expand_path("../lib", __FILE__)
2
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
- require "timmy/version"
4
-
5
- Gem::Specification.new do |spec|
6
- spec.name = "timmy"
7
- spec.version = Timmy::VERSION
8
- spec.authors = ["Karol Słuszniak"]
9
- spec.email = ["ksluszniak@gmail.com"]
10
-
11
- spec.summary = "Time execution of command and its stages marked by console output"
12
- spec.homepage = "https://github.com/karolsluszniak/timmy"
13
- spec.license = "MIT"
14
-
15
- spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
16
- `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
- end
18
- spec.bindir = "exe"
19
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
- spec.require_paths = ["lib"]
21
-
22
- spec.add_development_dependency "bundler", "~> 1.16"
23
- spec.add_development_dependency "rake", "~> 10.0"
24
- spec.add_development_dependency "minitest", "~> 5.0"
25
- end