ghazel-newrelic_rpm 3.4.0.2 → 3.5.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (231) hide show
  1. data/.gitignore +21 -0
  2. data/.project +23 -0
  3. data/.travis.yml +9 -0
  4. data/CHANGELOG +180 -1
  5. data/GUIDELINES_FOR_CONTRIBUTING.md +73 -0
  6. data/Gemfile +16 -0
  7. data/InstallationNotes.md +15 -0
  8. data/LICENSE +1 -1
  9. data/{README.rdoc → README.md} +71 -55
  10. data/Rakefile +54 -0
  11. data/config.dot +290 -0
  12. data/config/database.yml +5 -0
  13. data/init.rb +38 -0
  14. data/lib/new_relic/agent.rb +9 -4
  15. data/lib/new_relic/agent/agent.rb +189 -230
  16. data/lib/new_relic/agent/beacon_configuration.rb +34 -48
  17. data/lib/new_relic/agent/browser_monitoring.rb +108 -61
  18. data/lib/new_relic/agent/busy_calculator.rb +12 -4
  19. data/lib/new_relic/agent/configuration.rb +49 -0
  20. data/lib/new_relic/agent/configuration/defaults.rb +89 -0
  21. data/lib/new_relic/agent/configuration/environment_source.rb +56 -0
  22. data/lib/new_relic/agent/configuration/manager.rb +116 -0
  23. data/lib/new_relic/agent/configuration/server_source.rb +27 -0
  24. data/lib/new_relic/agent/configuration/yaml_source.rb +61 -0
  25. data/lib/new_relic/agent/database.rb +37 -22
  26. data/lib/new_relic/agent/error_collector.rb +47 -43
  27. data/lib/new_relic/agent/instrumentation/active_record.rb +1 -5
  28. data/lib/new_relic/agent/instrumentation/controller_instrumentation.rb +27 -6
  29. data/lib/new_relic/agent/instrumentation/data_mapper.rb +2 -10
  30. data/lib/new_relic/agent/instrumentation/delayed_job_instrumentation.rb +1 -1
  31. data/lib/new_relic/agent/instrumentation/memcache.rb +2 -2
  32. data/lib/new_relic/agent/instrumentation/metric_frame.rb +4 -14
  33. data/lib/new_relic/agent/instrumentation/passenger_instrumentation.rb +4 -18
  34. data/lib/new_relic/agent/instrumentation/rack.rb +1 -1
  35. data/lib/new_relic/agent/instrumentation/rails/action_controller.rb +3 -3
  36. data/lib/new_relic/agent/instrumentation/rails3/action_controller.rb +2 -2
  37. data/lib/new_relic/agent/instrumentation/rails3/errors.rb +5 -1
  38. data/lib/new_relic/agent/instrumentation/resque.rb +1 -1
  39. data/lib/new_relic/agent/instrumentation/sinatra.rb +14 -10
  40. data/lib/new_relic/agent/instrumentation/unicorn_instrumentation.rb +4 -3
  41. data/lib/new_relic/agent/method_tracer.rb +5 -1
  42. data/lib/new_relic/agent/new_relic_service.rb +231 -61
  43. data/lib/new_relic/agent/pipe_channel_manager.rb +37 -23
  44. data/lib/new_relic/agent/pipe_service.rb +5 -1
  45. data/lib/new_relic/agent/samplers/delayed_job_sampler.rb +2 -5
  46. data/lib/new_relic/agent/samplers/memory_sampler.rb +2 -2
  47. data/lib/new_relic/agent/sql_sampler.rb +44 -68
  48. data/lib/new_relic/agent/stats_engine/gc_profiler.rb +40 -24
  49. data/lib/new_relic/agent/stats_engine/metric_stats.rb +89 -14
  50. data/lib/new_relic/agent/stats_engine/samplers.rb +7 -3
  51. data/lib/new_relic/agent/stats_engine/transactions.rb +19 -11
  52. data/lib/new_relic/agent/thread.rb +27 -0
  53. data/lib/new_relic/agent/thread_profiler.rb +295 -0
  54. data/lib/new_relic/agent/transaction_info.rb +24 -4
  55. data/lib/new_relic/agent/transaction_sample_builder.rb +11 -11
  56. data/lib/new_relic/agent/transaction_sampler.rb +51 -61
  57. data/lib/new_relic/agent/worker_loop.rb +29 -15
  58. data/lib/new_relic/collection_helper.rb +1 -1
  59. data/lib/new_relic/commands/deployments.rb +19 -10
  60. data/lib/new_relic/control.rb +0 -1
  61. data/lib/new_relic/control/class_methods.rb +2 -3
  62. data/lib/new_relic/control/frameworks/rails.rb +24 -18
  63. data/lib/new_relic/control/frameworks/rails3.rb +18 -1
  64. data/lib/new_relic/control/frameworks/ruby.rb +2 -2
  65. data/lib/new_relic/control/instance_methods.rb +36 -53
  66. data/lib/new_relic/control/logging_methods.rb +5 -23
  67. data/lib/new_relic/control/server_methods.rb +11 -13
  68. data/lib/new_relic/delayed_job_injection.rb +1 -1
  69. data/lib/new_relic/helper.rb +13 -0
  70. data/lib/new_relic/language_support.rb +19 -22
  71. data/lib/new_relic/local_environment.rb +2 -3
  72. data/lib/new_relic/metric_data.rb +10 -2
  73. data/lib/new_relic/metric_spec.rb +6 -2
  74. data/lib/new_relic/noticed_error.rb +24 -9
  75. data/lib/new_relic/rack.rb +4 -0
  76. data/lib/new_relic/rack/browser_monitoring.rb +28 -10
  77. data/lib/new_relic/rack/developer_mode.rb +3 -0
  78. data/lib/new_relic/rack/error_collector.rb +56 -0
  79. data/lib/new_relic/transaction_sample.rb +23 -13
  80. data/lib/new_relic/transaction_sample/segment.rb +13 -15
  81. data/lib/new_relic/version.rb +3 -3
  82. data/lib/tasks/tests.rake +8 -8
  83. data/newrelic.yml +15 -32
  84. data/newrelic_rpm.gemspec +158 -38
  85. data/newrelic_rpm.gemspec.erb +55 -0
  86. data/test/config/newrelic.yml +3 -2
  87. data/test/intentional_fail.rb +10 -0
  88. data/test/multiverse/.gitignore +10 -0
  89. data/test/multiverse/README.md +90 -0
  90. data/test/multiverse/Rakefile +17 -0
  91. data/test/multiverse/lib/multiverse/color.rb +13 -0
  92. data/test/multiverse/lib/multiverse/envfile.rb +66 -0
  93. data/test/multiverse/lib/multiverse/environment.rb +16 -0
  94. data/test/multiverse/lib/multiverse/output_collector.rb +29 -0
  95. data/test/multiverse/lib/multiverse/runner.rb +44 -0
  96. data/test/multiverse/lib/multiverse/suite.rb +162 -0
  97. data/test/multiverse/script/run_one +3 -0
  98. data/test/multiverse/script/runner +9 -0
  99. data/test/multiverse/suites/active_record/Envfile +13 -0
  100. data/test/multiverse/suites/active_record/ar_method_aliasing.rb +94 -0
  101. data/test/multiverse/suites/active_record/config/newrelic.yml +22 -0
  102. data/test/multiverse/suites/active_record/encoding_test.rb +26 -0
  103. data/test/multiverse/suites/agent_only/Envfile +3 -0
  104. data/test/multiverse/suites/agent_only/config/newrelic.yml +22 -0
  105. data/test/multiverse/suites/agent_only/http_response_code_test.rb +53 -0
  106. data/test/multiverse/suites/agent_only/marshaling_test.rb +109 -0
  107. data/test/multiverse/suites/agent_only/method_visibility_test.rb +98 -0
  108. data/test/multiverse/suites/agent_only/pipe_manager_test.rb +33 -0
  109. data/test/multiverse/suites/agent_only/service_timeout_test.rb +29 -0
  110. data/test/multiverse/suites/agent_only/test_trace_method_with_punctuation.rb +30 -0
  111. data/test/multiverse/suites/agent_only/test_trace_transaction_with_punctuation.rb +32 -0
  112. data/test/multiverse/suites/agent_only/thread_profiling_test.rb +80 -0
  113. data/test/multiverse/suites/datamapper/Envfile +8 -0
  114. data/test/multiverse/suites/datamapper/config/newrelic.yml +22 -0
  115. data/test/multiverse/suites/datamapper/encoding_test.rb +36 -0
  116. data/test/multiverse/suites/monitor_mode_false/Envfile +2 -0
  117. data/test/multiverse/suites/monitor_mode_false/config/newrelic.yml +25 -0
  118. data/test/multiverse/suites/monitor_mode_false/no_dns_resolv.rb +29 -0
  119. data/test/multiverse/suites/no_load/Envfile +2 -0
  120. data/test/multiverse/suites/no_load/config/newrelic.yml +23 -0
  121. data/test/multiverse/suites/no_load/start_up_test.rb +14 -0
  122. data/test/multiverse/suites/rails_3_error_tracing/Envfile +15 -0
  123. data/test/multiverse/suites/rails_3_error_tracing/config/newrelic.yml +165 -0
  124. data/test/multiverse/suites/rails_3_error_tracing/error_tracing_test.rb +236 -0
  125. data/test/multiverse/suites/rails_3_gc/Envfile +8 -0
  126. data/test/multiverse/suites/rails_3_gc/config/newrelic.yml +167 -0
  127. data/test/multiverse/suites/rails_3_gc/instrumentation_test.rb +92 -0
  128. data/test/multiverse/suites/rails_3_queue_time/Envfile +15 -0
  129. data/test/multiverse/suites/rails_3_queue_time/config/newrelic.yml +165 -0
  130. data/test/multiverse/suites/rails_3_queue_time/queue_time_test.rb +75 -0
  131. data/test/multiverse/suites/rails_3_views/.gitignore +3 -0
  132. data/test/multiverse/suites/rails_3_views/Envfile +16 -0
  133. data/test/multiverse/suites/rails_3_views/app/views/foos/_foo.html.haml +1 -0
  134. data/test/multiverse/suites/rails_3_views/app/views/test/_a_partial.html.erb +1 -0
  135. data/test/multiverse/suites/rails_3_views/app/views/test/_mid_partial.html.erb +1 -0
  136. data/test/multiverse/suites/rails_3_views/app/views/test/_top_partial.html.erb +3 -0
  137. data/test/multiverse/suites/rails_3_views/app/views/test/deep_partial.html.erb +3 -0
  138. data/test/multiverse/suites/rails_3_views/app/views/test/haml_view.html.haml +6 -0
  139. data/test/multiverse/suites/rails_3_views/app/views/test/index.html.erb +4 -0
  140. data/test/multiverse/suites/rails_3_views/config/newrelic.yml +164 -0
  141. data/test/multiverse/suites/rails_3_views/view_instrumentation_test.rb +245 -0
  142. data/test/multiverse/suites/resque/Envfile +21 -0
  143. data/test/multiverse/suites/resque/config/newrelic.yml +22 -0
  144. data/test/multiverse/suites/resque/dump.rdb +0 -0
  145. data/test/multiverse/suites/resque/instrumentation_test.rb +73 -0
  146. data/test/multiverse/suites/rum_auto_instrumentation/Envfile +4 -0
  147. data/test/multiverse/suites/rum_auto_instrumentation/config/newrelic.yml +24 -0
  148. data/test/multiverse/suites/rum_auto_instrumentation/problem_response.html +422 -0
  149. data/test/multiverse/suites/rum_auto_instrumentation/responses/worst_case_small.html +5000 -0
  150. data/test/multiverse/suites/rum_auto_instrumentation/sanity_test.rb +115 -0
  151. data/test/multiverse/suites/sinatra/Envfile +13 -0
  152. data/test/multiverse/suites/sinatra/config/newrelic.yml +24 -0
  153. data/test/multiverse/suites/sinatra/sinatra_metric_explosion_test.rb +76 -0
  154. data/test/multiverse/suites/sinatra/sinatra_routes_test.rb +46 -0
  155. data/test/multiverse/test/multiverse_test.rb +55 -0
  156. data/test/multiverse/test/suite_examples/one/a/Envfile +3 -0
  157. data/test/multiverse/test/suite_examples/one/a/a_test.rb +11 -0
  158. data/test/multiverse/test/suite_examples/one/a/config/newrelic.yml +24 -0
  159. data/test/multiverse/test/suite_examples/one/b/Envfile +3 -0
  160. data/test/multiverse/test/suite_examples/one/b/b_test.rb +11 -0
  161. data/test/multiverse/test/suite_examples/one/b/config/newrelic.yml +24 -0
  162. data/test/multiverse/test/suite_examples/three/a/Envfile +2 -0
  163. data/test/multiverse/test/suite_examples/three/a/fail_test.rb +6 -0
  164. data/test/multiverse/test/suite_examples/three/b/Envfile +2 -0
  165. data/test/multiverse/test/suite_examples/three/b/win_test.rb +6 -0
  166. data/test/multiverse/test/suite_examples/two/a/Envfile +1 -0
  167. data/test/multiverse/test/suite_examples/two/a/fail_test.rb +6 -0
  168. data/test/new_relic/agent/agent/connect_test.rb +134 -164
  169. data/test/new_relic/agent/agent/start_test.rb +111 -81
  170. data/test/new_relic/agent/agent/start_worker_thread_test.rb +6 -33
  171. data/test/new_relic/agent/agent_test.rb +88 -9
  172. data/test/new_relic/agent/agent_test_controller.rb +1 -1
  173. data/test/new_relic/agent/agent_test_controller_test.rb +42 -10
  174. data/test/new_relic/agent/beacon_configuration_test.rb +63 -67
  175. data/test/new_relic/agent/browser_monitoring_test.rb +150 -79
  176. data/test/new_relic/agent/configuration/environment_source_test.rb +74 -0
  177. data/test/new_relic/agent/configuration/manager_test.rb +149 -0
  178. data/test/new_relic/agent/configuration/server_source_test.rb +45 -0
  179. data/test/new_relic/agent/configuration/yaml_source_test.rb +56 -0
  180. data/test/new_relic/agent/error_collector/notice_error_test.rb +63 -50
  181. data/test/new_relic/agent/error_collector_test.rb +22 -12
  182. data/test/new_relic/agent/instrumentation/active_record_instrumentation_test.rb +21 -11
  183. data/test/new_relic/agent/instrumentation/metric_frame_test.rb +6 -0
  184. data/test/new_relic/agent/instrumentation/task_instrumentation_test.rb +2 -2
  185. data/test/new_relic/agent/method_tracer/class_methods/add_method_tracer_test.rb +5 -5
  186. data/test/new_relic/agent/method_tracer_test.rb +6 -6
  187. data/test/new_relic/agent/mock_scope_listener.rb +3 -0
  188. data/test/new_relic/agent/new_relic_service_test.rb +208 -23
  189. data/test/new_relic/agent/pipe_channel_manager_test.rb +34 -17
  190. data/test/new_relic/agent/rpm_agent_test.rb +27 -23
  191. data/test/new_relic/agent/sql_sampler_test.rb +81 -56
  192. data/test/new_relic/agent/stats_engine/metric_stats/harvest_test.rb +3 -20
  193. data/test/new_relic/agent/stats_engine/metric_stats_test.rb +12 -1
  194. data/test/new_relic/agent/stats_engine_test.rb +17 -8
  195. data/test/new_relic/agent/thread_profiler_test.rb +536 -0
  196. data/test/new_relic/agent/thread_test.rb +76 -0
  197. data/test/new_relic/agent/threaded_test.rb +65 -0
  198. data/test/new_relic/agent/transaction_info_test.rb +45 -4
  199. data/test/new_relic/agent/transaction_sample_builder_test.rb +8 -6
  200. data/test/new_relic/agent/transaction_sampler_test.rb +193 -204
  201. data/test/new_relic/agent/worker_loop_test.rb +20 -0
  202. data/test/new_relic/agent_test.rb +69 -41
  203. data/test/new_relic/collection_helper_test.rb +7 -8
  204. data/test/new_relic/command/deployments_test.rb +18 -2
  205. data/test/new_relic/control/frameworks/rails_test.rb +26 -0
  206. data/test/new_relic/control/logging_methods_test.rb +78 -52
  207. data/test/new_relic/control_test.rb +91 -129
  208. data/test/new_relic/fake_collector.rb +103 -31
  209. data/test/new_relic/fake_service.rb +8 -2
  210. data/test/new_relic/load_test.rb +13 -0
  211. data/test/new_relic/local_environment_test.rb +7 -10
  212. data/test/new_relic/metric_data_test.rb +45 -16
  213. data/test/new_relic/noticed_error_test.rb +14 -0
  214. data/test/new_relic/rack/browser_monitoring_test.rb +15 -9
  215. data/test/new_relic/rack/developer_mode_test.rb +13 -7
  216. data/test/new_relic/rack/error_collector_test.rb +74 -0
  217. data/test/new_relic/transaction_sample/segment_test.rb +23 -4
  218. data/test/new_relic/transaction_sample_test.rb +47 -2
  219. data/test/script/build_test_gem.sh +9 -3
  220. data/test/script/ci.sh +48 -21
  221. data/test/script/ci_multiverse_runner.sh +11 -11
  222. data/test/test_helper.rb +37 -18
  223. data/ui/helpers/developer_mode_helper.rb +21 -11
  224. data/ui/views/layouts/newrelic_default.rhtml +1 -0
  225. data/ui/views/newrelic/show_sample.rhtml +1 -1
  226. data/ui/views/newrelic/threads.rhtml +2 -10
  227. data/vendor/gems/metric_parser-0.1.0.pre1/.specification +116 -0
  228. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/servlet_init.rb +7 -0
  229. metadata +132 -58
  230. data/lib/new_relic/control/configuration.rb +0 -206
  231. data/test/new_relic/control/configuration_test.rb +0 -77
