| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
import pytest |
| 6 |
from scalene.scalene_output import ScaleneOutput |
| 7 |
from scalene.scalene_json import ScaleneJSON |
| 8 |
from scalene.scalene_statistics import ScaleneStatistics |
| 9 |
from rich.console import Console |
| 10 |
from rich.table import Table |
| 11 |
from typing import Callable |
| 12 |
from collections import defaultdict |
| 13 |
import random |
| 14 |
|
| 15 |
|
| 16 |
@pytest.fixture |
| 17 |
def scalene_output(): |
| 18 |
return ScaleneOutput() |
| 19 |
|
| 20 |
|
| 21 |
@pytest.fixture |
| 22 |
def scalene_json(): |
| 23 |
return ScaleneJSON() |
| 24 |
|
| 25 |
|
| 26 |
@pytest.fixture |
| 27 |
def scalene_stats(): |
| 28 |
stats = ScaleneStatistics() |
| 29 |
stats.cpu_samples = defaultdict(lambda: defaultdict(float)) |
| 30 |
stats.memory_free_samples = defaultdict(lambda: defaultdict(list)) |
| 31 |
stats.memory_malloc_samples = defaultdict(lambda: defaultdict(list)) |
| 32 |
return stats |
| 33 |
|
| 34 |
|
| 35 |
@pytest.fixture |
| 36 |
def console(): |
| 37 |
return Console() |
| 38 |
|
| 39 |
|
| 40 |
@pytest.fixture |
| 41 |
def table(): |
| 42 |
return Table() |
| 43 |
|
| 44 |
|
| 45 |
def test_output_profile_line( |
| 46 |
scalene_output, scalene_json, scalene_stats, console, table |
| 47 |
): |
| 48 |
fname = "test.py" |
| 49 |
line_no = 1 |
| 50 |
line = "print('Hello, world!')" |
| 51 |
profile_this_code: Callable[[str, int], bool] = lambda fname, line_no: True |
| 52 |
|
| 53 |
|
| 54 |
scalene_stats.total_cpu_samples = 100 |
| 55 |
scalene_stats.cpu_samples[fname][line_no] = 2 |
| 56 |
scalene_stats.cpu_samples["<other>"][1] = 98 |
| 57 |
scalene_stats.memory_free_samples[fname][line_no] = [(0, 0.5)] |
| 58 |
scalene_stats.memory_malloc_samples[fname][line_no] = [(0, 0.5)] |
| 59 |
scalene_stats.max_footprint = 1024 |
| 60 |
|
| 61 |
|
| 62 |
json_output = { |
| 63 |
"n_peak_mb": 0.5, |
| 64 |
"n_cpu_percent_c": 0.5, |
| 65 |
"n_gpu_percent": 0.5, |
| 66 |
"n_cpu_percent_python": 0.5, |
| 67 |
"n_usage_fraction": 0.5, |
| 68 |
"n_sys_percent": 0.5, |
| 69 |
"n_python_fraction": 0.5, |
| 70 |
"n_copy_mb_s": 0.5, |
| 71 |
"memory_samples": [(0, 0.5)], |
| 72 |
} |
| 73 |
scalene_json.output_profile_line = lambda **kwargs: json_output |
| 74 |
|
| 75 |
|
| 76 |
scalene_output.highlight_percentage = 0.1 |
| 77 |
scalene_output.highlight_color = "red" |
| 78 |
scalene_output.gpu = True |
| 79 |
scalene_output.max_sparkline_len_line = 1 |
| 80 |
|
| 81 |
|
| 82 |
random.sample = lambda a, _: a |
| 83 |
|
| 84 |
|
| 85 |
result = scalene_output.output_profile_line( |
| 86 |
json=scalene_json, |
| 87 |
fname=fname, |
| 88 |
line_no=line_no, |
| 89 |
line=line, |
| 90 |
console=console, |
| 91 |
tbl=table, |
| 92 |
stats=scalene_stats, |
| 93 |
profile_this_code=profile_this_code, |
| 94 |
force_print=False, |
| 95 |
suppress_lineno_print=False, |
| 96 |
is_function_summary=False, |
| 97 |
profile_memory=True, |
| 98 |
reduced_profile=False, |
| 99 |
) |
| 100 |
|
| 101 |
|
| 102 |
assert result == True |
| 103 |
assert len(table.rows) == 1 |
| 104 |
|
| 105 |
del scalene_output |
| 106 |
del scalene_json |
| 107 |
del scalene_stats |
| 108 |
del console |
| 109 |
del table |
| 110 |
random.sample = random.Random().sample |