genki-newrelic_rpm 2.10.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (158) hide show
  1. data/CHANGELOG +316 -0
  2. data/LICENSE +37 -0
  3. data/Manifest +156 -0
  4. data/README.md +138 -0
  5. data/Rakefile +22 -0
  6. data/bin/mongrel_rpm +33 -0
  7. data/bin/newrelic_cmd +4 -0
  8. data/cert/cacert.pem +34 -0
  9. data/genki-newrelic_rpm.gemspec +32 -0
  10. data/init.rb +38 -0
  11. data/install.rb +37 -0
  12. data/lib/new_relic/agent.rb +280 -0
  13. data/lib/new_relic/agent/agent.rb +627 -0
  14. data/lib/new_relic/agent/chained_call.rb +13 -0
  15. data/lib/new_relic/agent/collection_helper.rb +61 -0
  16. data/lib/new_relic/agent/error_collector.rb +125 -0
  17. data/lib/new_relic/agent/instrumentation/active_merchant.rb +18 -0
  18. data/lib/new_relic/agent/instrumentation/active_record_instrumentation.rb +83 -0
  19. data/lib/new_relic/agent/instrumentation/authlogic.rb +8 -0
  20. data/lib/new_relic/agent/instrumentation/controller_instrumentation.rb +368 -0
  21. data/lib/new_relic/agent/instrumentation/data_mapper.rb +90 -0
  22. data/lib/new_relic/agent/instrumentation/dispatcher_instrumentation.rb +132 -0
  23. data/lib/new_relic/agent/instrumentation/memcache.rb +21 -0
  24. data/lib/new_relic/agent/instrumentation/merb/controller.rb +26 -0
  25. data/lib/new_relic/agent/instrumentation/merb/dispatcher.rb +13 -0
  26. data/lib/new_relic/agent/instrumentation/merb/errors.rb +8 -0
  27. data/lib/new_relic/agent/instrumentation/net.rb +12 -0
  28. data/lib/new_relic/agent/instrumentation/passenger_instrumentation.rb +20 -0
  29. data/lib/new_relic/agent/instrumentation/rack.rb +77 -0
  30. data/lib/new_relic/agent/instrumentation/rails/action_controller.rb +59 -0
  31. data/lib/new_relic/agent/instrumentation/rails/action_web_service.rb +27 -0
  32. data/lib/new_relic/agent/instrumentation/rails/dispatcher.rb +38 -0
  33. data/lib/new_relic/agent/instrumentation/rails/errors.rb +27 -0
  34. data/lib/new_relic/agent/instrumentation/sinatra.rb +39 -0
  35. data/lib/new_relic/agent/method_tracer.rb +277 -0
  36. data/lib/new_relic/agent/patch_const_missing.rb +125 -0
  37. data/lib/new_relic/agent/sampler.rb +12 -0
  38. data/lib/new_relic/agent/samplers/cpu_sampler.rb +49 -0
  39. data/lib/new_relic/agent/samplers/memory_sampler.rb +137 -0
  40. data/lib/new_relic/agent/samplers/mongrel_sampler.rb +22 -0
  41. data/lib/new_relic/agent/shim_agent.rb +21 -0
  42. data/lib/new_relic/agent/stats_engine.rb +24 -0
  43. data/lib/new_relic/agent/stats_engine/metric_stats.rb +111 -0
  44. data/lib/new_relic/agent/stats_engine/samplers.rb +71 -0
  45. data/lib/new_relic/agent/stats_engine/transactions.rb +155 -0
  46. data/lib/new_relic/agent/transaction_sampler.rb +319 -0
  47. data/lib/new_relic/agent/worker_loop.rb +118 -0
  48. data/lib/new_relic/commands/deployments.rb +145 -0
  49. data/lib/new_relic/commands/new_relic_commands.rb +30 -0
  50. data/lib/new_relic/control.rb +436 -0
  51. data/lib/new_relic/control/external.rb +13 -0
  52. data/lib/new_relic/control/merb.rb +22 -0
  53. data/lib/new_relic/control/rails.rb +143 -0
  54. data/lib/new_relic/control/ruby.rb +34 -0
  55. data/lib/new_relic/control/sinatra.rb +14 -0
  56. data/lib/new_relic/histogram.rb +89 -0
  57. data/lib/new_relic/local_environment.rb +285 -0
  58. data/lib/new_relic/merbtasks.rb +6 -0
  59. data/lib/new_relic/metric_data.rb +44 -0
  60. data/lib/new_relic/metric_parser.rb +120 -0
  61. data/lib/new_relic/metric_parser/action_mailer.rb +9 -0
  62. data/lib/new_relic/metric_parser/active_merchant.rb +26 -0
  63. data/lib/new_relic/metric_parser/active_record.rb +25 -0
  64. data/lib/new_relic/metric_parser/controller.rb +54 -0
  65. data/lib/new_relic/metric_parser/controller_cpu.rb +38 -0
  66. data/lib/new_relic/metric_parser/errors.rb +6 -0
  67. data/lib/new_relic/metric_parser/external.rb +50 -0
  68. data/lib/new_relic/metric_parser/mem_cache.rb +12 -0
  69. data/lib/new_relic/metric_parser/view.rb +61 -0
  70. data/lib/new_relic/metric_parser/web_frontend.rb +14 -0
  71. data/lib/new_relic/metric_parser/web_service.rb +9 -0
  72. data/lib/new_relic/metric_spec.rb +52 -0
  73. data/lib/new_relic/metrics.rb +7 -0
  74. data/lib/new_relic/noticed_error.rb +25 -0
  75. data/lib/new_relic/rack/metric_app.rb +56 -0
  76. data/lib/new_relic/rack/newrelic.ru +25 -0
  77. data/lib/new_relic/rack/newrelic.yml +25 -0
  78. data/lib/new_relic/rack_app.rb +5 -0
  79. data/lib/new_relic/recipes.rb +82 -0
  80. data/lib/new_relic/stats.rb +360 -0
  81. data/lib/new_relic/transaction_analysis.rb +121 -0
  82. data/lib/new_relic/transaction_sample.rb +583 -0
  83. data/lib/new_relic/version.rb +54 -0
  84. data/lib/new_relic_api.rb +315 -0
  85. data/lib/newrelic_rpm.rb +40 -0
  86. data/lib/tasks/all.rb +4 -0
  87. data/lib/tasks/install.rake +7 -0
  88. data/lib/tasks/tests.rake +13 -0
  89. data/newrelic.yml +214 -0
  90. data/recipes/newrelic.rb +6 -0
  91. data/test/active_record_fixtures.rb +55 -0
  92. data/test/config/newrelic.yml +46 -0
  93. data/test/config/test_control.rb +39 -0
  94. data/test/new_relic/agent/active_record_instrumentation_test.rb +234 -0
  95. data/test/new_relic/agent/agent_controller_test.rb +107 -0
  96. data/test/new_relic/agent/agent_test.rb +117 -0
  97. data/test/new_relic/agent/agent_test_controller.rb +44 -0
  98. data/test/new_relic/agent/classloader_patch_test.rb +56 -0
  99. data/test/new_relic/agent/collection_helper_test.rb +118 -0
  100. data/test/new_relic/agent/dispatcher_instrumentation_test.rb +76 -0
  101. data/test/new_relic/agent/error_collector_test.rb +155 -0
  102. data/test/new_relic/agent/method_tracer_test.rb +335 -0
  103. data/test/new_relic/agent/metric_data_test.rb +56 -0
  104. data/test/new_relic/agent/mock_ar_connection.rb +40 -0
  105. data/test/new_relic/agent/mock_scope_listener.rb +23 -0
  106. data/test/new_relic/agent/net_instrumentation_test.rb +51 -0
  107. data/test/new_relic/agent/stats_engine/metric_stats_test.rb +79 -0
  108. data/test/new_relic/agent/stats_engine/samplers_test.rb +78 -0
  109. data/test/new_relic/agent/stats_engine/stats_engine_test.rb +177 -0
  110. data/test/new_relic/agent/task_instrumentation_test.rb +67 -0
  111. data/test/new_relic/agent/testable_agent.rb +13 -0
  112. data/test/new_relic/agent/transaction_sample_builder_test.rb +195 -0
  113. data/test/new_relic/agent/transaction_sample_test.rb +146 -0
  114. data/test/new_relic/agent/transaction_sampler_test.rb +387 -0
  115. data/test/new_relic/agent/worker_loop_test.rb +103 -0
  116. data/test/new_relic/control_test.rb +94 -0
  117. data/test/new_relic/deployments_api_test.rb +68 -0
  118. data/test/new_relic/environment_test.rb +75 -0
  119. data/test/new_relic/metric_parser_test.rb +172 -0
  120. data/test/new_relic/metric_spec_test.rb +177 -0
  121. data/test/new_relic/shim_agent_test.rb +9 -0
  122. data/test/new_relic/stats_test.rb +291 -0
  123. data/test/new_relic/version_number_test.rb +74 -0
  124. data/test/test_helper.rb +38 -0
  125. data/test/ui/newrelic_controller_test.rb +14 -0
  126. data/test/ui/newrelic_helper_test.rb +53 -0
  127. data/ui/controllers/newrelic_controller.rb +214 -0
  128. data/ui/helpers/google_pie_chart.rb +55 -0
  129. data/ui/helpers/newrelic_helper.rb +314 -0
  130. data/ui/views/layouts/newrelic_default.rhtml +47 -0
  131. data/ui/views/newrelic/_explain_plans.rhtml +27 -0
  132. data/ui/views/newrelic/_sample.rhtml +15 -0
  133. data/ui/views/newrelic/_segment.rhtml +28 -0
  134. data/ui/views/newrelic/_segment_limit_message.rhtml +1 -0
  135. data/ui/views/newrelic/_segment_row.rhtml +14 -0
  136. data/ui/views/newrelic/_show_sample_detail.rhtml +24 -0
  137. data/ui/views/newrelic/_show_sample_sql.rhtml +20 -0
  138. data/ui/views/newrelic/_show_sample_summary.rhtml +3 -0
  139. data/ui/views/newrelic/_sql_row.rhtml +11 -0
  140. data/ui/views/newrelic/_stack_trace.rhtml +30 -0
  141. data/ui/views/newrelic/_table.rhtml +12 -0
  142. data/ui/views/newrelic/explain_sql.rhtml +42 -0
  143. data/ui/views/newrelic/images/arrow-close.png +0 -0
  144. data/ui/views/newrelic/images/arrow-open.png +0 -0
  145. data/ui/views/newrelic/images/blue_bar.gif +0 -0
  146. data/ui/views/newrelic/images/file_icon.png +0 -0
  147. data/ui/views/newrelic/images/gray_bar.gif +0 -0
  148. data/ui/views/newrelic/images/new_relic_rpm_desktop.gif +0 -0
  149. data/ui/views/newrelic/images/textmate.png +0 -0
  150. data/ui/views/newrelic/index.rhtml +45 -0
  151. data/ui/views/newrelic/javascript/prototype-scriptaculous.js +7288 -0
  152. data/ui/views/newrelic/javascript/transaction_sample.js +107 -0
  153. data/ui/views/newrelic/sample_not_found.rhtml +2 -0
  154. data/ui/views/newrelic/show_sample.rhtml +77 -0
  155. data/ui/views/newrelic/show_source.rhtml +3 -0
  156. data/ui/views/newrelic/stylesheets/style.css +433 -0
  157. data/ui/views/newrelic/threads.rhtml +52 -0
  158. metadata +327 -0
