Module: TimeUp
- Defined in:
- lib/time_up.rb,
lib/time_up/version.rb
Defined Under Namespace
Constant Summary collapse
- VERSION =
"0.0.7"
Class Method Summary collapse
- .__ensure_timer(name) ⇒ Object
-
.__timers ⇒ Object
Internal methods.
- .active_timers ⇒ Object
- .all_elapsed ⇒ Object
- .all_stats ⇒ Object
- .all_timers ⇒ Object
- .delete_all ⇒ Object
- .percentile(name, percentage) ⇒ Object
- .print_detailed_summary(io = $stdout) ⇒ Object
- .print_summary(io = $stdout) ⇒ Object
- .reset_all ⇒ Object
-
.start(name, &blk) ⇒ Object
Delegate methods.
-
.stop_all ⇒ Object
Iterative methods.
- .timer(name) ⇒ Object
-
.total_elapsed ⇒ Object
Interrogative methods.
Class Method Details
.__ensure_timer(name) ⇒ Object
157 158 159 |
# File 'lib/time_up.rb', line 157 def self.__ensure_timer(name) raise Error.new("No timer named #{name.inspect}") unless __timers[name] end |
.__timers ⇒ Object
Internal methods
153 154 155 |
# File 'lib/time_up.rb', line 153 def self.__timers Thread.current[:time_up_timers] ||= {} end |
.active_timers ⇒ Object
68 69 70 |
# File 'lib/time_up.rb', line 68 def self.active_timers __timers.values.select(&:active?) end |
.all_elapsed ⇒ Object
44 45 46 47 48 |
# File 'lib/time_up.rb', line 44 def self.all_elapsed __timers.values.map { |timer| [timer.name, timer.elapsed] }.to_h end |
.all_stats ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/time_up.rb', line 54 def self.all_stats __timers.values.map { |timer| [timer.name, { elapsed: timer.elapsed, count: timer.count, min: timer.min, max: timer.max, mean: timer.mean, median: timer.median, "95th": timer.percentile(95) }] }.to_h end |
.all_timers ⇒ Object
50 51 52 |
# File 'lib/time_up.rb', line 50 def self.all_timers __timers.values end |
.delete_all ⇒ Object
147 148 149 150 |
# File 'lib/time_up.rb', line 147 def self.delete_all __timers.values.each { |t| t.reset(force: true) } Thread.current[:time_up_timers] = {} end |
.percentile(name, percentage) ⇒ Object
34 35 36 37 |
# File 'lib/time_up.rb', line 34 def self.percentile(name, percentage) __ensure_timer(name) __timers[name].percentile(percentage) end |
.print_detailed_summary(io = $stdout) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/time_up.rb', line 88 def self.print_detailed_summary(io = $stdout) cols = { names: ["Name"], elapsed: ["Elapsed"], count: ["Count"], min: ["Min"], max: ["Max"], mean: ["Mean"], median: ["Median"], "95th": ["95th %"] } __timers.values.each { |timer| cols[:names] << "#{timer.name.inspect}#{"*" if timer.active?}" cols[:elapsed] << "%.5f" % timer.elapsed cols[:count] << timer.count.to_s cols[:min] << "%.5f" % timer.min cols[:max] << "%.5f" % timer.max cols[:mean] << "%.5f" % timer.mean cols[:median] << "%.5f" % timer.median cols[:"95th"] << "%.5f" % timer.percentile(95) } widths = cols.map { |name, vals| [name, vals.map(&:length).max] }.to_h rows = cols[:names].size.times.map { |i| if i == 0 cols.keys.map { |name| cols[name][i].center(widths[name]) } else cols.keys.map { |name| cols[name][i].ljust(widths[name]) } end } full_width = widths.values.sum + (rows[0].size - 1) * 3 io.puts <<~SUMMARY #{"=" * full_width} #{rows[0].join(" | ")} #{"-" * full_width} #{rows[1..-1].map { |row| row.join(" | ") }.join("\n")} #{"* Denotes that the timer is still active\n" if __timers.values.any?(&:active?)} SUMMARY end |
.print_summary(io = $stdout) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/time_up.rb', line 72 def self.print_summary(io = $stdout) longest_name_length = __timers.values.map { |t| t.name.inspect.size }.max summaries = __timers.values.map { |timer| name = "#{timer.name.inspect}#{"*" if timer.active?}".ljust(longest_name_length + 1) "#{name}\t#{"%.5f" % timer.elapsed}s" } io.puts <<~SUMMARY TimeUp summary ======================== #{summaries.join("\n")} #{"* Denotes that the timer is still active\n" if __timers.values.any?(&:active?)} SUMMARY end |
.reset_all ⇒ Object
143 144 145 |
# File 'lib/time_up.rb', line 143 def self.reset_all __timers.values.each(&:reset) end |
.start(name, &blk) ⇒ Object
Delegate methods
13 14 15 |
# File 'lib/time_up.rb', line 13 def self.start(name, &blk) timer(name).start(&blk) end |
.stop_all ⇒ Object
Iterative methods
139 140 141 |
# File 'lib/time_up.rb', line 139 def self.stop_all __timers.values.each(&:stop) end |
.timer(name) ⇒ Object
8 9 10 |
# File 'lib/time_up.rb', line 8 def self.timer(name) __timers[name] ||= Timer.new(name) end |
.total_elapsed ⇒ Object
Interrogative methods
40 41 42 |
# File 'lib/time_up.rb', line 40 def self.total_elapsed __timers.values.sum(&:elapsed) end |