Skip to content

Commit

Permalink
di bench
Browse files Browse the repository at this point in the history
  • Loading branch information
p committed Jul 12, 2024
1 parent 7a8f393 commit f263a4f
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
44 changes: 44 additions & 0 deletions benchmarks/di_instrument_method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,50 @@ def run_benchmark
raise "Expected at least 1000 calls to the method, got #{calls}"
end

require 'datadog/di/init'
if defined?(DITarget)
raise "DITarget is already defined, this should not happen"
end
require_relative '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

hook_manager.clear_hooks
calls = 0
hook_manager.hook_line(targeted_file, targeted_line + 1) do
calls += 1
end

Benchmark.ips do |x|
benchmark_time = VALIDATE_BENCHMARK_MODE ? { time: 0.01, warmup: 0 } : { time: 10, warmup: 2 }
x.config(
**benchmark_time,
suite: report_to_dogstatsd_if_enabled_via_environment_variable(benchmark_name: 'profiler_gc')
)

x.report('line instrumentation - targeted') do
DITarget.new.test_method_for_line_probe
end

x.save! 'di-instrument-method-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.

hook_manager.clear_hooks
calls = 0

Expand Down
15 changes: 15 additions & 0 deletions benchmarks/di_target.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# This class must live in a separate file so that it can be loaded
# after code tracking for dynamic instrumentation is enabled.
class DITarget
# 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
# DIInstrumentMethodBenchmark#test_method.
# 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

0 comments on commit f263a4f

Please sign in to comment.