@@ -34,21 +34,23 @@ class NewRelic::Agent::RpmAgentTest < Test::Unit::TestCase # ActiveSupport::Test
34
34
  end
35
35
 
36
36
  should "startup_shutdown" do
37
- @agent = NewRelic::Agent::ShimAgent.instance
38
- @agent.shutdown
39
- assert (not @agent.started?)
40
- @agent.start
41
- assert !@agent.started?
42
- # this installs the real agent:
43
- NewRelic::Agent.manual_start
44
- @agent = NewRelic::Agent.instance
45
- assert @agent != NewRelic::Agent::ShimAgent.instance
46
- assert @agent.started?
47
- @agent.shutdown
48
- assert !@agent.started?
49
- @agent.start
50
- assert @agent.started?
51
- NewRelic::Agent.shutdown
37
+ with_config(:agent_enabled => true) do
38
+ @agent = NewRelic::Agent::ShimAgent.instance
39
+ @agent.shutdown
40
+ assert (not @agent.started?)
41
+ @agent.start
42
+ assert !@agent.started?
43
+ # this installs the real agent:
44
+ NewRelic::Agent.manual_start
45
+ @agent = NewRelic::Agent.instance
46
+ assert @agent != NewRelic::Agent::ShimAgent.instance
47
+ assert @agent.started?
48
+ @agent.shutdown
49
+ assert !@agent.started?
50
+ @agent.start
51
+ assert @agent.started?
52
+ NewRelic::Agent.shutdown
53
+ end
52
54
  end