@@ -0,0 +1,51 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__),'..','..','test_helper'))
2
+
3
+ class NewRelic::Agent::NetInstrumentationTest < Test::Unit::TestCase
4
+
5
+ def setup
6
+ NewRelic::Agent.manual_start
7
+ @engine = NewRelic::Agent.instance.stats_engine
8
+ @engine.clear_stats
9
+ end
10
+ def test_get
11
+ url = URI.parse('http://www.google.com/index.html')
12
+ res = Net::HTTP.start(url.host, url.port) {|http|
13
+ http.get('/index.html')
14
+ }
15
+ assert_match /<head>/, res.body
16
+ assert_equal @engine.metrics.sort,
17
+ %w[External/www.google.com/Net::HTTP/GET External/all External/www.google.com/all].sort
18
+ end
19
+ def test_get__simple
20
+ Net::HTTP.get URI.parse('http://www.google.com/index.html')
21
+ assert_equal @engine.metrics.sort,
22
+ %w[External/www.google.com/Net::HTTP/GET External/all External/www.google.com/all].sort
23
+ end
24
+ def test_ignore
25
+ NewRelic::Agent.disable_all_tracing do
26
+ url = URI.parse('http://www.google.com/index.html')
27
+ res = Net::HTTP.start(url.host, url.port) {|http|
28
+ http.post('/index.html','data')
29
+ }
30
+ end
31
+ assert_equal 0, @engine.metrics.size
32
+ end
33
+ def test_head
34
+ url = URI.parse('http://www.google.com/index.html')
35
+ res = Net::HTTP.start(url.host, url.port) {|http|
36
+ http.head('/index.html')
37
+ }
38
+ assert_equal %w[External/www.google.com/Net::HTTP/HEAD External/all External/www.google.com/all].sort,
39
+ @engine.metrics.sort
40
+ end
41
+
42
+ def test_post
43
+ url = URI.parse('http://www.google.com/index.html')
44
+ res = Net::HTTP.start(url.host, url.port) {|http|
45
+ http.post('/index.html','data')
46
+ }
47
+ assert_equal %w[External/www.google.com/Net::HTTP/POST External/all External/www.google.com/all].sort,
48
+ @engine.metrics.sort
49
+ end
50
+
51
+ end
@@ -0,0 +1,79 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__),'..','..','..','test_helper'))
2
+
3
+
4
+ class NewRelic::Agent::MetricStatsTest < Test::Unit::TestCase
5
+ def setup
6
+ NewRelic::Agent.manual_start
7
+ @engine = NewRelic::Agent.instance.stats_engine
8
+ rescue => e
9
+ puts e
10
+ puts e.backtrace.join("\n")
11
+ end
12
+ def teardown
13
+ @engine.harvest_timeslice_data({},{})
14
+ end
15
+ def test_get_no_scope
16
+ s1 = @engine.get_stats "a"
17
+ s2 = @engine.get_stats "a"
18
+ s3 = @engine.get_stats "b"
19
+
20
+ assert_not_nil s1
21
+ assert_not_nil s2
22
+ assert_not_nil s3
23
+
24
+ assert s1 == s2
25
+ assert s1 != s3
26
+ end
27
+
28
+ def test_harvest
29
+ s1 = @engine.get_stats "a"
30
+ s2 = @engine.get_stats "c"
31
+
32
+ s1.trace_call 10
33
+ s2.trace_call 1
34
+ s2.trace_call 3
35
+
36
+ assert @engine.get_stats("a").call_count == 1
37
+ assert @engine.get_stats("a").total_call_time == 10
38
+
39
+ assert @engine.get_stats("c").call_count == 2
40
+ assert @engine.get_stats("c").total_call_time == 4
41
+
42
+ metric_data = @engine.harvest_timeslice_data({}, {}).values
43
+
44
+ # after harvest, all the metrics should be reset
45
+ assert @engine.get_stats("a").call_count == 0
46
+ assert @engine.get_stats("a").total_call_time == 0
47
+
48
+ assert @engine.get_stats("c").call_count == 0
49
+ assert @engine.get_stats("c").total_call_time == 0
50
+
51
+ metric_data = metric_data.reverse if metric_data[0].metric_spec.name != "a"
52
+
53
+ assert metric_data[0].metric_spec.name == "a"
54
+
55
+ assert metric_data[0].stats.call_count == 1
56
+ assert metric_data[0].stats.total_call_time == 10
57
+ end
58
+
59
+ def test_harvest_with_merge
60
+ s = @engine.get_stats "a"
61
+ s.trace_call 1
62
+
63
+ assert @engine.get_stats("a").call_count == 1
64
+
65
+ harvest = @engine.harvest_timeslice_data({}, {})
66
+ assert s.call_count == 0
67
+ s.trace_call 2
68
+ assert s.call_count == 1
69
+
70
+ # this calk should merge the contents of the previous harvest,
71
+ # so the stats for metric "a" should have 2 data points
72
+ harvest = @engine.harvest_timeslice_data(harvest, {})
73
+ stats = harvest.fetch(NewRelic::MetricSpec.new("a")).stats
74
+ assert stats.call_count == 2
75
+ assert stats.total_call_time == 3
76
+ end
77
+
78
+ end
79
+
@@ -0,0 +1,78 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__),'..', '..','..','test_helper'))
2
+ require 'new_relic/agent/samplers/cpu_sampler'
3
+
4
+ class NewRelic::Agent::StatsEngine::SamplersTest < Test::Unit::TestCase
5
+
6
+ def setup
7
+ @stats_engine = NewRelic::Agent::StatsEngine.new
8
+ end
9
+ def test_cpu
10
+ s = NewRelic::Agent::Samplers::CpuSampler.new
11
+ # need to sleep because if you go to fast it will skip the points
12
+ s.stats_engine = @stats_engine
13
+ sleep 2
14
+ s.poll
15
+ sleep 2
16
+ s.poll
17
+ assert_equal 2, s.systemtime_stats.call_count
18
+ assert_equal 2, s.usertime_stats.call_count
19
+ assert s.usertime_stats.total_call_time >= 0, "user cpu greater/equal to 0: #{s.usertime_stats.total_call_time}"
20
+ assert s.systemtime_stats.total_call_time >= 0, "system cpu greater/equal to 0: #{s.systemtime_stats.total_call_time}"
21
+ end
22
+ def test_memory__default
23
+ s = NewRelic::Agent::Samplers::MemorySampler.new
24
+ s.stats_engine = @stats_engine
25
+ s.poll
26
+ s.poll
27
+ s.poll
28
+ assert_equal 3, s.stats.call_count
29
+ assert s.stats.total_call_time > 0.5, "cpu greater than 0.5 ms: #{s.stats.total_call_time}"
30
+ end
31
+ def test_memory__linux
32
+ NewRelic::Agent::Samplers::MemorySampler.any_instance.stubs(:platform).returns 'linux'
33
+ s = NewRelic::Agent::Samplers::MemorySampler.new
34
+ s.stats_engine = @stats_engine
35
+ s.poll
36
+ s.poll
37
+ s.poll
38
+ assert_equal 3, s.stats.call_count
39
+ assert s.stats.total_call_time > 0.5, "cpu greater than 0.5 ms: #{s.stats.total_call_time}"
40
+ end
41
+ def test_memory__solaris
42
+ NewRelic::Agent::Samplers::MemorySampler.any_instance.stubs(:platform).returns 'solaris'
43
+ NewRelic::Agent::Samplers::MemorySampler::ShellPS.any_instance.stubs(:get_memory).returns 999
44
+ s = NewRelic::Agent::Samplers::MemorySampler.new
45
+ s.stats_engine = @stats_engine
46
+ s.poll
47
+ assert_equal 1, s.stats.call_count
48
+ assert_equal 999, s.stats.total_call_time
49
+ end
50
+ def test_memory__windows
51
+ NewRelic::Agent::Samplers::MemorySampler.any_instance.stubs(:platform).returns 'win32'
52
+ assert_raise RuntimeError, /Unsupported platform/ do
53
+ NewRelic::Agent::Samplers::MemorySampler.new
54
+ end
55
+ end
56
+ def test_memory__is_supported
57
+ NewRelic::Agent::Samplers::MemorySampler.stubs(:platform).returns 'windows'
58
+ assert !(NewRelic::Agent::Samplers::MemorySampler.supported_on_this_platform?)
59
+ end
60
+ def test_mongrel
61
+ NewRelic::Agent::Instrumentation::DispatcherInstrumentation::BusyCalculator.stubs('is_busy?'.to_sym).returns(false)
62
+ mongrel = mock()
63
+ NewRelic::Control.instance.local_env.stubs(:mongrel).returns(mongrel)
64
+ list = mock()
65
+ workers = mock()
66
+ workers.stubs(:list).returns(list)
67
+ list.stubs(:length).returns(3)
68
+ mongrel.expects(:workers).returns(workers).at_least_once
69
+ s = NewRelic::Agent::Samplers::MongrelSampler.new
70
+ s.stats_engine = @stats_engine
71
+ s.poll
72
+ s.poll
73
+ s.poll
74
+ assert_equal 3, s.queue_stats.call_count
75
+ assert_equal 3, s.queue_stats.average_call_time, "mongrel queue length"
76
+ end
77
+
78
+ end
@@ -0,0 +1,177 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__),'..','..','..', 'test_helper'))
2
+
3
+
4
+ class NewRelic::Agent::StatsEngine::TransactionsTest < Test::Unit::TestCase
5
+ def setup
6
+ NewRelic::Agent.manual_start
7
+ @engine = NewRelic::Agent.instance.stats_engine
8
+ rescue => e
9
+ puts e
10
+ puts e.backtrace.join("\n")
11
+ end
12
+ def teardown
13
+ @engine.harvest_timeslice_data({},{})
14
+ end
15
+
16
+ def test_scope
17
+ @engine.push_scope "scope1"
18
+ assert @engine.peek_scope.name == "scope1"
19
+
20
+ expected = @engine.push_scope "scope2"
21
+ @engine.pop_scope expected, 0
22
+
23
+ scoped = @engine.get_stats "a"
24
+ scoped.trace_call 3
25
+
26
+ assert scoped.total_call_time == 3
27
+ unscoped = @engine.get_stats "a"
28
+
29
+ assert scoped == @engine.get_stats("a")
30
+ assert unscoped.total_call_time == 3
31
+ end
32
+
33
+ def test_scope__overlap
34
+ @engine.transaction_name = 'orlando'
35
+ self.class.trace_execution_scoped('disney', :deduct_call_time_from_parent => false) { sleep 0.1 }
36
+ orlando_disney = @engine.get_stats 'disney'
37
+
38
+ @engine.transaction_name = 'anaheim'
39
+ self.class.trace_execution_scoped('disney', :deduct_call_time_from_parent => false) { sleep 0.1 }
40
+ anaheim_disney = @engine.get_stats 'disney'
41
+
42
+ disney = @engine.get_stats_no_scope "disney"
43
+
44
+ assert_not_same orlando_disney, anaheim_disney
45
+ assert_not_equal orlando_disney, anaheim_disney
46
+ assert_equal 1, orlando_disney.call_count
47
+ assert_equal 1, anaheim_disney.call_count
48
+ assert_same disney, orlando_disney.unscoped_stats
49
+ assert_same disney, anaheim_disney.unscoped_stats
50
+ assert_equal 2, disney.call_count
51
+ assert_equal disney.total_call_time, orlando_disney.total_call_time + anaheim_disney.total_call_time
52
+
53
+ end
54
+
55
+ def test_simplethrowcase(depth=0)
56
+
57
+ fail "doh" if depth == 10
58
+
59
+ scope = @engine.push_scope "scope#{depth}"
60
+
61
+ begin
62
+ test_simplethrowcase(depth+1)
63
+ rescue StandardError => e
64
+ if (depth != 0)
65
+ raise e
66
+ end
67
+ ensure
68
+ @engine.pop_scope scope, 0
69
+ end
70
+
71
+ if depth == 0
72
+ assert @engine.peek_scope.nil?
73
+ end
74
+ end
75
+
76
+
77
+ def test_scope_failure
78
+ scope1 = @engine.push_scope "scope1"
79
+ @engine.push_scope "scope2"
80
+
81
+ begin
82
+ @engine.pop_scope scope1
83
+ fail "Didn't throw when scope push/pop mismatched"
84
+ rescue
85
+ # success
86
+ end
87
+ end
88
+
89
+ def test_children_time
90
+ t1 = Time.now
91
+
92
+ expected1 = @engine.push_scope "a"
93
+ sleep 0.1
94
+ t2 = Time.now
95
+
96
+ expected2 = @engine.push_scope "b"
97
+ sleep 0.2
98
+ t3 = Time.now
99
+
100
+ expected = @engine.push_scope "c"
101
+ sleep 0.3
102
+ scope = @engine.pop_scope expected, Time.now - t3
103
+
104
+ t4 = Time.now
105
+
106
+ check_time_approximate 0, scope.children_time
107
+ check_time_approximate 0.3, @engine.peek_scope.children_time
108
+
109
+ sleep 0.1
110
+ t5 = Time.now
111
+
112
+ expected = @engine.push_scope "d"
113
+ sleep 0.2
114
+ scope = @engine.pop_scope expected, Time.now - t5
115
+
116
+ t6 = Time.now
117
+
118
+ check_time_approximate 0, scope.children_time
119
+
120
+ scope = @engine.pop_scope expected2, Time.now - t2
121
+ assert_equal scope.name, 'b'
122
+
123
+ check_time_approximate (t4 - t3) + (t6 - t5), scope.children_time
124
+
125
+ scope = @engine.pop_scope expected1, Time.now - t1
126
+ assert_equal scope.name, 'a'
127
+
128
+ check_time_approximate (t6 - t2), scope.children_time
129
+ end
130
+
131
+ def test_simple_start_transaction
132
+ @engine.push_scope "scope"
133
+ @engine.start_transaction
134
+ assert @engine.peek_scope.nil?
135
+ end
136
+
137
+
138
+ # test for when the scope stack contains an element only used for tts and not metrics
139
+ def test_simple_tt_only_scope
140
+ scope1 = @engine.push_scope "a", 0, true
141
+ scope2 = @engine.push_scope "b", 10, false
142
+ scope3 = @engine.push_scope "c", 20, true
143
+
144
+ @engine.pop_scope scope3, 10
145
+ @engine.pop_scope scope2, 10
146
+ @engine.pop_scope scope1, 10
147
+
148
+ assert_equal 0, scope3.children_time
149
+ assert_equal 10, scope2.children_time
150
+ assert_equal 10, scope1.children_time
151
+ end
152
+
153
+ def test_double_tt_only_scope
154
+ scope1 = @engine.push_scope "a", 0, true
155
+ scope2 = @engine.push_scope "b", 10, false
156
+ scope3 = @engine.push_scope "c", 20, false
157
+ scope4 = @engine.push_scope "d", 30, true
158
+
159
+ @engine.pop_scope scope4, 10
160
+ @engine.pop_scope scope3, 10
161
+ @engine.pop_scope scope2, 10
162
+ @engine.pop_scope scope1, 10
163
+
164
+ assert_equal 0, scope4.children_time
165
+ assert_equal 10, scope3.children_time
166
+ assert_equal 10, scope2.children_time
167
+ assert_equal 10, scope1.children_time
168
+ end
169
+
170
+
171
+ private
172
+ def check_time_approximate(expected, actual)
173
+ assert((expected - actual).abs < 0.05, "Expected between #{expected - 0.05} and #{expected + 0.05}, got #{actual}")
174
+ end
175
+
176
+ end
177
+
@@ -0,0 +1,67 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__),'..','..','test_helper'))
2
+ require 'action_controller/base'
3
+
4
+ class TaskInstrumentationTest < Test::Unit::TestCase
5
+ include NewRelic::Agent::Instrumentation::ControllerInstrumentation
6
+ attr_accessor :agent
7
+ def setup
8
+ super
9
+ NewRelic::Agent.manual_start
10
+ @agent = NewRelic::Agent.instance
11
+ end
12
+ def teardown
13
+ @agent.stats_engine.clear_stats
14
+ NewRelic::Agent.shutdown
15
+ super
16
+ end
17
+
18
+ def test_run
19
+ run_task_inner 0
20
+ stat_names = %w[Controller/TaskInstrumentationTest/inner_task_0
21
+ ActiveRecord/all Controller HttpDispatcher
22
+ Apdex Apdex/TaskInstrumentationTest/inner_task_0].sort
23
+ expected_but_missing = stat_names - @agent.stats_engine.metrics
24
+ assert_equal 0, expected_but_missing.size, expected_but_missing.inspect
25
+ @agent.stats_engine.metrics.each do |n|
26
+ stat = @agent.stats_engine.get_stats(n)
27
+ # puts "#{'%-26s' % n}: #{stat.call_count} calls @ #{stat.average_call_time} sec/call"
28
+ end
29
+ assert_equal 1, @agent.stats_engine.get_stats_no_scope('Controller').call_count
30
+ assert_equal 1, @agent.stats_engine.get_stats('Controller/TaskInstrumentationTest/inner_task_0').call_count
31
+ end
32
+
33
+ def test_run_recursive
34
+ run_task_inner(3)
35
+ assert_equal 0, @agent.stats_engine.get_stats('Controller/TaskInstrumentationTest/inner_task_3').call_count
36
+ assert_equal 0, @agent.stats_engine.get_stats('Controller/TaskInstrumentationTest/inner_task_2').call_count
37
+ assert_equal 1, @agent.stats_engine.get_stats('Controller/TaskInstrumentationTest/inner_task_0').call_count
38
+ assert_equal 1, @agent.stats_engine.get_stats('Controller').call_count
39
+ end
40
+
41
+ def test_run_nested
42
+ run_task_outer(3)
43
+ @agent.stats_engine.metrics.sort.each do |n|
44
+ stat = @agent.stats_engine.get_stats(n)
45
+ # puts "#{'%-26s' % n}: #{stat.call_count} calls @ #{stat.average_call_time} sec/call"
46
+ end
47
+ assert_equal 0, @agent.stats_engine.get_stats('Controller/TaskInstrumentationTest/outer_task').call_count
48
+ assert_equal 2, @agent.stats_engine.get_stats('Controller').call_count
49
+ assert_equal 2, @agent.stats_engine.get_stats('Controller/TaskInstrumentationTest/inner_task_0').call_count
50
+ end
51
+
52
+ private
53
+
54
+ def run_task_inner(n)
55
+ sleep 0.1
56
+ return if n == 0
57
+ run_task_inner(n-1)
58
+ end
59
+
60
+ def run_task_outer(n=0)
61
+ run_task_inner(n)
62
+ run_task_inner(n)
63
+ end
64
+
65
+ add_transaction_tracer :run_task_inner, :name => 'inner_task_#{args[0]}'
66
+ add_transaction_tracer :run_task_outer, :name => 'outer_task'
67
+ end