1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
"""
A script used in test_complex_case.py
python ~/code/line_profiler/tests/complex_example.py
LINE_PROFILE=1 python ~/code/line_profiler/tests/complex_example.py
python -m kernprof -v ~/code/line_profiler/tests/complex_example.py
python -m kernprof -lv ~/code/line_profiler/tests/complex_example.py
cd ~/code/line_profiler/tests/
# Run the code by itself without any profiling
PROFILE_TYPE=none python complex_example.py
# NOTE: this fails because we are not running with kernprof
PROFILE_TYPE=implicit python complex_example.py
# Kernprof with the implicit profile decorator
# NOTE: multiprocessing breaks this invocation, so set process size to zero
PROFILE_TYPE=implicit python -m kernprof -b complex_example.py --process_size=0
python -m pstats ./complex_example.py.prof
# Explicit decorator with line kernprof
# NOTE: again, multiprocessing breaks when using kernprof
PROFILE_TYPE=explicit python -m kernprof -l complex_example.py --process_size=0
python -m line_profiler complex_example.py.lprof
# Explicit decorator with cProfile kernprof
# NOTE: again, multiprocessing breaks when using kernprof (does this happen in older verions?)
PROFILE_TYPE=explicit python -m kernprof -b complex_example.py --process_size=0
python -m pstats ./complex_example.py.prof
# Explicit decorator with environment enabling
PROFILE_TYPE=explicit LINE_PROFILE=1 python complex_example.py
# Explicit decorator without enabling it
PROFILE_TYPE=explicit LINE_PROFILE=0 python complex_example.py
# Use a custom defined line profiler object
PROFILE_TYPE=custom python complex_example.py
"""
import os
# The test will define how we expect the profile decorator to exist
PROFILE_TYPE = os.environ.get('PROFILE_TYPE', '')
if PROFILE_TYPE == 'implicit':
# Do nothing, assume kernprof will inject profile in for us
...
elif PROFILE_TYPE == 'none':
# Define a no-op profiler
def profile(func):
return func
elif PROFILE_TYPE == 'explicit':
# Use the explicit profile decorator
import line_profiler
profile = line_profiler.profile
elif PROFILE_TYPE == 'custom':
# Create a custom profile decorator
import line_profiler
import atexit
profile = line_profiler.LineProfiler()
@atexit.register
def _show_profile_on_end():
...
profile.print_stats(summarize=1, sort=1, stripzeros=1, rich=1)
else:
raise KeyError('')
@profile
def fib(n):
a, b = 0, 1
while a < n:
a, b = b, a + b
@profile
def fib_only_called_by_thread(n):
a, b = 0, 1
while a < n:
a, b = b, a + b
@profile
def fib_only_called_by_process(n):
a, b = 0, 1
while a < n:
a, b = b, a + b
@profile
def main():
"""
Run a lot of different Fibonacci jobs
"""
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--serial_size', type=int, default=10)
parser.add_argument('--thread_size', type=int, default=10)
parser.add_argument('--process_size', type=int, default=10)
args = parser.parse_args()
for i in range(args.serial_size):
fib(i)
funcy_fib(
i)
fib(i)
from concurrent.futures import ThreadPoolExecutor
executor = ThreadPoolExecutor(max_workers=4)
with executor:
jobs = []
for i in range(args.thread_size):
job = executor.submit(fib, i)
jobs.append(job)
job = executor.submit(funcy_fib, i)
jobs.append(job)
job = executor.submit(fib_only_called_by_thread, i)
jobs.append(job)
for job in jobs:
job.result()
from concurrent.futures import ProcessPoolExecutor
executor = ProcessPoolExecutor(max_workers=4)
with executor:
jobs = []
for i in range(args.process_size):
job = executor.submit(fib, i)
jobs.append(job)
job = executor.submit(funcy_fib, i)
jobs.append(job)
job = executor.submit(fib_only_called_by_process, i)
jobs.append(job)
for job in jobs:
job.result()
@profile
def funcy_fib(n):
"""
Alternative fib function where code splits out over multiple lines
"""
a, b = (
0, 1
)
while a < n:
# print(
# a, end=' ')
a, b = b, \
a + b
# print(
# )
if __name__ == '__main__':
"""
CommandLine:
cd ~/code/line_profiler/tests/
python complex_example.py --size 10
python complex_example.py --serial_size 100000 --thread_size 0 --process_size 0
"""
main()
|