Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Instrument GitLab Shell and log metrics data to a file
- Loading branch information
1 parent
b30d957
commit a4c8927
Showing
9 changed files
with
158 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
require 'logger' | ||
require_relative 'gitlab_config' | ||
|
||
module GitlabMetrics | ||
module System | ||
# 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 | ||
|
||
# Returns the current monotonic clock time in a given precision. | ||
# | ||
# Returns the time as a Fixnum. | ||
def self.monotonic_time | ||
Process.clock_gettime(Process::CLOCK_MONOTONIC, :millisecond) | ||
end | ||
end | ||
|
||
def self.logger | ||
@logger ||= Logger.new(GitlabConfig.new.metrics_log_file) | ||
end | ||
|
||
# Measures the execution time of a block. | ||
# | ||
# Example: | ||
# | ||
# GitlabMetrics.measure(:find_by_username_duration) do | ||
# User.find_by_username(some_username) | ||
# end | ||
# | ||
# name - The name of the field to store the execution time in. | ||
# | ||
# Returns the value yielded by the supplied block. | ||
def self.measure(name) | ||
start_real = System.monotonic_time | ||
start_cpu = System.cpu_time | ||
|
||
retval = yield | ||
|
||
real_time = System.monotonic_time - start_real | ||
cpu_time = System.cpu_time - start_cpu | ||
|
||
logger.debug { "name=#{name.inspect} wall_time=#{real_time.inspect} cpu_time=#{cpu_time.inspect}" } | ||
|
||
retval | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
require_relative 'spec_helper' | ||
require_relative '../lib/gitlab_metrics' | ||
|
||
describe GitlabMetrics do | ||
describe '::measure' do | ||
it 'returns the return value of the block' do | ||
val = described_class.measure('foo') { 10 } | ||
|
||
expect(val).to eq(10) | ||
end | ||
|
||
it 'write in a file metrics data' do | ||
result = nil | ||
expect(described_class.logger).to receive(:debug) do |&b| | ||
result = b.call | ||
end | ||
|
||
described_class.measure('foo') { 10 } | ||
|
||
expect(result).to match(/name=\"foo\" wall_time=\d+ cpu_time=\d+/) | ||
end | ||
|
||
it 'calls proper measure methods' do | ||
expect(described_class::System).to receive(:monotonic_time).twice.and_call_original | ||
expect(described_class::System).to receive(:cpu_time).twice.and_call_original | ||
|
||
described_class.measure('foo') { 10 } | ||
end | ||
end | ||
end |