|
1 |
| -import tempfile |
2 |
| -import sys |
3 | 1 | import os
|
| 2 | +import subprocess |
| 3 | +import sys |
| 4 | +import tempfile |
| 5 | + |
4 | 6 | import pytest
|
5 | 7 | import ubelt as ub
|
6 | 8 |
|
@@ -525,3 +527,96 @@ def test_autoprofile_exec_module(
|
525 | 527 | assert ('Function: add_four' in raw_output) == add_four
|
526 | 528 | assert ('Function: add_operator' in raw_output) == add_operator
|
527 | 529 | assert ('Function: _main' in raw_output) == main
|
| 530 | + |
| 531 | + |
| 532 | +@pytest.mark.parametrize( |
| 533 | + ('outfile', 'expected_outfile'), |
| 534 | + [(None, 'kernprof-stdin-*.lprof'), |
| 535 | + ('test-stdin.lprof', 'test-stdin.lprof')]) |
| 536 | +def test_autoprofile_from_stdin( |
| 537 | + outfile, expected_outfile, |
| 538 | +) -> None: |
| 539 | + """ |
| 540 | + Test the profiling of a script read from stdin. |
| 541 | + """ |
| 542 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 543 | + temp_dpath = ub.Path(tmpdir) |
| 544 | + |
| 545 | + with ub.ChDir(temp_dpath): |
| 546 | + script_fpath = _write_demo_module(ub.Path()) |
| 547 | + |
| 548 | + args = [sys.executable, '-m', 'kernprof', |
| 549 | + '-p', 'test_mod.submod1,test_mod.subpkg.submod3', '-l'] |
| 550 | + if outfile: |
| 551 | + args += ['-o', outfile] |
| 552 | + args += ['-'] |
| 553 | + proc = subprocess.run(args, |
| 554 | + input=script_fpath.read_text(), |
| 555 | + text=True, |
| 556 | + capture_output=True) |
| 557 | + print(proc.stdout) |
| 558 | + print(proc.stderr) |
| 559 | + proc.check_returncode() |
| 560 | + |
| 561 | + outfile, = temp_dpath.glob(expected_outfile) |
| 562 | + args = [sys.executable, '-m', 'line_profiler', str(outfile)] |
| 563 | + proc = ub.cmd(args) |
| 564 | + raw_output = proc.stdout |
| 565 | + print(raw_output) |
| 566 | + proc.check_returncode() |
| 567 | + |
| 568 | + assert 'Function: add_one' in raw_output |
| 569 | + assert 'Function: add_two' not in raw_output |
| 570 | + assert 'Function: add_three' in raw_output |
| 571 | + assert 'Function: main' not in raw_output |
| 572 | + |
| 573 | + |
| 574 | +@pytest.mark.parametrize( |
| 575 | + ('outfile', 'expected_outfile'), |
| 576 | + [(None, 'kernprof-command-*.lprof'), |
| 577 | + ('test-command.lprof', 'test-command.lprof')]) |
| 578 | +def test_autoprofile_from_inlined_script( |
| 579 | + outfile, expected_outfile, |
| 580 | +) -> None: |
| 581 | + """ |
| 582 | + Test the profiling of an inlined script (supplied with the `-c` |
| 583 | + flag). |
| 584 | + """ |
| 585 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 586 | + temp_dpath = ub.Path(tmpdir) |
| 587 | + |
| 588 | + _write_demo_module(temp_dpath) |
| 589 | + |
| 590 | + inlined_script = ( |
| 591 | + 'from test_mod import submod1, submod2; ' |
| 592 | + 'from test_mod.subpkg import submod3; ' |
| 593 | + 'import statistics; ' |
| 594 | + 'data = [1, 2, 3]; ' |
| 595 | + 'val = submod1.add_one(data); ' |
| 596 | + 'val = submod2.add_two(val); ' |
| 597 | + 'val = submod3.add_three(val); ' |
| 598 | + 'result = statistics.harmonic_mean(val); ' |
| 599 | + 'print(result); ' |
| 600 | + ) |
| 601 | + prof_file = 'test-command.lprof' |
| 602 | + |
| 603 | + args = [sys.executable, '-m', 'kernprof', |
| 604 | + '-p', 'test_mod.submod1,test_mod.subpkg.submod3', '-l'] |
| 605 | + if outfile: |
| 606 | + args += ['-o', outfile] |
| 607 | + args += ['-c', inlined_script] |
| 608 | + proc = ub.cmd(args, cwd=temp_dpath, verbose=2) |
| 609 | + print(proc.stdout) |
| 610 | + print(proc.stderr) |
| 611 | + proc.check_returncode() |
| 612 | + |
| 613 | + outfile, = temp_dpath.glob(expected_outfile) |
| 614 | + args = [sys.executable, '-m', 'line_profiler', str(outfile)] |
| 615 | + proc = ub.cmd(args) |
| 616 | + raw_output = proc.stdout |
| 617 | + print(raw_output) |
| 618 | + proc.check_returncode() |
| 619 | + |
| 620 | + assert 'Function: add_one' in raw_output |
| 621 | + assert 'Function: add_two' not in raw_output |
| 622 | + assert 'Function: add_three' in raw_output |
0 commit comments