53
55
 
54
56
  should "manual_start" do
@@ -67,28 +69,30 @@ class NewRelic::Agent::RpmAgentTest < Test::Unit::TestCase # ActiveSupport::Test
67
69
  end
68
70
  should "manual_overrides" do
69
71
  NewRelic::Agent.manual_start :app_name => "testjobs", :dispatcher_instance_id => "mailer"
70
- assert_equal "testjobs", NewRelic::Control.instance.app_names[0]
71
- assert_equal "mailer", NewRelic::Control.instance.dispatcher_instance_id
72
+ assert_equal "testjobs", NewRelic::Agent.config.app_names[0]
73
+ assert_equal "mailer", NewRelic::Control.instance.local_env.dispatcher_instance_id
72
74
  NewRelic::Agent.shutdown
73
75
  end
74
76
 
75
77
  should "restart" do
76
78
  NewRelic::Agent.manual_start :app_name => "noapp", :dispatcher_instance_id => ""
77
79
  NewRelic::Agent.manual_start :app_name => "testjobs", :dispatcher_instance_id => "mailer"
78
- assert_equal "testjobs", NewRelic::Control.instance.app_names[0]
79
- assert_equal "mailer", NewRelic::Control.instance.dispatcher_instance_id
80
+ assert_equal "testjobs", NewRelic::Agent.config.app_names[0]
81
+ assert_equal "mailer", NewRelic::Control.instance.local_env.dispatcher_instance_id
80
82
  NewRelic::Agent.shutdown
81
83
  end
82
84
 
83
85
  should "send_timeslice_data" do
84
86
  # this test fails due to a rubinius bug
85
87
  return if NewRelic::LanguageSupport.using_engine?('rbx')
86
- @agent.service.expects(:metric_data).returns({ NewRelic::MetricSpec.new("/A/b/c") => 1,
87
- NewRelic::MetricSpec.new("/A/b/c", "/X") => 2,
88
- NewRelic::MetricSpec.new("/A/b/d") => 3 }.to_a)
88
+ @agent.service = NewRelic::FakeService.new
89
+ @agent.service.expects(:metric_data).returns([ [{'name' => '/A/b/c'}, 1],
90
+ [{'name' => '/A/b/c', 'scope' => '/X'}, 2],
91
+ [{'name' => '/A/b/d'}, 3] ])
89
92
  @agent.send :harvest_and_send_timeslice_data
90
93
  assert_equal 3, @agent.metric_ids.size
91
- assert_equal 3, @agent.metric_ids[NewRelic::MetricSpec.new("/A/b/d") ], @agent.metric_ids.inspect
94
+ assert_equal(3, @agent.metric_ids[NewRelic::MetricSpec.new('/A/b/d')],
95
+ @agent.metric_ids.inspect)
92
96
  end
93
97
  should "set_record_sql" do
94
98
  @agent.set_record_sql(false)
@@ -1,7 +1,6 @@
1
1
  require File.expand_path(File.join(File.dirname(__FILE__),'..','..','test_helper'))
2
2
 
3
3
  class NewRelic::Agent::SqlSamplerTest < Test::Unit::TestCase
4
-
5
4
  def setup
6
5
  agent = NewRelic::Agent.instance
7
6
  stats_engine = NewRelic::Agent::StatsEngine.new
@@ -10,17 +9,17 @@ class NewRelic::Agent::SqlSamplerTest < Test::Unit::TestCase
10
9
  @connection = stub('ActiveRecord connection', :execute => 'result')
11
10
  NewRelic::Agent::Database.stubs(:get_connection).returns(@connection)
12
11
  end
13
-
12
+
14
13
  def test_notice_first_scope_push
15
- assert_nil @sampler.transaction_data
14
+ assert_nil @sampler.transaction_data
16
15
  @sampler.notice_first_scope_push nil
17
16
  assert_not_nil @sampler.transaction_data
18
17
  @sampler.notice_scope_empty
19
18
  assert_nil @sampler.transaction_data
20
19
  end
21
-
20
+
22
21
  def test_notice_sql_no_transaction
23
- assert_nil @sampler.transaction_data
22
+ assert_nil @sampler.transaction_data
24
23
  @sampler.notice_sql "select * from test", "Database/test/select", nil, 10
25
24
  end
26
25
 
