gitlab-org--gitlab-foss/lib/gitlab/metrics/transaction.rb
Yorick Peterse d7b4f36a3c
Use clock_gettime for all performance timestamps
Process.clock_gettime allows getting the real time in nanoseconds as
well as allowing one to get a monotonic timestamp. This offers greater
accuracy without the overhead of having to allocate a Time instance. In
general using Time.now/Time.new is about 2x slower than using
Process.clock_gettime(). For example:

    require 'benchmark/ips'

    Benchmark.ips do |bench|
      bench.report 'Time.now' do
        Time.now.to_f
      end

      bench.report 'clock_gettime' do
        Process.clock_gettime(Process::CLOCK_MONOTONIC, :millisecond)
      end

      bench.compare!
    end

Running this benchmark gives:

    Calculating -------------------------------------
                Time.now   108.052k i/100ms
           clock_gettime   125.984k i/100ms
    -------------------------------------------------
                Time.now      2.343M (± 7.1%) i/s -     11.670M
           clock_gettime      4.979M (± 0.8%) i/s -     24.945M

    Comparison:
           clock_gettime:  4979393.8 i/s
                Time.now:  2342986.8 i/s - 2.13x slower

Another benefit of using Process.clock_gettime() is that we can simplify
the code a bit since it can give timestamps in nanoseconds out of the
box.
2016-06-28 17:51:25 +02:00

128 lines
2.8 KiB
Ruby

module Gitlab
module Metrics
# Class for storing metrics information of a single transaction.
class Transaction
THREAD_KEY = :_gitlab_metrics_transaction
attr_reader :tags, :values, :methods
attr_accessor :action
def self.current
Thread.current[THREAD_KEY]
end
# action - A String describing the action performed, usually the class
# plus method name.
def initialize(action = nil)
@metrics = []
@methods = {}
@started_at = nil
@finished_at = nil
@values = Hash.new(0)
@tags = {}
@action = action
@memory_before = 0
@memory_after = 0
end
def duration
@finished_at ? (@finished_at - @started_at) : 0.0
end
def allocated_memory
@memory_after - @memory_before
end
def run
Thread.current[THREAD_KEY] = self
@memory_before = System.memory_usage
@started_at = System.monotonic_time
yield
ensure
@memory_after = System.memory_usage
@finished_at = System.monotonic_time
Thread.current[THREAD_KEY] = nil
end
def add_metric(series, values, tags = {})
@metrics << Metric.new("#{series_prefix}#{series}", values, tags)
end
# Measures the time it takes to execute a method.
#
# Multiple calls to the same method add up to the total runtime of the
# method.
#
# name - The full name of the method to measure (e.g. `User#sign_in`).
def measure_method(name, &block)
unless @methods[name]
series = "#{series_prefix}#{Instrumentation::SERIES}"
@methods[name] = MethodCall.new(name, series)
end
@methods[name].measure(&block)
end
def increment(name, value)
@values[name] += value
end
def set(name, value)
@values[name] = value
end
def add_tag(key, value)
@tags[key] = value
end
def finish
track_self
submit
end
def track_self
values = { duration: duration, allocated_memory: allocated_memory }
@values.each do |name, value|
values[name] = value
end
add_metric('transactions', values, @tags)
end
def submit
submit = @metrics.dup
@methods.each do |name, method|
submit << method.to_metric if method.above_threshold?
end
submit_hashes = submit.map do |metric|
hash = metric.to_hash
hash[:tags][:action] ||= @action if @action
hash
end
Metrics.submit_metrics(submit_hashes)
end
def sidekiq?
Sidekiq.server?
end
def series_prefix
sidekiq? ? 'sidekiq_' : 'rails_'
end
end
end
end