Export the profile to a Gecko JSON file.
(self, filename)
| 657 | self.opcode_state.clear() |
| 658 | |
| 659 | def export(self, filename): |
| 660 | """Export the profile to a Gecko JSON file.""" |
| 661 | |
| 662 | if self.sample_count > 0 and self.last_sample_time > 0: |
| 663 | self.interval = self.last_sample_time / self.sample_count |
| 664 | |
| 665 | # Spinner for progress indication |
| 666 | spinner = itertools.cycle(['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏']) |
| 667 | stop_spinner = threading.Event() |
| 668 | |
| 669 | def spin(): |
| 670 | message = 'Building Gecko profile...' |
| 671 | while not stop_spinner.is_set(): |
| 672 | sys.stderr.write(f'\r{next(spinner)} {message}') |
| 673 | sys.stderr.flush() |
| 674 | time.sleep(0.1) |
| 675 | # Clear the spinner line |
| 676 | sys.stderr.write('\r' + ' ' * (len(message) + 3) + '\r') |
| 677 | sys.stderr.flush() |
| 678 | |
| 679 | spinner_thread = threading.Thread(target=spin, daemon=True) |
| 680 | spinner_thread.start() |
| 681 | |
| 682 | try: |
| 683 | # Finalize any open markers before building profile |
| 684 | self._finalize_markers() |
| 685 | |
| 686 | profile = self._build_profile() |
| 687 | |
| 688 | with open(filename, "w") as f: |
| 689 | json.dump(profile, f, separators=(",", ":")) |
| 690 | finally: |
| 691 | stop_spinner.set() |
| 692 | spinner_thread.join(timeout=1.0) |
| 693 | # Small delay to ensure the clear happens |
| 694 | time.sleep(0.01) |
| 695 | |
| 696 | print(f"Gecko profile written to {filename}") |
| 697 | print( |
| 698 | f"Open in Firefox Profiler: https://profiler.firefox.com/" |
| 699 | ) |
| 700 | |
| 701 | def _build_marker_schema(self): |
| 702 | """Build marker schema definitions for Firefox Profiler.""" |