factorylabs-newrelic_rpm 2.10.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (160) hide show
  1. data/CHANGELOG +354 -0
  2. data/LICENSE +37 -0
  3. data/README-2.10.2.2 +10 -0
  4. data/README.md +138 -0
  5. data/bin/mongrel_rpm +33 -0
  6. data/bin/newrelic_cmd +4 -0
  7. data/cert/cacert.pem +34 -0
  8. data/install.rb +45 -0
  9. data/lib/new_relic/agent.rb +315 -0
  10. data/lib/new_relic/agent/agent.rb +647 -0
  11. data/lib/new_relic/agent/busy_calculator.rb +86 -0
  12. data/lib/new_relic/agent/chained_call.rb +13 -0
  13. data/lib/new_relic/agent/collection_helper.rb +66 -0
  14. data/lib/new_relic/agent/error_collector.rb +117 -0
  15. data/lib/new_relic/agent/instrumentation/active_merchant.rb +18 -0
  16. data/lib/new_relic/agent/instrumentation/active_record_instrumentation.rb +91 -0
  17. data/lib/new_relic/agent/instrumentation/authlogic.rb +8 -0
  18. data/lib/new_relic/agent/instrumentation/controller_instrumentation.rb +389 -0
  19. data/lib/new_relic/agent/instrumentation/data_mapper.rb +90 -0
  20. data/lib/new_relic/agent/instrumentation/delayed_job_instrumentation.rb +12 -0
  21. data/lib/new_relic/agent/instrumentation/memcache.rb +24 -0
  22. data/lib/new_relic/agent/instrumentation/merb/controller.rb +26 -0
  23. data/lib/new_relic/agent/instrumentation/merb/errors.rb +8 -0
  24. data/lib/new_relic/agent/instrumentation/metric_frame.rb +199 -0
  25. data/lib/new_relic/agent/instrumentation/net.rb +21 -0
  26. data/lib/new_relic/agent/instrumentation/passenger_instrumentation.rb +20 -0
  27. data/lib/new_relic/agent/instrumentation/rack.rb +109 -0
  28. data/lib/new_relic/agent/instrumentation/rails/action_controller.rb +59 -0
  29. data/lib/new_relic/agent/instrumentation/rails/action_web_service.rb +27 -0
  30. data/lib/new_relic/agent/instrumentation/rails/errors.rb +24 -0
  31. data/lib/new_relic/agent/instrumentation/sinatra.rb +39 -0
  32. data/lib/new_relic/agent/method_tracer.rb +348 -0
  33. data/lib/new_relic/agent/patch_const_missing.rb +125 -0
  34. data/lib/new_relic/agent/sampler.rb +46 -0
  35. data/lib/new_relic/agent/samplers/cpu_sampler.rb +50 -0
  36. data/lib/new_relic/agent/samplers/memory_sampler.rb +141 -0
  37. data/lib/new_relic/agent/samplers/mongrel_sampler.rb +23 -0
  38. data/lib/new_relic/agent/samplers/object_sampler.rb +24 -0
  39. data/lib/new_relic/agent/shim_agent.rb +21 -0
  40. data/lib/new_relic/agent/stats_engine.rb +22 -0
  41. data/lib/new_relic/agent/stats_engine/metric_stats.rb +116 -0
  42. data/lib/new_relic/agent/stats_engine/samplers.rb +74 -0
  43. data/lib/new_relic/agent/stats_engine/transactions.rb +154 -0
  44. data/lib/new_relic/agent/transaction_sampler.rb +315 -0
  45. data/lib/new_relic/agent/worker_loop.rb +118 -0
  46. data/lib/new_relic/commands/deployments.rb +145 -0
  47. data/lib/new_relic/commands/new_relic_commands.rb +30 -0
  48. data/lib/new_relic/control.rb +484 -0
  49. data/lib/new_relic/control/external.rb +13 -0
  50. data/lib/new_relic/control/merb.rb +24 -0
  51. data/lib/new_relic/control/rails.rb +151 -0
  52. data/lib/new_relic/control/ruby.rb +36 -0
  53. data/lib/new_relic/control/sinatra.rb +14 -0
  54. data/lib/new_relic/histogram.rb +89 -0
  55. data/lib/new_relic/local_environment.rb +299 -0
  56. data/lib/new_relic/merbtasks.rb +6 -0
  57. data/lib/new_relic/metric_data.rb +42 -0
  58. data/lib/new_relic/metric_parser.rb +124 -0
  59. data/lib/new_relic/metric_parser/action_mailer.rb +9 -0
  60. data/lib/new_relic/metric_parser/active_merchant.rb +26 -0
  61. data/lib/new_relic/metric_parser/active_record.rb +25 -0
  62. data/lib/new_relic/metric_parser/controller.rb +54 -0
  63. data/lib/new_relic/metric_parser/controller_cpu.rb +38 -0
  64. data/lib/new_relic/metric_parser/errors.rb +6 -0
  65. data/lib/new_relic/metric_parser/external.rb +50 -0
  66. data/lib/new_relic/metric_parser/mem_cache.rb +12 -0
  67. data/lib/new_relic/metric_parser/other_transaction.rb +15 -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 +67 -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/mongrel_rpm.ru +25 -0
  76. data/lib/new_relic/rack/newrelic.yml +26 -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 +362 -0
  80. data/lib/new_relic/transaction_analysis.rb +121 -0
  81. data/lib/new_relic/transaction_sample.rb +671 -0
  82. data/lib/new_relic/version.rb +54 -0
  83. data/lib/new_relic_api.rb +276 -0
  84. data/lib/newrelic_rpm.rb +45 -0
  85. data/lib/tasks/all.rb +4 -0
  86. data/lib/tasks/install.rake +7 -0
  87. data/lib/tasks/tests.rake +15 -0
  88. data/newrelic.yml +227 -0
  89. data/newrelic_rpm.gemspec +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 +38 -0
  94. data/test/new_relic/agent/active_record_instrumentation_test.rb +268 -0
  95. data/test/new_relic/agent/agent_controller_test.rb +254 -0
  96. data/test/new_relic/agent/agent_test_controller.rb +78 -0
  97. data/test/new_relic/agent/busy_calculator_test.rb +79 -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/error_collector_test.rb +173 -0
  101. data/test/new_relic/agent/method_tracer_test.rb +340 -0
  102. data/test/new_relic/agent/metric_data_test.rb +56 -0
  103. data/test/new_relic/agent/mock_ar_connection.rb +40 -0
  104. data/test/new_relic/agent/mock_scope_listener.rb +23 -0
  105. data/test/new_relic/agent/net_instrumentation_test.rb +63 -0
  106. data/test/new_relic/agent/rpm_agent_test.rb +125 -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 +88 -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 +177 -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 +404 -0
  115. data/test/new_relic/agent/worker_loop_test.rb +103 -0
  116. data/test/new_relic/control_test.rb +110 -0
  117. data/test/new_relic/delayed_job_test.rb +108 -0
  118. data/test/new_relic/deployments_api_test.rb +68 -0
  119. data/test/new_relic/environment_test.rb +75 -0
  120. data/test/new_relic/metric_parser_test.rb +172 -0
  121. data/test/new_relic/metric_spec_test.rb +177 -0
  122. data/test/new_relic/shim_agent_test.rb +9 -0
  123. data/test/new_relic/stats_test.rb +291 -0
  124. data/test/new_relic/version_number_test.rb +76 -0
  125. data/test/test_helper.rb +53 -0
  126. data/test/ui/newrelic_controller_test.rb +14 -0
  127. data/test/ui/newrelic_helper_test.rb +53 -0
  128. data/ui/controllers/newrelic_controller.rb +220 -0
  129. data/ui/helpers/google_pie_chart.rb +49 -0
  130. data/ui/helpers/newrelic_helper.rb +320 -0
  131. data/ui/views/layouts/newrelic_default.rhtml +47 -0
  132. data/ui/views/newrelic/_explain_plans.rhtml +27 -0
  133. data/ui/views/newrelic/_sample.rhtml +19 -0
  134. data/ui/views/newrelic/_segment.rhtml +28 -0
  135. data/ui/views/newrelic/_segment_limit_message.rhtml +1 -0
  136. data/ui/views/newrelic/_segment_row.rhtml +14 -0
  137. data/ui/views/newrelic/_show_sample_detail.rhtml +24 -0
  138. data/ui/views/newrelic/_show_sample_sql.rhtml +20 -0
  139. data/ui/views/newrelic/_show_sample_summary.rhtml +3 -0
  140. data/ui/views/newrelic/_sql_row.rhtml +11 -0
  141. data/ui/views/newrelic/_stack_trace.rhtml +30 -0
  142. data/ui/views/newrelic/_table.rhtml +12 -0
  143. data/ui/views/newrelic/explain_sql.rhtml +42 -0
  144. data/ui/views/newrelic/images/arrow-close.png +0 -0
  145. data/ui/views/newrelic/images/arrow-open.png +0 -0
  146. data/ui/views/newrelic/images/blue_bar.gif +0 -0
  147. data/ui/views/newrelic/images/file_icon.png +0 -0
  148. data/ui/views/newrelic/images/gray_bar.gif +0 -0
  149. data/ui/views/newrelic/images/new-relic-rpm-desktop.gif +0 -0
  150. data/ui/views/newrelic/images/new_relic_rpm_desktop.gif +0 -0
  151. data/ui/views/newrelic/images/textmate.png +0 -0
  152. data/ui/views/newrelic/index.rhtml +57 -0
  153. data/ui/views/newrelic/javascript/prototype-scriptaculous.js +7288 -0
  154. data/ui/views/newrelic/javascript/transaction_sample.js +107 -0
  155. data/ui/views/newrelic/sample_not_found.rhtml +2 -0
  156. data/ui/views/newrelic/show_sample.rhtml +80 -0
  157. data/ui/views/newrelic/show_source.rhtml +3 -0
  158. data/ui/views/newrelic/stylesheets/style.css +484 -0
  159. data/ui/views/newrelic/threads.rhtml +52 -0
  160. metadata +238 -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,125 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__),'..','..','test_helper'))
