newrelic_rpm 2.8.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of newrelic_rpm might be problematic. Click here for more details.

Files changed (107) hide show
  1. data/LICENSE +37 -0
  2. data/README +93 -0
  3. data/Rakefile +38 -0
  4. data/install.rb +37 -0
  5. data/lib/new_relic/agent.rb +26 -0
  6. data/lib/new_relic/agent/agent.rb +762 -0
  7. data/lib/new_relic/agent/chained_call.rb +13 -0
  8. data/lib/new_relic/agent/collection_helper.rb +81 -0
  9. data/lib/new_relic/agent/error_collector.rb +105 -0
  10. data/lib/new_relic/agent/instrumentation/active_record_instrumentation.rb +95 -0
  11. data/lib/new_relic/agent/instrumentation/controller_instrumentation.rb +151 -0
  12. data/lib/new_relic/agent/instrumentation/data_mapper.rb +90 -0
  13. data/lib/new_relic/agent/instrumentation/dispatcher_instrumentation.rb +105 -0
  14. data/lib/new_relic/agent/instrumentation/memcache.rb +18 -0
  15. data/lib/new_relic/agent/instrumentation/merb/controller.rb +17 -0
  16. data/lib/new_relic/agent/instrumentation/merb/dispatcher.rb +15 -0
  17. data/lib/new_relic/agent/instrumentation/merb/errors.rb +6 -0
  18. data/lib/new_relic/agent/instrumentation/rails/action_controller.rb +35 -0
  19. data/lib/new_relic/agent/instrumentation/rails/action_web_service.rb +27 -0
  20. data/lib/new_relic/agent/instrumentation/rails/dispatcher.rb +30 -0
  21. data/lib/new_relic/agent/instrumentation/rails/errors.rb +23 -0
  22. data/lib/new_relic/agent/instrumentation/rails/rails.rb +6 -0
  23. data/lib/new_relic/agent/method_tracer.rb +171 -0
  24. data/lib/new_relic/agent/patch_const_missing.rb +31 -0
  25. data/lib/new_relic/agent/samplers/cpu.rb +29 -0
  26. data/lib/new_relic/agent/samplers/memory.rb +55 -0
  27. data/lib/new_relic/agent/samplers/mongrel.rb +26 -0
  28. data/lib/new_relic/agent/stats_engine.rb +241 -0
  29. data/lib/new_relic/agent/synchronize.rb +40 -0
  30. data/lib/new_relic/agent/transaction_sampler.rb +281 -0
  31. data/lib/new_relic/agent/worker_loop.rb +128 -0
  32. data/lib/new_relic/api/deployments.rb +92 -0
  33. data/lib/new_relic/config.rb +194 -0
  34. data/lib/new_relic/config/merb.rb +35 -0
  35. data/lib/new_relic/config/rails.rb +113 -0
  36. data/lib/new_relic/config/ruby.rb +9 -0
  37. data/lib/new_relic/local_environment.rb +108 -0
  38. data/lib/new_relic/merbtasks.rb +6 -0
  39. data/lib/new_relic/metric_data.rb +26 -0
  40. data/lib/new_relic/metric_spec.rb +39 -0
  41. data/lib/new_relic/metrics.rb +7 -0
  42. data/lib/new_relic/noticed_error.rb +21 -0
  43. data/lib/new_relic/shim_agent.rb +95 -0
  44. data/lib/new_relic/stats.rb +359 -0
  45. data/lib/new_relic/transaction_analysis.rb +122 -0
  46. data/lib/new_relic/transaction_sample.rb +499 -0
  47. data/lib/new_relic/version.rb +111 -0
  48. data/lib/new_relic_api.rb +275 -0
  49. data/lib/newrelic_rpm.rb +27 -0
  50. data/lib/tasks/agent_tests.rake +14 -0
  51. data/lib/tasks/all.rb +4 -0
  52. data/lib/tasks/install.rake +7 -0
  53. data/newrelic.yml +137 -0
  54. data/recipes/newrelic.rb +46 -0
  55. data/test/config/newrelic.yml +26 -0
  56. data/test/config/test_config.rb +9 -0
  57. data/test/new_relic/agent/mock_ar_connection.rb +40 -0
  58. data/test/new_relic/agent/mock_scope_listener.rb +23 -0
  59. data/test/new_relic/agent/model_fixture.rb +17 -0
  60. data/test/new_relic/agent/tc_active_record.rb +91 -0
  61. data/test/new_relic/agent/tc_agent.rb +112 -0
  62. data/test/new_relic/agent/tc_collection_helper.rb +104 -0
  63. data/test/new_relic/agent/tc_controller.rb +98 -0
  64. data/test/new_relic/agent/tc_dispatcher_instrumentation.rb +52 -0
  65. data/test/new_relic/agent/tc_error_collector.rb +127 -0
  66. data/test/new_relic/agent/tc_method_tracer.rb +306 -0
  67. data/test/new_relic/agent/tc_stats_engine.rb +218 -0
  68. data/test/new_relic/agent/tc_synchronize.rb +37 -0
  69. data/test/new_relic/agent/tc_transaction_sample.rb +175 -0
  70. data/test/new_relic/agent/tc_transaction_sample_builder.rb +200 -0
  71. data/test/new_relic/agent/tc_transaction_sampler.rb +305 -0
  72. data/test/new_relic/agent/tc_worker_loop.rb +101 -0
  73. data/test/new_relic/agent/testable_agent.rb +13 -0
  74. data/test/new_relic/tc_config.rb +36 -0
  75. data/test/new_relic/tc_deployments_api.rb +37 -0
  76. data/test/new_relic/tc_environment.rb +94 -0
  77. data/test/new_relic/tc_metric_spec.rb +150 -0
  78. data/test/new_relic/tc_shim_agent.rb +9 -0
  79. data/test/new_relic/tc_stats.rb +141 -0
  80. data/test/test_helper.rb +39 -0
  81. data/test/ui/tc_newrelic_helper.rb +44 -0
  82. data/ui/controllers/newrelic_controller.rb +200 -0
  83. data/ui/helpers/google_pie_chart.rb +55 -0
  84. data/ui/helpers/newrelic_helper.rb +286 -0
  85. data/ui/views/layouts/newrelic_default.rhtml +49 -0
  86. data/ui/views/newrelic/_explain_plans.rhtml +27 -0
  87. data/ui/views/newrelic/_sample.rhtml +12 -0
  88. data/ui/views/newrelic/_segment.rhtml +28 -0
  89. data/ui/views/newrelic/_segment_row.rhtml +14 -0
  90. data/ui/views/newrelic/_show_sample_detail.rhtml +22 -0
  91. data/ui/views/newrelic/_show_sample_sql.rhtml +19 -0
  92. data/ui/views/newrelic/_show_sample_summary.rhtml +3 -0
  93. data/ui/views/newrelic/_sql_row.rhtml +11 -0
  94. data/ui/views/newrelic/_stack_trace.rhtml +30 -0
  95. data/ui/views/newrelic/_table.rhtml +12 -0
  96. data/ui/views/newrelic/explain_sql.rhtml +45 -0
  97. data/ui/views/newrelic/images/arrow-close.png +0 -0
  98. data/ui/views/newrelic/images/arrow-open.png +0 -0
  99. data/ui/views/newrelic/images/blue_bar.gif +0 -0
  100. data/ui/views/newrelic/images/gray_bar.gif +0 -0
  101. data/ui/views/newrelic/index.rhtml +37 -0
  102. data/ui/views/newrelic/javascript/transaction_sample.js +107 -0
  103. data/ui/views/newrelic/sample_not_found.rhtml +2 -0
  104. data/ui/views/newrelic/show_sample.rhtml +62 -0
  105. data/ui/views/newrelic/show_source.rhtml +3 -0
  106. data/ui/views/newrelic/stylesheets/style.css +394 -0
  107. metadata +180 -0
