wd_newrelic_rpm 3.5.6 → 3.5.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (106) hide show
  1. data/CHANGELOG +43 -3
  2. data/Gemfile +6 -2
  3. data/LICENSE +23 -0
  4. data/lib/new_relic/agent.rb +50 -3
  5. data/lib/new_relic/agent/agent.rb +40 -60
  6. data/lib/new_relic/agent/configuration/defaults.rb +9 -3
  7. data/lib/new_relic/agent/configuration/server_source.rb +4 -0
  8. data/lib/new_relic/agent/cross_app_monitor.rb +239 -0
  9. data/lib/new_relic/agent/cross_app_tracing.rb +281 -0
  10. data/lib/new_relic/agent/database.rb +28 -10
  11. data/lib/new_relic/agent/error_collector.rb +5 -0
  12. data/lib/new_relic/agent/event_listener.rb +4 -0
  13. data/lib/new_relic/agent/instrumentation/controller_instrumentation.rb +58 -39
  14. data/lib/new_relic/agent/instrumentation/metric_frame.rb +16 -3
  15. data/lib/new_relic/agent/instrumentation/net.rb +13 -11
  16. data/lib/new_relic/agent/instrumentation/queue_time.rb +50 -192
  17. data/lib/new_relic/agent/instrumentation/rails4/action_controller.rb +145 -0
  18. data/lib/new_relic/agent/instrumentation/rails4/errors.rb +45 -0
  19. data/lib/new_relic/agent/instrumentation/resque.rb +10 -10
  20. data/lib/new_relic/agent/instrumentation/sinatra.rb +19 -9
  21. data/lib/new_relic/agent/new_relic_service.rb +63 -9
  22. data/lib/new_relic/agent/pipe_service.rb +8 -12
  23. data/lib/new_relic/agent/rules_engine.rb +72 -0
  24. data/lib/new_relic/agent/shim_agent.rb +0 -1
  25. data/lib/new_relic/agent/sql_sampler.rb +3 -2
  26. data/lib/new_relic/agent/stats.rb +149 -0
  27. data/lib/new_relic/agent/stats_engine.rb +9 -0
  28. data/lib/new_relic/agent/stats_engine/gc_profiler.rb +1 -24
  29. data/lib/new_relic/agent/stats_engine/metric_stats.rb +84 -185
  30. data/lib/new_relic/agent/stats_engine/stats_hash.rb +58 -0
  31. data/lib/new_relic/agent/stats_engine/transactions.rb +10 -2
  32. data/lib/new_relic/agent/transaction_info.rb +31 -6
  33. data/lib/new_relic/agent/transaction_sample_builder.rb +19 -8
  34. data/lib/new_relic/agent/transaction_sampler.rb +17 -10
  35. data/lib/new_relic/helper.rb +32 -0
  36. data/lib/new_relic/local_environment.rb +24 -32
  37. data/lib/new_relic/okjson.rb +599 -0
  38. data/lib/new_relic/transaction_sample.rb +2 -1
  39. data/lib/new_relic/transaction_sample/segment.rb +2 -1
  40. data/lib/new_relic/version.rb +1 -1
  41. data/newrelic.yml +27 -41
  42. data/test/multiverse/suites/agent_only/Envfile +5 -1
  43. data/test/multiverse/suites/agent_only/audit_log_test.rb +2 -4
  44. data/test/multiverse/suites/agent_only/config/newrelic.yml +1 -2
  45. data/test/multiverse/suites/agent_only/{cross_process_test.rb → cross_application_tracing_test.rb} +3 -3
  46. data/test/multiverse/suites/agent_only/key_transactions_test.rb +66 -0
  47. data/test/multiverse/suites/agent_only/marshaling_test.rb +9 -22
  48. data/test/multiverse/suites/agent_only/rename_rule_test.rb +57 -0
  49. data/test/multiverse/suites/agent_only/start_up_test.rb +1 -1
  50. data/test/multiverse/suites/agent_only/thread_profiling_test.rb +17 -6
  51. data/test/multiverse/suites/rails/error_tracing_test.rb +20 -8
  52. data/test/multiverse/suites/rails/queue_time_test.rb +2 -2
  53. data/test/multiverse/suites/resque/instrumentation_test.rb +4 -3
  54. data/test/multiverse/suites/sinatra/Envfile +2 -0
  55. data/test/multiverse/suites/sinatra/config/newrelic.yml +1 -0
  56. data/test/multiverse/suites/sinatra/sinatra_metric_explosion_test.rb +5 -5
  57. data/test/multiverse/suites/sinatra/sinatra_test.rb +77 -10
  58. data/test/new_relic/agent/agent/connect_test.rb +45 -1
  59. data/test/new_relic/agent/agent/start_worker_thread_test.rb +0 -3
  60. data/test/new_relic/agent/agent_test.rb +20 -40
  61. data/test/new_relic/agent/agent_test_controller_test.rb +27 -60
  62. data/test/new_relic/agent/busy_calculator_test.rb +1 -1
  63. data/test/new_relic/agent/configuration/server_source_test.rb +8 -3
  64. data/test/new_relic/agent/cross_app_monitor_test.rb +237 -0
  65. data/test/new_relic/agent/database_test.rb +60 -16
  66. data/test/new_relic/agent/error_collector_test.rb +28 -4
  67. data/test/new_relic/agent/event_listener_test.rb +23 -2
  68. data/test/new_relic/agent/instrumentation/browser_monitoring_timings_test.rb +1 -1
  69. data/test/new_relic/agent/instrumentation/controller_instrumentation_test.rb +85 -12
  70. data/test/new_relic/agent/instrumentation/metric_frame_test.rb +95 -0
  71. data/test/new_relic/agent/instrumentation/net_instrumentation_test.rb +436 -59
  72. data/test/new_relic/agent/instrumentation/queue_time_test.rb +58 -357
  73. data/test/new_relic/agent/instrumentation/task_instrumentation_test.rb +2 -5
  74. data/test/new_relic/agent/method_tracer_test.rb +4 -2
  75. data/test/new_relic/agent/new_relic_service_test.rb +108 -6
  76. data/test/new_relic/agent/pipe_channel_manager_test.rb +1 -1
  77. data/test/new_relic/agent/pipe_service_test.rb +9 -9
  78. data/test/new_relic/agent/rpm_agent_test.rb +0 -11
  79. data/test/new_relic/agent/rules_engine_test.rb +82 -0
  80. data/test/new_relic/agent/shim_agent_test.rb +0 -4
  81. data/test/new_relic/agent/sql_sampler_test.rb +7 -0
  82. data/test/new_relic/agent/stats_engine/gc_profiler_test.rb +85 -0
  83. data/test/new_relic/agent/stats_engine/metric_stats_test.rb +110 -23
  84. data/test/new_relic/agent/stats_engine_test.rb +1 -46
  85. data/test/new_relic/agent/stats_hash_test.rb +93 -0
  86. data/test/new_relic/agent/stats_test.rb +197 -0
  87. data/test/new_relic/agent/transaction_info_test.rb +63 -11
  88. data/test/new_relic/agent/transaction_sample_builder_test.rb +10 -3
  89. data/test/new_relic/agent/transaction_sampler_test.rb +92 -80
  90. data/test/new_relic/agent/worker_loop_test.rb +1 -1
  91. data/test/new_relic/agent_test.rb +35 -5
  92. data/test/new_relic/control_test.rb +1 -1
  93. data/test/new_relic/fake_collector.rb +87 -9
  94. data/test/new_relic/helper_test.rb +24 -0
  95. data/test/new_relic/metric_data_test.rb +11 -11
  96. data/test/new_relic/metric_spec_test.rb +1 -1
  97. data/test/script/ci.sh +1 -1
  98. data/test/test_contexts.rb +0 -1
  99. data/test/test_helper.rb +21 -3
  100. metadata +34 -41
  101. data/lib/new_relic/agent/cross_process_monitoring.rb +0 -187
  102. data/lib/new_relic/stats.rb +0 -337
  103. data/test/new_relic/agent/cross_process_monitoring_test.rb +0 -190
  104. data/test/new_relic/agent/stats_engine/metric_stats/harvest_test.rb +0 -133
  105. data/test/new_relic/fakes_sending_data.rb +0 -30
  106. data/test/new_relic/stats_test.rb +0 -421
