Skip to content

Commit

Permalink
[PROF-9180] Benchmarks for allocation profiling
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexJF committed Feb 23, 2024
1 parent 81e6060 commit 329295c
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .gitlab/benchmarks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ only-profiling-gc:
DD_PROFILING_NO_SIGNALS_WORKAROUND_ENABLED: "false"
DD_PROFILING_FORCE_ENABLE_GC: "true"

only-profiling-alloc:
extends: .benchmarks
variables:
DD_BENCHMARKS_CONFIGURATION: only-profiling
DD_PROFILING_ENABLED: "true"
DD_PROFILING_NO_SIGNALS_WORKAROUND_ENABLED: "false"
DD_PROFILING_EXPERIMENTAL_ALLOCATION_ENABLED: "true"

profiling-and-tracing:
extends: .benchmarks
variables:
Expand Down
104 changes: 104 additions & 0 deletions benchmarks/profiler_memory_sample_serialize.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# 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'

require 'libdatadog'

puts "Libdatadog from: #{Libdatadog.pkgconfig_folder}"

# This benchmark measures the performance of sampling + serializing memory profiles. It enables us to evaluate changes to
# the profiler and/or libdatadog that may impact both individual samples, as well as samples over time.
#
METRIC_VALUES = { 'cpu-time' => 0, 'cpu-samples' => 0, 'wall-time' => 0, 'alloc-samples' => 1, 'timeline' => 0 }.freeze
OBJECT_CLASS = 'object'.freeze

def sample_object(recorder, depth = 0)
if depth <= 0
Datadog::Profiling::StackRecorder::Testing._native_track_object(
recorder,
Object.new,
1,
OBJECT_CLASS,
)
Datadog::Profiling::Collectors::Stack::Testing._native_sample(
Thread.current,
recorder,
METRIC_VALUES,
[],
[],
400,
false
)
else
sample_object(recorder, depth - 1)
end
end

class ProfilerMemorySampleSerializeBenchmark
def create_profiler
@heap_samples_enabled = ENV['HEAP_SAMPLES'] == 'true'
@heap_size_enabled = ENV['HEAP_SIZE'] == 'true'
@recorder = Datadog::Profiling::StackRecorder.new(
cpu_time_enabled: false,
alloc_samples_enabled: true,
heap_samples_enabled: @heap_samples_enabled,
heap_size_enabled: @heap_size_enabled,
heap_sample_every: 1,
timeline_enabled: false,
)
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_memory_sample_serialize')
)

x.report("sample #{ENV['CONFIG']} heap_samples=#{@heap_samples_enabled} heap_size=#{@heap_size_enabled}") do
samples_per_second = 100
simulate_seconds = 60

(samples_per_second * simulate_seconds).times do |i|
sample_object(@recorder, i % 400)
end

@recorder.serialize
nil
end

x.save! 'profiler_memory_sample_serialize-results.json' unless VALIDATE_BENCHMARK_MODE
x.compare!
end

@recorder.serialize
end

def run_forever
loop do
1000.times do
Datadog::Profiling::Collectors::ThreadContext::Testing._native_sample(@collector, PROFILER_OVERHEAD_STACK_THREAD)
end
@recorder.serialize
print '.'
end
end
end

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

ProfilerMemorySampleSerializeBenchmark.new.instance_exec do
create_profiler
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 @@ -20,4 +20,8 @@
describe 'profiler_sample_serialize' do
it('runs without raising errors') { expect_in_fork { load './benchmarks/profiler_sample_serialize.rb' } }
end

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

0 comments on commit 329295c

Please sign in to comment.