ghazel-newrelic_rpm 3.1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (255) hide show
  1. data/CHANGELOG +525 -0
  2. data/LICENSE +37 -0
  3. data/README.rdoc +179 -0
  4. data/bin/mongrel_rpm +33 -0
  5. data/bin/newrelic +13 -0
  6. data/bin/newrelic_cmd +5 -0
  7. data/cert/cacert.pem +118 -0
  8. data/cert/oldsite.pem +28 -0
  9. data/cert/site.pem +27 -0
  10. data/install.rb +9 -0
  11. data/lib/conditional_vendored_dependency_detection.rb +3 -0
  12. data/lib/conditional_vendored_metric_parser.rb +5 -0
  13. data/lib/new_relic/agent.rb +423 -0
  14. data/lib/new_relic/agent/agent.rb +1040 -0
  15. data/lib/new_relic/agent/beacon_configuration.rb +61 -0
  16. data/lib/new_relic/agent/browser_monitoring.rb +83 -0
  17. data/lib/new_relic/agent/busy_calculator.rb +91 -0
  18. data/lib/new_relic/agent/chained_call.rb +13 -0
  19. data/lib/new_relic/agent/error_collector.rb +203 -0
  20. data/lib/new_relic/agent/instrumentation.rb +9 -0
  21. data/lib/new_relic/agent/instrumentation/active_merchant.rb +21 -0
  22. data/lib/new_relic/agent/instrumentation/acts_as_solr.rb +62 -0
  23. data/lib/new_relic/agent/instrumentation/authlogic.rb +13 -0
  24. data/lib/new_relic/agent/instrumentation/controller_instrumentation.rb +413 -0
  25. data/lib/new_relic/agent/instrumentation/data_mapper.rb +232 -0
  26. data/lib/new_relic/agent/instrumentation/delayed_job_instrumentation.rb +44 -0
  27. data/lib/new_relic/agent/instrumentation/memcache.rb +56 -0
  28. data/lib/new_relic/agent/instrumentation/merb/controller.rb +36 -0
  29. data/lib/new_relic/agent/instrumentation/merb/errors.rb +23 -0
  30. data/lib/new_relic/agent/instrumentation/metric_frame.rb +326 -0
  31. data/lib/new_relic/agent/instrumentation/metric_frame/pop.rb +84 -0
  32. data/lib/new_relic/agent/instrumentation/net.rb +23 -0
  33. data/lib/new_relic/agent/instrumentation/passenger_instrumentation.rb +34 -0
  34. data/lib/new_relic/agent/instrumentation/queue_time.rb +210 -0
  35. data/lib/new_relic/agent/instrumentation/rack.rb +98 -0
  36. data/lib/new_relic/agent/instrumentation/rails/action_controller.rb +83 -0
  37. data/lib/new_relic/agent/instrumentation/rails/action_web_service.rb +36 -0
  38. data/lib/new_relic/agent/instrumentation/rails/active_record_instrumentation.rb +108 -0
  39. data/lib/new_relic/agent/instrumentation/rails/errors.rb +36 -0
  40. data/lib/new_relic/agent/instrumentation/rails3/action_controller.rb +60 -0
  41. data/lib/new_relic/agent/instrumentation/rails3/active_record_instrumentation.rb +112 -0
  42. data/lib/new_relic/agent/instrumentation/rails3/errors.rb +31 -0
  43. data/lib/new_relic/agent/instrumentation/sinatra.rb +54 -0
  44. data/lib/new_relic/agent/instrumentation/sunspot.rb +23 -0
  45. data/lib/new_relic/agent/instrumentation/unicorn_instrumentation.rb +16 -0
  46. data/lib/new_relic/agent/method_tracer.rb +422 -0
  47. data/lib/new_relic/agent/sampler.rb +50 -0
  48. data/lib/new_relic/agent/samplers/cpu_sampler.rb +58 -0
  49. data/lib/new_relic/agent/samplers/delayed_job_lock_sampler.rb +40 -0
  50. data/lib/new_relic/agent/samplers/memory_sampler.rb +144 -0
  51. data/lib/new_relic/agent/samplers/object_sampler.rb +26 -0
  52. data/lib/new_relic/agent/shim_agent.rb +29 -0
  53. data/lib/new_relic/agent/stats_engine.rb +24 -0
  54. data/lib/new_relic/agent/stats_engine/metric_stats.rb +182 -0
  55. data/lib/new_relic/agent/stats_engine/samplers.rb +89 -0
  56. data/lib/new_relic/agent/stats_engine/transactions.rb +184 -0
  57. data/lib/new_relic/agent/transaction_sample_builder.rb +101 -0
  58. data/lib/new_relic/agent/transaction_sampler.rb +378 -0
  59. data/lib/new_relic/agent/worker_loop.rb +81 -0
  60. data/lib/new_relic/collection_helper.rb +69 -0
  61. data/lib/new_relic/command.rb +85 -0
  62. data/lib/new_relic/commands/deployments.rb +105 -0
  63. data/lib/new_relic/commands/install.rb +91 -0
  64. data/lib/new_relic/control.rb +48 -0
  65. data/lib/new_relic/control/class_methods.rb +47 -0
  66. data/lib/new_relic/control/configuration.rb +157 -0
  67. data/lib/new_relic/control/frameworks/external.rb +16 -0
  68. data/lib/new_relic/control/frameworks/merb.rb +30 -0
  69. data/lib/new_relic/control/frameworks/rails.rb +151 -0
  70. data/lib/new_relic/control/frameworks/rails3.rb +70 -0
  71. data/lib/new_relic/control/frameworks/ruby.rb +42 -0
  72. data/lib/new_relic/control/frameworks/sinatra.rb +23 -0
  73. data/lib/new_relic/control/instance_methods.rb +154 -0
  74. data/lib/new_relic/control/instrumentation.rb +81 -0
  75. data/lib/new_relic/control/logging_methods.rb +88 -0
  76. data/lib/new_relic/control/profiling.rb +25 -0
  77. data/lib/new_relic/control/server_methods.rb +104 -0
  78. data/lib/new_relic/data_serialization.rb +84 -0
  79. data/lib/new_relic/delayed_job_injection.rb +38 -0
  80. data/lib/new_relic/histogram.rb +91 -0
  81. data/lib/new_relic/local_environment.rb +391 -0
  82. data/lib/new_relic/merbtasks.rb +6 -0
  83. data/lib/new_relic/metric_data.rb +46 -0
  84. data/lib/new_relic/metric_spec.rb +73 -0
  85. data/lib/new_relic/metrics.rb +9 -0
  86. data/lib/new_relic/noticed_error.rb +24 -0
  87. data/lib/new_relic/rack/browser_monitoring.rb +68 -0
  88. data/lib/new_relic/rack/developer_mode.rb +267 -0
  89. data/lib/new_relic/rack/metric_app.rb +65 -0
  90. data/lib/new_relic/rack/mongrel_rpm.ru +28 -0
  91. data/lib/new_relic/rack/newrelic.yml +27 -0
  92. data/lib/new_relic/rack_app.rb +6 -0
  93. data/lib/new_relic/recipes.rb +73 -0
  94. data/lib/new_relic/stats.rb +378 -0
  95. data/lib/new_relic/timer_lib.rb +27 -0
  96. data/lib/new_relic/transaction_analysis.rb +76 -0
  97. data/lib/new_relic/transaction_analysis/segment_summary.rb +47 -0
  98. data/lib/new_relic/transaction_sample.rb +272 -0
  99. data/lib/new_relic/transaction_sample/composite_segment.rb +27 -0
  100. data/lib/new_relic/transaction_sample/fake_segment.rb +9 -0
  101. data/lib/new_relic/transaction_sample/segment.rb +251 -0
  102. data/lib/new_relic/transaction_sample/summary_segment.rb +21 -0
  103. data/lib/new_relic/url_rule.rb +14 -0
  104. data/lib/new_relic/version.rb +55 -0
  105. data/lib/newrelic_rpm.rb +49 -0
  106. data/lib/tasks/all.rb +4 -0
  107. data/lib/tasks/install.rake +7 -0
  108. data/lib/tasks/tests.rake +19 -0
  109. data/newrelic.yml +265 -0
  110. data/newrelic_rpm.gemspec +330 -0
  111. data/recipes/newrelic.rb +6 -0
  112. data/test/active_record_fixtures.rb +77 -0
  113. data/test/config/newrelic.yml +48 -0
  114. data/test/config/test_control.rb +48 -0
  115. data/test/new_relic/agent/agent/connect_test.rb +316 -0
  116. data/test/new_relic/agent/agent/start_test.rb +340 -0
  117. data/test/new_relic/agent/agent/start_worker_thread_test.rb +155 -0
  118. data/test/new_relic/agent/agent_test.rb +173 -0
  119. data/test/new_relic/agent/agent_test_controller.rb +77 -0
  120. data/test/new_relic/agent/agent_test_controller_test.rb +366 -0
  121. data/test/new_relic/agent/apdex_from_server_test.rb +9 -0
  122. data/test/new_relic/agent/beacon_configuration_test.rb +104 -0
  123. data/test/new_relic/agent/browser_monitoring_test.rb +274 -0
  124. data/test/new_relic/agent/busy_calculator_test.rb +81 -0
  125. data/test/new_relic/agent/error_collector/notice_error_test.rb +257 -0
  126. data/test/new_relic/agent/error_collector_test.rb +168 -0
  127. data/test/new_relic/agent/instrumentation/active_record_instrumentation_test.rb +522 -0
  128. data/test/new_relic/agent/instrumentation/controller_instrumentation_test.rb +36 -0
  129. data/test/new_relic/agent/instrumentation/instrumentation_test.rb +11 -0
  130. data/test/new_relic/agent/instrumentation/metric_frame/pop_test.rb +179 -0
  131. data/test/new_relic/agent/instrumentation/metric_frame_test.rb +50 -0
  132. data/test/new_relic/agent/instrumentation/net_instrumentation_test.rb +84 -0
  133. data/test/new_relic/agent/instrumentation/queue_time_test.rb +387 -0
  134. data/test/new_relic/agent/instrumentation/rack_test.rb +35 -0
  135. data/test/new_relic/agent/instrumentation/task_instrumentation_test.rb +184 -0
  136. data/test/new_relic/agent/memcache_instrumentation_test.rb +107 -0
  137. data/test/new_relic/agent/method_tracer/class_methods/add_method_tracer_test.rb +164 -0
  138. data/test/new_relic/agent/method_tracer/instance_methods/trace_execution_scoped_test.rb +234 -0
  139. data/test/new_relic/agent/method_tracer_test.rb +386 -0
  140. data/test/new_relic/agent/mock_scope_listener.rb +23 -0
  141. data/test/new_relic/agent/rpm_agent_test.rb +149 -0
  142. data/test/new_relic/agent/sampler_test.rb +19 -0
  143. data/test/new_relic/agent/shim_agent_test.rb +20 -0
  144. data/test/new_relic/agent/stats_engine/metric_stats/harvest_test.rb +150 -0
  145. data/test/new_relic/agent/stats_engine/metric_stats_test.rb +81 -0
  146. data/test/new_relic/agent/stats_engine/samplers_test.rb +99 -0
  147. data/test/new_relic/agent/stats_engine_test.rb +185 -0
  148. data/test/new_relic/agent/transaction_sample_builder_test.rb +195 -0
  149. data/test/new_relic/agent/transaction_sampler_test.rb +945 -0
  150. data/test/new_relic/agent/worker_loop_test.rb +66 -0
  151. data/test/new_relic/collection_helper_test.rb +127 -0
  152. data/test/new_relic/command/deployments_test.rb +68 -0
  153. data/test/new_relic/control/class_methods_test.rb +62 -0
  154. data/test/new_relic/control/logging_methods_test.rb +157 -0
  155. data/test/new_relic/control_test.rb +195 -0
  156. data/test/new_relic/data_serialization_test.rb +70 -0
  157. data/test/new_relic/local_environment_test.rb +72 -0
  158. data/test/new_relic/metric_data_test.rb +125 -0
  159. data/test/new_relic/metric_spec_test.rb +95 -0
  160. data/test/new_relic/rack/all_test.rb +11 -0
  161. data/test/new_relic/rack/browser_monitoring_test.rb +84 -0
  162. data/test/new_relic/rack/developer_mode_test.rb +43 -0
  163. data/test/new_relic/stats_test.rb +426 -0
  164. data/test/new_relic/transaction_analysis/segment_summary_test.rb +77 -0
  165. data/test/new_relic/transaction_analysis_test.rb +121 -0
  166. data/test/new_relic/transaction_sample/composite_segment_test.rb +35 -0
  167. data/test/new_relic/transaction_sample/fake_segment_test.rb +17 -0
  168. data/test/new_relic/transaction_sample/segment_test.rb +454 -0
  169. data/test/new_relic/transaction_sample/summary_segment_test.rb +31 -0
  170. data/test/new_relic/transaction_sample_subtest_test.rb +56 -0
  171. data/test/new_relic/transaction_sample_test.rb +170 -0
  172. data/test/new_relic/version_number_test.rb +89 -0
  173. data/test/test_contexts.rb +29 -0
  174. data/test/test_helper.rb +144 -0
  175. data/ui/helpers/developer_mode_helper.rb +351 -0
  176. data/ui/helpers/google_pie_chart.rb +49 -0
  177. data/ui/views/layouts/newrelic_default.rhtml +47 -0
  178. data/ui/views/newrelic/_explain_plans.rhtml +27 -0
  179. data/ui/views/newrelic/_sample.rhtml +20 -0
  180. data/ui/views/newrelic/_segment.rhtml +28 -0
  181. data/ui/views/newrelic/_segment_limit_message.rhtml +1 -0
  182. data/ui/views/newrelic/_segment_row.rhtml +12 -0
  183. data/ui/views/newrelic/_show_sample_detail.rhtml +24 -0
  184. data/ui/views/newrelic/_show_sample_sql.rhtml +24 -0
  185. data/ui/views/newrelic/_show_sample_summary.rhtml +3 -0
  186. data/ui/views/newrelic/_sql_row.rhtml +16 -0
  187. data/ui/views/newrelic/_stack_trace.rhtml +15 -0
  188. data/ui/views/newrelic/_table.rhtml +12 -0
  189. data/ui/views/newrelic/explain_sql.rhtml +43 -0
  190. data/ui/views/newrelic/file/images/arrow-close.png +0 -0
  191. data/ui/views/newrelic/file/images/arrow-open.png +0 -0
  192. data/ui/views/newrelic/file/images/blue_bar.gif +0 -0
  193. data/ui/views/newrelic/file/images/file_icon.png +0 -0
  194. data/ui/views/newrelic/file/images/gray_bar.gif +0 -0
  195. data/ui/views/newrelic/file/images/new-relic-rpm-desktop.gif +0 -0
  196. data/ui/views/newrelic/file/images/new_relic_rpm_desktop.gif +0 -0
  197. data/ui/views/newrelic/file/images/textmate.png +0 -0
  198. data/ui/views/newrelic/file/javascript/jquery-1.4.2.js +6240 -0
  199. data/ui/views/newrelic/file/javascript/transaction_sample.js +120 -0
  200. data/ui/views/newrelic/file/stylesheets/style.css +490 -0
  201. data/ui/views/newrelic/index.rhtml +71 -0
  202. data/ui/views/newrelic/sample_not_found.rhtml +2 -0
  203. data/ui/views/newrelic/show_sample.rhtml +80 -0
  204. data/ui/views/newrelic/show_source.rhtml +3 -0
  205. data/ui/views/newrelic/threads.rhtml +53 -0
  206. data/vendor/gems/dependency_detection-0.0.1.build/LICENSE +19 -0
  207. data/vendor/gems/dependency_detection-0.0.1.build/README +0 -0
  208. data/vendor/gems/dependency_detection-0.0.1.build/lib/dependency_detection.rb +57 -0
  209. data/vendor/gems/dependency_detection-0.0.1.build/lib/dependency_detection/version.rb +3 -0
  210. data/vendor/gems/metric_parser-0.1.0.pre1/LICENSE +0 -0
  211. data/vendor/gems/metric_parser-0.1.0.pre1/README +0 -0
  212. data/vendor/gems/metric_parser-0.1.0.pre1/lib/metric_parser.rb +1 -0
  213. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser.rb +64 -0
  214. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/action_mailer.rb +14 -0
  215. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/active_merchant.rb +31 -0
  216. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/active_record.rb +33 -0
  217. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/apdex.rb +89 -0
  218. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/background_transaction.rb +7 -0
  219. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/client.rb +46 -0
  220. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/controller.rb +67 -0
  221. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/controller_cpu.rb +43 -0
  222. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/controller_ext.rb +17 -0
  223. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/database.rb +48 -0
  224. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/database_pool.rb +24 -0
  225. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/dot_net.rb +28 -0
  226. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/dot_net_parser.rb +17 -0
  227. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/errors.rb +11 -0
  228. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/external.rb +55 -0
  229. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/frontend.rb +40 -0
  230. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/gc.rb +20 -0
  231. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/hibernate_session.rb +7 -0
  232. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/java.rb +31 -0
  233. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/java_parser.rb +17 -0
  234. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/jsp.rb +34 -0
  235. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/jsp_tag.rb +7 -0
  236. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/mem_cache.rb +55 -0
  237. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/metric_parser.rb +122 -0
  238. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/orm.rb +27 -0
  239. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/other_transaction.rb +40 -0
  240. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/servlet.rb +7 -0
  241. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/servlet_context_listener.rb +7 -0
  242. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/servlet_filter.rb +7 -0
  243. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/solr.rb +27 -0
  244. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/solr_request_handler.rb +15 -0
  245. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/spring.rb +54 -0
  246. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/spring_controller.rb +6 -0
  247. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/spring_view.rb +6 -0
  248. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/struts_action.rb +20 -0
  249. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/struts_result.rb +20 -0
  250. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/version.rb +5 -0
  251. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/view.rb +66 -0
  252. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/web_frontend.rb +18 -0
  253. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/web_service.rb +14 -0
  254. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/web_transaction.rb +133 -0
  255. metadata +399 -0