@@ -1,6 +1,5 @@
1
1
  require File.expand_path(File.join(File.dirname(__FILE__),'..','..','..','test_helper'))
2
2
 
3
-
4
3
  class NewRelic::Agent::MetricStatsTest < Test::Unit::TestCase
5
4
  def setup
6
5
  NewRelic::Agent.manual_start
@@ -9,9 +8,9 @@ class NewRelic::Agent::MetricStatsTest < Test::Unit::TestCase
9
8
  puts e
10
9
  puts e.backtrace.join("\n")
11
10
  end
12
-
11
+
13
12
  def teardown
14
- @engine.harvest_timeslice_data({},{})
13
+ @engine.harvest_timeslice_data({})
15
14
  super
16
15
  end
17
16
 
@@ -25,7 +24,7 @@ class NewRelic::Agent::MetricStatsTest < Test::Unit::TestCase
25
24
  assert_not_nil s3
26
25
 
27
26
  assert s1 == s2
28
- assert s1 != s3
27
+ assert_not_same(s1, s3)
29
28
  end
30
29
 
31
30
  def test_harvest
@@ -43,51 +42,139 @@ class NewRelic::Agent::MetricStatsTest < Test::Unit::TestCase
43
42
  assert_equal 2, @engine.get_stats("c").call_count
