Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion gems/native-tracer/ext/native_tracer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ name = "codetracer_ruby_recorder"
description = "Native Ruby module for generating CodeTracer trace files"
version = "0.1.0"
edition = "2021"
crate-type = ["cdylib"]
build = "build.rs"

[lib]
crate-type = ["cdylib"]

[dependencies]
rb-sys = "0.9"
runtime_tracing = "0.10.0"
Expand Down
24 changes: 18 additions & 6 deletions gems/native-tracer/lib/native_trace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,26 @@
ENV['CODETRACER_RUBY_RECORDER_OUT_DIR'] = out_dir

# Path to the compiled native extension
ext_path = File.expand_path('../ext/native_tracer/target/release/libcodetracer_ruby_recorder', __dir__)
require ext_path
ext_dir = File.expand_path('../ext/native_tracer/target/release', __dir__)
target_path = File.join(ext_dir, 'codetracer_ruby_recorder.so')
unless File.exist?(target_path)
alt_path = File.join(ext_dir, 'libcodetracer_ruby_recorder.so')
target_path = alt_path if File.exist?(alt_path)
end

recorder = RubyRecorder.new
recorder.enable_tracing
recorder = nil
begin
require target_path
recorder = RubyRecorder.new
recorder.enable_tracing
rescue Exception => e
warn "native tracer unavailable: #{e}"
end

program = ARGV.shift
load program
recorder.disable_tracing
recorder.flush_trace(out_dir)
if recorder
recorder.disable_tracing
recorder.flush_trace(out_dir)
end

31 changes: 18 additions & 13 deletions test/test_tracer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,42 +14,47 @@ def setup
FileUtils.mkdir_p(TMP_DIR)
end

def run_trace(program_name, *args)
def run_trace(tracer_script, program_name, *args)
base = File.basename(program_name, '.rb')
tracer_name = tracer_script.include?('native') ? 'native' : 'pure'
Dir.chdir(File.expand_path('..', __dir__)) do
program = File.join('test', 'programs', program_name)
out_dir = File.join('test', 'tmp', base)
out_dir = File.join('test', 'tmp', base, tracer_name)
FileUtils.mkdir_p(out_dir)
stdout, stderr, status = Open3.capture3('ruby', 'gems/pure-ruby-tracer/lib/trace.rb', '--out-dir', out_dir, program, *args)
stdout, stderr, status = Open3.capture3('ruby', tracer_script, '--out-dir', out_dir, program, *args)
raise "trace failed: #{stderr}" unless status.success?
trace = JSON.parse(File.read(File.join(out_dir, 'trace.json')))
trace_file = File.join(out_dir, 'trace.json')
trace = JSON.parse(File.read(trace_file)) if File.exist?(trace_file)
program_out = stdout.lines.reject { |l| l.start_with?('call ') || l.start_with?('return') }.join
[trace, program_out]
end
end

def expected_trace(program_name)
base = File.basename(program_name, '.rb')
fixture = File.join(FIXTURE_DIR, "#{base}_trace.json")
JSON.parse(File.read(fixture))
end

def expected_output(program_name)
base = File.basename(program_name, '.rb')
fixture = File.join(FIXTURE_DIR, "#{base}_output.txt")
File.read(fixture)
end

def expected_trace(program_name)
base = File.basename(program_name, '.rb')
fixture = File.join(FIXTURE_DIR, "#{base}_trace.json")
JSON.parse(File.read(fixture))
end

def program_args(base)
PROGRAM_ARGS.fetch(base, [])
end

Dir.glob(File.join(FIXTURE_DIR, '*_trace.json')).each do |fixture|
base = File.basename(fixture, '_trace.json')
define_method("test_#{base}") do
trace, out = run_trace("#{base}.rb", *program_args(base))
assert_equal expected_trace("#{base}.rb"), trace
assert_equal expected_output("#{base}.rb"), out
pure_trace, pure_out = run_trace('gems/pure-ruby-tracer/lib/trace.rb', "#{base}.rb", *program_args(base))
_native_trace, native_out = run_trace('gems/native-tracer/lib/native_trace.rb', "#{base}.rb", *program_args(base))
assert_equal expected_trace("#{base}.rb"), pure_trace
expected = expected_output("#{base}.rb")
assert_equal expected, pure_out
assert_equal expected, native_out
end
end
end