@@ -0,0 +1,77 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__),'..', '..','test_helper'))
2
+ require 'new_relic/transaction_analysis/segment_summary'
3
+ class NewRelic::TransactionAnalysis::SegmentSummaryTest < Test::Unit::TestCase
4
+
5
+ def setup
6
+ @sample = mock('sample')
7
+ @ss = NewRelic::TransactionAnalysis::SegmentSummary.new('Controller/foo', @sample)
8
+ end
9
+
10
+ # these are mostly stub tests just making sure that the API doesn't
11
+ # change if anyone ever needs to modify it.
12
+
13
+ def test_insert
14
+ segment = mock('segment')
15
+ segment.expects(:metric_name).returns('Controller/foo')
16
+ segment.expects(:duration).returns(0.1)
17
+ segment.expects(:exclusive_duration).returns(0.1)
18
+ @ss << segment
19
+ assert_equal 0.1, @ss.total_time
20
+ assert_equal 0.1, @ss.exclusive_time
21
+ assert_equal 1, @ss.call_count
22
+ end
23
+
24
+ def test_insert_error
25
+ segment = mock('segment')
26
+ segment.expects(:metric_name).returns('Controller/bar').twice
27
+ assert_raise(ArgumentError) do
28
+ @ss << segment
29
+ end
30
+ end
31
+
32
+ def test_average_time
33
+ @ss.total_time = 0.1
34
+ @ss.call_count = 2
35
+ assert_equal 0.05, @ss.average_time
36
+ end
37
+
38
+ def test_average_exclusive_time
39
+ @ss.exclusive_time = 0.1
40
+ @ss.call_count = 2
41
+ assert_equal 0.05, @ss.average_exclusive_time
42
+ end
43
+
44
+ def test_exclusive_time_percentage_nil
45
+ @ss.exclusive_time = nil
46
+ assert_equal 0, @ss.exclusive_time_percentage
47
+ end
48
+
49
+ def test_exclusive_time_percentage
50
+ @ss.exclusive_time = 0.05
51
+ @sample.expects(:duration).returns(0.1).times(3)
52
+ assert_equal 0.5, @ss.exclusive_time_percentage
53
+ end
54
+
55
+ def test_total_time_percentage_nil
56
+ @ss.total_time = nil
57
+ assert_equal 0, @ss.total_time_percentage
58
+ end
59
+
60
+ def test_total_time_percentage
61
+ @ss.total_time = 0.05
62
+ @sample.expects(:duration).returns(0.1).times(3)
63
+ assert_equal 0.5, @ss.total_time_percentage
64
+ end
65
+
66
+ def test_ui_name_default
67
+ @ss.metric_name = 'Remainder'
68
+ assert_equal 'Remainder', @ss.ui_name
69
+ end
70
+
71
+ def test_ui_name_lookup
72
+ mocked_object = mock('metric parser obj')
73
+ mocked_object.expects(:developer_name).returns('Developer Name')
74
+ NewRelic::MetricParser::MetricParser.expects(:parse).with('Controller/foo').returns(mocked_object)
75
+ assert_equal 'Developer Name', @ss.ui_name
76
+ end
77
+ end
@@ -0,0 +1,121 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__),'..', 'test_helper'))
2
+ require 'new_relic/transaction_analysis'
3
+ class NewRelic::TransactionAnalysisTest < Test::Unit::TestCase
4
+ include NewRelic::TransactionAnalysis
5
+
6
+ # these are mostly stub tests just making sure that the API doesn't
7
+ # change if anyone ever needs to modify it.
8
+
9
+ def test_database_time
10
+ self.expects(:time_percentage).with(/^Database\/.*/)
11
+ database_time
12
+ end
13
+
14
+ def test_render_time
15
+ self.expects(:time_percentage).with(/^View\/.*/)
16
+ render_time
17
+ end
18
+
19
+ def test_breakdown_data_default
20
+ root_segment = mock('root_segment')
21
+ # this is for 1.9 compatibility - calling each on something calls
22
+ # #to_a on it - which is fun and exciting
23
+ root_segment.stubs(:to_a).returns([root_segment])
24
+ other_segment = mock('other_segment')
25
+ # this is for 1.9 compatibility - calling each on something calls
26
+ # #to_a on it - which is fun and exciting
27
+ other_segment.stubs(:to_a).returns([other_segment])
28
+ other_segment.expects(:metric_name).twice.returns('Controller/foo')
29
+ other_segment.expects(:duration).returns(0.1)
30
+ other_segment.expects(:exclusive_duration).returns(0.1)
31
+ self.expects(:each_segment).multiple_yields(root_segment, other_segment)
32
+ self.expects(:root_segment).twice.returns(root_segment)
33
+ self.expects(:duration).returns(0.1)
34
+ data = breakdown_data
35
+ assert_equal 'Controller/foo', data[0].metric_name
36
+ end
37
+
38
+ # kind of a hairy test, we're making sure that the data is truncated
39
+ # to one element by the limit
40
+ def test_breakdown_data_limit_one
41
+ root_segment = mock('root_segment')
42
+ # this is for 1.9 compatibility - calling each on something calls
43
+ # #to_a on it - which is fun and exciting
44
+ root_segment.stubs(:to_a).returns([root_segment])
45
+ other_segment = mock('other_segment')
46
+ # this is for 1.9 compatibility - calling each on something calls
47
+ # #to_a on it - which is fun and exciting
48
+ other_segment.stubs(:to_a).returns([other_segment])
49
+ other_segment.expects(:metric_name).twice.returns('Controller/foo')
50
+ other_segment.expects(:duration).returns(0.1)
51
+ other_segment.expects(:exclusive_duration).returns(0.1)
52
+ yet_another = mock('another segment')
53
+ # this is for 1.9 compatibility - calling each on something calls
54
+ # #to_a on it - which is fun and exciting
55
+ yet_another.stubs(:to_a).returns([yet_another])
56
+ yet_another.expects(:metric_name).twice.returns('Controller/bar')
57
+ yet_another.expects(:duration).returns(0.2)
58
+ yet_another.expects(:exclusive_duration).returns(0.2)
59
+ self.expects(:each_segment).multiple_yields(root_segment, other_segment, yet_another)
60
+ self.expects(:root_segment).times(3).returns(root_segment)
61
+ self.expects(:duration).returns(0.1)
62
+ data = breakdown_data(1)
63
+ assert_equal 1, data.size
64
+ assert_equal 'Controller/bar', data[0].metric_name
65
+ end
66
+
67
+ def test_breakdown_data_remainder
68
+ root_segment = mock('root_segment')
69
+ # this is for 1.9 compatibility - calling each on something calls
70
+ # #to_a on it - which is fun and exciting
71
+ root_segment.stubs(:to_a).returns([root_segment])
72
+ other_segment = mock('other_segment')
73
+ # this is for 1.9 compatibility - calling each on something calls
74
+ # #to_a on it - which is fun and exciting
75
+ other_segment.stubs(:to_a).returns([other_segment])
76
+ other_segment.expects(:metric_name).twice.returns('Controller/foo')
77
+ other_segment.expects(:duration).returns(0.1)
78
+ other_segment.expects(:exclusive_duration).returns(0.1)
79
+ self.expects(:each_segment).multiple_yields(root_segment, other_segment)
80
+ self.expects(:root_segment).twice.returns(root_segment)
81
+ self.expects(:duration).returns(0.2)
82
+ data = breakdown_data
83
+ assert_equal 2, data.size
84
+ assert_equal 'Controller/foo', data[0].metric_name
85
+ assert_equal 'Remainder', data[1].metric_name
86
+ end
87
+
88
+ def test_sql_segments_default
89
+ root_segment = mock('root_segment') # a segment with no data
90
+ root_segment.expects(:[]).with(:sql).returns(false)
91
+ root_segment.expects(:[]).with(:sql_obfuscated).returns(false)
92
+ root_segment.expects(:[]).with(:key).returns(nil)
93
+ # this is for 1.9 compatibility - calling each on something calls
94
+ # #to_a on it - which is fun and exciting
95
+ root_segment.stubs(:to_a).returns([root_segment])
96
+ other_segment = mock('other_segment') # a sql segment
97
+ other_segment.expects(:[]).with(:sql).returns(true)
98
+ # this is for 1.9 compatibility - calling each on something calls
99
+ # #to_a on it - which is fun and exciting
100
+ other_segment.stubs(:to_a).returns([other_segment])
101
+ self.expects(:each_segment).multiple_yields(root_segment, other_segment)
102
+ assert_equal [other_segment], sql_segments
103
+ end
104
+
105
+ def test_time_percentage_default
106
+ root_segment = mock('root_segment')
107
+ root_segment.expects(:metric_name).returns('ROOT')
108
+ # this is for 1.9 compatibility - calling each on something calls
109
+ # #to_a on it - which is fun and exciting
110
+ root_segment.stubs(:to_a).returns([root_segment])
111
+ other_segment = mock('other_segment')
112
+ other_segment.expects(:metric_name).returns('Controller/foo')
113
+ other_segment.expects(:duration).returns(0.1)
114
+ # this is for 1.9 compatibility - calling each on something calls
115
+ # #to_a on it - which is fun and exciting
116
+ other_segment.stubs(:to_a).returns([other_segment])
117
+ self.expects(:duration).returns(0.2)
118
+ self.expects(:each_segment).multiple_yields(root_segment, other_segment)
119
+ assert_equal 50.0, time_percentage(/Controller\/.*/)
120
+ end
121
+ end
@@ -0,0 +1,35 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'test_helper'))
2
+ require 'new_relic/transaction_sample/composite_segment'
3
+ class NewRelic::TransactionSample::CompositeSegmentTest < Test::Unit::TestCase
4
+ def test_composite_segment_creation
5
+ fake_segment = mock_segment
6
+ NewRelic::TransactionSample::CompositeSegment.new([fake_segment])
7
+ end
8
+
9
+ def test_detail_segments_equals
10
+ fake_segment = mock_segment
11
+ cs = NewRelic::TransactionSample::CompositeSegment.new([fake_segment])
12
+
13
+ # note that this is a bare mock
14
+ # nothing should be called on it, for now
15
+ other_fake_segment = mock('other segment')
16
+ cs.detail_segments = [other_fake_segment]
17
+
18
+ assert_equal cs.detail_segments, [other_fake_segment]
19
+ end
20
+
21
+ private
22
+
23
+ @@seg_count = 0
24
+ def mock_segment
25
+ @@seg_count += 1
26
+ segment = mock('segment ' + @@seg_count.to_s)
27
+ segment.expects(:entry_timestamp).returns(Time.now)
28
+ # note the following 'twice' - different than SummarySegment
29
+ segment.expects(:exit_timestamp).returns(Time.now).twice
30
+ segment.expects(:metric_name).returns('Custom/test/metric')
31
+ segment.expects(:called_segments).returns([])
32
+ segment
33
+ end
34
+ end
35
+
@@ -0,0 +1,17 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'test_helper'))
2
+ require 'new_relic/transaction_sample/fake_segment'
3
+ class NewRelic::TransactionSample::FakeSegmentTest < Test::Unit::TestCase
4
+ def test_fake_segment_creation
5
+ assert_nothing_raised do
6
+ NewRelic::TransactionSample::FakeSegment.new(0.1, 'Custom/test/metric', nil)
7
+ end
8
+ end
9
+
10
+ def test_parent_segment
11
+ # should be public in this class, but not in the parent class
12
+ s = NewRelic::TransactionSample::FakeSegment.new(0.1, 'Custom/test/metric', nil)
13
+ s.parent_segment = 'foo'
14
+ assert_equal('foo', s.instance_eval { @parent_segment } )
15
+ end
16
+ end
17
+
@@ -0,0 +1,454 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'test_helper'))
2
+ require 'new_relic/transaction_sample/segment'
3
+ class NewRelic::TransactionSample::SegmentTest < Test::Unit::TestCase
4
+ def test_segment_creation
5
+ # basic smoke test
6
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
7
+ assert_equal NewRelic::TransactionSample::Segment, s.class
8
+ end
9
+
10
+ def test_readers
11
+ t = Time.now
12
+ s = NewRelic::TransactionSample::Segment.new(t, 'Custom/test/metric', nil)
13
+ assert_equal(t, s.entry_timestamp)
14
+ assert_equal(nil, s.exit_timestamp)
15
+ assert_equal(nil, s.parent_segment)
16
+ assert_equal('Custom/test/metric', s.metric_name)
17
+ assert_equal(s.object_id, s.segment_id)
18
+ end
19
+
20
+ def test_end_trace
21
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
22
+ t = Time.now
23
+ s.end_trace(t)
24
+ assert_equal(t, s.exit_timestamp)
25
+ end
26
+
27
+ def test_add_called_segment
28
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
29
+ assert_equal [], s.called_segments
30
+ fake_segment = mock('segment')
31
+ fake_segment.expects(:parent_segment=).with(s)
32
+ s.add_called_segment(fake_segment)
33
+ assert_equal([fake_segment], s.called_segments)
34
+ end
35
+
36
+ def test_to_s
37
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
38
+ s.expects(:to_debug_str).with(0)
39
+ s.to_s
40
+ end
41
+
42
+ def test_to_json
43
+ t = Time.now
44
+ s = NewRelic::TransactionSample::Segment.new(t, 'Custom/test/metric', nil)
45
+ assert_equal({ :entry_timestamp => t, :exit_timestamp => nil, :metric_name => 'Custom/test/metric', :segment_id => s.object_id }.to_json, s.to_json)
46
+ end
47
+
48
+ def test_path_string
49
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
50
+ assert_equal("Custom/test/metric[]", s.path_string)
51
+
52
+ fake_segment = mock('segment')
53
+ fake_segment.expects(:parent_segment=).with(s)
54
+ fake_segment.expects(:path_string).returns('Custom/other/metric[]')
55
+
56
+
57
+ s.add_called_segment(fake_segment)
58
+ assert_equal("Custom/test/metric[Custom/other/metric[]]", s.path_string)
59
+ end
60
+
61
+ def test_to_s_compact
62
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
63
+ assert_equal("Custom/test/metric", s.to_s_compact)
64
+
65
+ fake_segment = mock('segment')
66
+ fake_segment.expects(:parent_segment=).with(s)
67
+ fake_segment.expects(:to_s_compact).returns('Custom/other/metric')
68
+ s.add_called_segment(fake_segment)
69
+
70
+ assert_equal("Custom/test/metric{Custom/other/metric}", s.to_s_compact)
71
+ end
72
+
73
+ def test_to_debug_str_basic
74
+ s = NewRelic::TransactionSample::Segment.new(0.0, 'Custom/test/metric', nil)
75
+ assert_equal(">> 0 ms [Segment] Custom/test/metric \n<< n/a Custom/test/metric\n", s.to_debug_str(0))
76
+ end
77
+
78
+ def test_to_debug_str_with_params
79
+ s = NewRelic::TransactionSample::Segment.new(0.0, 'Custom/test/metric', nil)
80
+ s.params = {:whee => 'a param'}
81
+ assert_equal(">> 0 ms [Segment] Custom/test/metric \n -whee : a param\n<< n/a Custom/test/metric\n", s.to_debug_str(0))
82
+ end
83
+
84
+ def test_to_debug_str_closed
85
+ s = NewRelic::TransactionSample::Segment.new(0.0, 'Custom/test/metric', nil)
86
+ s.end_trace(0.1)
87
+ assert_equal(">> 0 ms [Segment] Custom/test/metric \n<< 100 ms Custom/test/metric\n", s.to_debug_str(0))
88
+ end
89
+
90
+ def test_to_debug_str_closed_with_nonnumeric
91
+ s = NewRelic::TransactionSample::Segment.new(0.0, 'Custom/test/metric', nil)
92
+ s.end_trace("0.1")
93
+ assert_equal(">> 0 ms [Segment] Custom/test/metric \n<< 0.1 Custom/test/metric\n", s.to_debug_str(0))
94
+ end
95
+
96
+ def test_to_debug_str_one_child
97
+ s = NewRelic::TransactionSample::Segment.new(0.0, 'Custom/test/metric', nil)
98
+ s.add_called_segment(NewRelic::TransactionSample::Segment.new(0.1, 'Custom/test/other', nil))
99
+ assert_equal(">> 0 ms [Segment] Custom/test/metric \n >> 100 ms [Segment] Custom/test/other \n << n/a Custom/test/other\n<< n/a Custom/test/metric\n", s.to_debug_str(0))
100
+ # try closing it
101
+ s.called_segments.first.end_trace(0.15)
102
+ s.end_trace(0.2)
103
+ assert_equal(">> 0 ms [Segment] Custom/test/metric \n >> 100 ms [Segment] Custom/test/other \n << 150 ms Custom/test/other\n<< 200 ms Custom/test/metric\n", s.to_debug_str(0))
104
+ end
105
+
106
+ def test_to_debug_str_multichild
107
+ s = NewRelic::TransactionSample::Segment.new(0.0, 'Custom/test/metric', nil)
108
+ s.add_called_segment(NewRelic::TransactionSample::Segment.new(0.1, 'Custom/test/other', nil))
109
+ s.add_called_segment(NewRelic::TransactionSample::Segment.new(0.11, 'Custom/test/extra', nil))
110
+ assert_equal(">> 0 ms [Segment] Custom/test/metric \n >> 100 ms [Segment] Custom/test/other \n << n/a Custom/test/other\n >> 110 ms [Segment] Custom/test/extra \n << n/a Custom/test/extra\n<< n/a Custom/test/metric\n", s.to_debug_str(0))
111
+ ending = 0.12
112
+ s.called_segments.each { |x| x.end_trace(ending += 0.01) }
113
+ s.end_trace(0.2)
114
+ assert_equal(">> 0 ms [Segment] Custom/test/metric \n >> 100 ms [Segment] Custom/test/other \n << 130 ms Custom/test/other\n >> 110 ms [Segment] Custom/test/extra \n << 140 ms Custom/test/extra\n<< 200 ms Custom/test/metric\n", s.to_debug_str(0))
115
+ end
116
+
117
+ def test_to_debug_str_nested
118
+ inner = NewRelic::TransactionSample::Segment.new(0.2, 'Custom/test/inner', nil)
119
+ middle = NewRelic::TransactionSample::Segment.new(0.1, 'Custom/test/middle', nil)
120
+ s = NewRelic::TransactionSample::Segment.new(0.0, 'Custom/test/metric', nil)
121
+ middle.add_called_segment(inner)
122
+ s.add_called_segment(middle)
123
+ assert_equal(">> 0 ms [Segment] Custom/test/metric \n >> 100 ms [Segment] Custom/test/middle \n >> 200 ms [Segment] Custom/test/inner \n << n/a Custom/test/inner\n << n/a Custom/test/middle\n<< n/a Custom/test/metric\n", s.to_debug_str(0))
124
+
125
+ # close them
126
+ inner.end_trace(0.21)
127
+ middle.end_trace(0.22)
128
+ s.end_trace(0.23)
129
+ assert_equal(">> 0 ms [Segment] Custom/test/metric \n >> 100 ms [Segment] Custom/test/middle \n >> 200 ms [Segment] Custom/test/inner \n << 210 ms Custom/test/inner\n << 220 ms Custom/test/middle\n<< 230 ms Custom/test/metric\n", s.to_debug_str(0))
130
+ end
131
+
132
+ def test_called_segments_default
133
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
134
+ assert_equal([], s.called_segments)
135
+ end
136
+
137
+ def test_called_segments_with_segments
138
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
139
+ fake_segment = mock('segment')
140
+ fake_segment.expects(:parent_segment=).with(s)
141
+ s.add_called_segment(fake_segment)
142
+
143
+ assert_equal([fake_segment], s.called_segments)
144
+ end
145
+
146
+ def test_duration
147
+ fake_entry_timestamp = mock('entry timestamp')
148
+ fake_exit_timestamp = mock('exit timestamp')
149
+ fake_result = mock('numeric')
150
+ fake_exit_timestamp.expects(:-).with(fake_entry_timestamp).returns(fake_result)
151
+ fake_result.expects(:to_f).returns(0.5)
152
+
153
+ s = NewRelic::TransactionSample::Segment.new(fake_entry_timestamp, 'Custom/test/metric', nil)
154
+ s.end_trace(fake_exit_timestamp)
155
+ assert_equal(0.5, s.duration)
156
+ end
157
+
158
+ def test_exclusive_duration_no_children
159
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
160
+ s.expects(:duration).returns(0.5)
161
+ assert_equal(0.5, s.exclusive_duration)
162
+ end
163
+
164
+ def test_exclusive_duration_with_children
165
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
166
+
167
+ s.expects(:duration).returns(0.5)
168
+
169
+ fake_segment = mock('segment')
170
+ fake_segment.expects(:parent_segment=).with(s)
171
+ fake_segment.expects(:duration).returns(0.1)
172
+
173
+ s.add_called_segment(fake_segment)
174
+
175
+ assert_equal(0.4, s.exclusive_duration)
176
+ end
177
+
178
+ def test_count_segments_default
179
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
180
+ assert_equal(1, s.count_segments)
181
+ end
182
+
183
+ def test_count_segments_with_children
184
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
185
+
186
+ fake_segment = mock('segment')
187
+ fake_segment.expects(:parent_segment=).with(s)
188
+ fake_segment.expects(:count_segments).returns(1)
189
+
190
+ s.add_called_segment(fake_segment)
191
+
192
+ assert_equal(2, s.count_segments)
193
+ end
194
+
195
+ def test_truncate_returns_number_of_elements
196
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
197
+ assert_equal(1, s.truncate(1))
198
+ dup = s.dup
199
+ s.called_segments = [dup]
200
+ assert_equal(2, s.truncate(2))
201
+
202
+ s.called_segments = [dup, dup]
203
+ assert_equal(3, s.truncate(3))
204
+ end
205
+
206
+
207
+ def test_truncate_default
208
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
209
+
210
+ assert_equal(1, s.truncate(1))
211
+ end
212
+
213
+ def test_truncate_with_a_child
214
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
215
+
216
+ fake_segment = mock('segment')
217
+ fake_segment.expects(:parent_segment=).with(s)
218
+ fake_segment.expects(:truncate).with(1).returns(1)
219
+
220
+ s.add_called_segment(fake_segment)
221
+
222
+ assert_equal(2, s.truncate(2))
223
+ assert_equal([fake_segment], s.called_segments)
224
+
225
+ assert_equal(1, s.truncate(1))
226
+ assert_equal([], s.called_segments)
227
+ end
228
+
229
+ def test_truncate_with_multiple_children
230
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
231
+
232
+ fake_segment = mock('segment')
233
+ fake_segment.expects(:truncate).with(2).returns(1)
234
+
235
+ other_segment = mock('other segment')
236
+ other_segment.expects(:truncate).with(1).returns(1)
237
+
238
+ s.called_segments = [fake_segment, other_segment]
239
+ assert_equal(3, s.truncate(3))
240
+ assert_equal([fake_segment, other_segment], s.called_segments)
241
+ end
242
+
243
+ def test_truncate_removes_elements
244
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
245
+
246
+ fake_segment = mock('segment')
247
+ fake_segment.expects(:truncate).with(1).returns(1)
248
+
249
+ other_segment = mock('other segment')
250
+
251
+ s.called_segments = [fake_segment, other_segment]
252
+ assert_equal(2, s.truncate(2))
253
+ assert_equal([fake_segment], s.called_segments)
254
+ end
255
+
256
+ def test_key_equals
257
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
258
+ # doing this to hold the reference to the hash
259
+ params = {}
260
+ s.params = params
261
+ assert_equal(params, s.params)
262
+
263
+ # should delegate to the same hash we have above
264
+ s[:foo] = 'correct'
265
+
266
+ assert_equal('correct', params[:foo])
267
+ end
268
+
269
+ def test_key
270
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
271
+ s.params = {:foo => 'correct'}
272
+ assert_equal('correct', s[:foo])
273
+ end
274
+
275
+ def test_params
276
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
277
+
278
+ # should have a default value
279
+ assert_equal(nil, s.instance_eval { @params })
280
+ assert_equal({}, s.params)
281
+
282
+ # should otherwise take the value from the @params var
283
+ s.instance_eval { @params = {:foo => 'correct'} }
284
+ assert_equal({:foo => 'correct'}, s.params)
285
+ end
286
+
287
+ def test_each_segment_default
288
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
289
+ # in the base case it just yields the block to itself
290
+ count = 0
291
+ s.each_segment do |x|
292
+ count += 1
293
+ assert_equal(s, x)
294
+ end
295
+ # should only run once
296
+ assert_equal(1, count)
297
+ end
298
+
299
+ def test_each_segment_with_children
300
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
301
+
302
+ fake_segment = mock('segment')
303
+ fake_segment.expects(:parent_segment=).with(s)
304
+ fake_segment.expects(:each_segment).yields(fake_segment)
305
+
306
+ s.add_called_segment(fake_segment)
307
+
308
+ count = 0
309
+ s.each_segment do |x|
310
+ count += 1
311
+ end
312
+
313
+ assert_equal(2, count)
314
+ end
315
+
316
+ def test_find_segment_default
317
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
318
+ id_to_find = s.segment_id
319
+ # should return itself in the base case
320
+ assert_equal(s, s.find_segment(id_to_find))
321
+ end
322
+
323
+ def test_find_segment_not_found
324
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
325
+ assert_equal(nil, s.find_segment(-1))
326
+ end
327
+
328
+ def test_find_segment_with_children
329
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
330
+ id_to_find = s.segment_id
331
+ # should return itself in the base case
332
+ assert_equal(s, s.find_segment(id_to_find))
333
+ end
334
+
335
+ def test_explain_sql_no_sql
336
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
337
+ s.params = {:sql => nil}
338
+ assert_equal(nil, s.explain_sql)
339
+ end
340
+
341
+ def test_explain_sql_no_connection_config
342
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
343
+ s.params = {:sql => 'foo', :connection_config => nil}
344
+ assert_equal(nil, s.explain_sql)
345
+ end
346
+
347
+ def test_explain_sql_non_select
348
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
349
+ s.params = {:sql => 'foo', :connection_config => mock('config')}
350
+ assert_equal([], s.explain_sql)
351
+ end
352
+
353
+ def test_explain_sql_one_select_no_connection
354
+ # NB this test raises an error in the log, much as it might if a
355
+ # user supplied a config that was not valid. This is generally
356
+ # expected behavior - the get_connection method shouldn't allow
357
+ # errors to percolate up.
358
+ config = mock('config')
359
+ config.stubs(:[]).returns(nil)
360
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
361
+ s.params = {:sql => 'SELECT', :connection_config => config}
362
+ assert_equal([], s.explain_sql)
363
+ end
364
+
365
+ def test_explain_sql_one_select_with_connection
366
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
367
+ config = mock('config')
368
+ s.params = {:sql => 'SELECT', :connection_config => config}
369
+ connection = mock('connection')
370
+ # two rows, two columns
371
+ connection.expects(:execute).with('EXPLAIN SELECT').returns([["string", "string"], ["string", "string"]])
372
+ NewRelic::TransactionSample.expects(:get_connection).with(config).returns(connection)
373
+ assert_equal([[['string', 'string'], ['string', 'string']]], s.explain_sql)
374
+ end
375
+
376
+ # this basically casts the resultset to an array of rows, which are
377
+ # arrays of columns
378
+ def test_process_resultset
379
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
380
+ items = mock('bar')
381
+ row = ["column"]
382
+ items.expects(:respond_to?).with(:each).returns(true)
383
+ items.expects(:each).yields(row)
384
+ assert_equal([["column"]], s.process_resultset(items))
385
+ end
386
+
387
+ def test_explain_sql_two_selects_with_connection
388
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
389
+ config = mock('config')
390
+ s.params = {:sql => "SELECT true();\nSELECT false()", :connection_config => config}
391
+ connection = mock('connection')
392
+ # two rows, two columns
393
+ connection.expects(:execute).returns([["string", "string"], ["string", "string"]]).twice
394
+ NewRelic::TransactionSample.expects(:get_connection).with(config).returns(connection).twice
395
+ assert_equal([[['string', 'string'], ['string', 'string']], [['string', 'string'], ['string', 'string']]], s.explain_sql)
396
+ end
397
+
398
+ def test_explain_sql_raising_an_error
399
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
400
+ config = mock('config')
401
+ s.params = {:sql => 'SELECT', :connection_config => config}
402
+ connection = mock('connection')
403
+ NewRelic::TransactionSample.expects(:get_connection).with(config).raises(RuntimeError.new("whee"))
404
+ assert_nothing_raised do
405
+ s.explain_sql
406
+ end
407
+ end
408
+
409
+ def test_params_equal
410
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
411
+ assert_equal(nil, s.instance_eval { @params })
412
+
413
+ params = {:foo => 'correct'}
414
+
415
+ s.params = params
416
+ assert_equal(params, s.instance_eval { @params })
417
+ end
418
+
419
+ def test_handle_exception_in_explain
420
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
421
+ fake_error = Exception.new
422
+ fake_error.expects(:message).returns('a message')
423
+ NewRelic::Control.instance.log.expects(:error).with('Error getting explain plan: a message')
424
+ # backtrace can be basically any string, just should get logged
425
+ NewRelic::Control.instance.log.expects(:debug).with(instance_of(String))
426
+ s.handle_exception_in_explain do
427
+ raise(fake_error)
428
+ end
429
+ end
430
+
431
+ def test_obfuscated_sql
432
+ sql = 'some sql'
433
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
434
+ s[:sql] = sql
435
+ NewRelic::TransactionSample.expects(:obfuscate_sql).with(sql)
436
+ s.obfuscated_sql
437
+ end
438
+
439
+ def test_called_segments_equals
440
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
441
+ assert_equal(nil, s.instance_eval { @called_segments })
442
+ s.called_segments = [1, 2, 3]
443
+ assert_equal([1, 2, 3], s.instance_eval { @called_segments })
444
+ end
445
+
446
+ def test_parent_segment_equals
447
+ s = NewRelic::TransactionSample::Segment.new(Time.now, 'Custom/test/metric', nil)
448
+ assert_equal(nil, s.instance_eval { @parent_segment })
449
+ fake_segment = mock('segment')
450
+ s.send(:parent_segment=, fake_segment)
451
+ assert_equal(fake_segment, s.parent_segment)
452
+ end
453
+ end
454
+