44
43
  assert_equal 4, @engine.get_stats("c").total_call_time
45
44
 
46
- metric_data = @engine.harvest_timeslice_data({}, {}).values
45
+ harvested = @engine.harvest_timeslice_data({})
47
46
 
48
47
  # after harvest, all the metrics should be reset
49
48
  assert_equal 0, @engine.get_stats("a").call_count
50
49
  assert_equal 0, @engine.get_stats("a").total_call_time
51
-
52
50
  assert_equal 0, @engine.get_stats("c").call_count
53
51
  assert_equal 0, @engine.get_stats("c").total_call_time
54
52
 
55
- metric_data = metric_data.reverse if metric_data[0].metric_spec.name != "a"
56
-
57
- assert_equal 'a', metric_data[0].metric_spec.name
53
+ spec_a = NewRelic::MetricSpec.new('a')
54
+ assert(harvested.has_key?(spec_a))
55
+ assert_equal(1, harvested[spec_a].call_count)
56
+ assert_equal(10, harvested[spec_a].total_call_time)
57
+ end
58
58
 
59
- assert_equal 1, metric_data[0].stats.call_count
60
- assert_equal 10, metric_data[0].stats.total_call_time
59
+ def test_harvest_timeslice_data_applies_metric_rename_rules
60
+ rule = NewRelic::Agent::RulesEngine::Rule.new(
61
+ 'match_expression' => '[0-9]+',
62
+ 'replacement' => '*',
63
+ 'replace_all' => true
64
+ )
65
+ rules_engine = NewRelic::Agent::RulesEngine.new([rule])
66
+
67
+ @engine.get_stats_no_scope('Custom/foo/1/bar/22').record_data_point(1)
68
+ @engine.get_stats_no_scope('Custom/foo/3/bar/44').record_data_point(1)
69
+ @engine.get_stats_no_scope('Custom/foo/5/bar/66').record_data_point(1)
70
+
71
+ harvested = @engine.harvest_timeslice_data({}, rules_engine)
72
+
73
+ assert !harvested.has_key?(NewRelic::MetricSpec.new('Custom/foo/1/bar/22'))
74
+ assert !harvested.has_key?(NewRelic::MetricSpec.new('Custom/foo/3/bar/44'))
75
+ assert !harvested.has_key?(NewRelic::MetricSpec.new('Custom/foo/5/bar/66'))
76
+ merged = harvested[NewRelic::MetricSpec.new('Custom/foo/*/bar/*')]
77
+ assert_equal(3, merged.call_count)
61
78
  end
62
79
 
63
80
  def test_harvest_with_merge
