timmy 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 378e5c010407e6580faf572a2978188f53ec39f0
4
+ data.tar.gz: 8c3dd12f56e273ee41a833245624921798ef8c0d
5
+ SHA512:
6
+ metadata.gz: b2b47383177bbc8504e59536032cd2403dcf4c4677612017c9adecdb014d7bd9142e86bcd02e13cc308cac5356f9587301a9b0d5a426d44f59e8484991d6998a
7
+ data.tar.gz: 1fcdaff54247ee41a9fff01f76e27ec77f8fe7b34dd87a1b04204d316632bd0fa08fcfdfc0aca05656effc127b94fe79db47837dd491952e41f289997dad016a
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
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 ADDED
@@ -0,0 +1,6 @@
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 ADDED
@@ -0,0 +1,21 @@
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 ADDED
@@ -0,0 +1,38 @@
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 ADDED
@@ -0,0 +1,10 @@
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 ADDED
@@ -0,0 +1,14 @@
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 ADDED
@@ -0,0 +1,8 @@
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 ADDED
@@ -0,0 +1,11 @@
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()
@@ -0,0 +1,14 @@
1
+ module Timmy
2
+ class ConfigLoader
3
+ class << self
4
+ def load
5
+ home = Dir.home()
6
+ config = File.join(home, ".timmy.rb")
7
+
8
+ if File.exists?(config)
9
+ eval(File.read(config))
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,24 @@
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
@@ -0,0 +1,115 @@
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
@@ -0,0 +1,3 @@
1
+ module Timmy
2
+ VERSION = "0.1.0"
3
+ end
data/lib/timmy.rb ADDED
@@ -0,0 +1,4 @@
1
+ require "timmy/config_loader"
2
+ require "timmy/meters"
3
+ require "timmy/timer"
4
+ require "timmy/version"
data/timmy.gemspec ADDED
@@ -0,0 +1,25 @@
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
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: timmy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Karol Słuszniak
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-10-28 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: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ description:
56
+ email:
57
+ - ksluszniak@gmail.com
58
+ executables:
59
+ - timmy
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ 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
72
+ - lib/timmy.rb
73
+ - lib/timmy/config_loader.rb
74
+ - lib/timmy/meters.rb
75
+ - lib/timmy/timer.rb
76
+ - lib/timmy/version.rb
77
+ - timmy.gemspec
78
+ homepage: https://github.com/karolsluszniak/timmy
79
+ licenses:
80
+ - MIT
81
+ metadata: {}
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 2.6.14
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: Time execution of command and its stages marked by console output
102
+ test_files: []