2016-04-05 06:37:41 -04:00
|
|
|
# Instrumenting Ruby Code
|
|
|
|
|
|
|
|
GitLab Performance Monitoring allows instrumenting of custom blocks of Ruby
|
|
|
|
code. This can be used to measure the time spent in a specific part of a larger
|
2016-04-11 06:23:37 -04:00
|
|
|
chunk of code. The resulting data is stored as a field in the transaction that
|
|
|
|
executed the block.
|
2016-04-05 06:37:41 -04:00
|
|
|
|
2016-04-11 06:23:37 -04:00
|
|
|
To start measuring a block of Ruby code you should use `Gitlab::Metrics.measure`
|
|
|
|
and give it a name:
|
2016-04-05 06:37:41 -04:00
|
|
|
|
|
|
|
```ruby
|
2016-04-11 06:23:37 -04:00
|
|
|
Gitlab::Metrics.measure(:foo) do
|
2016-04-05 06:37:41 -04:00
|
|
|
...
|
|
|
|
end
|
|
|
|
```
|
|
|
|
|
2016-04-11 07:27:24 -04:00
|
|
|
3 values are measured for a block:
|
2016-04-05 06:37:41 -04:00
|
|
|
|
2016-04-11 07:27:24 -04:00
|
|
|
1. The real time elapsed, stored in NAME_real_time.
|
|
|
|
2. The CPU time elapsed, stored in NAME_cpu_time.
|
|
|
|
3. The call count, stored in NAME_call_count.
|
2016-04-05 06:37:41 -04:00
|
|
|
|
2016-04-11 06:23:37 -04:00
|
|
|
Both the real and CPU timings are measured in milliseconds.
|
2016-04-05 06:37:41 -04:00
|
|
|
|
2016-04-11 06:23:37 -04:00
|
|
|
Multiple calls to the same block will result in the final values being the sum
|
|
|
|
of all individual values. Take this code for example:
|
2016-04-05 06:37:41 -04:00
|
|
|
|
|
|
|
```ruby
|
2016-04-11 06:23:37 -04:00
|
|
|
3.times do
|
|
|
|
Gitlab::Metrics.measure(:sleep) do
|
|
|
|
sleep 1
|
|
|
|
end
|
2016-04-05 06:37:41 -04:00
|
|
|
end
|
|
|
|
```
|
2016-04-11 06:23:37 -04:00
|
|
|
|
|
|
|
Here the final value of `sleep_real_time` will be `3`, _not_ `1`.
|