64
81
  s = @engine.get_stats "a"
65
82
  s.trace_call 1
66
-
67
83
  assert_equal 1, @engine.get_stats("a").call_count
68
84
 
69
- harvest = @engine.harvest_timeslice_data({}, {})
85
+ harvest = @engine.harvest_timeslice_data({})
86
+
87
+ s = @engine.get_stats "a"
70
88
  assert_equal 0, s.call_count
71
89
  s.trace_call 2
72
90
  assert_equal 1, s.call_count
73
91
 
74
- # this calk should merge the contents of the previous harvest,
92
+ # this call should merge the contents of the previous harvest,
75
93
  # so the stats for metric "a" should have 2 data points
76
- harvest = @engine.harvest_timeslice_data(harvest, {})
77
- stats = harvest.fetch(NewRelic::MetricSpec.new("a")).stats
94
+ harvest = @engine.harvest_timeslice_data(harvest)
95
+ stats = harvest.fetch(NewRelic::MetricSpec.new("a"))
78
96
  assert_equal 2, stats.call_count
79
97
  assert_equal 3, stats.total_call_time
80
98
  end
81
99
 
82
- def test_rescues_from_synchronization_failure_on_write
83
- hash = NewRelic::Agent::StatsEngine::MetricStats::SynchronizedHash.new
84
- 10.times { |i| hash[i] = i }
100
+ def test_merge_merges
101
+ @engine.get_stats("foo").record_data_point(1)
102
+
103
+ other_stats_hash = NewRelic::Agent::StatsHash.new()
104
+ other_stats_hash.record(NewRelic::MetricSpec.new('foo'), 1)
105
+ other_stats_hash.record(NewRelic::MetricSpec.new('bar'), 1)
106
+
107
+ @engine.merge!(other_stats_hash)
108
+
109
+ foo_stats = @engine.get_stats('foo')
110
+ bar_stats = @engine.get_stats('bar')
111
+ assert_equal(2, foo_stats.call_count)
112
+ assert_equal(1, bar_stats.call_count)
113
+ end
114
+
115
+ def test_record_metric_unscoped_metrics_only_by_default
116
+ @engine.stubs(:default_scope).returns('scopey')
117
+ @engine.record_metric('foo', 42)
118
+ unscoped_stats = @engine.get_stats('foo', false)
119
+ scoped_stats = @engine.get_stats('foo', true, true, 'scopey')
120
+ assert_equal(1, unscoped_stats.call_count)
121
+ assert_equal(0, scoped_stats.call_count)
122
+ end
123
+
124
+ def test_record_metric_records_to_scoped_metric_if_requested
125
+ @engine.stubs(:default_scope).returns('scopey')
126
+ @engine.record_metric('foo', 42, :scoped => true)
127
+ unscoped_stats = @engine.get_stats('foo', false)
128
+ scoped_stats = @engine.get_stats('foo', true, true, 'scopey')
129
+ assert_equal(1, unscoped_stats.call_count)
130
+ assert_equal(1, scoped_stats.call_count)
131
+ end
85
132
 
86
- assert_nothing_raised do
87
- hash.each do |k, v|
88
- hash[11] = 1
133
+ def test_record_metric_elides_scoped_metric_if_not_in_transaction
134
+ @engine.clear_stats
135
+ @engine.stubs(:default_scope).returns(nil)
136
+ @engine.record_metric('foo', 42, :scoped => true)
137
+ unscoped_stats = @engine.get_stats('foo', false)
138
+ assert_equal(1, unscoped_stats.call_count)
139
+ assert_equal(1, @engine.metrics.size)
140
+ end
141
+
142
+ def test_record_metric_accepts_explicit_scope
143
+ @engine.stubs(:default_scope).returns('scopey')
144
+ @engine.record_metric('foo', 42, :scoped => true, :scope => 'not scopey')
145
+ unscoped_stats = @engine.get_stats('foo', false)
146
+ scoped_stats_scopey = @engine.get_stats('foo', true, true, 'scopey')
147
+ scoped_stats_not_scopey = @engine.get_stats('foo', true, true, 'not scopey')
148
+ assert_equal(1, unscoped_stats.call_count)
149
+ assert_equal(0, scoped_stats_scopey.call_count)
150
+ assert_equal(1, scoped_stats_not_scopey.call_count)
151
+ end
152
+
153
+ def test_record_metric_accepts_block
154
+ @engine.record_metric('foo') do |stats|
155
+ stats.call_count = 999
156
+ end
157
+ stats = @engine.get_stats_no_scope('foo')
158
+ assert_equal(999, stats.call_count)
159
+ end
160
+
161
+ def test_record_metric_is_thread_safe
162
+ threads = []
163
+ nthreads = 25
164
+ iterations = 100
165
+ nthreads.times do |tid|
166
+ threads << Thread.new do
167
+ iterations.times do
168
+ @engine.record_metric('m1', 1)
169
+ @engine.record_metric('m2', 1)
170
+ end
89
171
  end
