techarch-newrelic_rpm 2.10.2

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 +335 -0
  2. data/LICENSE +37 -0
  3. data/Manifest +159 -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/init.rb +38 -0
  10. data/install.rb +45 -0
  11. data/lib/new_relic/agent.rb +281 -0
  12. data/lib/new_relic/agent/agent.rb +636 -0
  13. data/lib/new_relic/agent/chained_call.rb +13 -0
  14. data/lib/new_relic/agent/collection_helper.rb +66 -0
  15. data/lib/new_relic/agent/error_collector.rb +121 -0
  16. data/lib/new_relic/agent/instrumentation/active_merchant.rb +18 -0
  17. data/lib/new_relic/agent/instrumentation/active_record_instrumentation.rb +88 -0
  18. data/lib/new_relic/agent/instrumentation/authlogic.rb +8 -0
  19. data/lib/new_relic/agent/instrumentation/controller_instrumentation.rb +437 -0
  20. data/lib/new_relic/agent/instrumentation/data_mapper.rb +90 -0
  21. data/lib/new_relic/agent/instrumentation/dispatcher_instrumentation.rb +167 -0
  22. data/lib/new_relic/agent/instrumentation/memcache.rb +24 -0
  23. data/lib/new_relic/agent/instrumentation/merb/controller.rb +26 -0
  24. data/lib/new_relic/agent/instrumentation/merb/dispatcher.rb +13 -0
  25. data/lib/new_relic/agent/instrumentation/merb/errors.rb +8 -0
  26. data/lib/new_relic/agent/instrumentation/net.rb +23 -0
  27. data/lib/new_relic/agent/instrumentation/passenger_instrumentation.rb +20 -0
  28. data/lib/new_relic/agent/instrumentation/rack.rb +108 -0
  29. data/lib/new_relic/agent/instrumentation/rails/action_controller.rb +59 -0
  30. data/lib/new_relic/agent/instrumentation/rails/action_web_service.rb +27 -0
  31. data/lib/new_relic/agent/instrumentation/rails/dispatcher.rb +41 -0
  32. data/lib/new_relic/agent/instrumentation/rails/errors.rb +27 -0
  33. data/lib/new_relic/agent/instrumentation/sinatra.rb +39 -0
  34. data/lib/new_relic/agent/method_tracer.rb +349 -0
  35. data/lib/new_relic/agent/patch_const_missing.rb +125 -0
  36. data/lib/new_relic/agent/sampler.rb +12 -0
  37. data/lib/new_relic/agent/samplers/cpu_sampler.rb +49 -0
  38. data/lib/new_relic/agent/samplers/memory_sampler.rb +138 -0
  39. data/lib/new_relic/agent/samplers/mongrel_sampler.rb +22 -0
  40. data/lib/new_relic/agent/shim_agent.rb +21 -0
  41. data/lib/new_relic/agent/stats_engine.rb +22 -0
  42. data/lib/new_relic/agent/stats_engine/metric_stats.rb +111 -0
  43. data/lib/new_relic/agent/stats_engine/samplers.rb +71 -0
  44. data/lib/new_relic/agent/stats_engine/transactions.rb +152 -0
  45. data/lib/new_relic/agent/transaction_sampler.rb +310 -0
  46. data/lib/new_relic/agent/worker_loop.rb +118 -0
  47. data/lib/new_relic/commands/deployments.rb +145 -0
  48. data/lib/new_relic/commands/new_relic_commands.rb +30 -0
  49. data/lib/new_relic/control.rb +473 -0
  50. data/lib/new_relic/control/external.rb +13 -0
  51. data/lib/new_relic/control/merb.rb +22 -0
  52. data/lib/new_relic/control/rails.rb +145 -0
  53. data/lib/new_relic/control/ruby.rb +36 -0
  54. data/lib/new_relic/control/sinatra.rb +14 -0
  55. data/lib/new_relic/histogram.rb +89 -0
  56. data/lib/new_relic/local_environment.rb +328 -0
  57. data/lib/new_relic/merbtasks.rb +6 -0
  58. data/lib/new_relic/metric_data.rb +42 -0
  59. data/lib/new_relic/metric_parser.rb +124 -0
  60. data/lib/new_relic/metric_parser/action_mailer.rb +9 -0
  61. data/lib/new_relic/metric_parser/active_merchant.rb +26 -0
  62. data/lib/new_relic/metric_parser/active_record.rb +25 -0
  63. data/lib/new_relic/metric_parser/controller.rb +54 -0
  64. data/lib/new_relic/metric_parser/controller_cpu.rb +38 -0
  65. data/lib/new_relic/metric_parser/errors.rb +6 -0
  66. data/lib/new_relic/metric_parser/external.rb +50 -0
  67. data/lib/new_relic/metric_parser/mem_cache.rb +12 -0
  68. data/lib/new_relic/metric_parser/view.rb +61 -0
  69. data/lib/new_relic/metric_parser/web_frontend.rb +14 -0
  70. data/lib/new_relic/metric_parser/web_service.rb +9 -0
  71. data/lib/new_relic/metric_spec.rb +52 -0
  72. data/lib/new_relic/metrics.rb +7 -0
  73. data/lib/new_relic/noticed_error.rb +23 -0
  74. data/lib/new_relic/rack/metric_app.rb +56 -0
  75. data/lib/new_relic/rack/newrelic.ru +25 -0
  76. data/lib/new_relic/rack/newrelic.yml +25 -0
  77. data/lib/new_relic/rack_app.rb +5 -0
  78. data/lib/new_relic/recipes.rb +82 -0
  79. data/lib/new_relic/stats.rb +361 -0
  80. data/lib/new_relic/transaction_analysis.rb +121 -0
  81. data/lib/new_relic/transaction_sample.rb +666 -0
  82. data/lib/new_relic/version.rb +54 -0
  83. data/lib/new_relic_api.rb +313 -0
  84. data/lib/newrelic_rpm.rb +40 -0
  85. data/lib/tasks/all.rb +4 -0
  86. data/lib/tasks/install.rake +7 -0
  87. data/lib/tasks/tests.rake +13 -0
  88. data/newrelic.yml +227 -0
  89. data/recipes/newrelic.rb +6 -0
  90. data/techarch-newrelic_rpm.gemspec +32 -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 +264 -0
  95. data/test/new_relic/agent/agent_controller_test.rb +107 -0
  96. data/test/new_relic/agent/agent_test.rb +119 -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 +125 -0
  100. data/test/new_relic/agent/dispatcher_instrumentation_test.rb +76 -0
  101. data/test/new_relic/agent/error_collector_test.rb +172 -0
  102. data/test/new_relic/agent/method_tracer_test.rb +340 -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 +63 -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 +81 -0
  109. data/test/new_relic/agent/stats_engine/stats_engine_test.rb +184 -0
  110. data/test/new_relic/agent/task_instrumentation_test.rb +126 -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 +186 -0
  114. data/test/new_relic/agent/transaction_sampler_test.rb +385 -0
  115. data/test/new_relic/agent/worker_loop_test.rb +103 -0
  116. data/test/new_relic/control_test.rb +113 -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 +220 -0
  128. data/ui/helpers/google_pie_chart.rb +55 -0
  129. data/ui/helpers/newrelic_helper.rb +317 -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 +19 -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 +57 -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 +80 -0
  155. data/ui/views/newrelic/show_source.rhtml +3 -0
  156. data/ui/views/newrelic/stylesheets/style.css +484 -0
  157. data/ui/views/newrelic/threads.rhtml +52 -0
  158. metadata +330 -0
