|
7 | 7 | import argparse |
8 | 8 | from _colorize import get_colors, can_colorize |
9 | 9 |
|
10 | | -CODE = '''\ |
11 | | -import time |
12 | | -import os |
13 | | -import sys |
14 | | -import math |
15 | | -
|
16 | | -def slow_fibonacci(n): |
17 | | - """Intentionally slow recursive fibonacci - should show up prominently in profiler""" |
18 | | - if n <= 1: |
19 | | - return n |
20 | | - return slow_fibonacci(n-1) + slow_fibonacci(n-2) |
21 | | -
|
22 | | -def medium_computation(): |
23 | | - """Medium complexity function""" |
24 | | - result = 0 |
25 | | - for i in range(1000): |
26 | | - result += math.sqrt(i) * math.sin(i) |
27 | | - return result |
28 | | -
|
29 | | -def fast_loop(): |
30 | | - """Fast simple loop""" |
31 | | - total = 0 |
32 | | - for i in range(100): |
33 | | - total += i |
34 | | - return total |
35 | | -
|
36 | | -def string_operations(): |
37 | | - """String manipulation that should be visible in profiler""" |
38 | | - text = "hello world " * 100 |
39 | | - words = text.split() |
40 | | - return " ".join(reversed(words)) |
41 | | -
|
42 | | -def nested_calls(): |
43 | | - """Nested function calls to test call stack depth""" |
44 | | - def level1(): |
45 | | - def level2(): |
46 | | - def level3(): |
47 | | - return medium_computation() |
48 | | - return level3() |
49 | | - return level2() |
50 | | - return level1() |
51 | | -
|
52 | | -def main_loop(): |
53 | | - """Main computation loop with different execution paths""" |
54 | | - iteration = 0 |
55 | | -
|
56 | | - while True: |
57 | | - iteration += 1 |
58 | | -
|
59 | | - # Different execution paths with different frequencies |
60 | | - if iteration % 50 == 0: |
61 | | - # Expensive operation - should show high per-call time |
62 | | - result = slow_fibonacci(20) |
63 | | -
|
64 | | - elif iteration % 10 == 0: |
65 | | - # Medium operation |
66 | | - result = nested_calls() |
67 | | -
|
68 | | - elif iteration % 5 == 0: |
69 | | - # String operations |
70 | | - result = string_operations() |
71 | | -
|
72 | | - else: |
73 | | - # Fast operation - most common |
74 | | - result = fast_loop() |
75 | | -
|
76 | | - # Small delay to make sampling more interesting |
77 | | - time.sleep(0.001) |
78 | | -
|
79 | | -if __name__ == "__main__": |
80 | | - main_loop() |
81 | | -''' |
82 | | - |
83 | | -DEEP_STATIC_CODE = """\ |
84 | | -import time |
85 | | -def factorial(n): |
86 | | - if n <= 1: |
87 | | - time.sleep(10000) |
88 | | - return 1 |
89 | | - return n * factorial(n-1) |
90 | | -
|
91 | | -factorial(900) |
92 | | -""" |
| 10 | +from snippets import CODE_EXAMPLES, CODE |
93 | 11 |
|
94 | | -CODE_WITH_TONS_OF_THREADS = '''\ |
95 | | -import time |
96 | | -import threading |
97 | | -import random |
98 | | -import math |
99 | | -
|
100 | | -def cpu_intensive_work(): |
101 | | - """Do some CPU intensive calculations""" |
102 | | - result = 0 |
103 | | - for _ in range(10000): |
104 | | - result += math.sin(random.random()) * math.cos(random.random()) |
105 | | - return result |
106 | | -
|
107 | | -def io_intensive_work(): |
108 | | - """Simulate IO intensive work with sleeps""" |
109 | | - time.sleep(0.1) |
110 | | -
|
111 | | -def mixed_workload(): |
112 | | - """Mix of CPU and IO work""" |
113 | | - while True: |
114 | | - if random.random() < 0.3: |
115 | | - cpu_intensive_work() |
116 | | - else: |
117 | | - io_intensive_work() |
118 | | -
|
119 | | -def create_threads(n): |
120 | | - """Create n threads doing mixed workloads""" |
121 | | - threads = [] |
122 | | - for _ in range(n): |
123 | | - t = threading.Thread(target=mixed_workload, daemon=True) |
124 | | - t.start() |
125 | | - threads.append(t) |
126 | | - return threads |
127 | | -
|
128 | | -# Start with 5 threads |
129 | | -active_threads = create_threads(5) |
130 | | -thread_count = 5 |
131 | | -
|
132 | | -# Main thread manages threads and does work |
133 | | -while True: |
134 | | - # Randomly add or remove threads |
135 | | - if random.random() < 0.1: # 10% chance each iteration |
136 | | - if random.random() < 0.5 and thread_count < 100: |
137 | | - # Add 1-5 new threads |
138 | | - new_count = random.randint(1, 5) |
139 | | - new_threads = create_threads(new_count) |
140 | | - active_threads.extend(new_threads) |
141 | | - thread_count += new_count |
142 | | - elif thread_count > 10: |
143 | | - # Remove 1-3 threads |
144 | | - remove_count = random.randint(1, 5) |
145 | | - # The threads will terminate naturally since they're daemons |
146 | | - active_threads = active_threads[remove_count:] |
147 | | - thread_count -= remove_count |
148 | | -
|
149 | | - cpu_intensive_work() |
150 | | - time.sleep(0.05) |
151 | | -''' |
152 | | - |
153 | | -ASYNC_CODE = '''\ |
154 | | -import asyncio |
155 | | -import contextlib |
156 | | -import math |
157 | | -
|
158 | | -def compute_slice(seed): |
159 | | - result = 0.0 |
160 | | - for i in range(2000): |
161 | | - result += math.sin(seed + i) * math.sqrt(i + 1) |
162 | | - return result |
163 | | -
|
164 | | -async def leaf_task(seed): |
165 | | - total = 0.0 |
166 | | - while True: |
167 | | - total += compute_slice(seed) |
168 | | - await asyncio.sleep(0) |
169 | | -
|
170 | | -async def parent_task(seed): |
171 | | - child = asyncio.create_task(leaf_task(seed + 1000), name=f"leaf-{seed}") |
172 | | - try: |
173 | | - while True: |
174 | | - compute_slice(seed) |
175 | | - await asyncio.sleep(0.001) |
176 | | - finally: |
177 | | - child.cancel() |
178 | | - with contextlib.suppress(asyncio.CancelledError): |
179 | | - await child |
180 | | -
|
181 | | -async def main(): |
182 | | - tasks = [ |
183 | | - asyncio.create_task(parent_task(i), name=f"parent-{i}") |
184 | | - for i in range(8) |
185 | | - ] |
186 | | - await asyncio.gather(*tasks) |
187 | | -
|
188 | | -if __name__ == "__main__": |
189 | | - asyncio.run(main()) |
190 | | -''' |
191 | | - |
192 | | -CODE_EXAMPLES = { |
193 | | - "basic": { |
194 | | - "code": CODE, |
195 | | - "description": "Mixed workload with fibonacci, computations, and string operations", |
196 | | - }, |
197 | | - "deep_static": { |
198 | | - "code": DEEP_STATIC_CODE, |
199 | | - "description": "Deep recursive call stack with 900+ frames (factorial)", |
200 | | - }, |
201 | | - "threads": { |
202 | | - "code": CODE_WITH_TONS_OF_THREADS, |
203 | | - "description": "Tons of threads doing mixed CPU/IO work", |
204 | | - }, |
205 | | - "asyncio": { |
206 | | - "code": ASYNC_CODE, |
207 | | - "description": "Asyncio tasks with active and awaited coroutine chains", |
208 | | - }, |
209 | | -} |
210 | 12 |
|
211 | 13 | OPERATIONS = { |
212 | 14 | "stack_trace": { |
|
0 commit comments