90
172
  end
173
+ threads.each { |t| t.join }
174
+
175
+ stats_m1 = @engine.get_stats_no_scope('m1')
176
+ stats_m2 = @engine.get_stats_no_scope('m2')
177
+ assert_equal(nthreads * iterations, stats_m1.call_count)
178
+ assert_equal(nthreads * iterations, stats_m2.call_count)
91
179
  end
92
180
  end
93
-
@@ -11,7 +11,7 @@ class NewRelic::Agent::StatsEngineTest < Test::Unit::TestCase
11
11
  end
12
12
 
13
13
  def teardown
14
- @engine.harvest_timeslice_data({},{})
14
+ @engine.harvest_timeslice_data({})
15
15
  mocha_teardown
16
16
  super
17
17
  end
@@ -177,51 +177,6 @@ class NewRelic::Agent::StatsEngineTest < Test::Unit::TestCase
177
177
  assert_equal 10, scope1.children_time.round
178
178
  end
179
179
 
180
-
181
- def test_collect_gc_data
182
- GC.disable unless NewRelic::LanguageSupport.using_engine?('jruby')
183
- if NewRelic::LanguageSupport.using_engine?('rbx')
184
- agent = ::Rubinius::Agent.loopback
185
- agent.stubs(:get).with('system.gc.young.total_wallclock') \
186
- .returns([:value, 1000], [:value, 2500])
187
- agent.stubs(:get).with('system.gc.full.total_wallclock') \
188
- .returns([:value, 2000], [:value, 3500])
189
- agent.stubs(:get).with('system.gc.young.count') \
190
- .returns([:value, 1], [:value, 2])
191
- agent.stubs(:get).with('system.gc.full.count') \
192
- .returns([:value, 1], [:value, 2])
193
- ::Rubinius::Agent.stubs(:loopback).returns(agent)
194
- elsif NewRelic::LanguageSupport.using_version?('1.9')
195
- ::GC::Profiler.stubs(:enabled?).returns(true)
196
- ::GC::Profiler.stubs(:total_time).returns(1.0, 4.0)
197
- ::GC.stubs(:count).returns(1, 3)
198
- ::GC::Profiler.stubs(:clear).returns(nil)
199
- elsif NewRelic::LanguageSupport.using_version?('1.8.7') &&
200
- RUBY_DESCRIPTION =~ /Ruby Enterprise Edition/
201
- ::GC.stubs(:time).returns(1000000, 4000000)
202
- ::GC.stubs(:collections).returns(1, 3)
203
- else
204
- return true # no need to test if we're not collecting GC metrics
205
- end
206
-
207
- engine = NewRelic::Agent.instance.stats_engine
208
- tracer = NewRelic::Agent::TransactionSampler.new
209
- tracer.instance_variable_set(:@last_sample,
210
- NewRelic::TransactionSample.new)
211
- engine.transaction_sampler = tracer
212
- engine.start_transaction
213
- scope = engine.push_scope "scope"
214
- engine.pop_scope scope, 0.01
215
- engine.end_transaction
216
-
217
- gc_stats = engine.get_stats('GC/cumulative')
218
- assert_equal 2, gc_stats.call_count
219
- assert_equal 3.0, gc_stats.total_call_time
220
- assert_equal(3.0, tracer.last_sample.params[:custom_params][:gc_time])
221
- ensure
222
- GC.enable unless NewRelic::LanguageSupport.using_engine?('jruby')
223
- end
224
-
225
180
  private
