Module: TickTock
- Defined in:
- lib/tick_tock.rb,
lib/tick_tock/card.rb,
lib/tick_tock/clock.rb,
lib/tick_tock/punch.rb,
lib/tick_tock/locals.rb,
lib/tick_tock/version.rb,
lib/tick_tock/card_logger.rb
Overview
TickTock makes it easy to wrap your Ruby code to measure nested timings and to log them – even when the code is asynchronous or lazy, so straight-forward blocks do not work.
The TickTock module uses Ruby’s module_function directive to enable two different usage patterns:
-
Calling methods directly on the module
-
Including the module into classes as a mix-in
Configuration
When calling methods directly on the module, the instance of Clock configured via TickTock.clock= will be used for all calls.
On the other hand, when including the TickTock module into a class, the class may choose to override #clock to return a specific instance.
Defined Under Namespace
Modules: Locals Classes: Card, CardLogger, Clock, Punch
Constant Summary collapse
- VERSION =
"0.2.0"
Class Attribute Summary collapse
-
.clock ⇒ Clock
The configured global instance of Clock, assigned by calling TickTock.clock=.
Basics: Start and stop a timing context collapse
-
.tick(subject: nil) ⇒ Object
Starts a new timing context, returns a “card” representing it.
-
.tock(card:) ⇒ Object
Completes a timing context represented by the given “card”.
Helpers: Wrap an asynchronous construct in a timing context collapse
-
.tick_tock(subject: nil) ⇒ Object
Executes the given block in a timing context using TickTock.tick and TickTock.tock.
-
.tick_tock_lazy(lazy_enum_to_wrap, subject: nil) ⇒ Enumerator::Lazy
Wraps a lazy enumerator with a timing context using lazy calls to TickTock.tick and TickTock.tock, which will be called when the enumerator starts and completes enumeration respectively.
-
.tick_tock_proc(callable_to_wrap = nil, subject: nil, save_context: false, &proc_to_wrap) ⇒ Proc
Wraps a Proc with a timing context using a call to TickTock.tick_tock.
Instance Method Summary collapse
-
#clock ⇒ Clock
When the TickTock module is included in a class, by default returns the configured global instance of Clock.
Class Attribute Details
Class Method Details
.tick(subject: nil) ⇒ Object
Starts a new timing context, returns a “card” representing it.
80 81 82 |
# File 'lib/tick_tock.rb', line 80 def tick(subject: nil) clock.tick(subject: subject) end |
.tick_tock(subject: nil) ⇒ Object
100 101 102 103 104 105 |
# File 'lib/tick_tock.rb', line 100 def tick_tock(subject: nil) card = tick(subject: subject) yield ensure tock(card: card) unless card.nil? end |
.tick_tock_lazy(lazy_enum_to_wrap, subject: nil) ⇒ Enumerator::Lazy
Wraps a lazy enumerator with a timing context using lazy calls to tick and tock, which will be called when the enumerator starts and completes enumeration respectively.
115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/tick_tock.rb', line 115 def tick_tock_lazy(lazy_enum_to_wrap, subject: nil) shared_state = [nil] lazy_tick = proc { shared_state[0] = tick(subject: subject); [] } lazy_tock = proc { shared_state[0] = tock(card: shared_state[0]); [] } arr_with_callbacks = [ [:dummy].lazy.flat_map(&lazy_tick), lazy_enum_to_wrap.lazy, [:dummy].lazy.flat_map(&lazy_tock) ] arr_with_callbacks.lazy.flat_map(&:itself) end |
.tick_tock_proc(callable_to_wrap = nil, subject: nil, save_context: false, &proc_to_wrap) ⇒ Proc
Wraps a Proc with a timing context using a call to tick_tock. Can optionally wrap the current nested timing contexts into the Proc, so that when executed asynchronously it will retain the same context.
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/tick_tock.rb', line 152 def tick_tock_proc( callable_to_wrap = nil, subject: nil, save_context: false, &proc_to_wrap ) tt_proc = proc do |*proc_args| # if original subject was a Proc, apply it to args to create the subject subject = subject&.respond_to?(:call) ? subject.call(*proc_args) : subject tick_tock(subject: subject) do (callable_to_wrap || proc_to_wrap).call(*proc_args) end end save_context ? Locals.wrap_proc(&tt_proc) : tt_proc end |
.tock(card:) ⇒ Object
Completes a timing context represented by the given “card”.
88 89 90 |
# File 'lib/tick_tock.rb', line 88 def tock(card:) clock.tock(card: card) end |