-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
193 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
""" | ||
A script used in test_complex_case.py | ||
""" | ||
import line_profiler | ||
import atexit | ||
|
||
|
||
profile = line_profiler.LineProfiler() | ||
|
||
|
||
@atexit.register | ||
def _show_profile_on_end(): | ||
profile.print_stats() | ||
|
||
|
||
@profile | ||
def fib(n): | ||
a, b = 0, 1 | ||
while a < n: | ||
print(a, end=' ') | ||
a, b = b, a + b | ||
print() | ||
|
||
|
||
@profile | ||
def funcy_fib(n): | ||
""" | ||
Alternatite 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( | ||
) | ||
|
||
|
||
@profile | ||
def fib_only_called_by_thread(n): | ||
a, b = 0, 1 | ||
while a < n: | ||
print(a, end=' ') | ||
a, b = b, a + b | ||
print() | ||
|
||
|
||
@profile | ||
def fib_only_called_by_process(n): | ||
a, b = 0, 1 | ||
while a < n: | ||
print(a, end=' ') | ||
a, b = b, a + b | ||
# FIXME: having two functions with the EXACT same code can cause issues | ||
# a = 'no longer exactly the same' | ||
print() | ||
|
||
|
||
@profile | ||
def main(): | ||
""" | ||
Run a lot of different Fibonacci jobs | ||
""" | ||
import argparse | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument('--size', type=int, default=10) | ||
args = parser.parse_args() | ||
|
||
size = args.size | ||
|
||
for i in range(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(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(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() | ||
|
||
|
||
if __name__ == '__main__': | ||
""" | ||
CommandLine: | ||
cd ~/code/line_profiler/tests/ | ||
python complex_example.py --size 10 | ||
""" | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
|
||
|
||
def profile_now(func): | ||
""" | ||
Wrap a function to print profile information after it is called. | ||
Args: | ||
func (Callable): function to profile | ||
Returns: | ||
Callable: the wrapped function | ||
""" | ||
import line_profiler | ||
profile = line_profiler.LineProfiler() | ||
new_func = profile(func) | ||
|
||
def wraper(*args, **kwargs): | ||
try: | ||
return new_func(*args, **kwargs) | ||
except Exception: | ||
pass | ||
finally: | ||
profile.print_stats(stripzeros=True) | ||
|
||
wraper.new_func = new_func | ||
return wraper | ||
|
||
|
||
def func_to_profile(): | ||
list(range(100000)) | ||
tuple(range(100000)) | ||
set(range(100000)) | ||
|
||
|
||
def test_profile_now(): | ||
func = func_to_profile | ||
profile_now(func)() | ||
|
||
|
||
def test_complex_example(): | ||
import sys | ||
import pathlib | ||
import subprocess | ||
|
||
try: | ||
test_dpath = pathlib.Path(__file__).parent | ||
except NameError: | ||
# for development | ||
test_dpath = pathlib.Path('~/code/line_profiler/tests').expanduser() | ||
|
||
complex_fpath = test_dpath / 'complex_example.py' | ||
|
||
proc = subprocess.run([sys.executable, complex_fpath], capture_output=True, | ||
universal_newlines=True) | ||
print(proc.stdout) | ||
print(proc.stderr) | ||
print(proc.returncode) | ||
proc.check_returncode() |