diff options
author | Yorick Peterse <yorickpeterse@gmail.com> | 2016-04-11 12:23:37 +0200 |
---|---|---|
committer | Yorick Peterse <yorickpeterse@gmail.com> | 2016-04-11 13:09:36 +0200 |
commit | 16926a676bdea4bfbbbaf9d390373073d2ff8bbd (patch) | |
tree | 3faba49dd9678474a603777a25501c0cf9ef8261 /lib | |
parent | 85279c07c9be889a25811a685110ab57a217651e (diff) | |
download | gitlab-ce-16926a676bdea4bfbbbaf9d390373073d2ff8bbd.tar.gz |
Store block timings as transaction values
This makes it easier to query, simplifies the code, and makes it
possible to figure out what transaction the data belongs to (simply
because it's now stored _in_ the transaction).
This new setup keeps track of both the real/wall time _and_ CPU time
spent in a block, both measured using milliseconds (to keep all units
the same).
Diffstat (limited to 'lib')
-rw-r--r-- | lib/gitlab/metrics.rb | 23 | ||||
-rw-r--r-- | lib/gitlab/metrics/system.rb | 11 |
2 files changed, 25 insertions, 9 deletions
diff --git a/lib/gitlab/metrics.rb b/lib/gitlab/metrics.rb index 4a3f47b5a95..4d64555027e 100644 --- a/lib/gitlab/metrics.rb +++ b/lib/gitlab/metrics.rb @@ -74,24 +74,29 @@ module Gitlab # # Example: # - # Gitlab::Metrics.measure(:find_by_username_timings) do + # Gitlab::Metrics.measure(:find_by_username_duration) do # User.find_by_username(some_username) # end # - # series - The name of the series to store the data in. - # values - A Hash containing extra values to add to the metric. - # tags - A Hash containing extra tags to add to the metric. + # name - The name of the field to store the execution time in. # # Returns the value yielded by the supplied block. - def self.measure(series, values = {}, tags = {}) + def self.measure(name) return yield unless Transaction.current - start = Time.now.to_f + real_start = Time.now.to_f + cpu_start = System.cpu_time + retval = yield - duration = (Time.now.to_f - start) * 1000.0 - values = values.merge(duration: duration) - Transaction.current.add_metric(series, values, tags) + cpu_stop = System.cpu_time + real_stop = Time.now.to_f + + real_time = (real_stop - real_start) * 1000.0 + cpu_time = cpu_stop - cpu_start + + Transaction.current.increment("#{name}_real_time", real_time) + Transaction.current.increment("#{name}_cpu_time", cpu_time) retval end diff --git a/lib/gitlab/metrics/system.rb b/lib/gitlab/metrics/system.rb index 83371265278..a7d183b2f94 100644 --- a/lib/gitlab/metrics/system.rb +++ b/lib/gitlab/metrics/system.rb @@ -30,6 +30,17 @@ module Gitlab 0 end end + + # THREAD_CPUTIME is not supported on OS X + if Process.const_defined?(:CLOCK_THREAD_CPUTIME_ID) + def self.cpu_time + Process.clock_gettime(Process::CLOCK_THREAD_CPUTIME_ID, :millisecond) + end + else + def self.cpu_time + Process.clock_gettime(Process::CLOCK_PROCESS_CPUTIME_ID, :millisecond) + end + end end end end |