@@ -33,45 +32,45 @@ class NewRelic::Agent::SqlSamplerTest < Test::Unit::TestCase
33
32
  assert_not_nil @sampler.transaction_data
34
33
  assert_equal 2, @sampler.transaction_data.sql_data.size
35
34
  end
36
-
35
+
37
36
  def test_harvest_slow_sql
38
37
  data = NewRelic::Agent::TransactionSqlData.new
39
38
  data.set_transaction_info("WebTransaction/Controller/c/a", "/c/a", {},
40
39
  'guid')
41
40
  data.sql_data.concat [
42
- NewRelic::Agent::SlowSql.new("select * from test", "Database/test/select", {}, 1.5),
43
- NewRelic::Agent::SlowSql.new("select * from test", "Database/test/select", {}, 1.2),
41
+ NewRelic::Agent::SlowSql.new("select * from test", "Database/test/select", {}, 1.5),
42
+ NewRelic::Agent::SlowSql.new("select * from test", "Database/test/select", {}, 1.2),
44
43
  NewRelic::Agent::SlowSql.new("select * from test2", "Database/test2/select", {}, 1.1)
45
44
  ]
46
45
  @sampler.harvest_slow_sql data
47
-
46
+
48
47
  assert_equal 2, @sampler.sql_traces.size
49
48
  end
50
-
49
+
51
50
  def test_sql_aggregation
