newrelic_rpm 3.6.6.147 → 3.6.7.152

Sign up to get free protection for your applications and to get access to all the features.
Files changed (265) hide show
  1. data.tar.gz.sig +0 -0
  2. data/.gitignore +0 -1
  3. data/CHANGELOG +55 -17
  4. data/Guardfile +7 -0
  5. data/Rakefile +6 -0
  6. data/bin/mongrel_rpm +33 -0
  7. data/bin/newrelic +13 -0
  8. data/bin/newrelic_cmd +5 -0
  9. data/bin/nrdebug +273 -0
  10. data/install.rb +3 -3
  11. data/lib/new_relic/agent.rb +4 -6
  12. data/lib/new_relic/agent/agent.rb +76 -72
  13. data/lib/new_relic/agent/autostart.rb +18 -5
  14. data/lib/new_relic/agent/browser_monitoring.rb +41 -35
  15. data/lib/new_relic/agent/browser_token.rb +38 -0
  16. data/lib/new_relic/agent/busy_calculator.rb +7 -5
  17. data/lib/new_relic/agent/commands/agent_command.rb +19 -0
  18. data/lib/new_relic/agent/commands/agent_command_router.rb +88 -0
  19. data/lib/new_relic/agent/commands/thread_profiler.rb +80 -0
  20. data/lib/new_relic/agent/configuration/default_source.rb +700 -0
  21. data/lib/new_relic/agent/configuration/environment_source.rb +83 -30
  22. data/lib/new_relic/agent/configuration/manager.rb +8 -8
  23. data/lib/new_relic/agent/configuration/mask_defaults.rb +2 -2
  24. data/lib/new_relic/agent/cross_app_monitor.rb +10 -21
  25. data/lib/new_relic/agent/cross_app_tracing.rb +3 -3
  26. data/lib/new_relic/agent/error_collector.rb +7 -10
  27. data/lib/new_relic/agent/http_clients/curb_wrappers.rb +1 -1
  28. data/lib/new_relic/agent/http_clients/excon_wrappers.rb +5 -1
  29. data/lib/new_relic/agent/http_clients/httpclient_wrappers.rb +5 -1
  30. data/lib/new_relic/agent/http_clients/net_http_wrappers.rb +5 -1
  31. data/lib/new_relic/agent/http_clients/typhoeus_wrappers.rb +1 -1
  32. data/lib/new_relic/agent/instrumentation/action_controller_subscriber.rb +5 -12
  33. data/lib/new_relic/agent/instrumentation/action_view_subscriber.rb +4 -0
  34. data/lib/new_relic/agent/instrumentation/{rails4/active_record.rb → active_record_4.rb} +1 -1
  35. data/lib/new_relic/agent/instrumentation/active_record_subscriber.rb +11 -11
  36. data/lib/new_relic/agent/instrumentation/authlogic.rb +8 -6
  37. data/lib/new_relic/agent/instrumentation/controller_instrumentation.rb +12 -6
  38. data/lib/new_relic/agent/instrumentation/curb.rb +11 -2
  39. data/lib/new_relic/agent/instrumentation/delayed_job_instrumentation.rb +1 -3
  40. data/lib/new_relic/agent/instrumentation/evented_subscriber.rb +11 -2
  41. data/lib/new_relic/agent/instrumentation/excon/middleware.rb +13 -4
  42. data/lib/new_relic/agent/instrumentation/puma.rb +2 -1
  43. data/lib/new_relic/agent/instrumentation/rack.rb +15 -17
  44. data/lib/new_relic/agent/method_tracer.rb +181 -191
  45. data/lib/new_relic/agent/new_relic_service.rb +16 -14
  46. data/lib/new_relic/agent/pipe_channel_manager.rb +1 -1
  47. data/lib/new_relic/agent/request_sampler.rb +32 -165
  48. data/lib/new_relic/agent/sampled_buffer.rb +79 -0
  49. data/lib/new_relic/agent/samplers/delayed_job_sampler.rb +1 -8
  50. data/lib/new_relic/agent/sql_sampler.rb +3 -3
  51. data/lib/new_relic/agent/stats_engine.rb +1 -1
  52. data/lib/new_relic/agent/stats_engine/gc_profiler.rb +1 -1
  53. data/lib/new_relic/agent/stats_engine/metric_stats.rb +2 -3
  54. data/lib/new_relic/agent/stats_engine/transactions.rb +2 -22
  55. data/lib/new_relic/agent/threading/agent_thread.rb +41 -0
  56. data/lib/new_relic/agent/threading/backtrace_node.rb +71 -0
  57. data/lib/new_relic/agent/threading/thread_profile.rb +172 -0
  58. data/lib/new_relic/agent/transaction.rb +16 -4
  59. data/lib/new_relic/agent/transaction_sample_builder.rb +13 -3
  60. data/lib/new_relic/agent/transaction_sampler.rb +5 -8
  61. data/lib/new_relic/agent/transaction_state.rb +166 -0
  62. data/lib/new_relic/agent/transaction_timings.rb +53 -0
  63. data/lib/new_relic/cli/command.rb +91 -0
  64. data/lib/new_relic/{commands → cli}/deployments.rb +13 -9
  65. data/lib/new_relic/{commands → cli}/install.rb +2 -2
  66. data/lib/new_relic/control/frameworks/rails.rb +1 -8
  67. data/lib/new_relic/control/instance_methods.rb +1 -2
  68. data/lib/new_relic/helper.rb +9 -0
  69. data/lib/new_relic/local_environment.rb +16 -63
  70. data/lib/new_relic/rack/browser_monitoring.rb +8 -5
  71. data/lib/new_relic/recipes.rb +44 -31
  72. data/lib/new_relic/transaction_sample.rb +6 -2
  73. data/lib/new_relic/version.rb +1 -1
  74. data/lib/tasks/install.rake +21 -1
  75. data/newrelic_rpm.gemspec +5 -1
  76. data/test/agent_helper.rb +4 -9
  77. data/test/environments/.gitignore +16 -0
  78. data/test/environments/lib/environments/runner.rb +104 -0
  79. data/test/environments/norails/Gemfile +11 -0
  80. data/test/environments/norails/Rakefile +9 -0
  81. data/test/environments/rails21/Gemfile +24 -0
  82. data/test/environments/rails21/Rakefile +12 -0
  83. data/test/environments/rails21/app/controllers/application.rb +20 -0
  84. data/test/environments/rails21/config/boot.rb +113 -0
  85. data/test/environments/rails21/config/database.yml +31 -0
  86. data/test/environments/rails21/config/environment.rb +28 -0
  87. data/test/environments/rails21/config/environments/development.rb +16 -0
  88. data/test/environments/rails21/config/environments/production.rb +8 -0
  89. data/test/environments/rails21/config/environments/test.rb +10 -0
  90. data/test/environments/rails21/config/routes.rb +5 -0
  91. data/test/environments/rails21/db/schema.rb +5 -0
  92. data/test/environments/rails22/Gemfile +29 -0
  93. data/test/environments/rails22/Rakefile +12 -0
  94. data/test/environments/rails22/app/controllers/application.rb +20 -0
  95. data/test/environments/rails22/config/boot.rb +113 -0
  96. data/test/environments/rails22/config/database.yml +31 -0
  97. data/test/environments/rails22/config/environment.rb +27 -0
  98. data/test/environments/rails22/config/environments/development.rb +16 -0
  99. data/test/environments/rails22/config/environments/production.rb +8 -0
  100. data/test/environments/rails22/config/environments/test.rb +10 -0
  101. data/test/environments/rails22/config/routes.rb +5 -0
  102. data/test/environments/rails22/db/schema.rb +5 -0
  103. data/test/environments/rails23/Gemfile +22 -0
  104. data/test/environments/rails23/Rakefile +12 -0
  105. data/test/environments/rails23/app/controllers/application.rb +20 -0
  106. data/test/environments/rails23/config/boot.rb +127 -0
  107. data/test/environments/rails23/config/database.yml +31 -0
  108. data/test/environments/rails23/config/environment.rb +25 -0
  109. data/test/environments/rails23/config/environments/development.rb +16 -0
  110. data/test/environments/rails23/config/environments/production.rb +8 -0
  111. data/test/environments/rails23/config/environments/test.rb +10 -0
  112. data/test/environments/rails23/config/preinitializer.rb +25 -0
  113. data/test/environments/rails23/config/routes.rb +5 -0
  114. data/test/environments/rails23/db/schema.rb +5 -0
  115. data/test/environments/rails30/Gemfile +19 -0
  116. data/test/environments/rails30/Rakefile +9 -0
  117. data/test/environments/rails30/config/application.rb +17 -0
  118. data/test/environments/rails30/config/boot.rb +10 -0
  119. data/test/environments/rails30/config/database.yml +31 -0
  120. data/test/environments/rails30/config/environment.rb +6 -0
  121. data/test/environments/rails30/config/initializers/new_rails_defaults.rb +11 -0
  122. data/test/environments/rails30/db/schema.rb +5 -0
  123. data/test/environments/rails31/Gemfile +19 -0
  124. data/test/environments/rails31/Rakefile +9 -0
  125. data/test/environments/rails31/config/application.rb +18 -0
  126. data/test/environments/rails31/config/boot.rb +10 -0
  127. data/test/environments/rails31/config/database.yml +31 -0
  128. data/test/environments/rails31/config/environment.rb +6 -0
  129. data/test/environments/rails31/config/initializers/new_rails_defaults.rb +21 -0
  130. data/test/environments/rails31/db/schema.rb +5 -0
  131. data/test/environments/rails32/Gemfile +25 -0
  132. data/test/environments/rails32/Rakefile +9 -0
  133. data/test/environments/rails32/config/application.rb +19 -0
  134. data/test/environments/rails32/config/boot.rb +10 -0
  135. data/test/environments/rails32/config/database.yml +31 -0
  136. data/test/environments/rails32/config/environment.rb +6 -0
  137. data/test/environments/rails32/db/schema.rb +5 -0
  138. data/test/environments/rails40/Gemfile +25 -0
  139. data/test/environments/rails40/Rakefile +9 -0
  140. data/test/environments/rails40/config/application.rb +18 -0
  141. data/test/environments/rails40/config/boot.rb +10 -0
  142. data/test/environments/rails40/config/database.yml +31 -0
  143. data/test/environments/rails40/config/environment.rb +6 -0
  144. data/test/environments/rails40/db/schema.rb +5 -0
  145. data/test/multiverse/lib/multiverse/suite.rb +24 -12
  146. data/test/multiverse/suites/agent_only/logging_test.rb +18 -1
  147. data/test/multiverse/suites/agent_only/rum_instrumentation_test.rb +1 -8
  148. data/test/multiverse/suites/agent_only/thread_profiling_test.rb +10 -7
  149. data/test/multiverse/suites/curb/curb_test.rb +17 -3
  150. data/test/multiverse/suites/excon/Envfile +1 -1
  151. data/test/multiverse/suites/excon/excon_test.rb +18 -2
  152. data/test/multiverse/suites/httpclient/httpclient_test.rb +2 -2
  153. data/test/multiverse/suites/net_http/net_http_test.rb +2 -2
  154. data/test/multiverse/suites/rails/error_tracing_test.rb +35 -8
  155. data/test/multiverse/suites/rails/ignore_test.rb +48 -0
  156. data/test/multiverse/suites/rails/mongrel_queue_depth_test.rb +44 -0
  157. data/test/multiverse/suites/sequel/sequel_test.rb +11 -0
  158. data/test/multiverse/suites/sinatra/ignoring_test.rb +2 -2
  159. data/test/multiverse/suites/typhoeus/Envfile +9 -2
  160. data/test/multiverse/suites/typhoeus/typhoeus_test.rb +3 -2
  161. data/test/new_relic/agent/agent/start_test.rb +3 -22
  162. data/test/new_relic/agent/agent_test.rb +6 -5
  163. data/test/new_relic/agent/autostart_test.rb +39 -1
  164. data/test/new_relic/agent/browser_monitoring_test.rb +43 -88
  165. data/test/new_relic/agent/browser_token_test.rb +52 -0
  166. data/test/new_relic/agent/busy_calculator_test.rb +1 -1
  167. data/test/new_relic/agent/commands/agent_command_router_test.rb +98 -0
  168. data/test/new_relic/agent/commands/agent_command_test.rb +37 -0
  169. data/test/new_relic/agent/commands/thread_profiler_test.rb +177 -0
  170. data/test/new_relic/agent/configuration/default_source_test.rb +81 -0
  171. data/test/new_relic/agent/configuration/environment_source_test.rb +81 -7
  172. data/test/new_relic/agent/configuration/manager_test.rb +16 -1
  173. data/test/new_relic/agent/cross_app_monitor_test.rb +3 -5
  174. data/test/new_relic/agent/error_collector_test.rb +14 -0
  175. data/test/new_relic/agent/instrumentation/action_controller_subscriber_test.rb +1 -1
  176. data/test/new_relic/agent/instrumentation/active_record_instrumentation_test.rb +2 -3
  177. data/test/new_relic/agent/instrumentation/active_record_subscriber_test.rb +29 -31
  178. data/test/new_relic/agent/instrumentation/controller_instrumentation_test.rb +22 -0
  179. data/test/new_relic/agent/method_tracer/instance_methods/trace_execution_scoped_test.rb +2 -2
  180. data/test/new_relic/agent/method_tracer_test.rb +2 -2
  181. data/test/new_relic/agent/new_relic_service_test.rb +5 -49
  182. data/test/new_relic/agent/pipe_channel_manager_test.rb +1 -0
  183. data/test/new_relic/agent/request_sampler_test.rb +32 -95
  184. data/test/new_relic/agent/rpm_agent_test.rb +7 -5
  185. data/test/new_relic/agent/sampled_buffer_test.rb +178 -0
  186. data/test/new_relic/agent/stats_engine/metric_stats_test.rb +0 -1
  187. data/test/new_relic/agent/stats_engine_test.rb +10 -17
  188. data/test/new_relic/agent/threading/agent_thread_test.rb +107 -0
  189. data/test/new_relic/agent/threading/backtrace_node_test.rb +96 -0
  190. data/test/new_relic/agent/{threaded_test.rb → threading/fake_thread.rb} +0 -25
  191. data/test/new_relic/agent/threading/thread_profile_test.rb +308 -0
  192. data/test/new_relic/agent/threading/threaded_test_case.rb +33 -0
  193. data/test/new_relic/agent/transaction/pop_test.rb +0 -16
  194. data/test/new_relic/agent/transaction_sample_builder_test.rb +23 -4
  195. data/test/new_relic/agent/transaction_sampler_test.rb +14 -15
  196. data/test/new_relic/agent/transaction_state_test.rb +75 -0
  197. data/test/new_relic/agent/transaction_test.rb +15 -0
  198. data/test/new_relic/agent/transaction_timings_test.rb +81 -0
  199. data/test/new_relic/agent_test.rb +12 -14
  200. data/test/new_relic/{command → cli}/deployments_test.rb +41 -13
  201. data/test/new_relic/control/frameworks/rails_test.rb +1 -2
  202. data/test/new_relic/control_test.rb +1 -2
  203. data/test/new_relic/dependency_detection_test.rb +14 -0
  204. data/test/new_relic/fake_collector.rb +1 -0
  205. data/test/new_relic/helper_test.rb +7 -2
  206. data/test/new_relic/http_client_test_cases.rb +33 -1
  207. data/test/new_relic/local_environment_test.rb +0 -23
  208. data/test/new_relic/metric_data_test.rb +4 -0
  209. data/test/new_relic/rack/browser_monitoring_test.rb +31 -30
  210. data/test/performance/lib/performance/result.rb +15 -4
  211. data/test/performance/lib/performance/timer.rb +7 -1
  212. data/test/performance/script/baselines +102 -0
  213. data/test/performance/suites/rum_autoinsertion.rb +57 -0
  214. data/test/rum/basic.result.html +10 -0
  215. data/test/rum/basic.source.html +10 -0
  216. data/test/rum/comments1.result.html +24 -0
  217. data/test/rum/comments1.source.html +24 -0
  218. data/test/rum/comments2.result.html +24 -0
  219. data/test/rum/comments2.source.html +24 -0
  220. data/test/rum/gt_in_quotes1.result.html +27 -0
  221. data/test/rum/gt_in_quotes1.source.html +27 -0
  222. data/test/rum/gt_in_quotes2.result.html +24 -0
  223. data/test/rum/gt_in_quotes2.source.html +24 -0
  224. data/test/rum/gt_in_quotes_mismatch.result.html +24 -0
  225. data/test/rum/gt_in_quotes_mismatch.source.html +24 -0
  226. data/test/rum/gt_in_single_quotes1.result.html +25 -0
  227. data/test/rum/gt_in_single_quotes1.source.html +25 -0
  228. data/test/rum/gt_in_single_quotes_mismatch.result.html +25 -0
  229. data/test/rum/gt_in_single_quotes_mismatch.source.html +25 -0
  230. data/test/rum/incomplete_non_meta_tags.result.html +10 -0
  231. data/test/rum/incomplete_non_meta_tags.source.html +10 -0
  232. data/test/rum/no_body.result.html +21 -0
  233. data/test/rum/no_body.source.html +21 -0
  234. data/test/rum/no_header.result.html +7 -0
  235. data/test/rum/no_header.source.html +7 -0
  236. data/test/rum/no_start_header.result.html +9 -0
  237. data/test/rum/no_start_header.source.html +9 -0
  238. data/test/rum/script1.result.html +19 -0
  239. data/test/rum/script1.source.html +19 -0
  240. data/test/rum/script2.result.html +17 -0
  241. data/test/rum/script2.source.html +17 -0
  242. data/test/rum/x_ua_meta_tag.result.html +10 -0
  243. data/test/rum/x_ua_meta_tag.source.html +10 -0
  244. data/test/rum/x_ua_meta_tag_multiline.result.html +11 -0
  245. data/test/rum/x_ua_meta_tag_multiline.source.html +11 -0
  246. data/test/rum/x_ua_meta_tag_with_others.result.html +11 -0
  247. data/test/rum/x_ua_meta_tag_with_others.source.html +11 -0
  248. data/test/rum/x_ua_meta_tag_with_spaces.result.html +10 -0
  249. data/test/rum/x_ua_meta_tag_with_spaces.source.html +10 -0
  250. data/test/script/ci.sh +1 -12
  251. data/test/test_helper.rb +1 -0
  252. data/vendor/gems/dependency_detection-0.0.1.build/lib/dependency_detection.rb +11 -0
  253. metadata +225 -32
  254. metadata.gz.sig +0 -0
  255. data/lib/new_relic/agent/configuration/defaults.rb +0 -157
  256. data/lib/new_relic/agent/instrumentation/browser_monitoring_timings.rb +0 -56
  257. data/lib/new_relic/agent/thread.rb +0 -37
  258. data/lib/new_relic/agent/thread_profiler.rb +0 -323
  259. data/lib/new_relic/agent/transaction_info.rb +0 -114
  260. data/lib/new_relic/command.rb +0 -89
  261. data/test/new_relic/agent/agent_test_controller_test.rb +0 -348
  262. data/test/new_relic/agent/instrumentation/browser_monitoring_timings_test.rb +0 -70
  263. data/test/new_relic/agent/thread_profiler_test.rb +0 -586
  264. data/test/new_relic/agent/thread_test.rb +0 -93
  265. data/test/new_relic/agent/transaction_info_test.rb +0 -99
