onyx_newrelic_rpm 2.12.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (165) hide show
  1. data/CHANGELOG +436 -0
  2. data/LICENSE +37 -0
  3. data/README.md +138 -0
  4. data/bin/mongrel_rpm +33 -0
  5. data/bin/newrelic_cmd +4 -0
  6. data/cert/cacert.pem +34 -0
  7. data/install.rb +46 -0
  8. data/lib/new_relic/agent/agent.rb +668 -0
  9. data/lib/new_relic/agent/busy_calculator.rb +91 -0
  10. data/lib/new_relic/agent/chained_call.rb +13 -0
  11. data/lib/new_relic/agent/error_collector.rb +128 -0
  12. data/lib/new_relic/agent/instrumentation/active_merchant.rb +18 -0
  13. data/lib/new_relic/agent/instrumentation/active_record_instrumentation.rb +92 -0
  14. data/lib/new_relic/agent/instrumentation/acts_as_solr.rb +45 -0
  15. data/lib/new_relic/agent/instrumentation/authlogic.rb +8 -0
  16. data/lib/new_relic/agent/instrumentation/controller_instrumentation.rb +404 -0
  17. data/lib/new_relic/agent/instrumentation/data_mapper.rb +90 -0
  18. data/lib/new_relic/agent/instrumentation/delayed_job_instrumentation.rb +22 -0
  19. data/lib/new_relic/agent/instrumentation/memcache.rb +40 -0
  20. data/lib/new_relic/agent/instrumentation/merb/controller.rb +26 -0
  21. data/lib/new_relic/agent/instrumentation/merb/errors.rb +9 -0
  22. data/lib/new_relic/agent/instrumentation/metric_frame.rb +307 -0
  23. data/lib/new_relic/agent/instrumentation/net.rb +17 -0
  24. data/lib/new_relic/agent/instrumentation/passenger_instrumentation.rb +22 -0
  25. data/lib/new_relic/agent/instrumentation/rack.rb +112 -0
  26. data/lib/new_relic/agent/instrumentation/rails/action_controller.rb +59 -0
  27. data/lib/new_relic/agent/instrumentation/rails/action_web_service.rb +27 -0
  28. data/lib/new_relic/agent/instrumentation/rails/errors.rb +24 -0
  29. data/lib/new_relic/agent/instrumentation/rails3/action_controller.rb +45 -0
  30. data/lib/new_relic/agent/instrumentation/rails3/errors.rb +21 -0
  31. data/lib/new_relic/agent/instrumentation/sinatra.rb +46 -0
  32. data/lib/new_relic/agent/instrumentation/sunspot.rb +17 -0
  33. data/lib/new_relic/agent/instrumentation/unicorn_instrumentation.rb +10 -0
  34. data/lib/new_relic/agent/method_tracer.rb +350 -0
  35. data/lib/new_relic/agent/sampler.rb +46 -0
  36. data/lib/new_relic/agent/samplers/cpu_sampler.rb +54 -0
  37. data/lib/new_relic/agent/samplers/delayed_job_lock_sampler.rb +37 -0
  38. data/lib/new_relic/agent/samplers/memory_sampler.rb +142 -0
  39. data/lib/new_relic/agent/samplers/object_sampler.rb +24 -0
  40. data/lib/new_relic/agent/shim_agent.rb +21 -0
  41. data/lib/new_relic/agent/stats_engine/metric_stats.rb +118 -0
  42. data/lib/new_relic/agent/stats_engine/samplers.rb +80 -0
  43. data/lib/new_relic/agent/stats_engine/transactions.rb +149 -0
  44. data/lib/new_relic/agent/stats_engine.rb +24 -0
  45. data/lib/new_relic/agent/transaction_sampler.rb +315 -0
  46. data/lib/new_relic/agent/worker_loop.rb +80 -0
  47. data/lib/new_relic/agent.rb +369 -0
  48. data/lib/new_relic/collection_helper.rb +69 -0
  49. data/lib/new_relic/commands/deployments.rb +145 -0
  50. data/lib/new_relic/commands/new_relic_commands.rb +30 -0
  51. data/lib/new_relic/control/external.rb +13 -0
  52. data/lib/new_relic/control/merb.rb +24 -0
  53. data/lib/new_relic/control/rails.rb +151 -0
  54. data/lib/new_relic/control/rails3.rb +75 -0
  55. data/lib/new_relic/control/ruby.rb +36 -0
  56. data/lib/new_relic/control/sinatra.rb +18 -0
  57. data/lib/new_relic/control.rb +528 -0
  58. data/lib/new_relic/delayed_job_injection.rb +27 -0
  59. data/lib/new_relic/histogram.rb +89 -0
  60. data/lib/new_relic/local_environment.rb +333 -0
  61. data/lib/new_relic/merbtasks.rb +6 -0
  62. data/lib/new_relic/metric_data.rb +42 -0
  63. data/lib/new_relic/metric_parser/action_mailer.rb +9 -0
  64. data/lib/new_relic/metric_parser/active_merchant.rb +26 -0
  65. data/lib/new_relic/metric_parser/active_record.rb +25 -0
  66. data/lib/new_relic/metric_parser/controller.rb +54 -0
  67. data/lib/new_relic/metric_parser/controller_cpu.rb +38 -0
  68. data/lib/new_relic/metric_parser/errors.rb +6 -0
  69. data/lib/new_relic/metric_parser/external.rb +50 -0
  70. data/lib/new_relic/metric_parser/mem_cache.rb +50 -0
  71. data/lib/new_relic/metric_parser/other_transaction.rb +15 -0
  72. data/lib/new_relic/metric_parser/view.rb +61 -0
  73. data/lib/new_relic/metric_parser/web_frontend.rb +14 -0
  74. data/lib/new_relic/metric_parser/web_service.rb +9 -0
  75. data/lib/new_relic/metric_parser.rb +125 -0
  76. data/lib/new_relic/metric_spec.rb +67 -0
  77. data/lib/new_relic/metrics.rb +9 -0
  78. data/lib/new_relic/noticed_error.rb +24 -0
  79. data/lib/new_relic/rack/metric_app.rb +58 -0
  80. data/lib/new_relic/rack/mongrel_rpm.ru +25 -0
  81. data/lib/new_relic/rack/newrelic.yml +26 -0
  82. data/lib/new_relic/rack_app.rb +5 -0
  83. data/lib/new_relic/recipes.rb +82 -0
  84. data/lib/new_relic/stats.rb +376 -0
  85. data/lib/new_relic/transaction_analysis.rb +124 -0
  86. data/lib/new_relic/transaction_sample.rb +654 -0
  87. data/lib/new_relic/version.rb +55 -0
  88. data/lib/new_relic_api.rb +276 -0
  89. data/lib/newrelic_rpm.rb +40 -0
  90. data/lib/tasks/all.rb +4 -0
  91. data/lib/tasks/install.rake +7 -0
  92. data/lib/tasks/tests.rake +15 -0
  93. data/newrelic.yml +235 -0
  94. data/onyx_newrelic_rpm.gemspec +221 -0
  95. data/recipes/newrelic.rb +6 -0
  96. data/test/active_record_fixtures.rb +55 -0
  97. data/test/config/newrelic.yml +43 -0
  98. data/test/config/test_control.rb +38 -0
  99. data/test/new_relic/agent/active_record_instrumentation_test.rb +287 -0
  100. data/test/new_relic/agent/agent_controller_test.rb +280 -0
  101. data/test/new_relic/agent/agent_test_controller.rb +82 -0
  102. data/test/new_relic/agent/busy_calculator_test.rb +79 -0
  103. data/test/new_relic/agent/collection_helper_test.rb +125 -0
  104. data/test/new_relic/agent/error_collector_test.rb +171 -0
  105. data/test/new_relic/agent/memcache_instrumentation_test.rb +103 -0
  106. data/test/new_relic/agent/method_tracer_test.rb +340 -0
  107. data/test/new_relic/agent/metric_data_test.rb +56 -0
  108. data/test/new_relic/agent/metric_frame_test.rb +51 -0
  109. data/test/new_relic/agent/mock_ar_connection.rb +40 -0
  110. data/test/new_relic/agent/mock_scope_listener.rb +23 -0
  111. data/test/new_relic/agent/net_instrumentation_test.rb +77 -0
  112. data/test/new_relic/agent/rpm_agent_test.rb +138 -0
  113. data/test/new_relic/agent/stats_engine/metric_stats_test.rb +79 -0
  114. data/test/new_relic/agent/stats_engine/samplers_test.rb +72 -0
  115. data/test/new_relic/agent/stats_engine/stats_engine_test.rb +184 -0
  116. data/test/new_relic/agent/task_instrumentation_test.rb +193 -0
  117. data/test/new_relic/agent/testable_agent.rb +13 -0
  118. data/test/new_relic/agent/transaction_sample_builder_test.rb +195 -0
  119. data/test/new_relic/agent/transaction_sample_test.rb +186 -0
  120. data/test/new_relic/agent/transaction_sampler_test.rb +385 -0
  121. data/test/new_relic/agent/worker_loop_test.rb +60 -0
  122. data/test/new_relic/control_test.rb +117 -0
  123. data/test/new_relic/deployments_api_test.rb +69 -0
  124. data/test/new_relic/environment_test.rb +75 -0
  125. data/test/new_relic/metric_parser_test.rb +172 -0
  126. data/test/new_relic/metric_spec_test.rb +177 -0
  127. data/test/new_relic/shim_agent_test.rb +9 -0
  128. data/test/new_relic/stats_test.rb +311 -0
  129. data/test/new_relic/version_number_test.rb +89 -0
  130. data/test/test_helper.rb +53 -0
  131. data/test/ui/newrelic_controller_test.rb +14 -0
  132. data/test/ui/newrelic_helper_test.rb +53 -0
  133. data/ui/controllers/newrelic_controller.rb +220 -0
  134. data/ui/helpers/google_pie_chart.rb +49 -0
  135. data/ui/helpers/newrelic_helper.rb +319 -0
  136. data/ui/views/layouts/newrelic_default.rhtml +47 -0
  137. data/ui/views/newrelic/_explain_plans.rhtml +27 -0
  138. data/ui/views/newrelic/_sample.rhtml +19 -0
  139. data/ui/views/newrelic/_segment.rhtml +28 -0
  140. data/ui/views/newrelic/_segment_limit_message.rhtml +1 -0
  141. data/ui/views/newrelic/_segment_row.rhtml +14 -0
  142. data/ui/views/newrelic/_show_sample_detail.rhtml +24 -0
  143. data/ui/views/newrelic/_show_sample_sql.rhtml +20 -0
  144. data/ui/views/newrelic/_show_sample_summary.rhtml +3 -0
  145. data/ui/views/newrelic/_sql_row.rhtml +11 -0
  146. data/ui/views/newrelic/_stack_trace.rhtml +30 -0
  147. data/ui/views/newrelic/_table.rhtml +12 -0
  148. data/ui/views/newrelic/explain_sql.rhtml +42 -0
  149. data/ui/views/newrelic/images/arrow-close.png +0 -0
  150. data/ui/views/newrelic/images/arrow-open.png +0 -0
  151. data/ui/views/newrelic/images/blue_bar.gif +0 -0
  152. data/ui/views/newrelic/images/file_icon.png +0 -0
  153. data/ui/views/newrelic/images/gray_bar.gif +0 -0
  154. data/ui/views/newrelic/images/new-relic-rpm-desktop.gif +0 -0
  155. data/ui/views/newrelic/images/new_relic_rpm_desktop.gif +0 -0
  156. data/ui/views/newrelic/images/textmate.png +0 -0
  157. data/ui/views/newrelic/index.rhtml +57 -0
  158. data/ui/views/newrelic/javascript/prototype-scriptaculous.js +7288 -0
  159. data/ui/views/newrelic/javascript/transaction_sample.js +107 -0
  160. data/ui/views/newrelic/sample_not_found.rhtml +2 -0
  161. data/ui/views/newrelic/show_sample.rhtml +80 -0
  162. data/ui/views/newrelic/show_source.rhtml +3 -0
  163. data/ui/views/newrelic/stylesheets/style.css +484 -0
  164. data/ui/views/newrelic/threads.rhtml +52 -0
  165. metadata +248 -0
