From f263a4f50981e26408f271018aaf97ebbb8440d3 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Fri, 12 Jul 2024 03:37:24 -0400 Subject: [PATCH] di bench --- benchmarks/di_instrument_method.rb | 44 ++++++++++++++++++++++++++++++ benchmarks/di_target.rb | 15 ++++++++++ 2 files changed, 59 insertions(+) create mode 100644 benchmarks/di_target.rb diff --git a/benchmarks/di_instrument_method.rb b/benchmarks/di_instrument_method.rb index f19592c8d72..c88cc8dfa29 100644 --- a/benchmarks/di_instrument_method.rb +++ b/benchmarks/di_instrument_method.rb @@ -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 diff --git a/benchmarks/di_target.rb b/benchmarks/di_target.rb new file mode 100644 index 00000000000..fb8fabf8c20 --- /dev/null +++ b/benchmarks/di_target.rb @@ -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