Skip to content

Commit c8ddcdd

Browse files
fix:
add some fixes on the DurationTracer, like undefined variables or method passed params add a basic test for DurationTracer fix the tmp file name
1 parent bf9f23e commit c8ddcdd

File tree

4 files changed

+81
-10
lines changed

4 files changed

+81
-10
lines changed

lib/rails_tracepoint_stack/duration/output.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def show_top_files
1717
private
1818

1919
def compute_duration!
20-
File.open("log-boot-4.txt", "r").each_line do |line|
20+
File.open("time_log.json", "r").each_line do |line|
2121
data = JSON.parse(line)
2222

2323
file = data["file"]

lib/rails_tracepoint_stack/duration/tmp_save.rb

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
require 'json'
12

23
module RailsTracepointStack
3-
module TmpSave
4-
def self.save(trace:, last_time:)
4+
class TmpSave
5+
def self.save(trace:, current_time:, last_time:)
56
duration = current_time - last_time
67
log_obj = {
78
time: current_time,
@@ -11,6 +12,7 @@ def self.save(trace:, last_time:)
1112
line: trace.line_number,
1213
duration: duration
1314
}
15+
p log_obj
1416
File.open("time_log.json", "a") do |file|
1517
file.puts log_obj.to_json
1618
end

lib/rails_tracepoint_stack/duration_tracer.rb

+7-7
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,22 @@
33

44
module RailsTracepointStack
55
class DurationTracer < RailsTracepointStack::TracerBase
6-
6+
77
private
88

99
def generate_tracer
10-
TracePoint.new(:line) do |tracepoint|
10+
@tracer ||= TracePoint.new(:line) do |tracepoint|
1111
trace = RailsTracepointStack::Trace.new(trace_point: tracepoint)
12-
12+
1313
next if ignore_trace?(trace: trace)
1414

1515
current_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
16-
17-
if last_time
18-
RailsTracepointStack::TmpSave.save(trace: trace, last_time: last_time)
16+
17+
if defined?(@last_time)
18+
RailsTracepointStack::TmpSave.save(trace: trace, current_time: current_time, last_time: @last_time)
1919
end
2020

21-
last_time = current_time
21+
@last_time = current_time
2222
end
2323
end
2424
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
require "spec_helper"
2+
require_relative "../../lib/rails_tracepoint_stack/duration_tracer"
3+
4+
class Foo
5+
def dummy_method
6+
nil
7+
end
8+
9+
def dummy_method_with_params(param_1, param_2)
10+
nil
11+
end
12+
end
13+
14+
RSpec.describe RailsTracepointStack::DurationTracer do
15+
let(:tracer) { RailsTracepointStack::DurationTracer.new }
16+
17+
before do
18+
allow(RailsTracepointStack::TmpSave)
19+
.to receive(:save)
20+
.and_return(true)
21+
22+
allow_any_instance_of(TracePoint)
23+
.to receive(:lineno).and_return(6)
24+
25+
allow_any_instance_of(TracePoint)
26+
.to receive(:defined_class).and_return(Foo)
27+
28+
allow_any_instance_of(TracePoint)
29+
.to receive(:method_id).and_return(:dummy_method)
30+
31+
allow_any_instance_of(TracePoint)
32+
.to receive(:path).and_return("/stubbed/path/to/file.rb")
33+
end
34+
35+
describe "when the log should not be ignored because not match any filter block" do
36+
before do
37+
allow(RailsTracepointStack::Filter::GemPath)
38+
.to receive(:full_gem_path)
39+
.and_return(["/another/path/to/gem"])
40+
41+
allow(RailsTracepointStack::Filter::RbConfig)
42+
.to receive(:ruby_lib_path)
43+
.and_return("/path/to/ruby/lib")
44+
45+
allow_any_instance_of(TracePoint)
46+
.to receive(:path)
47+
.and_return("/app/rails_tracepoint_stack/spec/tracer_spec.rb")
48+
49+
allow_any_instance_of(RailsTracepointStack::Configuration)
50+
.to receive(:log_format)
51+
.and_return(:text)
52+
end
53+
54+
it "calls logger with correct params" do
55+
tracer.enable do
56+
Foo.new.dummy_method
57+
Foo.new.dummy_method
58+
end
59+
60+
expect(RailsTracepointStack::TmpSave)
61+
.to have_received(:save)
62+
.with(
63+
trace: an_instance_of(RailsTracepointStack::Trace),
64+
current_time: an_instance_of(Float),
65+
last_time: an_instance_of(Float)
66+
)
67+
end
68+
end
69+
end

0 commit comments

Comments
 (0)