226
181
  def check_time_approximate(expected, actual)
227
182
  assert((expected - actual).abs < 0.1, "Expected between #{expected - 0.1} and #{expected + 0.1}, got #{actual}")
@@ -0,0 +1,93 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__),'..', '..','test_helper'))
2
+
3
+ class NewRelic::Agent::StatsHashTest < Test::Unit::TestCase
4
+ def setup
5
+ @hash = NewRelic::Agent::StatsHash.new
6
+ end
7
+
8
+ def test_creates_default_entries
9
+ stats = @hash['a/b/c/d']
10
+ assert_kind_of(NewRelic::Agent::Stats, stats)
11
+ end
12
+
13
+ def test_record_accpets_single_metric_spec
14
+ spec = NewRelic::MetricSpec.new('foo/bar')
15
+ stats = @hash[spec]
16
+ stats.expects(:record_data_point).with(42)
17
+ @hash.record(spec, 42)
18
+ end
19
+
20
+ def test_record_accepts_multiple_metric_specs
21
+ spec1 = NewRelic::MetricSpec.new('foo/bar', 'scope1')
22
+ spec2 = NewRelic::MetricSpec.new('foo/bar', 'scope2')
23
+ stats1 = @hash[spec1]
24
+ stats2 = @hash[spec2]
25
+ stats1.expects(:record_data_point).with(42)
26
+ stats2.expects(:record_data_point).with(42)
27
+ @hash.record([spec1, spec2], 42)
28
+ end
29
+
30
+ def test_record_accepts_single_metric_spec_with_block
31
+ spec = NewRelic::MetricSpec.new('foo')
32
+ stats = @hash[spec]
33
+ stats.expects(:do_stuff)
34
+ @hash.record(spec) do |s|
35
+ s.do_stuff
36
+ end
37
+ end
38
+
39
+ def test_record_accepts_multiple_metric_specs_with_block
40
+ specs = [
41
+ NewRelic::MetricSpec.new('foo'),
42
+ NewRelic::MetricSpec.new('bar')
43
+ ]
44
+ stats = specs.map { |spec| @hash[spec] }
45
+ stats.each { |stat| stat.expects(:do_stuff) }
46
+ @hash.record(specs) do |s|
47
+ s.do_stuff
48
+ end
49
+ end
50
+
51
+ def test_record_accepts_stats_value
52
+ spec = NewRelic::MetricSpec.new('foo')
53
+ other_stats = NewRelic::Agent::Stats.new
54
+ stats = @hash[spec]
55
+ stats.expects(:merge!).with(other_stats)
56
+ @hash.record(spec, other_stats)
57
+ end
58
+
59
+ def test_merge_merges
60
+ specs = [
61
+ NewRelic::MetricSpec.new('foo'),
62
+ NewRelic::MetricSpec.new('bar'),
63
+ NewRelic::MetricSpec.new('baz'),
64
+ NewRelic::MetricSpec.new('baz', 'a_scope')
65
+ ]
66
+
67
+ hash1 = NewRelic::Agent::StatsHash.new
68
+ hash1.record(specs[0], 1)
69
+ hash1.record(specs[1], 2)
70
+ hash1.record(specs[2], 3)
71
+
72
+ hash2 = NewRelic::Agent::StatsHash.new
73
+ hash2.record(specs[0], 1)
74
+ hash2.record(specs[1], 2)
75
+ hash2.record(specs[3], 3) # no scope
76
+
77
+ hash1.merge!(hash2)
78
+
79
+ assert_equal(4, hash1.keys.size)
80
+ assert_equal(2, hash1[specs[0]].call_count)
81
+ assert_equal(2, hash1[specs[1]].call_count)
82
+ assert_equal(1, hash1[specs[2]].call_count)
83
+ assert_equal(1, hash1[specs[3]].call_count)
84
+ end
85
+
86
+ def test_marshal_dump
87
+ hash = NewRelic::Agent::StatsHash.new()
88
+ hash.record('foo', 1)
89
+ hash.record('bar', 2)
90
+ copy = Marshal.load(Marshal.dump(hash))
91
+ assert_equal(hash, copy)
92
+ end
93
+ end
@@ -0,0 +1,197 @@
1
+ ENV['SKIP_RAILS'] = 'true'
2
+ require File.expand_path(File.join(File.dirname(__FILE__),'..', '..', 'test_helper'))
3
+
4
+ class NewRelic::Agent::StatsTest < Test::Unit::TestCase
5
+ def mock_plusequals(first, second, method, first_value, second_value)
6
+ first.expects(method).returns(first_value)
7
+ second.expects(method).returns(second_value)
8
+ first.expects("#{method}=".to_sym).with(first_value + second_value)
9
+ end
10
+
11
+ def test_update_totals
12
+ attrs = [:total_call_time, :total_exclusive_time, :sum_of_squares]
13
+ merged = setup_merge do |a, b|
14
+ attrs.each do |attr|
15
+ a.send("#{attr}=", 2)
16
+ b.send("#{attr}=", 3)
17
+ end
18
+ end
19
+ attrs.each do |attr|
20
+ assert_equal(5, merged.send(attr))
21
+ end
22
+ end
23
+
24
+ def setup_merge
25
+ a = NewRelic::Agent::Stats.new
26
+ b = NewRelic::Agent::Stats.new
27
+ a.reset
28
+ b.reset
29
+ yield a, b
30
+ a.merge(b)
31
+ end
32
+
33
+ def test_merge_expands_min_max_call_time
34
+ merged = setup_merge do |a, b|
35
+ a.call_count = 1
36
+ b.call_count = 1
37
+ a.min_call_time = 0.5
38
+ a.max_call_time = 3.0
39
+ b.min_call_time = 1.0
40
+ b.max_call_time = 4.0
41
+ end
42
+ assert_equal(0.5, merged.min_call_time)
43
+ assert_equal(4.0, merged.max_call_time)
44
+ end
45
+
46
+ def test_simple
47
+ stats = NewRelic::Agent::Stats.new
48
+ validate stats, 0, 0, 0, 0
49
+
50
+ assert_equal stats.call_count,0
51
+ stats.trace_call 10
52
+ stats.trace_call 20
53
+ stats.trace_call 30
54
+
55
+ validate stats, 3, (10+20+30), 10, 30
56
+ end
57
+
58
+ def test_to_s
59
+ s1 = NewRelic::Agent::Stats.new
60
+ s1.trace_call 10
61
+ assert_equal("[ 1 calls 10.0000s]", s1.to_s)
62
+ end
63
+
64
+ def test_apdex_recording
65
+ s = NewRelic::Agent::Stats.new
66
+
67
+ s.record_apdex_s
68
+ s.record_apdex_t
69
+
70
+ s.record_apdex_f
71
+ s.record_apdex_t
72
+
73
+ assert_equal(1, s.apdex_s)
74
+ assert_equal(1, s.apdex_f)
75
+ assert_equal(2, s.apdex_t)
76
+ end
77
+
78
+ def test_merge
79
+ s1 = NewRelic::Agent::Stats.new
80
+ s2 = NewRelic::Agent::Stats.new
81
+
82
+ s1.trace_call 10
83
+ s2.trace_call 20
84
+ s2.freeze
85
+
86
+ validate s2, 1, 20, 20, 20
87
+ s3 = s1.merge s2
88
+ validate s3, 2, (10+20), 10, 20
89
+ validate s1, 1, 10, 10, 10
90
+ validate s2, 1, 20, 20, 20
91
+
92
+ s1.merge! s2
93
+ validate s1, 2, (10+20), 10, 20
94
+ validate s2, 1, 20, 20, 20
95
+ end
96
+
97
+ def test_merge_with_exclusive
98
+ s1 = NewRelic::Agent::Stats.new
99
+
100
+ s2 = NewRelic::Agent::Stats.new
101
+
102
+ s1.trace_call 10, 5
103
+ s2.trace_call 20, 10
104
+ s2.freeze
105
+
106
+ validate s2, 1, 20, 20, 20, 10
107
+ s3 = s1.merge s2
108
+ validate s3, 2, (10+20), 10, 20, (10+5)
109
+ validate s1, 1, 10, 10, 10, 5
110
+ validate s2, 1, 20, 20, 20, 10
111
+
112
+ s1.merge! s2
113
+ validate s1, 2, (10+20), 10, 20, (5+10)
114
+ validate s2, 1, 20, 20, 20, 10
115
+ end
116
+
117
+ def test_merge_array
118
+ s1 = NewRelic::Agent::Stats.new
119
+ merges = []
120
+ merges << (NewRelic::Agent::Stats.new.trace_call 1)
121
+ merges << (NewRelic::Agent::Stats.new.trace_call 1)
122
+ merges << (NewRelic::Agent::Stats.new.trace_call 1)
123
+
124
+ s1.merge! merges
125
+ validate s1, 3, 3, 1, 1
126
+ end
127
+
128
+ def test_freeze
129
+ s1 = NewRelic::Agent::Stats.new
130
+
131
+ s1.trace_call 10
132
+ s1.freeze
133
+
134
+ begin
135
+ # the following should throw an exception because s1 is frozen
136
+ s1.trace_call 20
137
+ assert false
138
+ rescue StandardError
139
+ assert s1.frozen?
140
+ validate s1, 1, 10, 10, 10
141
+ end
142
+ end
143
+
144
+ def test_sum_of_squares_merge
145
+ s1 = NewRelic::Agent::Stats.new
146
+ s1.trace_call 4
147
+ s1.trace_call 7
148
+
149
+ s2 = NewRelic::Agent::Stats.new
150
+ s2.trace_call 13
151
+ s2.trace_call 16
152
+
153
+ s3 = s1.merge(s2)
154
+
155
+ assert_equal(s1.sum_of_squares, 4*4 + 7*7)
156
+ assert_equal(s3.sum_of_squares, 4*4 + 7*7 + 13*13 + 16*16, "check sum of squares")
157
+ end
158
+
159
+ def test_chained_stats_proxies_calls_to_scoped_and_unscoped
160
+ scoped_stats = NewRelic::Agent::Stats.new
161
+ unscoped_stats = NewRelic::Agent::Stats.new
162
+ chained_stats = NewRelic::Agent::ChainedStats.new(scoped_stats, unscoped_stats)
163
+ chained_stats.record_data_point(42)
164
+ assert_equal(1, scoped_stats.call_count)
165
+ assert_equal(1, unscoped_stats.call_count)
166
+ assert_equal(42, scoped_stats.total_call_time)
167
+ assert_equal(42, unscoped_stats.total_call_time)
168
+ end
169
+
170
+ def test_chained_stats_returns_values_from_scoped
171
+ scoped_stats = NewRelic::Agent::Stats.new
172
+ unscoped_stats = NewRelic::Agent::Stats.new
173
+ chained_stats = NewRelic::Agent::ChainedStats.new(scoped_stats, unscoped_stats)
174
+ scoped_stats.record_data_point(42)
175
+ assert_equal(1, chained_stats.call_count)
176
+ assert_equal(42, chained_stats.total_call_time)
177
+ end
178
+
179
+ if RUBY_VERSION >= '1.9'
180
+ def test_to_json_enforces_float_values
181
+ s1 = NewRelic::Agent::Stats.new
182
+ s1.trace_call 3.to_r
183
+ s1.trace_call 7.to_r
184
+
185
+ assert_equal 3.0, JSON.load(s1.to_json)['min_call_time']
186
+ end
187
+ end
188
+
189
+ private
190
+ def validate (stats, count, total, min, max, exclusive = nil)
191
+ assert_equal count, stats.call_count
192
+ assert_equal total, stats.total_call_time
193
+ assert_equal min, stats.min_call_time
194
+ assert_equal max, stats.max_call_time
195
+ assert_equal exclusive, stats.total_exclusive_time if exclusive
196
+ end
197
+ end