Skip to content

Commit

Permalink
Merge pull request #39 from DataDog/ivoanjo/prof-9926-ruby-test-exten…
Browse files Browse the repository at this point in the history
…sion-dir-rpath

[PROF-9226] Add test for Ruby profiler when extension dir is moved and a relative rpath is needed
  • Loading branch information
ivoanjo authored Jul 16, 2024
2 parents bb5d054 + 9ddc142 commit cf4f59f
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 1 deletion.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#==============================================================================#
# Directories to ignore (do not add trailing '/'s, they skip symlinks).
#==============================================================================#
binaries/*
data/*

.idea
binaries/*
gems.locked
21 changes: 21 additions & 0 deletions scenarios/ruby_extension_dir_and_rpath/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
FROM ruby:3.3

ENV DD_TRACE_DEBUG true
ENV DD_PROFILING_PPROF_PREFIX="/app/data/profiles_"
ENV EXECUTION_TIME_SEC 5

# Copy the Ruby program into the container
COPY ./scenarios/ruby_extension_dir_and_rpath/gems.rb ./scenarios/ruby_basic/main.rb /app/
RUN chmod 644 /app/*

# Set the working directory to the location of the program
WORKDIR /app

RUN bundle config set --local path 'vendor/bundle'
RUN bundle install
RUN rm -f vendor/bundle/ruby/*/gems/*/lib/datadog*so vendor/bundle/ruby/*/bundler/gems/*/lib/datadog*so
RUN bundle config set --local path 'vendor-moved/bundle'
RUN mv vendor vendor-moved

# Run the program when the container starts
CMD bundle exec ddprofrb exec ruby main.rb
21 changes: 21 additions & 0 deletions scenarios/ruby_extension_dir_and_rpath/expected_profile.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"test_name":"ruby_basic",
"stacks": [
{
"profile-type": "cpu-time",
"stack-content":
[
{
"regular_expression": "\u003cmain\u003e;a",
"percent": 33,
"error_margin": 20
},
{
"regular_expression": "\u003cmain\u003e;b",
"percent": 66,
"error_margin": 20
}
]
}
]
}
3 changes: 3 additions & 0 deletions scenarios/ruby_extension_dir_and_rpath/gems.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
source 'https://rubygems.org'

gem 'datadog'
51 changes: 51 additions & 0 deletions scenarios/ruby_extension_dir_and_rpath/main.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
require 'timeout'

# The Ruby profiler does not (yet) include a way of exporting a pprof to a file, so we implement it here:
class ExportToFile
PPROF_PREFIX = ENV.fetch('DD_PROFILING_PPROF_PREFIX')

def export(flush)
File.write("#{PPROF_PREFIX}#{flush.start.strftime('%Y%m%dT%H%M%SZ')}.pprof", flush.pprof_data)
true
end
end

Datadog.configure do |c|
c.profiling.enabled = true
c.profiling.exporter.transport = ExportToFile.new
end

Datadog::Profiling.wait_until_running

def a
x = 0
i = 0
while i < 10_000_000
x += i
i += 1
end
end

def b
x = 0
i = 0
while i < 20_000_000
x += i
i += 1
end
end

test_duration = 5
exec_time_env = ENV['EXECUTION_TIME_SEC']
if exec_time_env
test_duration = exec_time_env.to_i
exit(1) if test_duration == 0
end

puts "Executable #{__FILE__} starting for #{test_duration} seconds"
end_time = Time.now + test_duration
while Time.now < end_time
a
b
end
puts "Executable #{__FILE__} finished successfully"

0 comments on commit cf4f59f

Please sign in to comment.