Skip to content

Commit 583db15

Browse files
committed
Fetch thread names from ruby when outputting
1 parent 4f1d583 commit 583db15

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

lib/vernier/output/firefox.rb

+9
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,15 @@ def initialize(ruby_thread_id, profile, categorizer, name:, tid:, samples:, weig
165165
@tid = tid
166166
@name = name
167167

168+
if @name == ""
169+
tr = ObjectSpace._id2ref(@ruby_thread_id)
170+
if tr && tr.name && !tr.name.empty?
171+
@name = tr.name
172+
else
173+
@name = "UNNAMED [#{@ruby_thread_id}]"
174+
end
175+
end
176+
168177
timestamps ||= [0] * samples.size
169178
@samples, @weights, @timestamps = samples, weights, timestamps
170179
@sample_categories = sample_categories || ([0] * samples.size)

test/output/test_firefox.rb

+29
Original file line numberDiff line numberDiff line change
@@ -151,4 +151,33 @@ def test_custom_intervals
151151
markers = JSON.parse(output)["threads"].flat_map { _1["markers"]["data"] }
152152
assert_includes markers, {"type"=>"UserTiming", "entryType"=>"measure", "name"=>"custom"}
153153
end
154+
155+
156+
def test_thread_names
157+
current_name = Thread.current.name
158+
unnamed_id = nil
159+
result = Vernier.trace do
160+
Thread.current.name="main"
161+
th1 = Thread.new { sleep 0.01 }
162+
unnamed_id = th1.object_id
163+
th1.join
164+
end
165+
166+
output = Vernier::Output::Firefox.new(result).output
167+
168+
assert_valid_firefox_profile(output)
169+
170+
data = JSON.parse(output)
171+
threads = data["threads"]
172+
threads.each do |tr|
173+
if tr["isMainThread"]
174+
assert_equal "main", tr["name"]
175+
else
176+
assert_equal "UNNAMED [#{unnamed_id}]", tr["name"]
177+
end
178+
end
179+
180+
ensure
181+
Thread.current.name = current_name
182+
end
154183
end

0 commit comments

Comments
 (0)