Benchmark mode - measure raw sampling speed for specified duration
(unwinder, duration_seconds=10, blocking=False)
| 168 | |
| 169 | |
| 170 | def benchmark(unwinder, duration_seconds=10, blocking=False): |
| 171 | """Benchmark mode - measure raw sampling speed for specified duration""" |
| 172 | sample_count = 0 |
| 173 | fail_count = 0 |
| 174 | total_work_time = 0.0 |
| 175 | start_time = time.perf_counter() |
| 176 | end_time = start_time + duration_seconds |
| 177 | total_attempts = 0 |
| 178 | |
| 179 | colors = get_colors(can_colorize()) |
| 180 | |
| 181 | print( |
| 182 | f"{colors.BOLD_BLUE}Benchmarking sampling speed for {duration_seconds} seconds...{colors.RESET}" |
| 183 | ) |
| 184 | |
| 185 | try: |
| 186 | while time.perf_counter() < end_time: |
| 187 | total_attempts += 1 |
| 188 | work_start = time.perf_counter() |
| 189 | try: |
| 190 | if blocking: |
| 191 | unwinder.pause_threads() |
| 192 | try: |
| 193 | stack_trace = unwinder.get_stack_trace() |
| 194 | if stack_trace: |
| 195 | sample_count += 1 |
| 196 | finally: |
| 197 | if blocking: |
| 198 | unwinder.resume_threads() |
| 199 | except (OSError, RuntimeError, UnicodeDecodeError) as e: |
| 200 | fail_count += 1 |
| 201 | |
| 202 | work_end = time.perf_counter() |
| 203 | total_work_time += work_end - work_start |
| 204 | |
| 205 | if total_attempts % 10000 == 0: |
| 206 | avg_work_time_us = (total_work_time / total_attempts) * 1e6 |
| 207 | work_rate = ( |
| 208 | total_attempts / total_work_time if total_work_time > 0 else 0 |
| 209 | ) |
| 210 | success_rate = (sample_count / total_attempts) * 100 |
| 211 | |
| 212 | # Color code the success rate |
| 213 | if success_rate >= 95: |
| 214 | success_color = colors.GREEN |
| 215 | elif success_rate >= 80: |
| 216 | success_color = colors.YELLOW |
| 217 | else: |
| 218 | success_color = colors.RED |
| 219 | |
| 220 | print( |
| 221 | f"{colors.CYAN}Attempts:{colors.RESET} {total_attempts} | " |
| 222 | f"{colors.CYAN}Success:{colors.RESET} {success_color}{success_rate:.1f}%{colors.RESET} | " |
| 223 | f"{colors.CYAN}Rate:{colors.RESET} {colors.MAGENTA}{work_rate:.1f}Hz{colors.RESET} | " |
| 224 | f"{colors.CYAN}Avg:{colors.RESET} {colors.YELLOW}{avg_work_time_us:.2f}µs{colors.RESET}" |
| 225 | ) |
| 226 | except KeyboardInterrupt: |
| 227 | print(f"\n{colors.YELLOW}Benchmark interrupted by user{colors.RESET}") |
no test coverage detected
searching dependent graphs…