| Conditions | 22 |
| Total Lines | 100 |
| Code Lines | 63 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 0 |
| CRAP Score | 506 |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like utils.build_profiler_report.print_report() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | #!/usr/bin/python3 |
||
| 72 | def print_report(current_dict: dict, baseline_dict: dict = None) -> None: |
||
| 73 | """Print report with results of profiling to stdout""" |
||
| 74 | |||
| 75 | # If the targets/outputfiles have changed between baseline and current, we are using |
||
| 76 | # total_time_intersect to calculate delta (ratio of durations of targets) instead of total_time |
||
| 77 | if baseline_dict and baseline_dict.keys() != current_dict.keys(): |
||
| 78 | msg = ("Warning: the targets in the current logfile differ from those in the baseline" |
||
| 79 | "logfile; therefore the time and time percentage deltas TD and %TD for each target" |
||
| 80 | "as well as for the entire build are calculated without taking the added/removed" |
||
| 81 | "targets into account, but the total build time at the end does take them into" |
||
| 82 | "account. If the added/removed targets modify the behavior of targets in both" |
||
| 83 | "logfiles, the D delta may not make sense.\n-----\n") |
||
| 84 | print(msg) |
||
| 85 | target_mismatch = True |
||
| 86 | total_time_current_intersect = get_total_time_intersect(current_dict, baseline_dict) |
||
| 87 | total_time_baseline_intesect = get_total_time_intersect(baseline_dict, current_dict) |
||
| 88 | else: |
||
| 89 | target_mismatch = False |
||
| 90 | |||
| 91 | header = [f'{"Target:":60}', f"{'%':4}", f"{'D':5}", f"{'T':8}", |
||
| 92 | f"{'TD':8}", f"{'%TD':5}", "Note"] |
||
| 93 | print(' | '.join(header)) |
||
| 94 | |||
| 95 | total_time_current = get_total_time(current_dict) |
||
| 96 | if baseline_dict: |
||
| 97 | total_time_baseline = get_total_time(baseline_dict) |
||
| 98 | |||
| 99 | # sort targets/outputfiles by % taken of build time |
||
| 100 | current_dict = dict(sorted(current_dict.items(), key=lambda item: item[1], reverse=True)) |
||
| 101 | |||
| 102 | for target in current_dict.keys(): |
||
| 103 | # percentage of build time that the target took |
||
| 104 | perc = current_dict[target]/total_time_current * 100 |
||
| 105 | |||
| 106 | # difference between perc in current and in baseline |
||
| 107 | delta = 0 |
||
| 108 | if baseline_dict: |
||
| 109 | if target_mismatch: |
||
| 110 | if target in baseline_dict.keys(): |
||
| 111 | delta = current_dict[target]/total_time_current_intersect * 100 - \ |
||
|
|
|||
| 112 | baseline_dict[target]/total_time_baseline_intesect * 100 |
||
| 113 | else: |
||
| 114 | delta = perc - (baseline_dict[target]/total_time_baseline * 100) |
||
| 115 | if abs(delta) < 0.1: |
||
| 116 | delta = 0 |
||
| 117 | |||
| 118 | # time is the formatted build time of the target |
||
| 119 | time = format_time(current_dict[target]) |
||
| 120 | |||
| 121 | # time_delta is the formatted time difference between current and baseline |
||
| 122 | if baseline_dict and target in baseline_dict.keys(): |
||
| 123 | time_delta = current_dict[target] - baseline_dict[target] |
||
| 124 | if abs(time_delta) < 60: |
||
| 125 | time_delta = 0 |
||
| 126 | time_delta = format_time(time_delta) |
||
| 127 | else: |
||
| 128 | time_delta = 0 |
||
| 129 | |||
| 130 | # perc_time_delta is a percentage difference of before and after build times |
||
| 131 | if baseline_dict and target in baseline_dict.keys(): |
||
| 132 | perc_time_delta = (current_dict[target]/baseline_dict[target]) * 100 - 100 |
||
| 133 | else: |
||
| 134 | perc_time_delta = 0 |
||
| 135 | |||
| 136 | line = [f'{target:60}', f"{perc:4.1f}", f"{delta:5.1f}", f"{time:>8}", |
||
| 137 | f"{time_delta:>8}", f"{perc_time_delta:5.1f}"] |
||
| 138 | # if target was not in baseline, append note |
||
| 139 | if baseline_dict and target not in baseline_dict.keys(): |
||
| 140 | line.append("Not in baseline") |
||
| 141 | print(' | '.join(line)) |
||
| 142 | |||
| 143 | # Print time and % delta of the whole build time |
||
| 144 | if baseline_dict: |
||
| 145 | # total_perc_time_delta is the percentage change of build times between current and baseline |
||
| 146 | total_time_delta = total_time_current - total_time_baseline |
||
| 147 | if abs(total_time_delta) < 60: |
||
| 148 | total_time_delta = 0 |
||
| 149 | total_time_delta = format_time(total_time_delta) |
||
| 150 | total_perc_time_delta = (total_time_current / total_time_baseline) * 100 - 100 |
||
| 151 | line = ["-----\nTotal time:", format_time(total_time_current), |
||
| 152 | "| TD", f'{total_time_delta:>8}', "| %TD", f'{total_perc_time_delta:+5.1f}'] |
||
| 153 | # if there are different targets in current and baseline log, add intersect deltas, |
||
| 154 | # which compare build times while omitting conficting build targets |
||
| 155 | if target_mismatch: |
||
| 156 | intersect_time_delta = total_time_current_intersect - total_time_baseline_intesect |
||
| 157 | if abs(intersect_time_delta) < 60: |
||
| 158 | intersect_time_delta = 0 |
||
| 159 | intersect_time_delta = format_time(intersect_time_delta) |
||
| 160 | line.append(f'| intersect TD {intersect_time_delta:>8}') |
||
| 161 | intersect_perc_time_delta = (total_time_current_intersect / |
||
| 162 | total_time_baseline_intesect) * 100 - 100 |
||
| 163 | line.append(f'| intersect %TD {intersect_perc_time_delta:+5.1f}') |
||
| 164 | print(' '.join(line)) |
||
| 165 | else: |
||
| 166 | print("-----\nTotal time:", format_time(total_time_current)) |
||
| 167 | |||
| 168 | # Print targets which are present in baseline but not in current log |
||
| 169 | if baseline_dict: |
||
| 170 | removed = [target for target in baseline_dict.keys() if target not in current_dict.keys()] |
||
| 171 | print("-----\nTargets omitted from baseline:\n", '\n'.join(removed)) |
||
| 172 | |||
| 206 |