@@ -0,0 +1,177 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__),'..', 'test_helper'))
2
+ ##require "new_relic/stats"
3
+ class NewRelic::MetricSpecTest < Test::Unit::TestCase
4
+
5
+ def test_simple
6
+ stats = NewRelic::MethodTraceStats.new
7
+ validate stats, 0, 0, 0, 0
8
+
9
+ assert_equal stats.call_count,0
10
+ stats.trace_call 10
11
+ stats.trace_call 20
12
+ stats.trace_call 30
13
+
14
+ validate stats, 3, (10+20+30), 10, 30
15
+ end
16
+
17
+ def test_equal
18
+ spec1 = NewRelic::MetricSpec.new('Controller')
19
+ spec2 = NewRelic::MetricSpec.new('Controller', nil)
20
+
21
+ assert spec1.eql?(NewRelic::MetricSpec.new('Controller'))
22
+ assert spec2.eql?(NewRelic::MetricSpec.new('Controller', nil))
23
+ assert spec1.eql?(spec2)
24
+ assert !spec2.eql?(NewRelic::MetricSpec.new('Controller', '/dude'))
25
+ end
26
+
27
+ define_method(:'test_<=>') do
28
+ s1 = NewRelic::MetricSpec.new('ActiveRecord')
29
+ s2 = NewRelic::MetricSpec.new('Controller')
30
+ assert_equal [s1, s2].sort, [s1,s2]
31
+ assert_equal [s2, s1].sort, [s1,s2]
32
+
33
+ s1 = NewRelic::MetricSpec.new('Controller', nil)
34
+ s2 = NewRelic::MetricSpec.new('Controller', 'hap')
35
+ assert_equal [s2, s1].sort, [s1, s2]
36
+ assert_equal [s1, s2].sort, [s1, s2]
37
+
38
+ s1 = NewRelic::MetricSpec.new('Controller', 'hap')
39
+ s2 = NewRelic::MetricSpec.new('Controller', nil)
40
+ assert_equal [s2, s1].sort, [s2, s1]
41
+ assert_equal [s1, s2].sort, [s2, s1]
42
+
43
+ s1 = NewRelic::MetricSpec.new('Controller')
44
+ s2 = NewRelic::MetricSpec.new('Controller')
45
+ assert_equal [s2, s1].sort, [s2, s1] # unchanged due to no sort criteria
46
+ assert_equal [s1, s2].sort, [s1, s2] # unchanged due to no sort criteria
47
+
48
+ s1 = NewRelic::MetricSpec.new('Controller', nil)
49
+ s2 = NewRelic::MetricSpec.new('Controller', nil)
50
+ assert_equal [s2, s1].sort, [s2, s1] # unchanged due to no sort criteria
51
+ assert_equal [s1, s2].sort, [s1, s2] # unchanged due to no sort criteria
52
+ end
53
+
54
+ def test_merge
55
+ s1 = NewRelic::MethodTraceStats.new
56
+ s2 = NewRelic::MethodTraceStats.new
57
+
58
+ s1.trace_call 10
59
+ s2.trace_call 20
60
+ s2.freeze
61
+
62
+ validate s2, 1, 20, 20, 20
63
+ s3 = s1.merge s2
64
+ validate s3, 2, (10+20), 10, 20
65
+ validate s1, 1, 10, 10, 10
66
+ validate s2, 1, 20, 20, 20
67
+
68
+ s1.merge! s2
69
+ validate s1, 2, (10+20), 10, 20
70
+ validate s2, 1, 20, 20, 20
71
+ end
72
+
73
+ def test_merge_with_exclusive
74
+ s1 = NewRelic::MethodTraceStats.new
75
+
76
+ s2 = NewRelic::MethodTraceStats.new
77
+
78
+ s1.trace_call 10, 5
79
+ s2.trace_call 20, 10
80
+ s2.freeze
81
+
82
+ validate s2, 1, 20, 20, 20, 10
83
+ s3 = s1.merge s2
84
+ validate s3, 2, (10+20), 10, 20, (10+5)
85
+ validate s1, 1, 10, 10, 10, 5
86
+ validate s2, 1, 20, 20, 20, 10
87
+
88
+ s1.merge! s2
89
+ validate s1, 2, (10+20), 10, 20, (5+10)
90
+ validate s2, 1, 20, 20, 20, 10
91
+ end
92
+
93
+ def test_merge_array
94
+ s1 = NewRelic::MethodTraceStats.new
95
+ merges = []
96
+ merges << (NewRelic::MethodTraceStats.new.trace_call 1)
97
+ merges << (NewRelic::MethodTraceStats.new.trace_call 1)
98
+ merges << (NewRelic::MethodTraceStats.new.trace_call 1)
99
+
100
+ s1.merge! merges
101
+ validate s1, 3, 3, 1, 1
102
+ end
103
+
104
+ def test_freeze
105
+ s1 = NewRelic::MethodTraceStats.new
106
+
107
+ s1.trace_call 10
108
+ s1.freeze
109
+
110
+ begin
111
+ # the following should throw an exception because s1 is frozen
112
+ s1.trace_call 20
113
+ assert false
114
+ rescue StandardError
115
+ assert s1.frozen?
116
+ validate s1, 1, 10, 10, 10
117
+ end
118
+ end
119
+
120
+ def test_std_dev
121
+ s = NewRelic::MethodTraceStats.new
122
+
123
+ s.trace_call 10
124
+ s.trace_call 10
125
+ s.trace_call 10
126
+ s.trace_call 10
127
+ s.trace_call 10
128
+ s.trace_call 10
129
+ assert s.standard_deviation == 0
130
+
131
+ s = NewRelic::MethodTraceStats.new
132
+ s.trace_call 4
133
+ s.trace_call 7
134
+ s.trace_call 13
135
+ s.trace_call 16
136
+ s.trace_call 8
137
+ s.trace_call 4
138
+ assert_equal(s.sum_of_squares, 4**2 + 7**2 + 13**2 + 16**2 + 8**2 + 4**2)
139
+
140
+ s.trace_call 9
141
+ s.trace_call 3
142
+ s.trace_call 1000
143
+ s.trace_call 4
144
+
145
+ # calculated stdev (population, not sample) from a spreadsheet.
146
+ assert_in_delta(s.standard_deviation, 297.76, 0.01)
147
+ end
148
+
149
+ def test_std_dev_merge
150
+ s1 = NewRelic::MethodTraceStats.new
151
+ s1.trace_call 4
152
+ s1.trace_call 7
153
+
154
+ s2 = NewRelic::MethodTraceStats.new
155
+ s2.trace_call 13
156
+ s2.trace_call 16
157
+
158
+ s3 = s1.merge(s2)
159
+
160
+ assert(s1.sum_of_squares, 4*4 + 7*7)
161
+ assert_in_delta(s1.standard_deviation, 1.5, 0.01)
162
+
163
+ assert_in_delta(s2.standard_deviation, 1.5, 0.01)
164
+ assert_equal(s3.sum_of_squares, 4*4 + 7*7 + 13*13 + 16*16, "check sum of squares")
165
+ assert_in_delta(s3.standard_deviation, 4.743, 0.01)
166
+ end
167
+
168
+ private
169
+ def validate (stats, count, total, min, max, exclusive = nil)
170
+ assert_equal stats.call_count, count
171
+ assert_equal stats.total_call_time, total
172
+ assert_equal stats.average_call_time, (count > 0 ? total / count : 0)
173
+ assert_equal stats.min_call_time, min
174
+ assert_equal stats.max_call_time, max
175
+ assert_equal stats.total_exclusive_time, exclusive if exclusive
176
+ end
177
+ end
@@ -0,0 +1,9 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__),'/../test_helper'))
2
+ # TODO
3
+ class NewRelic::ShimAgentTest < Test::Unit::TestCase
4
+
5
+ def test_shim
6
+ end
7
+
8
+
9
+ end
@@ -0,0 +1,311 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__),'..', 'test_helper'))
2
+ ##require "new_relic/stats"
3
+
4
+ module NewRelic; class TestObjectForStats
5
+ include Stats
6
+ attr_accessor :total_call_time
7
+ attr_accessor :total_exclusive_time
8
+ attr_accessor :begin_time
9
+ attr_accessor :end_time
10
+ attr_accessor :call_count
11
+ end; end
12
+
13
+
14
+ class NewRelic::StatsTest < Test::Unit::TestCase
15
+
16
+ def test_simple
17
+ stats = NewRelic::MethodTraceStats.new
18
+ validate stats, 0, 0, 0, 0
19
+
20
+ assert_equal stats.call_count,0
21
+ stats.trace_call 10
22
+ stats.trace_call 20
23
+ stats.trace_call 30
24
+
25
+ validate stats, 3, (10+20+30), 10, 30
26
+ end
27
+
28
+ def test_to_s
29
+ s1 = NewRelic::MethodTraceStats.new
30
+ s1.trace_call 10
31
+ assert_equal(s1.to_s, "Begin=0.0, Duration=0.0 s, Count=1, Total=10000, Total Exclusive=10000, Avg=10000, Min=10000, Max=10000, StdDev=0")
32
+ end
33
+
34
+ def test_time_str
35
+ s1 = NewRelic::MethodTraceStats.new
36
+ assert_equal(s1.time_str(10), "10 ms")
37
+ assert_equal(s1.time_str(4999), "4999 ms")
38
+ assert_equal(s1.time_str(5000), "5.00 s")
39
+ assert_equal(s1.time_str(5010), "5.01 s")
40
+ assert_equal(s1.time_str(9999), "10.00 s")
41
+ assert_equal(s1.time_str(10000), "10.0 s")
42
+ assert_equal(s1.time_str(20000), "20.0 s")
43
+ end
44
+
45
+ def test_fraction_of
46
+ s1 = NewRelic::MethodTraceStats.new
47
+ s2 = NewRelic::MethodTraceStats.new
48
+ s1.trace_call 10
49
+ s2.trace_call 20
50
+ assert_equal(s1.fraction_of(s2).to_s, 'NaN')
51
+ end
52
+
53
+ def test_fraction_of2
54
+ s1 = NewRelic::MethodTraceStats.new
55
+ s1.trace_call 10
56
+ s2 = NewRelic::MethodTraceStats.new
57
+ assert_equal(s1.fraction_of(s2).to_s, 'NaN')
58
+ end
59
+
60
+ def test_multiply_by
61
+ s1 = NewRelic::MethodTraceStats.new
62
+ s1.trace_call 10
63
+ assert_equal(s1.multiply_by(10).to_s, "Begin=0.0, Duration=0.0 s, Count=10, Total=100000, Total Exclusive=10000, Avg=10000, Min=10000, Max=10000, StdDev=0")
64
+ end
65
+
66
+ def test_get_apdex
67
+ s1 = NewRelic::MethodTraceStats.new
68
+ s1.trace_call 10
69
+ assert_equal(s1.get_apdex, [1, 10, 10])
70
+ end
71
+
72
+ def test_apdex_score
73
+ s1 = NewRelic::MethodTraceStats.new
74
+ s1.trace_call 10
75
+ # FIXME make this test the real logic
76
+ # don't ask me what this means, but it's what's coming out the
77
+ # other end when I actually run it.
78
+ assert_in_delta(s1.apdex_score, 0.285714285714286, 0.0000001)
79
+ end
80
+
81
+ def test_as_percentage
82
+ s1 = NewRelic::MethodTraceStats.new
83
+ s1.trace_call 10
84
+ assert_equal(s1.as_percentage, 1000.0)
85
+ end
86
+
87
+ def test_calls_per_minute
88
+
89
+ s1 = NewRelic::TestObjectForStats.new
90
+ s1.call_count = 1
91
+ s1.begin_time = Time.at(0)
92
+ s1.end_time = Time.at(30)
93
+ assert_equal(s1.calls_per_minute, 2)
94
+ end
95
+
96
+ def test_total_call_time_per_minute
97
+ s1 = NewRelic::TestObjectForStats.new
98
+ s1.begin_time = Time.at(0)
99
+ s1.end_time = Time.at(0)
100
+ assert_equal(0, s1.total_call_time_per_minute)
101
+ s1.begin_time = Time.at(0)
102
+ s1.end_time = Time.at(30)
103
+ s1.total_call_time = 10
104
+ assert_equal(20, s1.total_call_time_per_minute)
105
+ end
106
+
107
+ def test_time_percentage
108
+ s1 = NewRelic::TestObjectForStats.new
109
+ s1.begin_time = Time.at(0)
110
+ s1.end_time = Time.at(0)
111
+ assert_equal(0, s1.time_percentage)
112
+ s1.total_call_time = 10
113
+ s1.begin_time = Time.at(0)
114
+ s1.end_time = Time.at(30)
115
+ assert_equal((1.0 / 3.0), s1.time_percentage)
116
+ s1.total_call_time = 20
117
+ assert_equal((2.0 / 3.0), s1.time_percentage)
118
+ end
119
+
120
+ def test_exclusive_time_percentage
121
+ s1 = NewRelic::TestObjectForStats.new
122
+ s1.begin_time = Time.at(0)
123
+ s1.end_time = Time.at(0)
124
+ assert_equal(0, s1.exclusive_time_percentage)
125
+ s1.total_exclusive_time = 10
126
+ s1.begin_time = Time.at(0)
127
+ s1.end_time = Time.at(30)
128
+ assert_equal((1.0 / 3.0), s1.exclusive_time_percentage)
129
+ s1.total_exclusive_time = 20
130
+ assert_equal((2.0 / 3.0), s1.exclusive_time_percentage)
131
+ end
132
+
133
+ def test_sum_merge
134
+ s1 = NewRelic::MethodTraceStats.new
135
+ s2 = NewRelic::MethodTraceStats.new
136
+ s1.trace_call 10
137
+ s2.trace_call 20
138
+ s2.freeze
139
+
140
+ validate s1, 1, 10, 10, 10
141
+ validate s2, 1, 20, 20, 20
142
+ s1.sum_merge! s2
143
+ validate s1, 1, (10+20), 10 + 20, 20 + 10
144
+ validate s2, 1, 20, 20, 20
145
+ end
146
+
147
+ def test_sum_merge_with_exclusive
148
+ s1 = NewRelic::MethodTraceStats.new
149
+ s2 = NewRelic::MethodTraceStats.new
150
+
151
+ s1.trace_call 10, 5
152
+ s2.trace_call 20, 10
153
+ s2.freeze
154
+
155
+ validate s1, 1, 10, 10, 10, 5
156
+ validate s2, 1, 20, 20, 20, 10
157
+ s1.sum_merge! s2
158
+ validate s1, 1, (10+20), 10 + 20, 20 + 10, (10+5)
159
+ end
160
+
161
+ def test_merge
162
+ s1 = NewRelic::MethodTraceStats.new
163
+ s2 = NewRelic::MethodTraceStats.new
164
+
165
+ s1.trace_call 10
166
+ s2.trace_call 20
167
+ s2.freeze
168
+
169
+ validate s2, 1, 20, 20, 20
170
+ s3 = s1.merge s2
171
+ validate s3, 2, (10+20), 10, 20
172
+ validate s1, 1, 10, 10, 10
173
+ validate s2, 1, 20, 20, 20
174
+
175
+ s1.merge! s2
176
+ validate s1, 2, (10+20), 10, 20
177
+ validate s2, 1, 20, 20, 20
178
+ end
179
+
180
+ def test_merge_with_exclusive
181
+ s1 = NewRelic::MethodTraceStats.new
182
+
183
+ s2 = NewRelic::MethodTraceStats.new
184
+
185
+ s1.trace_call 10, 5
186
+ s2.trace_call 20, 10
187
+ s2.freeze
188
+
189
+ validate s2, 1, 20, 20, 20, 10
190
+ s3 = s1.merge s2
191
+ validate s3, 2, (10+20), 10, 20, (10+5)
192
+ validate s1, 1, 10, 10, 10, 5
193
+ validate s2, 1, 20, 20, 20, 10
194
+
195
+ s1.merge! s2
196
+ validate s1, 2, (10+20), 10, 20, (5+10)
197
+ validate s2, 1, 20, 20, 20, 10
198
+ end
199
+
200
+ def test_merge_array
201
+ s1 = NewRelic::MethodTraceStats.new
202
+ merges = []
203
+ merges << (NewRelic::MethodTraceStats.new.trace_call 1)
204
+ merges << (NewRelic::MethodTraceStats.new.trace_call 1)
205
+ merges << (NewRelic::MethodTraceStats.new.trace_call 1)
206
+
207
+ s1.merge! merges
208
+ validate s1, 3, 3, 1, 1
209
+ end
210
+ def test_round
211
+ stats = NewRelic::MethodTraceStats.new
212
+ stats.record_data_point(0.125222, 0.025)
213
+ stats.record_data_point(0.125222, 0.025)
214
+ stats.record_data_point(0.125222, 0.025)
215
+ assert_equal 0.047041647852, stats.sum_of_squares
216
+ assert_equal 0.375666, stats.total_call_time
217
+ stats.round!
218
+ assert_equal 0.376, stats.total_call_time
219
+ assert_equal 0.047, stats.sum_of_squares
220
+
221
+ end
222
+
223
+ def test_freeze
224
+ s1 = NewRelic::MethodTraceStats.new
225
+
226
+ s1.trace_call 10
227
+ s1.freeze
228
+
229
+ begin
230
+ # the following should throw an exception because s1 is frozen
231
+ s1.trace_call 20
232
+ assert false
233
+ rescue StandardError
234
+ assert s1.frozen?
235
+ validate s1, 1, 10, 10, 10
236
+ end
237
+ end
238
+
239
+ def test_std_dev
240
+ s = NewRelic::MethodTraceStats.new
241
+ s.trace_call 1
242
+ assert s.standard_deviation == 0
243
+
244
+ s = NewRelic::MethodTraceStats.new
245
+ s.trace_call 10
246
+ s.trace_call 10
247
+ s.sum_of_squares = nil
248
+ assert s.standard_deviation == 0
249
+
250
+ s = NewRelic::MethodTraceStats.new
251
+ s.trace_call 0.001
252
+ s.trace_call 0.001
253
+ assert s.standard_deviation == 0
254
+
255
+
256
+ s = NewRelic::MethodTraceStats.new
257
+ s.trace_call 10
258
+ s.trace_call 10
259
+ s.trace_call 10
260
+ s.trace_call 10
261
+ s.trace_call 10
262
+ s.trace_call 10
263
+ assert s.standard_deviation == 0
264
+
265
+ s = NewRelic::MethodTraceStats.new
266
+ s.trace_call 4
267
+ s.trace_call 7
268
+ s.trace_call 13
269
+ s.trace_call 16
270
+ s.trace_call 8
271
+ s.trace_call 4
272
+ assert_equal(s.sum_of_squares, 4**2 + 7**2 + 13**2 + 16**2 + 8**2 + 4**2)
273
+
274
+ s.trace_call 9
275
+ s.trace_call 3
276
+ s.trace_call 1000
277
+ s.trace_call 4
278
+
279
+ # calculated stdev (population, not sample) from a spreadsheet.
280
+ assert_in_delta(s.standard_deviation, 297.76, 0.01)
281
+ end
282
+
283
+ def test_std_dev_merge
284
+ s1 = NewRelic::MethodTraceStats.new
285
+ s1.trace_call 4
286
+ s1.trace_call 7
287
+
288
+ s2 = NewRelic::MethodTraceStats.new
289
+ s2.trace_call 13
290
+ s2.trace_call 16
291
+
292
+ s3 = s1.merge(s2)
293
+
294
+ assert(s1.sum_of_squares, 4*4 + 7*7)
295
+ assert_in_delta(s1.standard_deviation, 1.5, 0.01)
296
+
297
+ assert_in_delta(s2.standard_deviation, 1.5, 0.01)
298
+ assert_equal(s3.sum_of_squares, 4*4 + 7*7 + 13*13 + 16*16, "check sum of squares")
299
+ assert_in_delta(s3.standard_deviation, 4.743, 0.01)
300
+ end
301
+
302
+ private
303
+ def validate (stats, count, total, min, max, exclusive = nil)
304
+ assert_equal stats.call_count, count
305
+ assert_equal stats.total_call_time, total
306
+ assert_equal stats.average_call_time, (count > 0 ? total / count : 0)
307
+ assert_equal stats.min_call_time, min
308
+ assert_equal stats.max_call_time, max
309
+ assert_equal stats.total_exclusive_time, exclusive if exclusive
310
+ end
311
+ end