52
- sql_trace = NewRelic::Agent::SqlTrace.new("select * from test",
51
+ sql_trace = NewRelic::Agent::SqlTrace.new("select * from test",
53
52
  NewRelic::Agent::SlowSql.new("select * from test",
54
53
  "Database/test/select", {}, 1.2),
55
54
  "tx_name", "uri")
56
-
55
+
57
56
  sql_trace.aggregate NewRelic::Agent::SlowSql.new("select * from test", "Database/test/select", {}, 1.5), "slowest_tx_name", "slow_uri"
58
57
  sql_trace.aggregate NewRelic::Agent::SlowSql.new("select * from test", "Database/test/select", {}, 1.1), "other_tx_name", "uri2"
59
-
58
+
60
59
  assert_equal 3, sql_trace.call_count
61
60
  assert_equal "slowest_tx_name", sql_trace.path
62
61
  assert_equal "slow_uri", sql_trace.url
63
62
  assert_equal 1.5, sql_trace.max_call_time
64
63
  end
65
-
64
+
66
65
  def test_harvest
67
66
  data = NewRelic::Agent::TransactionSqlData.new
68
67
  data.set_transaction_info("WebTransaction/Controller/c/a", "/c/a", {},
69
68
  'guid')
70
- data.sql_data.concat [NewRelic::Agent::SlowSql.new("select * from test", "Database/test/select", {}, 1.5),
71
- NewRelic::Agent::SlowSql.new("select * from test", "Database/test/select", {}, 1.2),
69
+ data.sql_data.concat [NewRelic::Agent::SlowSql.new("select * from test", "Database/test/select", {}, 1.5),
70
+ NewRelic::Agent::SlowSql.new("select * from test", "Database/test/select", {}, 1.2),
72
71
  NewRelic::Agent::SlowSql.new("select * from test2", "Database/test2/select", {}, 1.1)]
73
72
  @sampler.harvest_slow_sql data
74
-
73
+
75
74
  sql_traces = @sampler.harvest
76
75
  assert_equal 2, sql_traces.size
77
76
  end
@@ -83,10 +82,10 @@ class NewRelic::Agent::SqlSamplerTest < Test::Unit::TestCase
83
82
  data.sql_data << NewRelic::Agent::SlowSql.new("select * from test#{(i+97).chr}",
84
83
  "Database/test#{(i+97).chr}/select", {}, i)
85
84
  end
86
-
85
+
87
86
  @sampler.harvest_slow_sql data
88
87
  result = @sampler.harvest
89
-
88
+
90
89
  assert_equal(10, result.size)
91
90
  assert_equal(14, result.sort{|a,b| b.max_call_time <=> a.max_call_time}.first.total_call_time)
92
91
  end
@@ -96,13 +95,13 @@ class NewRelic::Agent::SqlSamplerTest < Test::Unit::TestCase
96
95
  data.set_transaction_info("WebTransaction/Controller/c/a", "/c/a", {},
97
96
  'guid')
98
97
  queries = [
99
- NewRelic::Agent::SlowSql.new("select * from test where foo in (1, 2) ", "Database/test/select", {}, 1.5),
100
- NewRelic::Agent::SlowSql.new("select * from test where foo in (1,2, 3 ,4, 5,6, 'snausage')", "Database/test/select", {}, 1.2),
98
+ NewRelic::Agent::SlowSql.new("select * from test where foo in (1, 2) ", "Database/test/select", {}, 1.5),
99
+ NewRelic::Agent::SlowSql.new("select * from test where foo in (1,2, 3 ,4, 5,6, 'snausage')", "Database/test/select", {}, 1.2),
101
100
  NewRelic::Agent::SlowSql.new("select * from test2 where foo in (1,2)", "Database/test2/select", {}, 1.1)
102
101
  ]
103
102
  data.sql_data.concat(queries)
104
103
  @sampler.harvest_slow_sql data
105
-
104
+
106
105
  sql_traces = @sampler.harvest
107
106
  assert_equal 2, sql_traces.size
108
107
  end
@@ -117,19 +116,19 @@ class NewRelic::Agent::SqlSamplerTest < Test::Unit::TestCase
117
116
  data.set_transaction_info("WebTransaction/Controller/c/a", "/c/a", {},
118
117
  'guid')
119
118
  queries = [
120
- NewRelic::Agent::SlowSql.new("select * from test", "Database/test/select", {}, 1.5),
121
- NewRelic::Agent::SlowSql.new("select * from test", "Database/test/select", {}, 1.2),
119
+ NewRelic::Agent::SlowSql.new("select * from test", "Database/test/select", {}, 1.5),
120
+ NewRelic::Agent::SlowSql.new("select * from test", "Database/test/select", {}, 1.2),
122
121
  NewRelic::Agent::SlowSql.new("select * from test2", "Database/test2/select", {}, 1.1)
123
122
  ]
124
123
  data.sql_data.concat(queries)
125
- @sampler.harvest_slow_sql data
124
+ @sampler.harvest_slow_sql data
126
125
  sql_traces = @sampler.harvest
127
126
  assert_equal(["header0", "header1", "header2"],
128
127
  sql_traces[0].params[:explain_plan][0].sort)
129
128
  assert_equal(["header0", "header1", "header2"],
130
129
  sql_traces[1].params[:explain_plan][0].sort)
131
130
  assert_equal(["foo0", "foo1", "foo2"],
132
- sql_traces[0].params[:explain_plan][1][0].sort)
131
+ sql_traces[0].params[:explain_plan][1][0].sort)
133
132
  assert_equal(["bar0", "bar1", "bar2"],
134
133
  sql_traces[1].params[:explain_plan][1][0].sort)
135
134
  end
@@ -145,48 +144,74 @@ class NewRelic::Agent::SqlSamplerTest < Test::Unit::TestCase
145
144
  assert_equal(NewRelic::Agent.instance.transaction_sampler.builder.sample.guid,
146
145
  NewRelic::Agent.instance.sql_sampler.transaction_data.guid)
147
146
  end
148
-
147
+
149
148
  def test_should_not_collect_explain_plans_when_disabled
150
- NewRelic::Control.instance['transaction_tracer'] = { 'explain_enabled' => false }
151
- data = NewRelic::Agent::TransactionSqlData.new
152
- data.set_transaction_info("WebTransaction/Controller/c/a", "/c/a", {},
153
- 'guid')
154
-
155
- queries = [
156
- NewRelic::Agent::SlowSql.new("select * from test",
157
- "Database/test/select", {}, 1.5)
158
- ]
159
- data.sql_data.concat(queries)
160
- @sampler.harvest_slow_sql data
161
- sql_traces = @sampler.harvest
162
- assert_equal(nil, sql_traces[0].params[:explain_plan])
163
- NewRelic::Control.instance['transaction_tracer'] = { 'explain_enabled' => true }
149
+ with_config(:'transaction_tracer.explain_enabled' => false) do
150
+ data = NewRelic::Agent::TransactionSqlData.new
151
+ data.set_transaction_info("WebTransaction/Controller/c/a", "/c/a", {},
152
+ 'guid')
153
+
154
+ queries = [
155
+ NewRelic::Agent::SlowSql.new("select * from test",
156
+ "Database/test/select", {}, 1.5)
157
+ ]
158
+ data.sql_data.concat(queries)
159
+ @sampler.harvest_slow_sql data
160
+ sql_traces = @sampler.harvest
161
+ assert_equal(nil, sql_traces[0].params[:explain_plan])
162
+ end
164
163
  end
165
164
 
166
165
  def test_sql_id_fits_in_a_mysql_int_11
167
- sql_trace = NewRelic::Agent::SqlTrace.new("select * from test",
166
+ sql_trace = NewRelic::Agent::SqlTrace.new("select * from test",
168
167
  NewRelic::Agent::SlowSql.new("select * from test",
169
168
  "Database/test/select", {}, 1.2),
170
169
  "tx_name", "uri")
171
-
170
+
172
171
  assert -2147483648 <= sql_trace.sql_id, "sql_id too small"
173
172
  assert 2147483647 >= sql_trace.sql_id, "sql_id too large"
174
173
  end
175
174
 
176
175
  def test_sends_obfuscated_queries_when_configured
177
- NewRelic::Control.instance['transaction_tracer'] = { 'record_sql' => 'obfuscated' }
178
-
179
- data = NewRelic::Agent::TransactionSqlData.new
180
- data.set_transaction_info("WebTransaction/Controller/c/a", "/c/a", {},
181
- 'guid')
182
- data.sql_data.concat([NewRelic::Agent::SlowSql.new("select * from test where foo = 'bar'",
183
- "Database/test/select", {}, 1.5),
184
- NewRelic::Agent::SlowSql.new("select * from test where foo in (1,2,3,4,5)",
185
- "Database/test/select", {}, 1.2)])
186
- @sampler.harvest_slow_sql(data)
187
- sql_traces = @sampler.harvest
176
+ with_config(:'transaction_tracer.record_sql' => 'obfuscated') do
177
+ data = NewRelic::Agent::TransactionSqlData.new
178
+ data.set_transaction_info("WebTransaction/Controller/c/a", "/c/a", {},
179
+ 'guid')
180
+ data.sql_data.concat([NewRelic::Agent::SlowSql.new("select * from test where foo = 'bar'",
181
+ "Database/test/select", {}, 1.5),
182
+ NewRelic::Agent::SlowSql.new("select * from test where foo in (1,2,3,4,5)",
183
+ "Database/test/select", {}, 1.2)])
184
+ @sampler.harvest_slow_sql(data)
185
+ sql_traces = @sampler.harvest
186
+
187
+ assert_equal('select * from test where foo = ?', sql_traces[0].sql)
188
+ assert_equal('select * from test where foo in (?,?,?,?,?)', sql_traces[1].sql)
189
+ end
190
+ end
188
191
 
189
- assert_equal('select * from test where foo = ?', sql_traces[0].sql)
190
- assert_equal('select * from test where foo in (?,?,?,?,?)', sql_traces[1].sql)
192
+ def test_to_collector_array
193
+ with_config(:'transaction_tracer.explain_enabled' => false) do
194
+ data = NewRelic::Agent::TransactionSqlData.new
195
+ data.set_transaction_info("WebTransaction/Controller/c/a", "/c/a", {},
196
+ 'guid')
197
+ data.sql_data.concat([NewRelic::Agent::SlowSql.new("select * from test",
198
+ "Database/test/select",
199
+ {}, 1.5)])
200
+ @sampler.harvest_slow_sql(data)
201
+ sql_traces = @sampler.harvest
202
+
203
+ params = RUBY_VERSION >= '1.9.2' ? "eJyrrgUAAXUA+Q==\n" : {}
204
+ expected = [ 'WebTransaction/Controller/c/a', '/c/a', 526336943,
205
+ 'select * from test', 'Database/test/select',
206
+ 1, 1500, 1500, 1500, params ]
207
+
208
+ if NewRelic::Agent::NewRelicService::JsonMarshaller.is_supported?
209
+ marshaller = NewRelic::Agent::NewRelicService::JsonMarshaller.new
210
+ else
211
+ marshaller = NewRelic::Agent::NewRelicService::PrubyMarshaller.new
212
+ end
213
+
214
+ assert_equal expected, sql_traces[0].to_collector_array(marshaller)
215
+ end
191
216
  end
192
217
  end
@@ -1,30 +1,13 @@
1
1
  require File.expand_path(File.join(File.dirname(__FILE__),'..','..','..','..','test_helper'))
2
2
  require 'new_relic/agent/stats_engine/metric_stats'
3
+
3
4
  class NewRelic::Agent::StatsEngine::MetricStats::HarvestTest < Test::Unit::TestCase
4
5
  include NewRelic::Agent::StatsEngine::MetricStats::Harvest
5
6
 
6
7
  attr_accessor :stats_hash
7
- def test_merge_stats_trivial
8
- self.stats_hash = {}
9
- merge_stats({}, {})
10
- end
11
-
12
8
  def test_merge_stats_with_nil_stats
13
- metric_ids = mock('metric ids')
14
- mock_stats_hash = mock('stats_hash')
15
- mock_spec = mock('spec')
16
- mock_stats = mock('stats')
17
- mock_stats_hash.expects(:each).yields(mock_spec, mock_stats)
18
- self.stats_hash = mock_stats_hash
19
-
20
- self.expects(:coerce_to_metric_spec).with(mock_spec).returns(mock_spec)
21
- self.expects(:clone_and_reset_stats).with(mock_spec, mock_stats).returns(mock_stats)
22
- self.expects(:merge_old_data!).with(mock_spec, mock_stats, {})
23
- metric_ids.expects(:[]).with(mock_spec).returns('an id')
24
- self.expects(:add_data_to_send_unless_empty).with({}, mock_stats, mock_spec, 'an id')
25
-
26
-
27
- merge_stats({}, metric_ids)
9
+ self.stats_hash = NewRelic::Agent::StatsEngine::MetricStats::SynchronizedHash.new
10
+ assert_equal({}, merge_stats({}, {}))
28
11
  end
29
12
 
30
13
 
@@ -44,7 +44,7 @@ class NewRelic::Agent::MetricStatsTest < Test::Unit::TestCase
44
44
  assert_equal 4, @engine.get_stats("c").total_call_time
45
45
 
46
46
  metric_data = @engine.harvest_timeslice_data({}, {}).values
47
-
47
+
48
48
  # after harvest, all the metrics should be reset
49
49
  assert_equal 0, @engine.get_stats("a").call_count
50
50
  assert_equal 0, @engine.get_stats("a").total_call_time
@@ -78,5 +78,16 @@ class NewRelic::Agent::MetricStatsTest < Test::Unit::TestCase
78
78
  assert_equal 2, stats.call_count
79
79
  assert_equal 3, stats.total_call_time
80
80
  end
81
+
82
+ def test_rescues_from_synchronization_failure_on_write
83
+ hash = NewRelic::Agent::StatsEngine::MetricStats::SynchronizedHash.new
84
+ 10.times { |i| hash[i] = i }
85
+
86
+ assert_nothing_raised do
87
+ hash.each do |k, v|
88
+ hash[11] = 1
89
+ end
90
+ end
91
+ end
81
92
  end
82
93
 
@@ -9,7 +9,7 @@ class NewRelic::Agent::StatsEngineTest < Test::Unit::TestCase
9
9
  puts e
10
10
  puts e.backtrace.join("\n")
11
11
  end
12
-
12
+
13
13
  def teardown
14
14
  @engine.harvest_timeslice_data({},{})
15
15
  mocha_teardown
@@ -193,26 +193,35 @@ class NewRelic::Agent::StatsEngineTest < Test::Unit::TestCase
193
193
  ::Rubinius::Agent.stubs(:loopback).returns(agent)
194
194
  elsif NewRelic::LanguageSupport.using_version?('1.9')
195
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
- elsif NewRelic::LanguageSupport.using_version?('1.8')
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/
199
201
  ::GC.stubs(:time).returns(1000000, 4000000)
200
202
  ::GC.stubs(:collections).returns(1, 3)
203
+ else
204
+ return true # no need to test if we're not collecting GC metrics
201
205
  end
202
-
206
+
203
207
  engine = NewRelic::Agent.instance.stats_engine
204
- scope = engine.push_scope "scope"
208
+ tracer = NewRelic::Agent::TransactionSampler.new
209
+ tracer.instance_variable_set(:@last_sample,
210
+ NewRelic::TransactionSample.new)
211
+ engine.transaction_sampler = tracer
205
212
  engine.start_transaction
213
+ scope = engine.push_scope "scope"
206
214
  engine.pop_scope scope, 0.01
207
215
  engine.end_transaction
208
-
216
+
209
217
  gc_stats = engine.get_stats('GC/cumulative')
210
218
  assert_equal 2, gc_stats.call_count
211
219
  assert_equal 3.0, gc_stats.total_call_time
220
+ assert_equal(3.0, tracer.last_sample.params[:custom_params][:gc_time])
212
221
  ensure
213
222
  GC.enable unless NewRelic::LanguageSupport.using_engine?('jruby')
214
223
  end
215
-
224
+
216
225
  private
217
226
  def check_time_approximate(expected, actual)
218
227
  assert((expected - actual).abs < 0.1, "Expected between #{expected - 0.1} and #{expected + 0.1}, got #{actual}")
@@ -0,0 +1,536 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__),'..','..','test_helper'))
2
+ require 'base64'
3
+ require 'thread'
4
+ require 'timeout'
5
+ require 'zlib'
6
+ require 'new_relic/agent/threaded_test'
7
+ require 'new_relic/agent/thread_profiler'
8
+
9
+ if RUBY_VERSION < '1.9.2'
10
+ class ThreadProfilerUnsupportedTest < Test::Unit::TestCase
11
+ def setup
12
+ @profiler = NewRelic::Agent::ThreadProfiler.new
13
+ end
14
+
15
+ def test_thread_profiling_isnt_supported
16
+ assert_equal false, NewRelic::Agent::ThreadProfiler.is_supported?
17
+ end
18
+
19
+ def test_wont_start_when_not_supported
20
+ @profiler.start(0, 0, 0, true)
21
+ assert_equal false, @profiler.running?
22
+ end
23
+
24
+ def test_stop_is_safe_when_not_supported
25
+ @profiler.start(0, 0, 0, true)
26
+ @profiler.stop(true)
27
+ end
28
+
29
+ end
30
+ end
31
+
32
+ if RUBY_VERSION >= '1.9.2'
33
+ require 'json'
34
+
35
+ class ThreadProfilerTest < ThreadedTest
36
+
37
+ START_COMMAND = [[666,{
38
+ "name" => "start_profiler",
39
+ "arguments" => {
40
+ "profile_id" => 42,
41
+ "sample_period" => 0.02,
42
+ "duration" => 0.025,
43
+ "only_runnable_threads" => false,
44
+ "only_request_threads" => false,
45
+ "profile_agent_code" => false,
46
+ }
47
+ }]]
48
+
49
+ STOP_COMMAND = [[666,{
50
+ "name" => "stop_profiler",
51
+ "arguments" => {
52
+ "profile_id" => 42,
53
+ "report_data" => true,
54
+ }
55
+ }]]
56
+
57
+ STOP_AND_DISCARD_COMMAND = [[666,{
58
+ "name" => "stop_profiler",
59
+ "arguments" => {
60
+ "profile_id" => 42,
61
+ "report_data" => false,
62
+ }
63
+ }]]
64
+
65
+ NO_COMMAND = []
66
+
67
+ def setup
68
+ super
69
+ @profiler = NewRelic::Agent::ThreadProfiler.new
70
+ end
71
+
72
+ def test_is_supported
73
+ assert NewRelic::Agent::ThreadProfiler.is_supported?
74
+ end
75
+
76
+ def test_is_not_running
77
+ assert !@profiler.running?
78
+ end
79
+
80
+ def test_is_running
81
+ @profiler.start(0, 0, 0, true)
82
+ assert @profiler.running?
83
+ end
84
+
85
+ def test_is_not_finished_if_no_profile_started
86
+ assert !@profiler.finished?
87
+ end
88
+
89
+ def test_can_stop_a_running_profile
90
+ @profiler.start(0, 0, 0, true)
91
+ assert @profiler.running?
92
+
93
+ @profiler.stop(true)
94
+
95
+ assert @profiler.finished?
96
+ assert_not_nil @profiler.profile
97
+ end
98
+
99
+ def test_can_stop_a_running_profile_and_discard
100
+ @profiler.start(0, 0, 0, true)
101
+ assert @profiler.running?
102
+
103
+ @profiler.stop(false)
104
+
105
+ assert_nil @profiler.profile
106
+ end
107
+
108
+ def test_wont_crash_if_stopping_when_not_started
109
+ @profiler.stop(true)
110
+ assert_equal false, @profiler.running?
111
+ end
112
+
113
+ def test_respond_to_commands_with_no_commands_doesnt_run
114
+ @profiler.respond_to_commands(NO_COMMAND)
115
+ assert_equal false, @profiler.running?
116
+ end
117
+
118
+ def test_respond_to_commands_starts_running
119
+ @profiler.respond_to_commands(START_COMMAND)
120
+ assert_equal true, @profiler.running?
121
+ end
122
+
123
+ def test_respond_to_commands_stops
124
+ @profiler.start(0, 0, 0, true)
125
+ assert @profiler.running?
126
+
127
+ @profiler.respond_to_commands(STOP_COMMAND)
128
+ assert_equal true, @profiler.profile.finished?
129
+ end
130
+
131
+ def test_respond_to_commands_stops_and_discards
132
+ @profiler.start(0, 0, 0, true)
133
+ assert @profiler.running?
134
+
135
+ @profiler.respond_to_commands(STOP_AND_DISCARD_COMMAND)
136
+ assert_nil @profiler.profile
137
+ end
138
+
139
+ def test_respond_to_commands_wont_start_second_profile
140
+ @profiler.start(0, 0, 0, true)
141
+ original_profile = @profiler.profile
142
+
143
+ @profiler.respond_to_commands(START_COMMAND)
144
+
145
+ assert_equal original_profile, @profiler.profile
146
+ end
147
+
148
+ def test_response_to_commands_start_notifies_of_result
149
+ saw_command_id = nil
150
+ @profiler.respond_to_commands(START_COMMAND) { |id, err| saw_command_id = id }
151
+ assert_equal 666, saw_command_id
152
+ end
153
+
154
+ def test_response_to_commands_start_notifies_of_error
155
+ saw_command_id = nil
156
+ error = nil
157
+
158
+ @profiler.respond_to_commands(START_COMMAND)
159
+ @profiler.respond_to_commands(START_COMMAND) { |id, err| saw_command_id = id; error = err }
160
+
161
+ assert_equal 666, saw_command_id
162
+ assert_not_nil error
163
+ end
164
+
165
+ def test_response_to_commands_stop_notifies_of_result
166
+ saw_command_id = nil
167
+ @profiler.start(0,0, 0, true)
168
+ @profiler.respond_to_commands(STOP_COMMAND) { |id, err| saw_command_id = id }
169
+ assert_equal 666, saw_command_id
170
+ end
171
+
172
+ def test_command_attributes_passed_along
173
+ @profiler.respond_to_commands(START_COMMAND)
174
+ assert_equal 42, @profiler.profile.profile_id
175
+ assert_equal 0.02, @profiler.profile.interval
176
+ assert_equal false, @profiler.profile.profile_agent_code
177
+ end
178
+
179
+ def test_missing_name_in_command
180
+ command = [[666,{ "arguments" => {} } ]]
181
+ @profiler.respond_to_commands(command)
182
+
183
+ assert_equal false, @profiler.running?
184
+ end
185
+
186
+ def test_malformed_agent_command
187
+ command = [[666]]
188
+ @profiler.respond_to_commands(command)
189
+
190
+ assert_equal false, @profiler.running?
191
+ end
192
+
193
+ end
194
+
195
+ class ThreadProfileTest < ThreadedTest
196
+
197
+ def setup
198
+ super
199
+
200
+ @single_trace = [
201
+ "irb.rb:69:in `catch'",
202
+ "irb.rb:69:in `start'",
203
+ "irb:12:in `<main>'"
204
+ ]
205
+
206
+ @profile = NewRelic::Agent::ThreadProfile.new(-1, 0.029, 0.01, true)
207
+ end
208
+
209
+ # Running Tests
210
+ def test_profiler_collects_backtrace_from_every_thread
211
+ FakeThread.list << FakeThread.new
212
+ FakeThread.list << FakeThread.new
213
+
214
+ @profile.run
215
+
216
+ assert_equal 2, @profile.poll_count
217
+ assert_equal 4, @profile.sample_count
218
+ end
219
+
220
+ def test_profiler_collects_into_request_bucket
221
+ FakeThread.list << FakeThread.new(
222
+ :bucket => :request,
223
+ :backtrace => @single_trace)
224
+
225
+ @profile.run
226
+
227
+ assert_equal 1, @profile.traces[:request].size
228
+ end
229
+
230
+ def test_profiler_collects_into_background_bucket
231
+ FakeThread.list << FakeThread.new(
232
+ :bucket => :background,
233
+ :backtrace => @single_trace)
234
+
235
+ @profile.run
236
+
237
+ assert_equal 1, @profile.traces[:background].size
238
+ end
239
+
240
+ def test_profiler_collects_into_other_bucket
241
+ FakeThread.list << FakeThread.new(
242
+ :bucket => :other,
243
+ :backtrace => @single_trace)
244
+
245
+ @profile.run
246
+
247
+ assert_equal 1, @profile.traces[:other].size
248
+ end
249
+
250
+ def test_profiler_collects_into_agent_bucket
251
+ FakeThread.list << FakeThread.new(
252
+ :bucket => :agent,
253
+ :backtrace => @single_trace)
254
+
255
+ @profile.run
256
+
257
+ assert_equal 1, @profile.traces[:agent].size
258
+ end
259
+
260
+ def test_profiler_ignores_agent_threads_when_told_to
261
+ FakeThread.list << FakeThread.new(
262
+ :bucket => :ignore,
263
+ :backtrace => @single_trace)
264
+
265
+ @profile.run
266
+
267
+ @profile.traces.each do |key, trace|
268
+ assert trace.empty?, "Trace :#{key} should have been empty"
269
+ end
270
+ end
271
+
272
+ def test_profiler_tries_to_scrub_backtraces
273
+ FakeThread.list << FakeThread.new(
274
+ :bucket => :agent,
275
+ :backtrace => @single_trace,
276
+ :scrubbed_backtrace => @single_trace[0..0])
277
+
278
+ @profile.run
279
+
280
+ assert_equal [], @profile.traces[:agent].first.children
281
+ end
282
+
283
+ def test_profile_can_be_stopped
284
+ # Can't easily stop in middle of processing since FakeThread's synchronous
285
+ # Mark to bail immediately, then see we didn't record anything
286
+ @profile.stop
287
+
288
+ @profile.run
289
+
290
+ assert_not_nil @profile.stop_time
291
+ assert_equal true, @profile.finished?
292
+
293
+ assert_equal 0, @profile.poll_count
294
+ @profile.traces.each do |key, trace|
295
+ assert_equal [], trace, "Trace for :#{key} should have been empty"
296
+ end
297
+ end
298
+
299
+ def test_profiler_tracks_time
300
+ @profile.run
301
+
302
+ assert_not_nil @profile.start_time
303
+ assert_not_nil @profile.stop_time
304
+ end
305
+
306
+ def test_finished
307
+ assert !@profile.finished?
308
+
309
+ @profile.run.join
310
+
311
+ assert @profile.finished?
312
+ end
313
+
314
+ # Parsing and Aggregation Tests
315
+ def test_parse_backtrace
316
+ trace = [
317
+ "/Users/jclark/.rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/irb.rb:69:in `catch'",
318
+ "/Users/jclark/.rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/irb.rb:69:in `start'",
319
+ "/Users/jclark/.rbenv/versions/1.9.3/bin/irb:12:in `<main>'"
320
+ ]
321
+
322
+ result = NewRelic::Agent::ThreadProfile.parse_backtrace(trace)
323
+ assert_equal({ :method => 'catch',
324
+ :file => '/Users/jclark/.rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/irb.rb',
325
+ :line_no => 69 }, result[0])
326
+ assert_equal({ :method => 'start',
327
+ :file => '/Users/jclark/.rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/irb.rb',
328
+ :line_no => 69 }, result[1])
329
+ assert_equal({ :method => '<main>',
330
+ :file => '/Users/jclark/.rbenv/versions/1.9.3/bin/irb',
331
+ :line_no => 12 }, result[2])
332
+ end
333
+
334
+ def test_aggregate_empty_trace
335
+ result = @profile.aggregate([])
336
+ assert_nil result
337
+ end
338
+
339
+ def test_aggregate_nil_trace
340
+ result = @profile.aggregate(nil)
341
+ assert_nil result
342
+ end
343
+
344
+ def test_aggregate_builds_tree_from_first_trace
345
+ result = @profile.aggregate(@single_trace)
346
+
347
+ tree = NewRelic::Agent::ThreadProfile::Node.new(@single_trace[-1])
348
+ child = NewRelic::Agent::ThreadProfile::Node.new(@single_trace[-2], tree)
349
+ NewRelic::Agent::ThreadProfile::Node.new(@single_trace[-3], child)
350
+
351
+ assert_equal tree, result
352
+ end
353
+
354
+ def test_aggregate_builds_tree_from_overlapping_traces
355
+ result = @profile.aggregate(@single_trace)
356
+ result = @profile.aggregate(@single_trace, [result])
357
+
358
+ tree = NewRelic::Agent::ThreadProfile::Node.new(@single_trace[-1])
359
+ tree.runnable_count += 1
360
+ child = NewRelic::Agent::ThreadProfile::Node.new(@single_trace[-2], tree)
361
+ child.runnable_count += 1
362
+ grand = NewRelic::Agent::ThreadProfile::Node.new(@single_trace[-3], child)
363
+ grand.runnable_count += 1
364
+
365
+ assert_equal tree, result
366
+ end
367
+
368
+ def test_aggregate_builds_tree_from_diverging_traces
369
+ other_trace = [
370
+ "irb.rb:69:in `catch'",
371
+ "chunky_bacon.rb:42:in `start'",
372
+ "irb:12:in `<main>'"
373
+ ]
374
+
375
+ result = @profile.aggregate(@single_trace)
376
+ result = @profile.aggregate(@single_trace, [result])
377
+
378
+ tree = NewRelic::Agent::ThreadProfile::Node.new(@single_trace[-1])
379
+ tree.runnable_count += 1
380
+
381
+ child = NewRelic::Agent::ThreadProfile::Node.new(@single_trace[-2], tree)
382
+ grand = NewRelic::Agent::ThreadProfile::Node.new(@single_trace[-3], child)
383
+
384
+ other_child = NewRelic::Agent::ThreadProfile::Node.new(other_trace[-2], tree)
385
+ other_grand = NewRelic::Agent::ThreadProfile::Node.new(other_trace[-3], other_child)
386
+
387
+ assert_equal tree, result
388
+ end
389
+
390
+ def test_prune_tree
391
+ @profile.aggregate(@single_trace)
392
+
393
+ t = @profile.prune!(1)
394
+
395
+ assert_equal 0, @profile.traces[:request].first.children.size
396
+ end
397
+
398
+ def test_prune_keeps_highest_counts
399
+ @profile.aggregate(@single_trace, @profile.traces[:request])
400
+ @profile.aggregate(@single_trace, @profile.traces[:other])
401
+ @profile.aggregate(@single_trace, @profile.traces[:other])
402
+
403
+ @profile.prune!(1)
404
+
405
+ assert_equal [], @profile.traces[:request]
406
+ assert_equal 1, @profile.traces[:other].size
407
+ assert_equal [], @profile.traces[:other][0].children
408
+ end
409
+
410
+ def test_prune_keeps_highest_count_then_depths
411
+ @profile.aggregate(@single_trace, @profile.traces[:request])
412
+ @profile.aggregate(@single_trace, @profile.traces[:other])
413
+
414
+ @profile.prune!(2)
415
+
416
+ assert_equal 1, @profile.traces[:request].size
417
+ assert_equal 1, @profile.traces[:other].size
418
+ assert_equal [], @profile.traces[:request][0].children
419
+ assert_equal [], @profile.traces[:other][0].children
420
+ end
421
+
422
+ def test_to_compressed_array
423
+ @profile.instance_variable_set(:@start_time, 1350403938892.524)
424
+ @profile.instance_variable_set(:@stop_time, 1350403939904.375)
425
+ @profile.instance_variable_set(:@poll_count, 10)
426
+ @profile.instance_variable_set(:@sample_count, 2)
427
+
428
+ trace = ["thread_profiler.py:1:in `<module>'"]
429
+ 10.times { @profile.aggregate(trace, @profile.traces[:other]) }
430
+
431
+ trace = [
432
+ "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py:489:in `__bootstrap'",
433
+ "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py:512:in `__bootstrap_inner'",
434
+ "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py:480:in `run'",
435
+ "thread_profiler.py:76:in `_profiler_loop'",
436
+ "thread_profiler.py:103:in `_run_profiler'",
437
+ "thread_profiler.py:165:in `collect_thread_stacks'"]
438
+ 10.times { @profile.aggregate(trace, @profile.traces[:agent]) }
439
+
440
+ expected = [[
441
+ -1,
442
+ 1350403938892.524,
443
+ 1350403939904.375,
444
+ 10,
445
+ "eJy9klFPwjAUhf/LfW7WDQTUGBPUiYkGdAxelqXZRpGGrm1uS8xi/O924JQX\n9Un7dm77ndN7c19hlt7FCZxnWQZug7xYMYN6LSTHwDRA4KLWq53kl0CinEQh\nCUmW5zmBJH5axPPUk16MJ/E0/cGk0lLyyrGPS+uKamu943DQeX5HMtypz5In\nwv6vRCeZ1NoAGQ2PCDpvrOM1fRAlFtjQWyxq/qJxa+lj4zZaBeuuQpccrdDK\n0l4wolKU1OxftOoQLNTzIdL/EcjJafjnQYyVWjvrsDBMKNVOZBD1/jO27fPs\naBG+DoGr8fX9JJktpjftVry9A9unzGo=\n",
446
+ 2,
447
+ 0
448
+ ]]
449
+
450
+ assert_equal expected, @profile.to_compressed_array
451
+ end
452
+
453
+ def test_compress
454
+ original = '{"OTHER": [[["thread_profiler.py", "<module>", 1], 10, 0, []]], "REQUEST": [], "AGENT": [[["/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", "__bootstrap", 489], 10, 0, [[["/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", "__bootstrap_inner", 512], 10, 0, [[["/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", "run", 480], 10, 0, [[["thread_profiler.py", "_profiler_loop", 76], 10, 0, [[["thread_profiler.py", "_run_profiler", 103], 10, 0, [[["thread_profiler.py", "collect_thread_stacks", 165], 10, 0, []]]]]]]]]]]]], "BACKGROUND": []}'
455
+ assert_equal(
456
+ "eJy9UtFOwjAU/ZWlz2QdKKCGmKBOTDSgY/iyLM02ijR0vcttiVmM/047J0LiA080bdJz2nPPbe/9IrP4KYzIjZckCTFr5NmSVQgrITn6VU06HhmVsNxKfmv33dSuoOPZmaSpBSQK3xbhPHYBHBxPwmncRqPzWhte0heRY4Y1fcSs5J+AG01fa7MG5a9+GfrOUQtQmvb8IZUip1Vzw6GfpIT6aNNhLAcw2mBWWXh5dX2Q01lcmVCKoyX73d5ZvHGrmpcGx27/V2uPmQRwPzQcnCSzJnvOVTq4OEVWgJS8MKw91SYrNtrJB/3jVvkbVnU3vn+eRLPF9KHpm+8dYyPRqg==",
457
+ NewRelic::Agent::ThreadProfile.compress(original).gsub(/\n/, ''))
458
+ end
459
+ end
460
+
461
+ class ThreadProfileNodeTest < Test::Unit::TestCase
462
+ SINGLE_LINE = "irb.rb:69:in `catch'"
463
+
464
+ def test_single_node_converts_to_array
465
+ line = "irb.rb:69:in `catch'"
466
+ node = NewRelic::Agent::ThreadProfile::Node.new(line)
467
+
468
+ assert_equal([
469
+ ["irb.rb", "catch", 69],
470
+ 0, 0,
471
+ []],
472
+ node.to_array)
473
+ end
474
+
475
+ def test_multiple_nodes_converts_to_array
476
+ line = "irb.rb:69:in `catch'"
477
+ child_line = "bacon.rb:42:in `yum'"
478
+ node = NewRelic::Agent::ThreadProfile::Node.new(line)
479
+ child = NewRelic::Agent::ThreadProfile::Node.new(child_line, node)
480
+
481
+ assert_equal([
482
+ ["irb.rb", "catch", 69],
483
+ 0, 0,
484
+ [
485
+ [
486
+ ['bacon.rb', 'yum', 42],
487
+ 0,0,
488
+ []
489
+ ]
490
+ ]],
491
+ node.to_array)
492
+ end
493
+
494
+ def test_add_child_twice
495
+ parent = NewRelic::Agent::ThreadProfile::Node.new(SINGLE_LINE)
496
+ child = NewRelic::Agent::ThreadProfile::Node.new(SINGLE_LINE)
497
+
498
+ parent.add_child(child)
499
+ parent.add_child(child)
500
+
501
+ assert_equal 1, parent.children.size
502
+ end
503
+
504
+ def test_prune_keeps_children
505
+ parent = NewRelic::Agent::ThreadProfile::Node.new(SINGLE_LINE)
506
+ child = NewRelic::Agent::ThreadProfile::Node.new(SINGLE_LINE, parent)
507
+
508
+ parent.prune!
509
+
510
+ assert_equal [child], parent.children
511
+ end
512
+
513
+ def test_prune_removes_children
514
+ parent = NewRelic::Agent::ThreadProfile::Node.new(SINGLE_LINE)
515
+ child = NewRelic::Agent::ThreadProfile::Node.new(SINGLE_LINE, parent)
516
+
517
+ child.to_prune = true
518
+ parent.prune!
519
+
520
+ assert_equal [], parent.children
521
+ end
522
+
523
+ def test_prune_removes_children
524
+ parent = NewRelic::Agent::ThreadProfile::Node.new(SINGLE_LINE)
525
+ child = NewRelic::Agent::ThreadProfile::Node.new(SINGLE_LINE, parent)
526
+ grandchild = NewRelic::Agent::ThreadProfile::Node.new(SINGLE_LINE, child)
527
+
528
+ grandchild.to_prune = true
529
+ parent.prune!
530
+
531
+ assert_equal [child], parent.children
532
+ assert_equal [], child.children
533
+ end
534
+
535
+ end
536
+ end