Conditions | 23 |
Total Lines | 107 |
Code Lines | 67 |
Lines | 0 |
Ratio | 0 % |
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 |
||
87 | def print_report(current_dict: dict, baseline_dict: dict = None) -> None: |
||
88 | """Print report with results of profiling to stdout""" |
||
89 | |||
90 | # If the targets/outputfiles have changed between baseline and current, we are using |
||
91 | # total_time_intersect to calculate delta (ratio of durations of targets) instead of total_time |
||
92 | if baseline_dict and baseline_dict.keys() != current_dict.keys(): |
||
93 | msg = ("Warning: the targets in the current logfile differ from those in the baseline" |
||
94 | "logfile; therefore the time and time percentage deltas TD and %TD for each target" |
||
95 | "as well as for the entire build are calculated without taking the added/removed" |
||
96 | "targets into account, but the total build time at the end does take them into" |
||
97 | "account. If the added/removed targets modify the behavior of targets in both" |
||
98 | "logfiles, the D delta may not make sense.\n-----\n") |
||
99 | print(msg) |
||
100 | target_mismatch = True |
||
101 | total_time_current_intersect = get_total_time_intersect(current_dict, baseline_dict) |
||
102 | total_time_baseline_intesect = get_total_time_intersect(baseline_dict, current_dict) |
||
103 | else: |
||
104 | target_mismatch = False |
||
105 | |||
106 | header = [f'{"Target:":60}', f"{'%':4}", f"{'D':5}", f"{'T':8}", |
||
107 | f"{'TD':8}", f"{'%TD':5}", "Note"] |
||
108 | print(' | '.join(header)) |
||
109 | |||
110 | total_time_current = get_total_time(current_dict) |
||
111 | if baseline_dict: |
||
112 | total_time_baseline = get_total_time(baseline_dict) |
||
113 | |||
114 | # sort targets/outputfiles by % taken of build time |
||
115 | current_dict = dict(sorted(current_dict.items(), key=lambda item: item[1], reverse=True)) |
||
116 | |||
117 | for target in current_dict.keys(): |
||
118 | # percentage of build time that the target took |
||
119 | perc = current_dict[target]/total_time_current * 100 |
||
120 | |||
121 | # difference between perc in current and in baseline |
||
122 | delta = 0 |
||
123 | if baseline_dict: |
||
124 | if target_mismatch: |
||
125 | if target in baseline_dict.keys(): |
||
126 | delta = current_dict[target]/total_time_current_intersect * 100 - \ |
||
|
|||
127 | baseline_dict[target]/total_time_baseline_intesect * 100 |
||
128 | else: |
||
129 | delta = perc - (baseline_dict[target]/total_time_baseline * 100) |
||
130 | if abs(delta) < 0.1: |
||
131 | delta = 0 |
||
132 | |||
133 | # time is the formatted build time of the target |
||
134 | time = format_time(current_dict[target]) |
||
135 | |||
136 | # time_delta is the formatted time difference between current and baseline |
||
137 | if baseline_dict and target in baseline_dict.keys(): |
||
138 | time_delta = current_dict[target] - baseline_dict[target] |
||
139 | if abs(time_delta) < 60: |
||
140 | time_delta = 0 |
||
141 | time_delta = format_time(time_delta) |
||
142 | else: |
||
143 | time_delta = 0 |
||
144 | |||
145 | # perc_time_delta is a percentage difference of before and after build times |
||
146 | if baseline_dict and target in baseline_dict.keys(): |
||
147 | perc_time_delta = (current_dict[target]/baseline_dict[target]) * 100 - 100 |
||
148 | else: |
||
149 | perc_time_delta = 0 |
||
150 | |||
151 | line = [f'{target:60}', f"{perc:4.1f}", f"{delta:5.1f}", f"{time:>8}", |
||
152 | f"{time_delta:>8}", f"{perc_time_delta:5.1f}"] |
||
153 | # if target was not in baseline, append note |
||
154 | if baseline_dict and target not in baseline_dict.keys(): |
||
155 | line.append("Not in baseline") |
||
156 | |||
157 | # if target has multiple output files, print them on separate lines |
||
158 | # (times only on the last line) |
||
159 | if(';' in target): |
||
160 | print("\n".join(target.rsplit(';', 1)[0].split(';'))) |
||
161 | split_target = target.rsplit(';', 1)[1] |
||
162 | line[0] = f'{split_target:60}' |
||
163 | print(' | '.join(line)) |
||
164 | |||
165 | # Print time and % delta of the whole build time |
||
166 | if baseline_dict: |
||
167 | # total_perc_time_delta is the percentage change of build times between current and baseline |
||
168 | total_time_delta = total_time_current - total_time_baseline |
||
169 | if abs(total_time_delta) < 60: |
||
170 | total_time_delta = 0 |
||
171 | total_time_delta = format_time(total_time_delta) |
||
172 | total_perc_time_delta = (total_time_current / total_time_baseline) * 100 - 100 |
||
173 | line = ["-----\nTotal time:", format_time(total_time_current), |
||
174 | "| TD", f'{total_time_delta:>8}', "| %TD", f'{total_perc_time_delta:+5.1f}'] |
||
175 | # if there are different targets in current and baseline log, add intersect deltas, |
||
176 | # which compare build times while omitting conficting build targets |
||
177 | if target_mismatch: |
||
178 | intersect_time_delta = total_time_current_intersect - total_time_baseline_intesect |
||
179 | if abs(intersect_time_delta) < 60: |
||
180 | intersect_time_delta = 0 |
||
181 | intersect_time_delta = format_time(intersect_time_delta) |
||
182 | line.append(f'| intersect TD {intersect_time_delta:>8}') |
||
183 | intersect_perc_time_delta = (total_time_current_intersect / |
||
184 | total_time_baseline_intesect) * 100 - 100 |
||
185 | line.append(f'| intersect %TD {intersect_perc_time_delta:+5.1f}') |
||
186 | print(' '.join(line)) |
||
187 | else: |
||
188 | print("-----\nTotal time:", format_time(total_time_current)) |
||
189 | |||
190 | # Print targets which are present in baseline but not in current log |
||
191 | if baseline_dict: |
||
192 | removed = [target for target in baseline_dict.keys() if target not in current_dict.keys()] |
||
193 | print("-----\nTargets omitted from baseline:\n", '\n'.join(removed)) |
||
194 | |||
228 |