Skip to content

Commit 581828d

Browse files
fixup! gh-135953: Add Gecko reporter to sampling profiler
1 parent 0ae7d2c commit 581828d

File tree

2 files changed

+9
-33
lines changed

2 files changed

+9
-33
lines changed

Lib/profiling/sampling/gecko_format.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from dataclasses import dataclass, field
88
from typing import List, Dict, Optional, Any
99

10-
from Lib.profiling.sampling.string_table import StringTable
10+
from .string_table import StringTable
1111

1212
PYTHON_CATEGORY = 0
1313
OTHER_CATEGORY = 1
@@ -267,7 +267,7 @@ def to_dict(self) -> Dict[str, Any]:
267267
}
268268

269269
class GeckoBuilder:
270-
def __init__(self, string_table: StringTable, start_time: float = None):
270+
def __init__(self, string_table: StringTable, start_time: float):
271271
self.string_table = string_table
272272
self.start_time = start_time
273273
self.threads_data = {}
@@ -359,7 +359,7 @@ def build_profile(self):
359359
isMainThread=(thread_id == 0),
360360
processType="default",
361361
processName="Python",
362-
processStartupTime=(self.start_time or 0) * 1000,
362+
processStartupTime=None,
363363
processShutdownTime=None,
364364
registerTime=0,
365365
unregisterTime=None,
@@ -376,7 +376,7 @@ def build_profile(self):
376376
threads.append(gecko_thread.to_dict())
377377

378378
gecko_meta = GeckoMeta(
379-
startTime=(self.start_time or 0) * 1000
379+
startTime=self.start_time * 1000
380380
)
381381

382382
gecko_profile = GeckoProfile(

Lib/profiling/sampling/stack_collector.py

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -309,45 +309,21 @@ class GeckoCollector(StackTraceCollector):
309309

310310
def __init__(self, *args, **kwargs):
311311
super().__init__(*args, **kwargs)
312-
self.samples = []
313-
self.start_time = None
314312
self._string_table = StringTable()
315-
self._func_intern = {}
313+
self._builder = GeckoBuilder(self._string_table, time.time())
316314

317315
def process_frames(self, frames, thread_id):
318316
current_time = time.time()
319-
if self.start_time is None:
320-
self.start_time = current_time
321-
322-
# Intern frames to avoid storing duplicates
323-
interned_frames = [
324-
self._func_intern.setdefault(frame, frame) for frame in frames
325-
]
326-
327-
self.samples.append({
328-
'timestamp': current_time,
329-
'thread_id': thread_id,
330-
'frames': interned_frames
331-
})
317+
self._builder.add_sample(frames, current_time, thread_id)
332318

333319
def export(self, filename):
334-
if not self.samples:
320+
if not self._builder.threads_data:
335321
print("No samples to export")
336322
return
337323

338-
builder = GeckoBuilder(self._string_table, self.start_time)
339-
340-
for sample in self.samples:
341-
builder.add_sample(
342-
sample['frames'],
343-
sample['timestamp'],
344-
sample['thread_id']
345-
)
346-
347-
profile = builder.build_profile()
324+
profile = self._builder.build_profile()
348325

349326
with open(filename, 'w', encoding='utf-8') as file:
350327
json.dump(profile, file, indent=2)
351328

352-
thread_count = len(set(sample['thread_id'] for sample in self.samples))
353-
print(f"Gecko profile exported: {filename} ({thread_count} threads, {len(self.samples)} samples)")
329+
print(f"Gecko profile exported: {filename}")

0 commit comments

Comments
 (0)