time_m 0.1.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 +7 -0
- data/.rspec +3 -0
- data/.standard.yml +3 -0
- data/CHANGELOG.md +5 -0
- data/LICENSE.txt +21 -0
- data/README.md +84 -0
- data/Rakefile +10 -0
- data/docs/time_m.png +0 -0
- data/docs/time_m_code.png +0 -0
- data/lib/time_m/timer.rb +25 -0
- data/lib/time_m/version.rb +5 -0
- data/lib/time_m.rb +104 -0
- metadata +80 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 1625e7f38895cbe0171813d220f22f43f9020a3bfc57ef73427fc2ba1be48bb8
|
4
|
+
data.tar.gz: 377a2a49b5419c047fc763fc39b2aa821eb2e32b71897595721170754273739a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5343059d77c20b496b61d0962f18b1b37194ac74ed60527e22ee385bce269e1eb520b3dff3d5cfe57625d4240a50d89716e47770030d96947d1cd38fcb11905d
|
7
|
+
data.tar.gz: d2f8d1889ebc3b9541b310f8ee7fbb655cb7117b30c89ce55ba564b32d7b7662f23e9391c7bc769e62722587295f67d74575cb01fdefc5602858814a3707e2ae
|
data/.rspec
ADDED
data/.standard.yml
ADDED
data/CHANGELOG.md
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2025 Igor Kasyanchuk
|
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,84 @@
|
|
1
|
+
# Time.measure
|
2
|
+
|
3
|
+
The easiest way to measure execution time of your code. No more `puts` or `logger.info` with `Time.now - time`.
|
4
|
+
|
5
|
+
Start measuring your code, and if you value your time, use this gem :)
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
group :development, :test do
|
11
|
+
gem "time_m"
|
12
|
+
end
|
13
|
+
```
|
14
|
+
|
15
|
+
## Usage
|
16
|
+
|
17
|
+
To make is super easy to use, it extends `Time` class with `m` (or `measure`) method. There is also an advanced way to use it, see below.
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
Time.m("test") do
|
21
|
+
sleep 0.1
|
22
|
+
end
|
23
|
+
|
24
|
+
# or
|
25
|
+
|
26
|
+
def index
|
27
|
+
Time.m
|
28
|
+
some_method_1
|
29
|
+
Time.m
|
30
|
+
some_method_2
|
31
|
+
Time.m
|
32
|
+
some_method_3
|
33
|
+
Time.m
|
34
|
+
end
|
35
|
+
```
|
36
|
+
|
37
|
+
## How it works
|
38
|
+
|
39
|
+
Under the hood, it uses `Process.clock_gettime(Process::CLOCK_MONOTONIC)` to get the current time.
|
40
|
+
|
41
|
+
After first call, it stores the time in a thread-local variable using `Thread.current`.
|
42
|
+
|
43
|
+
With second call, it calculates the difference between the current time and the time stored in the thread-local variable.
|
44
|
+
|
45
|
+
## Advanced usage
|
46
|
+
|
47
|
+
Support of multiple timers. You can call any method (it's using `method_missing` under the hood) to start a new timer with it's name.
|
48
|
+
|
49
|
+
It will print duration if the same method is called again.
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
def index
|
53
|
+
TM.index
|
54
|
+
TM.some_method_1
|
55
|
+
some_method_1
|
56
|
+
TM.some_method_1
|
57
|
+
TM.some_method_2
|
58
|
+
some_method_2
|
59
|
+
TM.some_method_2
|
60
|
+
TM.index
|
61
|
+
end
|
62
|
+
```
|
63
|
+
|
64
|
+
## Development
|
65
|
+
|
66
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
67
|
+
|
68
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
69
|
+
|
70
|
+
## TODO
|
71
|
+
|
72
|
+
- config for colors
|
73
|
+
- config to specify print of caller
|
74
|
+
- config to enable extension for Time class
|
75
|
+
- config for custom output
|
76
|
+
- instrumentation support?
|
77
|
+
|
78
|
+
## Contributing
|
79
|
+
|
80
|
+
You are welcome to contribute to the project.
|
81
|
+
|
82
|
+
## License
|
83
|
+
|
84
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/docs/time_m.png
ADDED
Binary file
|
Binary file
|
data/lib/time_m/timer.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
module TimeM
|
2
|
+
class Timer
|
3
|
+
attr_reader :timers
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@timers = {}
|
7
|
+
end
|
8
|
+
|
9
|
+
def init(name = nil)
|
10
|
+
@timers[name] ||= {}
|
11
|
+
now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
12
|
+
@timers[name][:time] = now
|
13
|
+
touch(name, now)
|
14
|
+
end
|
15
|
+
|
16
|
+
def touch(name, time)
|
17
|
+
@timers[name] ||= {}
|
18
|
+
@timers[name][:last_time] = time
|
19
|
+
end
|
20
|
+
|
21
|
+
def reset_all
|
22
|
+
@timers = {}
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/lib/time_m.rb
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "time_m/version"
|
4
|
+
require_relative "time_m/timer"
|
5
|
+
require "colorize"
|
6
|
+
|
7
|
+
module TimeM
|
8
|
+
NAME = "| time_m |"
|
9
|
+
SKIP_FLAG = -1
|
10
|
+
|
11
|
+
def self.method_missing(method, *args, &block) # rubocop:disable Style/MissingRespondToMissing
|
12
|
+
name = method.to_s
|
13
|
+
label = args.first.to_s
|
14
|
+
label = (label == "") ? nil : label
|
15
|
+
|
16
|
+
m(name, label, &block)
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.m(name = nil, label = nil, &block)
|
20
|
+
now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
21
|
+
if block_given?
|
22
|
+
yield.tap do |res|
|
23
|
+
output(name, label, Process.clock_gettime(Process::CLOCK_MONOTONIC) - now)
|
24
|
+
end
|
25
|
+
else
|
26
|
+
timer = tracker.timers[name]
|
27
|
+
if timer.nil?
|
28
|
+
tracker.init(name)
|
29
|
+
output(name, label)
|
30
|
+
else
|
31
|
+
diff = now - timer[:last_time] if timer[:last_time]
|
32
|
+
output(name, label, now - timer[:time], diff)
|
33
|
+
tracker.touch(name, now)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
private_class_method
|
39
|
+
|
40
|
+
def self.output(name, label, time = SKIP_FLAG, last_time = nil)
|
41
|
+
display_name = label || name || caller[1..2].detect { |line| !line.include?("/lib/time_m") }
|
42
|
+
display_name = display_name.gsub(Rails.root.to_s + "/", "") if defined?(Rails)
|
43
|
+
|
44
|
+
if time == SKIP_FLAG
|
45
|
+
info NAME.on_light_yellow
|
46
|
+
info " #{display_name} ⏲", new_line: true
|
47
|
+
return
|
48
|
+
end
|
49
|
+
|
50
|
+
time = time.round(3)
|
51
|
+
last_time = last_time.round(3) if last_time
|
52
|
+
info NAME.on_light_yellow
|
53
|
+
|
54
|
+
if display_name
|
55
|
+
info " #{display_name} ∑"
|
56
|
+
warning sprintf("%0.3f", time)
|
57
|
+
info " seconds"
|
58
|
+
|
59
|
+
if last_time
|
60
|
+
info ", "
|
61
|
+
success "+#{sprintf("%0.3f", last_time)}"
|
62
|
+
info " seconds", new_line: true
|
63
|
+
else
|
64
|
+
info new_line: true
|
65
|
+
end
|
66
|
+
else
|
67
|
+
info " Elapsed time: #{sprintf("%0.3f", time)} seconds", new_line: true
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def self.tracker
|
72
|
+
Thread.current[:time_m_timer] ||= Timer.new
|
73
|
+
end
|
74
|
+
|
75
|
+
# colors: https://github.com/fazibear/colorize/blob/master/lib/colorize/class_methods.rb
|
76
|
+
|
77
|
+
def self.info(str = "", new_line: false)
|
78
|
+
log(str.magenta.italic, new_line: new_line)
|
79
|
+
end
|
80
|
+
|
81
|
+
def self.success(str = "", new_line: false)
|
82
|
+
log(str.light_green.bold, new_line: new_line)
|
83
|
+
end
|
84
|
+
|
85
|
+
def self.warning(str = "", new_line: false)
|
86
|
+
log(str.light_yellow.bold, new_line: new_line)
|
87
|
+
end
|
88
|
+
|
89
|
+
def self.log(str = "", new_line: false)
|
90
|
+
new_line ? puts(str) : print(str)
|
91
|
+
end
|
92
|
+
|
93
|
+
module TimeExt
|
94
|
+
def m(name = nil, label = nil, &block)
|
95
|
+
TimeM.m(name, label, &block)
|
96
|
+
end
|
97
|
+
|
98
|
+
alias_method :measure, :m
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
TM = TimeM
|
103
|
+
|
104
|
+
Time.extend(TimeM::TimeExt)
|
metadata
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: time_m
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Igor Kasyanchuk
|
8
|
+
bindir: exe
|
9
|
+
cert_chain: []
|
10
|
+
date: 2025-06-24 00:00:00.000000000 Z
|
11
|
+
dependencies:
|
12
|
+
- !ruby/object:Gem::Dependency
|
13
|
+
name: colorize
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
15
|
+
requirements:
|
16
|
+
- - ">="
|
17
|
+
- !ruby/object:Gem::Version
|
18
|
+
version: '0'
|
19
|
+
type: :runtime
|
20
|
+
prerelease: false
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
22
|
+
requirements:
|
23
|
+
- - ">="
|
24
|
+
- !ruby/object:Gem::Version
|
25
|
+
version: '0'
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: debug
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
description: Measure time in your code
|
41
|
+
email:
|
42
|
+
- igorkasyanchuk@gmail.com
|
43
|
+
executables: []
|
44
|
+
extensions: []
|
45
|
+
extra_rdoc_files: []
|
46
|
+
files:
|
47
|
+
- ".rspec"
|
48
|
+
- ".standard.yml"
|
49
|
+
- CHANGELOG.md
|
50
|
+
- LICENSE.txt
|
51
|
+
- README.md
|
52
|
+
- Rakefile
|
53
|
+
- docs/time_m.png
|
54
|
+
- docs/time_m_code.png
|
55
|
+
- lib/time_m.rb
|
56
|
+
- lib/time_m/timer.rb
|
57
|
+
- lib/time_m/version.rb
|
58
|
+
homepage: https://github.com/igorkasyanchuk/time_m
|
59
|
+
licenses:
|
60
|
+
- MIT
|
61
|
+
metadata:
|
62
|
+
homepage_uri: https://github.com/igorkasyanchuk/time_m
|
63
|
+
rdoc_options: []
|
64
|
+
require_paths:
|
65
|
+
- lib
|
66
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
requirements: []
|
77
|
+
rubygems_version: 3.6.3
|
78
|
+
specification_version: 4
|
79
|
+
summary: Measure time in your code
|
80
|
+
test_files: []
|