2
+ ##require 'new_relic/agent/agent'
3
+ ##require 'new_relic/local_environment'
4
+
5
+ class RpmAgentTest < ActiveSupport::TestCase
6
+
7
+ attr_reader :agent
8
+
9
+ # Fake out the agent to think mongrel is running
10
+ def setup
11
+ NewRelic::Agent.manual_start
12
+ @agent = NewRelic::Agent.instance
13
+ end
14
+ def teardown
15
+ NewRelic::Control.instance['app_name']=nil
16
+ NewRelic::Control.instance['dispatcher']=nil
17
+ NewRelic::Control.instance['dispatcher_instance_id']=nil
18
+ end
19
+ def test_agent_setup
20
+ assert NewRelic::Agent.instance.class == NewRelic::Agent::Agent
21
+ assert_raise RuntimeError do
22
+ NewRelic::Control.instance.init_plugin :agent_enabled => false
23
+ end
24
+ end
25
+
26
+ def test_public_apis
27
+ assert_raise RuntimeError do
28
+ NewRelic::Agent.set_sql_obfuscator(:unknown) do |sql|
29
+ puts sql
30
+ end
31
+ end
32
+
33
+ ignore_called = false
34
+ NewRelic::Agent.ignore_error_filter do |e|
35
+ ignore_called = true
36
+ nil
37
+ end
38
+ NewRelic::Agent.notice_error(ActionController::RoutingError.new("message"), :x => "y")
39
+ assert ignore_called
40
+ NewRelic::Agent.ignore_error_filter
41
+
42
+ end
43
+
44
+ def test_startup_shutdown
45
+ @agent = NewRelic::Agent::ShimAgent.instance
46
+ @agent.shutdown
47
+ assert (not @agent.started?)
48
+ @agent.start
49
+ assert !@agent.started?
50
+ # this installs the real agent:
51
+ NewRelic::Agent.manual_start
52
+ @agent = NewRelic::Agent.instance
53
+ assert @agent != NewRelic::Agent::ShimAgent.instance
54
+ assert @agent.started?
55
+ @agent.shutdown
56
+ assert !@agent.started?
57
+ @agent.start
58
+ assert @agent.started?
59
+ end
60
+
61
+ def test_manual_overrides
62
+ NewRelic::Agent.manual_start :app_name => "testjobs", :dispatcher_instance_id => "mailer"
63
+ assert_equal "testjobs", NewRelic::Control.instance.app_names[0]
64
+ assert_equal "mailer", NewRelic::Control.instance.dispatcher_instance_id
65
+ end
66
+ def test_restart
67
+ NewRelic::Agent.manual_start :app_name => "noapp", :dispatcher_instance_id => ""
68
+ NewRelic::Agent.manual_start :app_name => "testjobs", :dispatcher_instance_id => "mailer"
69
+ assert_equal "testjobs", NewRelic::Control.instance.app_names[0]
70
+ assert_equal "mailer", NewRelic::Control.instance.dispatcher_instance_id
71
+ end
72
+
73
+ def test_send_timeslice_data
74
+ @agent.expects(:invoke_remote).returns({NewRelic::MetricSpec.new("/A/b/c") => 1, NewRelic::MetricSpec.new("/A/b/c", "/X") => 2, NewRelic::MetricSpec.new("/A/b/d") => 3 }.to_a)
75
+ @agent.send :harvest_and_send_timeslice_data
76
+ assert_equal 3, @agent.metric_ids.size
77
+ assert_equal 3, @agent.metric_ids[NewRelic::MetricSpec.new("/A/b/d") ], @agent.metric_ids.inspect
78
+ end
79
+ def test_set_record_sql
80
+ @agent.set_record_sql(false)
81
+ assert !Thread::current[:record_sql]
82
+ NewRelic::Agent.disable_sql_recording do
83
+ assert_equal false, Thread::current[:record_sql]
84
+ NewRelic::Agent.disable_sql_recording do
85
+ assert_equal false, Thread::current[:record_sql]
86
+ end
87
+ assert_equal false, Thread::current[:record_sql]
88
+ end
89
+ assert !Thread::current[:record_sql], Thread::current[:record_sql]
90
+ ensure
91
+ @agent.set_record_sql(nil)
92
+ end
93
+
94
+ def test_version
95
+ assert_match /\d\.\d+\.\d+/, NewRelic::VERSION::STRING
96
+ end
97
+
98
+ def test_invoke_remote__ignore_non_200_results
99
+ NewRelic::Agent::Agent.class_eval do
100
+ public :invoke_remote
101
+ end
102
+ response_mock = mock()
103
+ Net::HTTP.any_instance.stubs(:request).returns(response_mock)
104
+ response_mock.stubs(:message).returns("bogus error")
105
+
106
+ for code in %w[500 504 400 302 503] do
107
+ assert_raise NewRelic::Agent::IgnoreSilentlyException, "Ignore #{code}" do
108
+ response_mock.stubs(:code).returns(code)
109
+ NewRelic::Agent.agent.invoke_remote :get_data_report_period, 0
110
+ end
111
+ end
112
+ end
113
+ def test_invoke_remote__throw_other_errors
114
+ NewRelic::Agent::Agent.class_eval do
115
+ public :invoke_remote
116
+ end
117
+ response_mock = Net::HTTPSuccess.new nil, nil, nil
118
+ response_mock.stubs(:body).returns("")
119
+ Marshal.stubs(:load).raises(RuntimeError, "marshal issue")
120
+ Net::HTTP.any_instance.stubs(:request).returns(response_mock)
121
+ assert_raise RuntimeError do
122
+ NewRelic::Agent.agent.invoke_remote :get_data_report_period, 0xFEFE
123
+ end
124
+ end
125
+ 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,88 @@
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
+ NewRelic::Agent.instance.stubs(:stats_engine).returns(@stats_engine)
9
+ end
10
+ def test_cpu
11
+ s = NewRelic::Agent::Samplers::CpuSampler.new
12
+ # need to sleep because if you go to fast it will skip the points
13
+ s.stats_engine = @stats_engine
14
+ sleep 2
15
+ s.poll
16
+ sleep 2
17
+ s.poll
18
+ assert_equal 2, s.systemtime_stats.call_count
19
+ assert_equal 2, s.usertime_stats.call_count
20
+ assert s.usertime_stats.total_call_time >= 0, "user cpu greater/equal to 0: #{s.usertime_stats.total_call_time}"
21
+ assert s.systemtime_stats.total_call_time >= 0, "system cpu greater/equal to 0: #{s.systemtime_stats.total_call_time}"
22
+ end
23
+ def test_memory__default
24
+ s = NewRelic::Agent::Samplers::MemorySampler.new
25
+ s.stats_engine = @stats_engine
26
+ s.poll
27
+ s.poll
28
+ s.poll
29
+ assert_equal 3, s.stats.call_count
30
+ assert s.stats.total_call_time > 0.5, "cpu greater than 0.5 ms: #{s.stats.total_call_time}"
31
+ end
32
+ def test_memory__linux
33
+ return if RUBY_PLATFORM =~ /darwin/
34
+ NewRelic::Agent::Samplers::MemorySampler.any_instance.stubs(:platform).returns 'linux'
35
+ s = NewRelic::Agent::Samplers::MemorySampler.new
36
+ s.stats_engine = @stats_engine
37
+ s.poll
38
+ s.poll
39
+ s.poll
40
+ assert_equal 3, s.stats.call_count
41
+ assert s.stats.total_call_time > 0.5, "cpu greater than 0.5 ms: #{s.stats.total_call_time}"
42
+ end
43
+ def test_memory__solaris
44
+ return if defined? JRuby
45
+ NewRelic::Agent::Samplers::MemorySampler.any_instance.stubs(:platform).returns 'solaris'
46
+ NewRelic::Agent::Samplers::MemorySampler::ShellPS.any_instance.stubs(:get_memory).returns 999
47
+ s = NewRelic::Agent::Samplers::MemorySampler.new
48
+ s.stats_engine = @stats_engine
49
+ s.poll
50
+ assert_equal 1, s.stats.call_count
51
+ assert_equal 999, s.stats.total_call_time
52
+ end
53
+ def test_memory__windows
54
+ return if defined? JRuby
55
+ NewRelic::Agent::Samplers::MemorySampler.any_instance.stubs(:platform).returns 'win32'
56
+ assert_raise NewRelic::Agent::Sampler::Unsupported do
57
+ NewRelic::Agent::Samplers::MemorySampler.new
58
+ end
59
+ end
60
+ def test_load_samplers
61
+ @stats_engine.expects(:add_harvest_sampler).once unless defined? JRuby
62
+ @stats_engine.expects(:add_sampler).once
63
+ NewRelic::Control.instance.load_samplers
64
+ assert_equal 4, NewRelic::Agent::Sampler.sampler_classes.size
65
+ end
66
+ def test_memory__is_supported
67
+ NewRelic::Agent::Samplers::MemorySampler.stubs(:platform).returns 'windows'
68
+ assert !NewRelic::Agent::Samplers::MemorySampler.supported_on_this_platform? || defined? JRuby
69
+ end
70
+ def test_mongrel
71
+ NewRelic::Agent::BusyCalculator.stubs('is_busy?'.to_sym).returns(false)
72
+ mongrel = mock()
73
+ NewRelic::Control.instance.local_env.stubs(:mongrel).returns(mongrel)
74
+ list = mock()
75
+ workers = mock()
76
+ workers.stubs(:list).returns(list)
77
+ list.stubs(:length).returns(3)
78
+ mongrel.expects(:workers).returns(workers).at_least_once
79
+ s = NewRelic::Agent::Samplers::MongrelSampler.new
80
+ s.stats_engine = @stats_engine
81
+ s.poll
82
+ s.poll
83
+ s.poll
84
+ assert_equal 3, s.queue_stats.call_count
85
+ assert_equal 3, s.queue_stats.average_call_time, "mongrel queue length"
86
+ end
87
+
88
+ end