@@ -0,0 +1,107 @@
1
+ # encoding: utf-8
2
+ # This file is distributed under New Relic's license terms.
3
+ # See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.
4
+
5
+ require File.expand_path(File.join(File.dirname(__FILE__),'..','..','..','test_helper'))
6
+ require 'new_relic/agent/threading/agent_thread'
7
+
8
+ module NewRelic::Agent::Threading
9
+ class AgentThreadTest < Test::Unit::TestCase
10
+
11
+ def test_sets_label
12
+ t = AgentThread.new("labelled") {}
13
+ assert_equal "labelled", t[:newrelic_label]
14
+ end
15
+
16
+ def test_bucket_thread_as_agent_when_profiling
17
+ t = AgentThread.new("labelled") {}
18
+ assert_equal :agent, AgentThread.bucket_thread(t, true)
19
+ end
20
+
21
+ def test_bucket_thread_as_agent_when_not_profiling
22
+ t = AgentThread.new("labelled") {}
23
+ assert_equal :ignore, AgentThread.bucket_thread(t, false)
24
+ end
25
+
26
+ def test_bucket_thread_as_request
27
+ t = ::Thread.new {
28
+ txn = NewRelic::Agent::Transaction.new
29
+ txn.request = "has a request"
30
+
31
+ NewRelic::Agent::TransactionState.get.current_transaction_stack = [txn]
32
+ }.join
33
+
34
+ assert_equal :request, AgentThread.bucket_thread(t, DONT_CARE)
35
+ end
36
+
37
+ def test_bucket_thread_as_background
38
+ t = ::Thread.new {
39
+ txn = NewRelic::Agent::Transaction.new
40
+ NewRelic::Agent::TransactionState.get.current_transaction_stack = [txn]
41
+ }.join
42
+
43
+ assert_equal :background, AgentThread.bucket_thread(t, DONT_CARE)
44
+ end
45
+
46
+ def test_bucket_thread_as_other_empty_txn_stack
47
+ t = ::Thread.new {
48
+ NewRelic::Agent::TransactionState.get.current_transaction_stack = []
49
+ }.join
50
+
51
+ assert_equal :other, AgentThread.bucket_thread(t, DONT_CARE)
52
+ end
53
+
54
+ def test_bucket_thread_as_other_no_txn_stack
55
+ t = ::Thread.new {
56
+ NewRelic::Agent::TransactionState.get.current_transaction_stack = nil
57
+ }.join
58
+
59
+ assert_equal :other, AgentThread.bucket_thread(t, DONT_CARE)
60
+ end
61
+
62
+ def test_bucket_thread_as_other
63
+ t = ::Thread.new {}
64
+ assert_equal :other, AgentThread.bucket_thread(t, DONT_CARE)
65
+ end
66
+
67
+ def test_runs_block
68
+ called = false
69
+
70
+ t = AgentThread.new("labelled") { called = true }
71
+ t.join
72
+
73
+ assert called
74
+ end
75
+
76
+ TRACE = [
77
+ "/Users/jclark/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/eventmachine-0.12.10/lib/eventmachine.rb:100:in `catch'",
78
+ "/Users/jclark/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/newrelic_rpm-3.5.3.452.dev/lib/new_relic/agent/agent.rb:200:in `start_worker_thread'",
79
+ "/Users/jclark/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/thin-1.5.0/lib/thin/backends/base.rb:300:in `block (3 levels) in run'",
80
+ ]
81
+
82
+ def test_scrubs_backtrace_when_not_profiling_agent_code
83
+ result = AgentThread.scrub_backtrace(stub(:backtrace => TRACE), false)
84
+ assert_equal [TRACE[0], TRACE[2]], result
85
+ end
86
+
87
+ def test_doesnt_scrub_backtrace_when_profiling_agent_code
88
+ result = AgentThread.scrub_backtrace(stub(:backtrace => TRACE), true)
89
+ assert_equal TRACE, result
90
+ end
91
+
92
+ def test_scrub_backtrace_handles_errors_during_backtrace
93
+ dummy_thread = stub
94
+ dummy_thread.stubs(:backtrace).raises(StandardError.new('nah'))
95
+ assert_nothing_raised do
96
+ AgentThread.scrub_backtrace(dummy_thread, true)
97
+ end
98
+ end
99
+
100
+ def test_scrub_backtrace_handles_nil_backtrace
101
+ bt = AgentThread.scrub_backtrace(stub(:backtrace => nil), false)
102
+ assert_nil(bt)
103
+ end
104
+
105
+ DONT_CARE = true
106
+ end
107
+ end
@@ -0,0 +1,96 @@
1
+ # encoding: utf-8
2
+ # This file is distributed under New Relic's license terms.
3
+ # See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.
4
+
5
+ require File.expand_path(File.join(File.dirname(__FILE__),'..','..','..','test_helper'))
6
+ require 'new_relic/agent/threading/backtrace_node'
7
+
8
+ module NewRelic::Agent::Threading
9
+ class BacktraceNodeTest < Test::Unit::TestCase
10
+ SINGLE_LINE = "irb.rb:69:in `catch'"
11
+
12
+ def test_single_node_converts_to_array
13
+ line = "irb.rb:69:in `catch'"
14
+ node = BacktraceNode.new(line)
15
+
16
+ assert_equal([
17
+ ["irb.rb", "catch", 69],
18
+ 0, 0,
19
+ []],
20
+ node.to_array)
21
+ end
22
+
23
+ def test_multiple_nodes_converts_to_array
24
+ line = "irb.rb:69:in `catch'"
25
+ child_line = "bacon.rb:42:in `yum'"
26
+ node = BacktraceNode.new(line)
27
+ child = BacktraceNode.new(child_line, node)
28
+
29
+ assert_equal([
30
+ ["irb.rb", "catch", 69],
31
+ 0, 0,
32
+ [
33
+ [
34
+ ['bacon.rb', 'yum', 42],
35
+ 0,0,
36
+ []
37
+ ]
38
+ ]],
39
+ node.to_array)
40
+ end
41
+
42
+ def test_gracefully_handle_bad_values_in_to_array
43
+ node = BacktraceNode.new(SINGLE_LINE)
44
+ node.instance_variable_set(:@line_no, "blarg")
45
+ node.runnable_count = Rational(10, 1)
46
+
47
+ assert_equal([
48
+ ["irb.rb", "catch", 0],
49
+ 10, 0,
50
+ []],
51
+ node.to_array)
52
+ end
53
+
54
+ def test_add_child_twice
55
+ parent = BacktraceNode.new(SINGLE_LINE)
56
+ child = BacktraceNode.new(SINGLE_LINE)
57
+
58
+ parent.add_child(child)
59
+ parent.add_child(child)
60
+
61
+ assert_equal 1, parent.children.size
62
+ end
63
+
64
+ def test_prune_keeps_children
65
+ parent = BacktraceNode.new(SINGLE_LINE)
66
+ child = BacktraceNode.new(SINGLE_LINE, parent)
67
+
68
+ parent.prune!
69
+
70
+ assert_equal [child], parent.children
71
+ end
72
+
73
+ def test_prune_removes_children
74
+ parent = BacktraceNode.new(SINGLE_LINE)
75
+ child = BacktraceNode.new(SINGLE_LINE, parent)
76
+
77
+ child.to_prune = true
78
+ parent.prune!
79
+
80
+ assert_equal [], parent.children
81
+ end
82
+
83
+ def test_prune_removes_grandchildren
84
+ parent = BacktraceNode.new(SINGLE_LINE)
85
+ child = BacktraceNode.new(SINGLE_LINE, parent)
86
+ grandchild = BacktraceNode.new(SINGLE_LINE, child)
87
+
88
+ grandchild.to_prune = true
89
+ parent.prune!
90
+
91
+ assert_equal [child], parent.children
92
+ assert_equal [], child.children
93
+ end
94
+
95
+ end
96
+ end
@@ -2,31 +2,6 @@
2
2
  # This file is distributed under New Relic's license terms.