@@ -0,0 +1,218 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__),'..','..','test_helper'))
2
+ require 'test/unit'
3
+ ##require 'new_relic/agent/stats_engine'
4
+
5
+
6
+ module NewRelic::Agent
7
+ class StatsEngineTests < Test::Unit::TestCase
8
+ def setup
9
+ @engine = StatsEngine.new
10
+ end
11
+
12
+ def test_get_no_scope
13
+ s1 = @engine.get_stats "a"
14
+ s2 = @engine.get_stats "a"
15
+ s3 = @engine.get_stats "b"
16
+
17
+ assert_not_nil s1
18
+ assert_not_nil s2
19
+ assert_not_nil s3
20
+
21
+ assert s1 == s2
22
+ assert s1 != s3
23
+ end
24
+
25
+ def test_harvest
26
+ s1 = @engine.get_stats "a"
27
+ s2 = @engine.get_stats "c"
28
+
29
+ s1.trace_call 10
30
+ s2.trace_call 1
31
+ s2.trace_call 3
32
+
33
+ assert @engine.get_stats("a").call_count == 1
34
+ assert @engine.get_stats("a").total_call_time == 10
35
+
36
+ assert @engine.get_stats("c").call_count == 2
37
+ assert @engine.get_stats("c").total_call_time == 4
38
+
39
+ metric_data = @engine.harvest_timeslice_data({}, {}).values
40
+
41
+ # after harvest, all the metrics should be reset
42
+ assert @engine.get_stats("a").call_count == 0
43
+ assert @engine.get_stats("a").total_call_time == 0
44
+
45
+ assert @engine.get_stats("c").call_count == 0
46
+ assert @engine.get_stats("c").total_call_time == 0
47
+
48
+ metric_data = metric_data.reverse if metric_data[0].metric_spec.name != "a"
49
+
50
+ assert metric_data[0].metric_spec.name == "a"
51
+
52
+ assert metric_data[0].stats.call_count == 1
53
+ assert metric_data[0].stats.total_call_time == 10
54
+ end
55
+
56
+ def test_harvest_with_merge
57
+ s = @engine.get_stats "a"
58
+ s.trace_call 1
59
+
60
+ assert @engine.get_stats("a").call_count == 1
61
+
62
+ harvest = @engine.harvest_timeslice_data({}, {})
63
+ assert s.call_count == 0
64
+ s.trace_call 2
65
+ assert s.call_count == 1
66
+
67
+ # this calk should merge the contents of the previous harvest,
68
+ # so the stats for metric "a" should have 2 data points
69
+ harvest = @engine.harvest_timeslice_data(harvest, {})
70
+ stats = harvest.fetch(NewRelic::MetricSpec.new("a")).stats
71
+ assert stats.call_count == 2
72
+ assert stats.total_call_time == 3
73
+ end
74
+
75
+ def test_scope
76
+ @engine.push_scope "scope1"
77
+ assert @engine.peek_scope.name == "scope1"
78
+
79
+ expected = @engine.push_scope "scope2"
80
+ @engine.pop_scope expected, 0
81
+
82
+ scoped = @engine.get_stats "a"
83
+ scoped.trace_call 3
84
+
85
+ assert scoped.total_call_time == 3
86
+ unscoped = @engine.get_stats "a"
87
+
88
+ assert scoped == @engine.get_stats("a")
89
+ assert unscoped.total_call_time == 3
90
+ end
91
+
92
+
93
+ def test_simplethrowcase(depth=0)
94
+
95
+ fail "doh" if depth == 10
96
+
97
+ scope = @engine.push_scope "scope#{depth}"
98
+
99
+ begin
100
+ test_simplethrowcase(depth+1)
101
+ rescue StandardError => e
102
+ if (depth != 0)
103
+ raise e
104
+ end
105
+ ensure
106
+ @engine.pop_scope scope, 0
107
+ end
108
+
109
+ if depth == 0
110
+ assert @engine.peek_scope.nil?
111
+ end
112
+ end
113
+
114
+
115
+ def test_scope_failure
116
+ scope1 = @engine.push_scope "scope1"
117
+ @engine.push_scope "scope2"
118
+
119
+ begin
120
+ @engine.pop_scope scope1
121
+ fail "Didn't throw when scope push/pop mismatched"
122
+ rescue
123
+ # success
124
+ end
125
+ end
126
+
127
+ def test_children_time
128
+ t1 = Time.now
129
+
130
+ expected1 = @engine.push_scope "a"
131
+ sleep 0.1
132
+ t2 = Time.now
133
+
134
+ expected2 = @engine.push_scope "b"
135
+ sleep 0.2
136
+ t3 = Time.now
137
+
138
+ expected = @engine.push_scope "c"
139
+ sleep 0.3
140
+ scope = @engine.pop_scope expected, Time.now - t3
141
+
142
+ t4 = Time.now
143
+
144
+ check_time_approximate 0, scope.children_time
145
+ check_time_approximate 0.3, @engine.peek_scope.children_time
146
+
147
+ sleep 0.1
148
+ t5 = Time.now
149
+
150
+ expected = @engine.push_scope "d"
151
+ sleep 0.2
152
+ scope = @engine.pop_scope expected, Time.now - t5
153
+
154
+ t6 = Time.now
155
+
156
+ check_time_approximate 0, scope.children_time
157
+
158
+ scope = @engine.pop_scope expected2, Time.now - t2
159
+ assert_equal scope.name, 'b'
160
+
161
+ check_time_approximate (t4 - t3) + (t6 - t5), scope.children_time
162
+
163
+ scope = @engine.pop_scope expected1, Time.now - t1
164
+ assert_equal scope.name, 'a'
165
+
166
+ check_time_approximate (t6 - t2), scope.children_time
167
+ end
168
+
169
+ def test_simple_start_transaction
170
+ @engine.push_scope "scope"
171
+ @engine.start_transaction
172
+ assert @engine.peek_scope.nil?
173
+ end
174
+
175
+
176
+ # test for when the scope stack contains an element only used for tts and not metrics
177
+ def test_simple_tt_only_scope
178
+ scope1 = @engine.push_scope "a", 0, true
179
+ scope2 = @engine.push_scope "b", 10, false
180
+ scope3 = @engine.push_scope "c", 20, true
181
+
182
+ @engine.pop_scope scope3, 10
183
+ @engine.pop_scope scope2, 10
184
+ @engine.pop_scope scope1, 10
185
+
186
+ assert_equal 0, scope3.children_time
187
+ assert_equal 10, scope2.children_time
188
+ assert_equal 10, scope1.children_time
189
+ end
190
+
191
+ def test_double_tt_only_scope
192
+ scope1 = @engine.push_scope "a", 0, true
193
+ scope2 = @engine.push_scope "b", 10, false
194
+ scope3 = @engine.push_scope "c", 20, false
195
+ scope4 = @engine.push_scope "d", 30, true
196
+
197
+ @engine.pop_scope scope4, 10
198
+ @engine.pop_scope scope3, 10
199
+ @engine.pop_scope scope2, 10
200
+ @engine.pop_scope scope1, 10
201
+
202
+ assert_equal 0, scope4.children_time
203
+ assert_equal 10, scope3.children_time
204
+ assert_equal 10, scope2.children_time
205
+ assert_equal 10, scope1.children_time
206
+ end
207
+
208
+
209
+ private
210
+ def check_time_approximate(expected, actual)
211
+ assert((expected - actual).abs < 0.01, "Expected #{expected}, got #{actual}")
212
+ end
213
+
214
+ end
215
+
216
+
217
+
218
+ end
@@ -0,0 +1,37 @@
1
+ require 'test/unit'
2
+ ##require 'new_relic/agent/testable_agent'
3
+
4
+ class TestSync
5
+ include NewRelic::Agent::Synchronize
6
+ end
7
+
8
+ class AgentSynchronizeTests < Test::Unit::TestCase
9
+
10
+
11
+ def test_sync
12
+ t = TestSync.new
13
+
14
+ worked = false
15
+ t.synchronize_sync do
16
+ worked = true
17
+ end
18
+
19
+ assert worked
20
+
21
+ worked = false
22
+
23
+ t.synchronize_mutex do
24
+ worked = true
25
+ end
26
+
27
+ assert worked
28
+
29
+ worked = false
30
+
31
+ t.synchronize_thread do
32
+ worked = true
33
+ end
34
+
35
+ assert worked
36
+ end
37
+ end
@@ -0,0 +1,175 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__),'..','..','test_helper'))
2
+ require 'new_relic/agent/mock_ar_connection'
3
+ ##require 'new_relic/agent/testable_agent'
4
+ ##require 'new_relic/agent/transaction_sampler'
5
+ ##require 'new_relic/transaction_sample'
6
+ require 'test/unit'
7
+
8
+ ::SQL_STATEMENT = "SELECT * from sandwiches"
9
+
10
+ class NewRelic::TransationSampleTests < Test::Unit::TestCase
11
+
12
+ def setup
13
+ super
14
+ NewRelic::Agent::Agent.instance.shutdown
15
+ NewRelic::Agent::Agent.instance.start(:test, :test)
16
+ end
17
+ def shutdown
18
+ NewRelic::Agent::Agent.instance.shutdown
19
+ super
20
+ end
21
+ def initialize(test)
22
+ super(test)
23
+ end
24
+
25
+ def test_sql
26
+ assert ActiveRecord::Base.test_connection({}).disconnected == false
27
+
28
+ t = get_sql_transaction(::SQL_STATEMENT, ::SQL_STATEMENT)
29
+
30
+ s = t.prepare_to_send(:obfuscate_sql => true, :explain_enabled => true, :explain_sql => 0.00000001)
31
+
32
+ explain_count = 0
33
+
34
+ s.each_segment do |segment|
35
+
36
+ if segment.params[:explanation]
37
+ explanations = segment.params[:explanation]
38
+
39
+ explanations.each do |explanation|
40
+ assert_equal Array, explanation.class
41
+ assert_equal "EXPLAIN #{::SQL_STATEMENT}", explanation[0][0]
42
+ explain_count += 1
43
+ end
44
+ end
45
+ end
46
+
47
+ assert_equal 2, explain_count
48
+ assert ActiveRecord::Base.test_connection({}).disconnected
49
+ end
50
+
51
+
52
+ def test_disable_sql
53
+ t = nil
54
+ NewRelic::Agent.disable_sql_recording do
55
+ t = get_sql_transaction(::SQL_STATEMENT, ::SQL_STATEMENT)
56
+ end
57
+
58
+ s = t.prepare_to_send(:obfuscate_sql => true, :explain_sql => 0.00000001)
59
+
60
+ s.each_segment do |segment|
61
+ fail if segment.params[:explanation] || segment.params[:obfuscated_sql]
62
+ end
63
+ end
64
+
65
+
66
+ def test_disable_tt
67
+ NewRelic::Agent.disable_transaction_tracing do
68
+ t = get_sql_transaction(::SQL_STATEMENT, ::SQL_STATEMENT)
69
+ assert t.nil?
70
+ end
71
+
72
+ t = get_sql_transaction(::SQL_STATEMENT, ::SQL_STATEMENT)
73
+ assert t
74
+ end
75
+
76
+
77
+ def test_record_sql_off
78
+ t = get_sql_transaction(::SQL_STATEMENT, ::SQL_STATEMENT)
79
+
80
+ s = t.prepare_to_send(:obfuscate_sql => true, :explain_sql => 0.00000001, :record_sql => :off)
81
+
82
+ s.each_segment do |segment|
83
+ fail if segment.params[:explanation] || segment.params[:obfuscated_sql] || segment.params[:sql]
84
+ end
85
+ end
86
+
87
+
88
+ def test_record_sql_raw
89
+ t = get_sql_transaction(::SQL_STATEMENT, ::SQL_STATEMENT)
90
+
91
+ s = t.prepare_to_send(:obfuscate_sql => true, :explain_sql => 0.00000001, :record_sql => :raw)
92
+
93
+ got_one = false
94
+ s.each_segment do |segment|
95
+ fail if segment.params[:obfuscated_sql]
96
+ got_one = got_one || segment.params[:explanation] || segment.params[:sql]
97
+ end
98
+
99
+ assert got_one
100
+ end
101
+
102
+
103
+ def test_record_sql_obfuscated
104
+ t = get_sql_transaction(::SQL_STATEMENT, ::SQL_STATEMENT)
105
+
106
+ s = t.prepare_to_send(:obfuscate_sql => true, :explain_sql => 0.00000001, :record_sql => :obfuscated)
107
+
108
+ got_one = false
109
+ s.each_segment do |segment|
110
+ fail if segment.params[:sql]
111
+ got_one = got_one || segment.params[:explanation] || segment.params[:sql_obfuscated]
112
+ end
113
+
114
+ assert got_one
115
+ end
116
+
117
+
118
+ def test_sql_throw
119
+ ActiveRecord::Base.test_connection({}).throw = true
120
+
121
+ t = get_sql_transaction(::SQL_STATEMENT, ::SQL_STATEMENT)
122
+
123
+ # the sql connection will throw
124
+ t.prepare_to_send(:obfuscate_sql => true, :explain_sql => 0.00000001)
125
+ end
126
+
127
+ def test_exclusive_duration
128
+ t = NewRelic::TransactionSample.new
129
+
130
+ t.params[:test] = "hi"
131
+
132
+ s1 = t.create_segment(1.0, "controller")
133
+
134
+ t.root_segment.add_called_segment(s1)
135
+
136
+ s2 = t.create_segment(2.0, "AR1")
137
+
138
+ s2.params[:test] = "test"
139
+
140
+ s2.to_s
141
+
142
+ s1.add_called_segment(s2)
143
+
144
+ s2.end_trace 3.0
145
+ s1.end_trace 4.0
146
+
147
+ t.to_s
148
+
149
+ assert_equal 3.0, s1.duration
150
+ assert_equal 2.0, s1.exclusive_duration
151
+ end
152
+
153
+
154
+
155
+ private
156
+ def get_sql_transaction(*sql)
157
+ sampler = NewRelic::Agent::TransactionSampler.new(NewRelic::Agent.instance)
158
+ sampler.notice_first_scope_push Time.now.to_f
159
+ sampler.notice_transaction '/path', nil, :jim => "cool"
160
+ sampler.notice_push_scope "a"
161
+
162
+ sampler.notice_transaction '/path/2', nil, :jim => "cool"
163
+
164
+ sql.each {|sql_statement| sampler.notice_sql(sql_statement, {:adapter => "test"}, 0 ) }
165
+
166
+ sleep 1.0
167
+
168
+ sampler.notice_pop_scope "a"
169
+ sampler.notice_scope_empty
170
+
171
+ sampler.get_samples[0]
172
+ end
173
+
174
+
175
+ end
@@ -0,0 +1,200 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__),'..','..','test_helper'))
2
+ ##require 'new_relic/agent/transaction_sampler'
3
+ require 'test/unit'
4
+
5
+ module NewRelic
6
+ module Agent
7
+ class TransationSampleBuilderTests < Test::Unit::TestCase
8
+
9
+ def setup
10
+ @builder = TransactionSampleBuilder.new
11
+ end
12
+
13
+ def test_build_sample
14
+ build_segment("a") do
15
+ build_segment("aa") do
16
+ build_segment("aaa")
17
+ end
18
+ build_segment("ab") do
19
+ build_segment("aba") do
20
+ build_segment("abaa")
21
+ end
22
+ build_segment("aba")
23
+ build_segment("abc") do
24
+ build_segment("abca")
25
+ build_segment("abcd")
26
+ end
27
+ end
28
+ end
29
+ build_segment "b"
30
+ build_segment "c" do
31
+ build_segment "ca"
32
+ build_segment "cb" do
33
+ build_segment "cba"
34
+ end
35
+ end
36
+
37
+ @builder.finish_trace(Time.now.to_f)
38
+ validate_builder
39
+ end
40
+
41
+ def test_freeze
42
+ build_segment "a" do
43
+ build_segment "aa"
44
+ end
45
+
46
+ begin
47
+ builder.sample
48
+ assert false
49
+ rescue Exception => e
50
+ # expected
51
+ end
52
+
53
+ @builder.finish_trace(Time.now.to_f)
54
+
55
+ validate_builder
56
+
57
+ begin
58
+ build_segment "b"
59
+ assert_false
60
+ rescue TypeError => e
61
+ # expected
62
+ end
63
+ end
64
+
65
+ # this is really a test for transaction sample
66
+ def test_omit_segments_with
67
+ build_segment "Controller/my_controller/index" do
68
+ sleep 0.010
69
+
70
+ build_segment "Rails/Application Code Loading" do
71
+ sleep 0.020
72
+
73
+ build_segment "foo/bar" do
74
+ sleep 0.010
75
+ end
76
+ end
77
+
78
+ build_segment "a" do
79
+ build_segment "ab"
80
+ sleep 0.010
81
+ end
82
+ build_segment "b" do
83
+ build_segment "ba"
84
+ sleep 0.05
85
+ build_segment "bb"
86
+ build_segment "bc" do
87
+ build_segment "bca"
88
+ sleep 0.05
89
+ end
90
+ end
91
+ build_segment "c"
92
+ end
93
+ @builder.finish_trace(Time.now.to_f)
94
+
95
+ validate_builder false
96
+
97
+ sample = @builder.sample
98
+
99
+ should_be_a_copy = sample.omit_segments_with('OMIT NOTHING')
100
+ validate_segment should_be_a_copy.root_segment, false
101
+
102
+ assert sample.to_s == should_be_a_copy.to_s
103
+
104
+ without_code_loading = sample.omit_segments_with('Rails/Application Code Loading')
105
+ validate_segment without_code_loading.root_segment, false
106
+
107
+ # after we take out code loading, the delta should be approximately
108
+ # 30 milliseconds
109
+ delta = (sample.duration - without_code_loading.duration).to_ms
110
+
111
+ # allow a few milliseconds for slop just in case this is running on a 386 ;)
112
+ assert delta >= 30, "delta #{delta} should be between 30 and 50"
113
+ # disable this test for a couple days:
114
+ assert delta <= 50, "delta #{delta} should be between 30 and 50"
115
+
116
+ # ensure none of the segments have this regex
117
+ without_code_loading.each_segment do |segment|
118
+ assert_nil segment.metric_name =~ /Rails\/Application Code Loading/
119
+ end
120
+ end
121
+ def test_unbalanced_handling
122
+ assert_raise RuntimeError do
123
+ build_segment("a") do
124
+ begin
125
+ build_segment("aa") do
126
+ build_segment("aaa") do
127
+ raise "a problem"
128
+ end
129
+ end
130
+ rescue; end
131
+ end
132
+ end
133
+ end
134
+ def test_marshal
135
+ build_segment "a" do
136
+ build_segment "ab"
137
+ end
138
+ build_segment "b" do
139
+ build_segment "ba"
140
+ build_segment "bb"
141
+ build_segment "bc" do
142
+ build_segment "bca"
143
+ end
144
+ end
145
+ build_segment "c"
146
+
147
+ @builder.finish_trace(Time.now.to_f)
148
+ validate_builder
149
+
150
+ dump = Marshal.dump @builder.sample
151
+ sample = Marshal.restore(dump)
152
+ validate_segment(sample.root_segment)
153
+ end
154
+
155
+ def test_parallel_first_level_segments
156
+ build_segment "a" do
157
+ build_segment "ab"
158
+ end
159
+ build_segment "b"
160
+ build_segment "c"
161
+
162
+ @builder.finish_trace(Time.now.to_f)
163
+ validate_builder
164
+ end
165
+
166
+ def validate_builder(check_names = true)
167
+ validate_segment @builder.sample.root_segment, check_names
168
+ end
169
+
170
+ def validate_segment(s, check_names = true)
171
+ parent = s.parent_segment
172
+
173
+ unless p.nil?
174
+ assert p.called_segments.include?(s)
175
+ assert p.metric_name.length == s.metric_name.length - 1 if check_names
176
+ assert p.metric_name < s.metric_name if check_names
177
+ assert p.entry_timestamp <= s.entry_timestamp
178
+ end
179
+
180
+ assert s.exit_timestamp >= s.entry_timestamp
181
+
182
+ children = s.called_segments
183
+ last_segment = s
184
+ children.each do |child|
185
+ assert child.metric_name > last_segment.metric_name if check_names
186
+ assert child.entry_timestamp >= last_segment.entry_timestamp
187
+ last_metric = child
188
+
189
+ validate_segment(child)
190
+ end
191
+ end
192
+
193
+ def build_segment(metric, time = 0, &proc)
194
+ @builder.trace_entry(metric, Time.now.to_f)
195
+ proc.call if proc
196
+ @builder.trace_exit(metric, Time.now.to_f)
197
+ end
198
+ end
199
+ end
200
+ end