Skip to content

Commit

Permalink
Merge pull request #2450 from DataDog/ivoanjo/add-sample-loop-benchma…
Browse files Browse the repository at this point in the history
…rk-new-profiler

Add benchmark to test new profiler
  • Loading branch information
ivoanjo authored Dec 1, 2022
2 parents 970c87a + 10809ec commit 1a454cc
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
68 changes: 68 additions & 0 deletions benchmarks/profiler_sample_loop_v2.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# typed: ignore

# 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 'ddtrace'
require 'pry'
require_relative 'dogstatsd_reporter'

# This benchmark measures the performance of the main stack sampling loop of the profiler

class ProfilerSampleLoopBenchmark
def create_profiler
@recorder = Datadog::Profiling::StackRecorder.new
@collector = Datadog::Profiling::Collectors::CpuAndWallTime.new(recorder: @recorder, max_frames: 400, tracer: nil)
end

def thread_with_very_deep_stack(depth: 500)
deep_stack = proc do |n|
if n > 0
deep_stack.call(n - 1)
else
sleep
end
end

Thread.new { deep_stack.call(depth) }.tap { |t| t.name = "Deep stack #{depth}" }
end

def run_benchmark
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_sample_loop_v2'))

x.report("stack collector #{ENV['CONFIG']}") do
Datadog::Profiling::Collectors::CpuAndWallTime::Testing._native_sample(@collector)
end

x.save! 'profiler-sample-loop-v2-results.json' unless VALIDATE_BENCHMARK_MODE
x.compare!
end

@recorder.serialize
end

def run_forever
while true
1000.times { Datadog::Profiling::Collectors::CpuAndWallTime::Testing._native_sample(@collector) }
@recorder.serialize
print '.'
end
end
end

puts "Current pid is #{Process.pid}"

ProfilerSampleLoopBenchmark.new.instance_exec do
create_profiler
4.times { thread_with_very_deep_stack }
if ARGV.include?('--forever')
run_forever
else
run_benchmark
end
end
4 changes: 4 additions & 0 deletions spec/datadog/profiling/validate_benchmarks_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
it('runs without raising errors') { expect_in_fork { load './benchmarks/profiler_sample_loop.rb' } }
end

describe 'profiler_sample_loop_v2' do
it('runs without raising errors') { expect_in_fork { load './benchmarks/profiler_sample_loop_v2.rb' } }
end

describe 'profiler_http_transport' do
it('runs without raising errors') { expect_in_fork { load './benchmarks/profiler_http_transport.rb' } }
end
Expand Down

0 comments on commit 1a454cc

Please sign in to comment.