-
Notifications
You must be signed in to change notification settings - Fork 402
Expand file tree
/
Copy pathdi_instrument.rb
More file actions
313 lines (259 loc) · 9.62 KB
/
di_instrument.rb
File metadata and controls
313 lines (259 loc) · 9.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#
# "Instrumentation" part of Dynamic Instrumentation benchmarks.
#
# Typical result:
#
# Comparison:
# no instrumentation: 589490.0 i/s
# method instrumentation - cleared: 545807.2 i/s - 1.08x slower
# line instrumentation - cleared: 539686.5 i/s - 1.09x slower
# no instrumentation - again: 535761.0 i/s - 1.10x slower
# method instrumentation: 129159.5 i/s - 4.56x slower
# line instrumentation - targeted: 128848.6 i/s - 4.58x slower
# line instrumentation: 10771.7 i/s - 54.73x slower
#
# Targeted line and method instrumentations have similar performance at
# about 25% of baseline. Note that the instrumented method is fairly
# small and probably runs very quickly by itself, so while this is not the
# worst possible case for instrumentation (that would be an empty method),
# likely the vast majority of real world uses of DI would have way expensive
# target code and the relative overhead of instrumentation will be significantly
# lower than it is in this benchmark.
#
# Untargeted line instrumentation is extremely slow, too slow to be usable.
#
# In theory, after instrumentation is removed, performance should return to
# the baseline. We are currently observing about a 6-10% performance loss.
# Two theories for why this is so:
# 1. Some overhead remains in the code - to be investigated.
# 2. The benchmarks were run on a laptop, and during the benchmarking
# process the CPU is heating up and it can't turbo to the same speeds at
# the end of the run as it can at the beginning. Meaning the observed 6-10%
# slowdown at the end is an environmental issue and not an implementation
# problem.
#
# Used to quickly run benchmark under RSpec as part of the usual test suite, to validate it didn't bitrot
VALIDATE_BENCHMARK_MODE = ENV['VALIDATE_BENCHMARK'] == 'true'
return unless __FILE__ == $PROGRAM_NAME || VALIDATE_BENCHMARK_MODE
require 'benchmark/ips'
require 'datadog'
# Need to require datadog/di explicitly because dynamic instrumentation is not
# currently integrated into the Ruby tracer due to being under development.
require 'datadog/di'
begin
require 'datadog/di/proc_responder'
rescue LoadError
# Old tree
end
class DIInstrumentBenchmark
class Target
def test_method
# Perform some work to take up time
SecureRandom.uuid
end
def not_instrumented
SecureRandom.uuid
end
# This method must have an executable line as its first line,
# otherwise line instrumentation won't work.
# The code in this method should be identical to test_method above.
# The two methods are separate so that instrumentation targets are
# different, to avoid a false positive if line instrumemntation fails
# to work and method instrumentation isn't cleared and continues to
# invoke the callback.
def test_method_for_line_probe
SecureRandom.uuid
end
end
attr_reader :instrumenter
def logger
@logger ||= Logger.new($stderr)
end
def configure
settings = Datadog.configuration
yield settings if block_given?
redactor = Datadog::DI::Redactor.new(settings)
serializer = Datadog::DI::Serializer.new(settings, redactor)
@instrumenter = Datadog::DI::Instrumenter.new(settings, serializer, logger,
code_tracker: Datadog::DI.code_tracker)
end
def run_benchmark
configure
m = Target.instance_method(:test_method_for_line_probe)
file, line = m.source_location
Benchmark.ips do |x|
benchmark_time = VALIDATE_BENCHMARK_MODE ? {time: 0.01, warmup: 0} : {time: 10, warmup: 2}
x.config(
**benchmark_time,
)
x.report('no instrumentation') do
Target.new.test_method
end
x.save! "#{File.basename(__FILE__, '.rb')}-results.json" unless VALIDATE_BENCHMARK_MODE
x.compare!
end
calls = 0
probe = Datadog::DI::Probe.new(id: 1, type: :log,
type_name: 'DIInstrumentBenchmark::Target', method_name: 'test_method')
executed_proc = lambda do |context|
calls += 1
end
if defined?(Datadog::DI::ProcResponder)
responder = Datadog::DI::ProcResponder.new(executed_proc)
rv = instrumenter.hook_method(probe, responder)
else
rv = instrumenter.hook_method(probe, &executed_proc)
end
unless rv
raise "Method probe was not successfully installed"
end
Benchmark.ips do |x|
benchmark_time = VALIDATE_BENCHMARK_MODE ? {time: 0.01, warmup: 0} : {time: 10, warmup: 2}
x.config(
**benchmark_time,
)
x.report('method instrumentation') do
Target.new.test_method
end
x.save! "#{File.basename(__FILE__, '.rb')}-results.json" unless VALIDATE_BENCHMARK_MODE
x.compare!
end
if calls < 1
raise "Method instrumentation did not work - callback was never invoked"
end
if calls < 1000 && !VALIDATE_BENCHMARK_MODE
raise "Expected at least 1000 calls to the method, got #{calls}"
end
instrumenter.unhook(probe)
# We benchmark untargeted and targeted trace points; untargeted ones
# are prohibited by default, permit them.
# In order to install untargeted trace point, we currently need to
# disable code tracking.
Datadog::DI.deactivate_tracking!
configure do |c|
c.dynamic_instrumentation.internal.untargeted_trace_points = true
end
calls = 0
probe = Datadog::DI::Probe.new(id: 1, type: :log,
file: file, line_no: line + 1)
if defined?(Datadog::DI::ProcResponder)
responder = Datadog::DI::ProcResponder.new(executed_proc)
rv = instrumenter.hook_line(probe, responder)
else
rv = instrumenter.hook_line(probe, &executed_proc)
end
unless rv
raise "Line probe (in method) was not successfully installed"
end
Benchmark.ips do |x|
benchmark_time = VALIDATE_BENCHMARK_MODE ? {time: 0.01, warmup: 0} : {time: 10, warmup: 2}
x.config(
**benchmark_time,
)
x.report('line instrumentation - untargeted') do
Target.new.test_method_for_line_probe
end
x.save! "#{File.basename(__FILE__, '.rb')}-results.json" unless VALIDATE_BENCHMARK_MODE
x.compare!
end
if calls < 1
raise "Line instrumentation did not work - callback was never invoked"
end
if calls < 1000 && !VALIDATE_BENCHMARK_MODE
raise "Expected at least 1000 calls to the method, got #{calls}"
end
instrumenter.unhook(probe)
Datadog::DI.activate_tracking!
configure do |c|
c.dynamic_instrumentation.internal.untargeted_trace_points = false
end
if defined?(DITarget)
raise "DITarget is already defined, this should not happen"
end
require_relative 'support/di_target'
unless defined?(DITarget)
raise "DITarget is not defined, this should not happen"
end
m = DITarget.instance_method(:test_method_for_line_probe)
targeted_file, targeted_line = m.source_location
calls = 0
probe = Datadog::DI::Probe.new(id: 1, type: :log,
file: targeted_file, line_no: targeted_line + 1)
rv = if defined?(Datadog::DI::ProcResponder)
instrumenter.hook_line(probe, responder)
else
instrumenter.hook_line(probe, &executed_proc)
end
unless rv
raise "Line probe (targeted) was not successfully installed"
end
Benchmark.ips do |x|
benchmark_time = VALIDATE_BENCHMARK_MODE ? {time: 0.01, warmup: 0} : {time: 10, warmup: 2}
x.config(
**benchmark_time,
)
x.report('line instrumentation - targeted') do
DITarget.new.test_method_for_line_probe
end
x.save! "#{File.basename(__FILE__, '.rb')}-results.json" unless VALIDATE_BENCHMARK_MODE
x.compare!
end
if calls < 1
raise "Targeted line instrumentation did not work - callback was never invoked"
end
if calls < 1000 && !VALIDATE_BENCHMARK_MODE
raise "Expected at least 1000 calls to the method, got #{calls}"
end
# Now, remove all installed hooks and check that the performance of
# target code is approximately what it was prior to hook installation.
instrumenter.unhook(probe)
calls = 0
Benchmark.ips do |x|
benchmark_time = VALIDATE_BENCHMARK_MODE ? {time: 0.01, warmup: 0} : {time: 10, warmup: 2}
x.config(
**benchmark_time,
)
# This benchmark should produce identical results to the
# "no instrumentation" benchmark.
x.report('method instrumentation - cleared') do
Target.new.test_method
end
x.save! "#{File.basename(__FILE__, '.rb')}-results.json" unless VALIDATE_BENCHMARK_MODE
x.compare!
end
if calls != 0
raise "Method instrumentation was not cleared (#{calls} calls recorded)"
end
Benchmark.ips do |x|
benchmark_time = VALIDATE_BENCHMARK_MODE ? {time: 0.01, warmup: 0} : {time: 10, warmup: 2}
x.config(
**benchmark_time,
)
# This benchmark should produce identical results to the
# "no instrumentation" benchmark.
x.report('line instrumentation - cleared') do
Target.new.test_method_for_line_probe
end
x.save! "#{File.basename(__FILE__, '.rb')}-results.json" unless VALIDATE_BENCHMARK_MODE
x.compare!
end
if calls != 0
raise "Line instrumentation was not cleared (#{calls} calls recorded)"
end
Benchmark.ips do |x|
benchmark_time = VALIDATE_BENCHMARK_MODE ? {time: 0.01, warmup: 0} : {time: 10, warmup: 2}
x.config(
**benchmark_time,
)
x.report('no instrumentation - again') do
Target.new.not_instrumented
end
x.save! "#{File.basename(__FILE__, '.rb')}-results.json" unless VALIDATE_BENCHMARK_MODE
x.compare!
end
end
end
puts "Current pid is #{Process.pid}"
DIInstrumentBenchmark.new.instance_exec do
run_benchmark
end