@@ -0,0 +1,56 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__),'..','..','test_helper'))
2
+ require 'test/unit'
3
+
4
+
5
+ class NewRelic::Agent::MetricDataTest < Test::Unit::TestCase
6
+
7
+
8
+ # test to make sure the MetricData class can serialize to json
9
+ def test_json
10
+ spec = NewRelic::MetricSpec.new("controller", "metric#find")
11
+
12
+ import = ActiveSupport::JSON.decode(spec.to_json)
13
+
14
+ compare_spec(spec, import)
15
+
16
+ stats = NewRelic::MethodTraceStats.new
17
+
18
+ import = ActiveSupport::JSON.decode(stats.to_json)
19
+
20
+ compare_stat(stats, import)
21
+
22
+ metric_data = NewRelic::MetricData.new(spec, stats, 10)
23
+
24
+ import = ActiveSupport::JSON.decode(metric_data.to_json)
25
+
26
+ compare_metric_data(metric_data, import)
27
+ end
28
+
29
+
30
+ private
31
+
32
+ def compare_spec(spec, import)
33
+ assert_equal 2, import.length
34
+ assert_equal spec.name, import['name']
35
+ assert_equal spec.scope, import['scope']
36
+ end
37
+
38
+ def compare_stat(stats, import)
39
+ assert_equal 6, import.length
40
+ assert_equal stats.total_call_time, import['total_call_time']
41
+ assert_equal stats.max_call_time, import['max_call_time']
42
+ assert_equal stats.min_call_time, import['min_call_time']
43
+ assert_equal stats.sum_of_squares, import['sum_of_squares']
44
+ assert_equal stats.call_count, import['call_count']
45
+ assert_equal stats.total_exclusive_time, import['total_exclusive_time']
46
+ end
47
+
48
+ def compare_metric_data(metric_data, import)
49
+ assert_equal 3, import.length
50
+ assert_equal metric_data.metric_id, import['metric_id']
51
+ compare_spec(metric_data.metric_spec, import['metric_spec']) unless metric_data.metric_id
52
+ compare_stat(metric_data.stats, import['stats'])
53
+ end
54
+
55
+ end
56
+
@@ -0,0 +1,40 @@
1
+ module ActiveRecord
2
+ class Base
3
+ class << self
4
+ def test_connection(config)
5
+ @connect ||= Connection.new
6
+ end
7
+ end
8
+ end
9
+
10
+ class Connection
11
+ attr_accessor :throw
12
+ attr_reader :disconnected
13
+
14
+ def initialize
15
+ @disconnected = false
16
+ @throw = false
17
+ end
18
+
19
+ def disconnect!()
20
+ @disconnected = true
21
+ end
22
+
23
+ def find()
24
+ # used to test that we've instrumented this...
25
+ end
26
+
27
+ def select_rows(s)
28
+ execute(s)
29
+ end
30
+ def execute(s)
31
+ fail "" if @throw
32
+ if s != "EXPLAIN #{::SQL_STATEMENT}"
33
+ fail "Unexpected sql statement #{s}"
34
+ end
35
+ s
36
+ end
37
+ end
38
+ end
39
+
40
+
@@ -0,0 +1,23 @@
1
+
2
+ class NewRelic::Agent::MockScopeListener
3
+
4
+ attr_reader :scope
5
+
6
+ def initialize
7
+ @scope = {}
8
+ end
9
+
10
+ def notice_first_scope_push(time)
11
+ end
12
+
13
+ def notice_push_scope(scope, time)
14
+ @scope[scope] = true
15
+ end
16
+
17
+ def notice_pop_scope(scope, time)
18
+ end
19
+
20
+ def notice_scope_empty(time)
21
+
22
+ end
23
+ end
@@ -0,0 +1,63 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__),'..','..','test_helper'))
2
+
3
+ class NewRelic::Agent::NetInstrumentationTest < Test::Unit::TestCase
4
+ include NewRelic::Agent::Instrumentation::ControllerInstrumentation
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 %w[External/www.google.com/Net::HTTP/GET External/allOther External/www.google.com/all].sort,
17
+ @engine.metrics.sort
18
+ end
19
+ def test_transactional
20
+ perform_action_with_newrelic_trace("task") do
21
+ url = URI.parse('http://www.google.com/index.html')
22
+ res = Net::HTTP.start(url.host, url.port) {|http|
23
+ http.get('/index.html')
24
+ }
25
+ assert_match /<head>/, res.body
26
+ end
27
+ assert_equal @engine.metrics.select{|m| m =~ /^External/}.sort,
28
+ %w[External/www.google.com/Net::HTTP/GET External/allWeb External/www.google.com/all
29
+ External/www.google.com/Net::HTTP/GET:Controller/NewRelic::Agent::NetInstrumentationTest/task].sort
30
+ end
31
+ def test_get__simple
32
+ Net::HTTP.get URI.parse('http://www.google.com/index.html')
33
+ assert_equal @engine.metrics.sort,
34
+ %w[External/www.google.com/Net::HTTP/GET External/allOther External/www.google.com/all].sort
35
+ end
36
+ def test_ignore
37
+ NewRelic::Agent.disable_all_tracing do
38
+ url = URI.parse('http://www.google.com/index.html')
39
+ res = Net::HTTP.start(url.host, url.port) {|http|
40
+ http.post('/index.html','data')
41
+ }
42
+ end
43
+ assert_equal 0, @engine.metrics.size
44
+ end
45
+ def test_head
46
+ url = URI.parse('http://www.google.com/index.html')
47
+ res = Net::HTTP.start(url.host, url.port) {|http|
48
+ http.head('/index.html')
49
+ }
50
+ assert_equal %w[External/www.google.com/Net::HTTP/HEAD External/allOther External/www.google.com/all].sort,
51
+ @engine.metrics.sort
52
+ end
53
+
54
+ def test_post
55
+ url = URI.parse('http://www.google.com/index.html')
56
+ res = Net::HTTP.start(url.host, url.port) {|http|
57
+ http.post('/index.html','data')
58
+ }
59
+ assert_equal %w[External/www.google.com/Net::HTTP/POST External/allOther External/www.google.com/all].sort,
60
+ @engine.metrics.sort
61
+ end
62
+
63
+ 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,81 @@
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
+ return if RUBY_PLATFORM =~ /darwin/
33
+ NewRelic::Agent::Samplers::MemorySampler.any_instance.stubs(:platform).returns 'linux'
34
+ s = NewRelic::Agent::Samplers::MemorySampler.new
35
+ s.stats_engine = @stats_engine
36
+ s.poll
37
+ s.poll
38
+ s.poll
39
+ assert_equal 3, s.stats.call_count
40
+ assert s.stats.total_call_time > 0.5, "cpu greater than 0.5 ms: #{s.stats.total_call_time}"
41
+ end
42
+ def test_memory__solaris
43
+ return if defined? JRuby
44
+ NewRelic::Agent::Samplers::MemorySampler.any_instance.stubs(:platform).returns 'solaris'
45
+ NewRelic::Agent::Samplers::MemorySampler::ShellPS.any_instance.stubs(:get_memory).returns 999
46
+ s = NewRelic::Agent::Samplers::MemorySampler.new
47
+ s.stats_engine = @stats_engine
48
+ s.poll
49
+ assert_equal 1, s.stats.call_count
50
+ assert_equal 999, s.stats.total_call_time
51
+ end
52
+ def test_memory__windows
53
+ return if defined? JRuby
54
+ NewRelic::Agent::Samplers::MemorySampler.any_instance.stubs(:platform).returns 'win32'
55
+ assert_raise RuntimeError, /Unsupported platform/ do
56
+ NewRelic::Agent::Samplers::MemorySampler.new
57
+ end
58
+ end
59
+ def test_memory__is_supported
60
+ NewRelic::Agent::Samplers::MemorySampler.stubs(:platform).returns 'windows'
61
+ assert !NewRelic::Agent::Samplers::MemorySampler.supported_on_this_platform? || defined? JRuby
62
+ end
63
+ def test_mongrel
64
+ NewRelic::Agent::Instrumentation::DispatcherInstrumentation::BusyCalculator.stubs('is_busy?'.to_sym).returns(false)
65
+ mongrel = mock()
66
+ NewRelic::Control.instance.local_env.stubs(:mongrel).returns(mongrel)
67
+ list = mock()
68
+ workers = mock()
69
+ workers.stubs(:list).returns(list)
70
+ list.stubs(:length).returns(3)
71
+ mongrel.expects(:workers).returns(workers).at_least_once
72
+ s = NewRelic::Agent::Samplers::MongrelSampler.new
73
+ s.stats_engine = @stats_engine
74
+ s.poll
75
+ s.poll
76
+ s.poll
77
+ assert_equal 3, s.queue_stats.call_count
78
+ assert_equal 3, s.queue_stats.average_call_time, "mongrel queue length"
79
+ end
80
+
81
+ end
@@ -0,0 +1,184 @@
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::StatsEngine.new
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
+ NewRelic::Agent.instance.stubs(:stats_engine).returns(@engine)
35
+
36
+ @engine.transaction_name = 'orlando'
37
+ self.class.trace_execution_scoped('disney', :deduct_call_time_from_parent => false) { sleep 0.1 }
38
+ orlando_disney = @engine.get_stats 'disney'
39
+
40
+ @engine.transaction_name = 'anaheim'
41
+ self.class.trace_execution_scoped('disney', :deduct_call_time_from_parent => false) { sleep 0.1 }
42
+ anaheim_disney = @engine.get_stats 'disney'
43
+
44
+ disney = @engine.get_stats_no_scope "disney"
45
+
46
+ assert_not_same orlando_disney, anaheim_disney
47
+ assert_not_equal orlando_disney, anaheim_disney
48
+ assert_equal 1, orlando_disney.call_count
49
+ assert_equal 1, anaheim_disney.call_count
50
+ assert_same disney, orlando_disney.unscoped_stats
51
+ assert_same disney, anaheim_disney.unscoped_stats
52
+ assert_equal 2, disney.call_count
53
+ assert_equal disney.total_call_time, orlando_disney.total_call_time + anaheim_disney.total_call_time
54
+
55
+ end
56
+
57
+ def test_simplethrowcase(depth=0)
58
+
59
+ fail "doh" if depth == 10
60
+
61
+ scope = @engine.push_scope "scope#{depth}"
62
+
63
+ begin
64
+ test_simplethrowcase(depth+1)
65
+ rescue StandardError => e
66
+ if (depth != 0)
67
+ raise e
68
+ end
69
+ ensure
70
+ @engine.pop_scope scope, 0
71
+ end
72
+
73
+ if depth == 0
74
+ assert @engine.peek_scope.nil?
75
+ end
76
+ end
77
+
78
+
79
+ def test_scope_failure
80
+ scope1 = @engine.push_scope "scope1"
81
+ @engine.push_scope "scope2"
82
+
83
+ begin
84
+ @engine.pop_scope scope1
85
+ fail "Didn't throw when scope push/pop mismatched"
86
+ rescue
87
+ # success
88
+ end
89
+ end
90
+
91
+ def test_children_time
92
+ t1 = Time.now
93
+
94
+ expected1 = @engine.push_scope "a"
95
+ sleep 0.1
96
+ t2 = Time.now
97
+
98
+ expected2 = @engine.push_scope "b"
99
+ sleep 0.2
100
+ t3 = Time.now
101
+
102
+ expected = @engine.push_scope "c"
103
+ sleep 0.3
104
+ scope = @engine.pop_scope expected, Time.now - t3
105
+
106
+ t4 = Time.now
107
+
108
+ check_time_approximate 0, scope.children_time
109
+ check_time_approximate 0.3, @engine.peek_scope.children_time
110
+
111
+ sleep 0.1
112
+ t5 = Time.now
113
+
114
+ expected = @engine.push_scope "d"
115
+ sleep 0.2
116
+ scope = @engine.pop_scope expected, Time.now - t5
117
+
118
+ t6 = Time.now
119
+
120
+ check_time_approximate 0, scope.children_time
121
+
122
+ scope = @engine.pop_scope expected2, Time.now - t2
123
+ assert_equal scope.name, 'b'
124
+
125
+ check_time_approximate (t4 - t3) + (t6 - t5), scope.children_time
126
+
127
+ scope = @engine.pop_scope expected1, Time.now - t1
128
+ assert_equal scope.name, 'a'
129
+
130
+ check_time_approximate (t6 - t2), scope.children_time
131
+ end
132
+
133
+ def test_simple_start_transaction
134
+ assert @engine.peek_scope.nil?
135
+ scope = @engine.push_scope "scope"
136
+ @engine.start_transaction
137
+ assert !@engine.peek_scope.nil?
138
+ @engine.pop_scope scope, 0.01
139
+ assert @engine.peek_scope.nil?
140
+ @engine.end_transaction
141
+ assert @engine.peek_scope.nil?
142
+ end
143
+
144
+
145
+ # test for when the scope stack contains an element only used for tts and not metrics
146
+ def test_simple_tt_only_scope
147
+ scope1 = @engine.push_scope "a", 0, true
148
+ scope2 = @engine.push_scope "b", 10, false
149
+ scope3 = @engine.push_scope "c", 20, true
150
+
151
+ @engine.pop_scope scope3, 10
152
+ @engine.pop_scope scope2, 10
153
+ @engine.pop_scope scope1, 10
154
+
155
+ assert_equal 0, scope3.children_time
156
+ assert_equal 10, scope2.children_time
157
+ assert_equal 10, scope1.children_time
158
+ end
159
+
160
+ def test_double_tt_only_scope
161
+ scope1 = @engine.push_scope "a", 0, true
162
+ scope2 = @engine.push_scope "b", 10, false
163
+ scope3 = @engine.push_scope "c", 20, false
164
+ scope4 = @engine.push_scope "d", 30, true
165
+
166
+ @engine.pop_scope scope4, 10
167
+ @engine.pop_scope scope3, 10
168
+ @engine.pop_scope scope2, 10
169
+ @engine.pop_scope scope1, 10
170
+
171
+ assert_equal 0, scope4.children_time
172
+ assert_equal 10, scope3.children_time
173
+ assert_equal 10, scope2.children_time
174
+ assert_equal 10, scope1.children_time
175
+ end
176
+
177
+
178
+ private
179
+ def check_time_approximate(expected, actual)
180
+ assert((expected - actual).abs < 0.1, "Expected between #{expected - 0.1} and #{expected + 0.1}, got #{actual}")
181
+ end
182
+
183
+ end
184
+