3
3
  # See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.
4
4
 
5
- class ThreadedTest < Test::Unit::TestCase
6
- def setup
7
- @original_thread_class = NewRelic::Agent::AgentThread
8
- swap_thread_class(FakeThread)
9
- end
10
-
11
- def teardown
12
- swap_thread_class(@original_thread_class)
13
- @original_thread_class = nil
14
-
15
- FakeThread.list.clear
16
- end
17
-
18
- def default_test
19
- # no-op to keep quiet....
20
- end
21
-
22
- private
23
-
24
- def swap_thread_class(klass)
25
- NewRelic::Agent.send(:remove_const, "AgentThread") if NewRelic::Agent.const_defined?("AgentThread")
26
- NewRelic::Agent.const_set("AgentThread", klass)
27
- end
28
- end
29
-
30
5
  class FakeThread
31
6
  @@list = []
32
7
 
@@ -0,0 +1,308 @@
1
+ # encoding: utf-8
2
+ # This file is distributed under New Relic's license terms.
3
+ # See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.
4
+
5
+ require File.expand_path(File.join(File.dirname(__FILE__),'..','..','..','test_helper'))
6
+
7
+ require 'new_relic/agent/threading/thread_profile'
8
+ require 'new_relic/agent/threading/threaded_test_case'
9
+
10
+ if NewRelic::Agent::Commands::ThreadProfiler.is_supported?
11
+
12
+ module NewRelic::Agent::Threading
13
+ class ThreadProfileTest < ThreadedTestCase
14
+
15
+ def setup
16
+ super
17
+
18
+ @single_trace = [
19
+ "irb.rb:69:in `catch'",
20
+ "irb.rb:69:in `start'",
21
+ "irb:12:in `<main>'"
22
+ ]
23
+
24
+ @profile = ThreadProfile.new(create_agent_command)
25
+
26
+ # Run the worker_loop for the thread profile based on two iterations
27
+ # This takes time fussiness out of the equation and keeps the tests stable
28
+ @profile.instance_variable_set(:@worker_loop, NewRelic::Agent::WorkerLoop.new(:limit => 2))
29
+ end
30
+
31
+ # Running Tests
32
+ def test_profiler_collects_backtrace_from_every_thread
33
+ FakeThread.list << FakeThread.new
34
+ FakeThread.list << FakeThread.new
35
+
36
+ @profile.run
37
+
38
+ assert_equal 2, @profile.poll_count
39
+ assert_equal 4, @profile.sample_count
40
+ end
41
+
42
+ def test_profiler_collects_into_request_bucket
43
+ FakeThread.list << FakeThread.new(
44
+ :bucket => :request,
45
+ :backtrace => @single_trace)
46
+
47
+ @profile.run
48
+
49
+ assert_equal 1, @profile.traces[:request].size
50
+ end
51
+
52
+ def test_profiler_collects_into_background_bucket
53
+ FakeThread.list << FakeThread.new(
54
+ :bucket => :background,
55
+ :backtrace => @single_trace)
56
+
57
+ @profile.run
58
+
59
+ assert_equal 1, @profile.traces[:background].size
60
+ end
61
+
62
+ def test_profiler_collects_into_other_bucket
63
+ FakeThread.list << FakeThread.new(
64
+ :bucket => :other,
65
+ :backtrace => @single_trace)
66
+
67
+ @profile.run
68
+
69
+ assert_equal 1, @profile.traces[:other].size
70
+ end
71
+
72
+ def test_profiler_collects_into_agent_bucket
73
+ FakeThread.list << FakeThread.new(
74
+ :bucket => :agent,
75
+ :backtrace => @single_trace)
76
+
77
+ @profile.run
78
+
79
+ assert_equal 1, @profile.traces[:agent].size
80
+ end
81
+
82
+ def test_profiler_ignores_agent_threads_when_told_to
83
+ FakeThread.list << FakeThread.new(
84
+ :bucket => :ignore,
85
+ :backtrace => @single_trace)
86
+
87
+ @profile.run
88
+
89
+ @profile.traces.each do |key, trace|
90
+ assert trace.empty?, "Trace :#{key} should have been empty"
91
+ end
92
+ end
93
+
94
+ def test_profiler_tries_to_scrub_backtraces
95
+ FakeThread.list << FakeThread.new(
96
+ :bucket => :agent,
97
+ :backtrace => @single_trace,
98
+ :scrubbed_backtrace => @single_trace[0..0])
99
+
100
+ @profile.run
101
+
102
+ assert_equal [], @profile.traces[:agent].first.children
103
+ end
104
+
105
+ def test_profile_can_be_stopped
106
+ # Can't easily stop in middle of processing since FakeThread's synchronous
107
+ # Mark to bail immediately, then see we didn't record anything
108
+ @profile.stop
109
+
110
+ @profile.run
111
+
112
+ assert_not_nil @profile.stop_time
113
+ assert_equal true, @profile.finished?
114
+
115
+ assert_equal 0, @profile.poll_count
116
+ @profile.traces.each do |key, trace|
117
+ assert_equal [], trace, "Trace for :#{key} should have been empty"
118
+ end
119
+ end
120
+
121
+ def test_profiler_tracks_time
122
+ @profile.run
123
+
124
+ assert_not_nil @profile.start_time
125
+ assert_not_nil @profile.stop_time
126
+ end
127
+
128
+ def test_finished
129
+ assert !@profile.finished?
130
+
131
+ @profile.run.join
132
+
133
+ assert @profile.finished?
134
+ end
135
+
136
+ # Parsing and Aggregation Tests
137
+ def test_parse_backtrace
138
+ trace = [
139
+ "/Users/jclark/.rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/irb.rb:69:in `catch'",
140
+ "/Users/jclark/.rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/irb.rb:69:in `start'",
141
+ "/Users/jclark/.rbenv/versions/1.9.3/bin/irb:12:in `<main>'"
142
+ ]
143
+
144
+ result = ThreadProfile.parse_backtrace(trace)
145
+ assert_equal({ :method => 'catch',
146
+ :file => '/Users/jclark/.rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/irb.rb',
147
+ :line_no => 69 }, result[0])
148
+ assert_equal({ :method => 'start',
149
+ :file => '/Users/jclark/.rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/irb.rb',
150
+ :line_no => 69 }, result[1])
151
+ assert_equal({ :method => '<main>',
152
+ :file => '/Users/jclark/.rbenv/versions/1.9.3/bin/irb',
153
+ :line_no => 12 }, result[2])
154
+ end
155
+
156
+ def test_aggregate_empty_trace
157
+ result = @profile.aggregate([])
158
+ assert_nil result
159
+ end
160
+
161
+ def test_aggregate_nil_trace
162
+ result = @profile.aggregate(nil)
163
+ assert_nil result
164
+ end
165
+
166
+ def test_aggregate_builds_tree_from_first_trace
167
+ result = @profile.aggregate(@single_trace)
168
+
169
+ tree = BacktraceNode.new(@single_trace[-1])
170
+ child = BacktraceNode.new(@single_trace[-2], tree)
171
+ BacktraceNode.new(@single_trace[-3], child)
172
+
173
+ assert_equal tree, result
174
+ end
175
+
176
+ def test_aggregate_builds_tree_from_overlapping_traces
177
+ result = @profile.aggregate(@single_trace)
178
+ result = @profile.aggregate(@single_trace, [result])
179
+
180
+ tree = BacktraceNode.new(@single_trace[-1])
181
+ tree.runnable_count += 1
182
+ child = BacktraceNode.new(@single_trace[-2], tree)
183
+ child.runnable_count += 1
184
+ grand = BacktraceNode.new(@single_trace[-3], child)
185
+ grand.runnable_count += 1
186
+
187
+ assert_equal tree, result
188
+ end
189
+
190
+ def test_aggregate_builds_tree_from_diverging_traces
191
+ other_trace = [
192
+ "irb.rb:69:in `catch'",
193
+ "chunky_bacon.rb:42:in `start'",
194
+ "irb:12:in `<main>'"
195
+ ]
196
+
197
+ result = @profile.aggregate(@single_trace)
198
+ result = @profile.aggregate(@single_trace, [result])
199
+
200
+ tree = BacktraceNode.new(@single_trace[-1])
201
+ tree.runnable_count += 1
202
+
203
+ child = BacktraceNode.new(@single_trace[-2], tree)
204
+ grand = BacktraceNode.new(@single_trace[-3], child)
205
+
206
+ other_child = BacktraceNode.new(other_trace[-2], tree)
207
+ other_grand = BacktraceNode.new(other_trace[-3], other_child)
208
+
209
+ assert_equal tree, result
210
+ end
211
+
212
+ def test_prune_tree
213
+ @profile.aggregate(@single_trace)
214
+
215
+ t = @profile.prune!(1)
216
+
217
+ assert_equal 0, @profile.traces[:request].first.children.size
218
+ end
219
+
220
+ def test_prune_keeps_highest_counts
221
+ @profile.aggregate(@single_trace, @profile.traces[:request])
222
+ @profile.aggregate(@single_trace, @profile.traces[:other])
223
+ @profile.aggregate(@single_trace, @profile.traces[:other])
224
+
225
+ @profile.prune!(1)
226
+
227
+ assert_equal [], @profile.traces[:request]
228
+ assert_equal 1, @profile.traces[:other].size
229
+ assert_equal [], @profile.traces[:other][0].children
230
+ end
231
+
232
+ def test_prune_keeps_highest_count_then_depths
233
+ @profile.aggregate(@single_trace, @profile.traces[:request])
234
+ @profile.aggregate(@single_trace, @profile.traces[:other])
235
+
236
+ @profile.prune!(2)
237
+
238
+ assert_equal 1, @profile.traces[:request].size
239
+ assert_equal 1, @profile.traces[:other].size
240
+ assert_equal [], @profile.traces[:request][0].children
241
+ assert_equal [], @profile.traces[:other][0].children
242
+ end
243
+
244
+ def build_well_known_trace
245
+ trace = ["thread_profiler.py:1:in `<module>'"]
246
+ 10.times { @profile.aggregate(trace, @profile.traces[:other]) }
247
+
248
+ trace = [
249
+ "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py:489:in `__bootstrap'",
250
+ "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py:512:in `__bootstrap_inner'",
251
+ "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py:480:in `run'",
252
+ "thread_profiler.py:76:in `_profiler_loop'",
253
+ "thread_profiler.py:103:in `_run_profiler'",
254
+ "thread_profiler.py:165:in `collect_thread_stacks'"]
255
+ 10.times { @profile.aggregate(trace, @profile.traces[:agent]) }
256
+ end
257
+
258
+ WELL_KNOWN_TRACE_ENCODED = "eJy9klFPwjAUhf/LfW7WDQTUGBPUiYkGdAxelqXZRpGGrm1uS8xi/O924JQX\n9Un7dm77ndN7c19hlt7FCZxnWQZug7xYMYN6LSTHwDRA4KLWq53kl0CinEQh\nCUmW5zmBJH5axPPUk16MJ/E0/cGk0lLyyrGPS+uKamu943DQeX5HMtypz5In\nwv6vRCeZ1NoAGQ2PCDpvrOM1fRAlFtjQWyxq/qJxa+lj4zZaBeuuQpccrdDK\n0l4wolKU1OxftOoQLNTzIdL/EcjJafjnQYyVWjvrsDBMKNVOZBD1/jO27fPs\naBG+DoGr8fX9JJktpjftVry9A9unzGo=\n"
259
+
260
+ def test_to_collector_array
261
+ @profile.instance_variable_set(:@profile_id, "-1")
262
+ @profile.instance_variable_set(:@start_time, 1350403938892.524)
263
+ @profile.instance_variable_set(:@stop_time, 1350403939904.375)
264
+ @profile.instance_variable_set(:@poll_count, 10)
265
+ @profile.instance_variable_set(:@sample_count, 2)
266
+
267
+ build_well_known_trace
268
+
269
+ expected = [[
270
+ -1,
271
+ 1350403938892.524,
272
+ 1350403939904.375,
273
+ 10,
274
+ WELL_KNOWN_TRACE_ENCODED,
275
+ 2,
276
+ 0
277
+ ]]
278
+
279
+ marshaller = NewRelic::Agent::NewRelicService::JsonMarshaller.new
280
+ assert_equal expected, @profile.to_collector_array(marshaller.default_encoder)
281
+ end
282
+
283
+ def test_to_collector_array_with_bad_values
284
+ @profile.instance_variable_set(:@profile_id, "-1")
285
+ @profile.instance_variable_set(:@start_time, "")
286
+ @profile.instance_variable_set(:@stop_time, nil)
287
+ @profile.instance_variable_set(:@poll_count, Rational(10, 1))
288
+ @profile.instance_variable_set(:@sample_count, nil)
289
+
290
+ build_well_known_trace
291
+
292
+ expected = [[
293
+ -1,
294
+ 0.0,
295
+ 0.0,
296
+ 10,
297
+ WELL_KNOWN_TRACE_ENCODED,
298
+ 0,
299
+ 0
300
+ ]]
301
+
302
+ marshaller = NewRelic::Agent::NewRelicService::JsonMarshaller.new
303
+ assert_equal expected, @profile.to_collector_array(marshaller.default_encoder)
304
+ end
305
+ end
306
+
307
+ end
308
+ end