BDE 4.14.0 Production release
|
Provide a scoped guard for recording elapsed time.
This component provides a scoped guard class intended to simplify the task of recording (to a metric) the elapsed time of a block of code. The balm::StopwatchScopedGuard
is supplied the identity of a metric on construction, and an optional enumerated constant indicating the time units to report values in (by default, values are reported in seconds). The guard measures the elapsed time between its construction and destruction, and on destruction records that elapsed time, in the indicated time units, to the supplied metric.
Bloomberg software may alternatively use the GUTS telemetry API, which is integrated into Bloomberg infrastructure.
The balm::StopwatchScopedGuard
class and the macros defined in the balm_metrics component provide the same basic functionality. Clients may find that using a balm::StopwatchScopedGuard
object (in coordination with a balm::Metric
object) is better suited to collecting metrics associated with a particular instance of a stateful object, while macros are better suited to collecting metrics associated with a particular code path (rather than an object instance). In most instances, however, choosing between the two is a matter of taste.
balm::StopwatchScopedGuard
is const thread-safe, meaning that accessors may be invoked concurrently from different threads, but it is not safe to access or modify a balm::StopwatchScopedGuard
in one thread while thread modifies the same object. Note however, that at this another time balm::StopwatchScopedGuard
provides no manipulator methods.
This section illustrates intended use of this component.
This example demonstrates how to create the default balm::MetricManager
instance and perform a trivial configuration.
First we create a balm::DefaultMetricsManagerScopedGuard
, which manages the lifetime of the default metrics manager instance. At construction, we provide the scoped guard an output stream (stdout
) that it will publish metrics to. Note that the default metrics manager is intended to be created and destroyed by the owner of main
. An instance of the manager should be created during the initialization of an application (while the task has a single thread) and destroyed just prior to termination (when there is similarly a single thread).
Once the default instance has been created, it can be accessed using the instance
operation:
Note that the default metrics manager will be released when managerGuard
exits this scoped and is destroyed. Clients that choose to explicitly call the balm::DefaultMetricsManager::create
method must also explicitly call the balm::DefaultMetricsManager::release
method.
Alternatively, we can use the balm::StopwatchScopedGuard
to record metric values. In the following example we implement a hypothetical request processor similar to the one in example 3. We use a balm::Metric
(d_elapsedTime
) and a balm::StopwatchScopedGuard
(guard
) to record the elapsed time of the request-processing function.