Skip to content

Commit 28d90a8

Browse files
committed
use faster time method
1 parent e1bbd12 commit 28d90a8

4 files changed

Lines changed: 26 additions & 25 deletions

File tree

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ group :localdev do
1414
end
1515

1616
group :test do
17-
gem "timecop"
1817
gem 'tins', '~> 1.6.0'
18+
gem 'mocha'
1919
if RbConfig::CONFIG['ruby_version'].start_with?("1.9")
2020
gem 'json', '< 2'
2121
gem 'public_suffix', '< 1.5'

lib/datadog/statsd.rb

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -218,10 +218,11 @@ def timing(stat, ms, opts={})
218218
# $statsd.time('account.activate') { @account.activate! }
219219
def time(stat, opts={})
220220
opts = {:sample_rate => opts} if opts.is_a? Numeric
221-
start = Time.now
221+
start = (PROCESS_TIME_SUPPORTED ? Process.clock_gettime(Process::CLOCK_MONOTONIC) : Time.now.to_f)
222222
return yield
223223
ensure
224-
time_since(stat, start, opts)
224+
finished = (PROCESS_TIME_SUPPORTED ? Process.clock_gettime(Process::CLOCK_MONOTONIC) : Time.now.to_f)
225+
timing(stat, ((finished - start) * 1000).round, opts)
225226
end
226227
# Sends a value to be tracked as a set to the statsd server.
227228
#
@@ -237,7 +238,6 @@ def set(stat, value, opts={})
237238
send_stats stat, value, SET_TYPE, opts
238239
end
239240

240-
241241
# This method allows you to send custom service check statuses.
242242
#
243243
# @param [String] name Service check name
@@ -356,9 +356,10 @@ def close()
356356
DOT = ".".freeze
357357
DOUBLE_COLON = "::".freeze
358358
UNDERSCORE = "_".freeze
359+
PROCESS_TIME_SUPPORTED = (RUBY_VERSION >= "2.1.0")
359360

360361
private_constant :NEW_LINE, :ESC_NEW_LINE, :COMMA, :BLANK, :PIPE, :DOT,
361-
:DOUBLE_COLON, :UNDERSCORE
362+
:DOUBLE_COLON, :UNDERSCORE, :PROCESS_TIME_SUPPORTED
362363

363364
def escape_event_content(msg)
364365
msg.gsub NEW_LINE, ESC_NEW_LINE
@@ -383,10 +384,6 @@ def escape_service_check_message(msg)
383384
escape_event_content(msg).gsub('m:'.freeze, 'm\:'.freeze)
384385
end
385386

386-
def time_since(stat, start, opts)
387-
timing(stat, ((Time.now - start) * 1000).round, opts)
388-
end
389-
390387
def join_array_to_str(str, array, joiner)
391388
array.each_with_index do |item, i|
392389
str << joiner unless i == 0

spec/helper.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
require 'rubygems'
21
require 'bundler/setup'
32
require 'minitest/autorun'
43
require 'faker'
54

6-
$LOAD_PATH.unshift(File.dirname(__FILE__))
7-
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
5+
$LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
86

97
require 'simplecov'
108
SimpleCov.start

spec/statsd_spec.rb

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
require 'helper'
1+
require_relative 'helper'
22
require 'socket'
33
require 'stringio'
4-
require 'timecop'
4+
require 'mocha/mini_test'
55

66
describe Datadog::Statsd do
77
class Datadog::Statsd
@@ -265,32 +265,27 @@ class Datadog::Statsd
265265
describe "#time" do
266266
describe "With actual time testing" do
267267
before do
268-
# Freezing time to prevent random test failures
269-
Timecop.freeze Time.now
270-
end
271-
272-
after do
273-
Timecop.return
268+
stub_time 0 # Freezing time to prevent random test failures
274269
end
275270

276271
it "should format the message according to the statsd spec" do
277272
@statsd.time('foobar') do
278-
Timecop.freeze(Time.now + 1)
273+
stub_time 1
279274
end
280275
@statsd.socket.recv.must_equal ['foobar:1000|ms']
281276
end
282277

283278
it "should still time if block is failing" do
284279
@statsd.time('foobar') do
285-
Timecop.freeze(Time.now + 1)
280+
stub_time 1
286281
raise StandardError, 'This is failing'
287282
end rescue
288283
@statsd.socket.recv.must_equal ['foobar:1000|ms']
289284
end
290285

291286
def helper_time_return
292287
@statsd.time('foobar') do
293-
Timecop.freeze(Time.now + 1)
288+
stub_time 1
294289
return
295290
end
296291
end
@@ -315,8 +310,9 @@ def helper_time_return
315310
describe "with a sample rate" do
316311
before { class << @statsd; def rand; 0; end; end } # ensure delivery
317312
it "should format the message according to the statsd spec" do
313+
stub_time 0
318314
@statsd.time('foobar', :sample_rate=>0.5) do
319-
Timecop.freeze(Time.now + 1)
315+
stub_time 1
320316
end
321317
@statsd.socket.recv.must_equal ['foobar:1000|ms|@0.5']
322318
end
@@ -325,8 +321,9 @@ def helper_time_return
325321
describe "with a sample rate like statsd-ruby" do
326322
before { class << @statsd; def rand; 0; end; end } # ensure delivery
327323
it "should format the message according to the statsd spec" do
324+
stub_time 0
328325
@statsd.time('foobar', 0.5) do
329-
Timecop.freeze(Time.now + 1)
326+
stub_time 1
330327
end
331328
@statsd.socket.recv.must_equal ['foobar:1000|ms|@0.5']
332329
end
@@ -863,6 +860,15 @@ def send(*)
863860
@statsd.close
864861
end
865862
end
863+
864+
def stub_time(shift)
865+
t = 12345.0 + shift
866+
if RUBY_VERSION >= "2.1.0"
867+
Process.stubs(:clock_gettime).returns(t)
868+
else
869+
Time.stubs(:now).returns(Time.at(t))
870+
end
871+
end
866872
end
867873

868874
describe Datadog::Statsd do

0 commit comments

Comments
 (0)