| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
# Unit tests for decopy |
| 4 |
# Copyright: 2018, Maximiliano Curia <[email protected]> |
| 5 |
# |
| 6 |
# License: ISC |
| 7 |
# Permission to use, copy, modify, and/or distribute this software for any |
| 8 |
# purpose with or without fee is hereby granted, provided that the above |
| 9 |
# copyright notice and this permission notice appear in all copies. |
| 10 |
# . |
| 11 |
# THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH |
| 12 |
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY |
| 13 |
# AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, |
| 14 |
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM |
| 15 |
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE |
| 16 |
# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 17 |
# PERFORMANCE OF THIS SOFTWARE. |
| 18 |
|
| 19 |
import json |
| 20 |
import sys |
| 21 |
import time |
| 22 |
from pathlib import Path |
| 23 |
|
| 24 |
try: |
| 25 |
from tqdm import tqdm |
| 26 |
except ImportError: |
| 27 |
|
| 28 |
def tqdm(*a, **kw): # type: ignore[no-redef] |
| 29 |
if a: |
| 30 |
return a[0] |
| 31 |
return kw.get("iterable") |
| 32 |
|
| 33 |
|
| 34 |
from decopy.common.re_types import ReSub, ReSubGroup |
| 35 |
from decopy.licensing import holders, license_subs, licenses |
| 36 |
|
| 37 |
|
| 38 |
class SpeedCheck: |
| 39 |
def run(self): |
| 40 |
self.benchmark_licenses_res() |
| 41 |
self.benchmark_clean_comments() |
| 42 |
self.benchmark_copyright() |
| 43 |
self.benchmark_resubs() |
| 44 |
return 0 |
| 45 |
|
| 46 |
def load_all_licenses(self): |
| 47 |
texts = [] |
| 48 |
for filename in Path().glob("tests/testdata/licenses/*.json"): |
| 49 |
with filename.open() as file_: |
| 50 |
license_ = json.load(file_) |
| 51 |
for key in ("licenseText", "standardLicenseHeader", "licenseExceptionText"): |
| 52 |
if key not in license_: |
| 53 |
continue |
| 54 |
texts.append(license_[key]) |
| 55 |
return texts |
| 56 |
|
| 57 |
def benchmark_licenses_res(self): |
| 58 |
texts = self.load_all_licenses() |
| 59 |
times = [] |
| 60 |
|
| 61 |
for i, pattern in tqdm( |
| 62 |
enumerate(licenses.COMPILED_RES), |
| 63 |
desc="Timing license res", |
| 64 |
total=len(licenses.COMPILED_RES), |
| 65 |
dynamic_ncols=True, |
| 66 |
): |
| 67 |
start = time.time() |
| 68 |
for text in texts: |
| 69 |
pattern.search(text) |
| 70 |
elapsed = time.time() - start |
| 71 |
times.append((elapsed, i)) |
| 72 |
|
| 73 |
base, amount = 0, 0 |
| 74 |
interesting = times[base : base + amount] |
| 75 |
if interesting: |
| 76 |
print( |
| 77 |
"Sum of current selection: ", |
| 78 |
sum(elapsed for elapsed, _ in interesting), |
| 79 |
) |
| 80 |
times.sort() |
| 81 |
|
| 82 |
for elapsed, i in [times[0], *interesting, *times[-5:]]: |
| 83 |
license_re = licenses.LICENSES_RES[i] |
| 84 |
print( |
| 85 |
f"Time for {i} ({license_re.license}): {elapsed}\n{license_re.re}", |
| 86 |
) |
| 87 |
|
| 88 |
def benchmark_resubs(self): |
| 89 |
texts = self.load_all_licenses() |
| 90 |
times: list[tuple[float, int | tuple[int, int]]] = [] |
| 91 |
resubs = ( |
| 92 |
license_subs.ARTISTIC_SUBS |
| 93 |
+ license_subs.BSD_SUBS |
| 94 |
+ license_subs.CC_BY_SUBS |
| 95 |
+ license_subs.CECILL_SUBS |
| 96 |
+ license_subs.CNRI_SUBS |
| 97 |
+ license_subs.EFL_SUBS |
| 98 |
+ license_subs.GNU_VERSION_SUBS |
| 99 |
+ license_subs.GNU_EXCEPTION_SUBS |
| 100 |
+ license_subs.GFDL_SUBS |
| 101 |
+ license_subs.LPPL_SUBS |
| 102 |
+ license_subs.MIT_SUBS |
| 103 |
+ license_subs.ZPL_SUBS |
| 104 |
) |
| 105 |
|
| 106 |
for i, resub in tqdm( |
| 107 |
enumerate(resubs), |
| 108 |
desc="Timing resubs", |
| 109 |
total=len(resubs), |
| 110 |
dynamic_ncols=True, |
| 111 |
): |
| 112 |
if isinstance(resub, ReSubGroup): |
| 113 |
for j, regroupsub in enumerate(resub.subs): |
| 114 |
start = time.time() |
| 115 |
for text in texts: |
| 116 |
regroupsub.pattern.subn(regroupsub.repl, text, 1) |
| 117 |
elapsed = time.time() - start |
| 118 |
times.append((elapsed, (i, j))) |
| 119 |
else: |
| 120 |
start = time.time() |
| 121 |
for text in texts: |
| 122 |
resub.pattern.subn(resub.repl, text, 1) |
| 123 |
elapsed = time.time() - start |
| 124 |
times.append((elapsed, i)) |
| 125 |
|
| 126 |
base, amount = 0, 0 |
| 127 |
interesting = times[base : base + amount] |
| 128 |
if interesting: |
| 129 |
print( |
| 130 |
"Sum of current selection: ", |
| 131 |
sum(elapsed for elapsed, _ in interesting), |
| 132 |
) |
| 133 |
times.sort() |
| 134 |
|
| 135 |
for elapsed, indexes in [times[0], *interesting, *times[-5:]]: |
| 136 |
if isinstance(indexes, tuple): |
| 137 |
group = resubs[indexes[0]] |
| 138 |
assert isinstance(group, ReSubGroup) |
| 139 |
resub = group.subs[indexes[1]] |
| 140 |
else: |
| 141 |
resub = resubs[indexes] |
| 142 |
if not isinstance(resub, ReSub): |
| 143 |
print(f"W: Not a ReSub {indexes}") |
| 144 |
print(f"Time for {indexes} (repl: {resub}): {elapsed}") |
| 145 |
continue |
| 146 |
print(f"Time for {indexes} (repl: {resub.repl}): {elapsed}") |
| 147 |
|
| 148 |
def benchmark_clean_comments(self): |
| 149 |
texts = self.load_all_licenses() |
| 150 |
times = [] |
| 151 |
|
| 152 |
for i, sub in tqdm( |
| 153 |
enumerate(licenses.COMMENTS_SUBS), |
| 154 |
desc="Timing comment res", |
| 155 |
total=len(licenses.COMMENTS_SUBS), |
| 156 |
dynamic_ncols=True, |
| 157 |
): |
| 158 |
start = time.time() |
| 159 |
for text in texts: |
| 160 |
sub.pattern.sub(sub.repl, text) |
| 161 |
elapsed = time.time() - start |
| 162 |
times.append((elapsed, i)) |
| 163 |
|
| 164 |
base, amount = 0, 0 |
| 165 |
interesting = times[base : base + amount] |
| 166 |
if interesting: |
| 167 |
print( |
| 168 |
"Sum of current selection: ", |
| 169 |
sum(elapsed for elapsed, _ in interesting), |
| 170 |
) |
| 171 |
times.sort() |
| 172 |
|
| 173 |
for elapsed, i in [times[0], *interesting, *times[-5:]]: |
| 174 |
print(f"Time for {i} : {elapsed}") |
| 175 |
|
| 176 |
def benchmark_copyright(self): |
| 177 |
texts = self.load_all_licenses() |
| 178 |
times = [] |
| 179 |
|
| 180 |
patterns = ( |
| 181 |
holders.COPYRIGHT_PRE_IGNORE_RES |
| 182 |
+ holders.COPYRIGHT_POST_IGNORE_RES |
| 183 |
+ (holders.COPYRIGHT_PRE_INDICATOR_RE, holders.COPYRIGHT_INDICATOR_RE) |
| 184 |
) |
| 185 |
|
| 186 |
for i, pattern in tqdm( |
| 187 |
enumerate(patterns), |
| 188 |
desc="Timing copyright res", |
| 189 |
total=len(patterns), |
| 190 |
dynamic_ncols=True, |
| 191 |
): |
| 192 |
start = time.time() |
| 193 |
for text in texts: |
| 194 |
pattern.findall(text) |
| 195 |
elapsed = time.time() - start |
| 196 |
times.append((elapsed, i)) |
| 197 |
|
| 198 |
base, amount = 0, 0 |
| 199 |
# len(licenses.COPYRIGHT_PRE_IGNORE_RES) + 1, \ |
| 200 |
# len(licenses.COPYRIGHT_POST_IGNORE_RES) |
| 201 |
interesting = times[base : base + amount] |
| 202 |
if interesting: |
| 203 |
print( |
| 204 |
"Sum of current selection: ", |
| 205 |
sum(elapsed for elapsed, _ in interesting), |
| 206 |
) |
| 207 |
times.sort() |
| 208 |
|
| 209 |
for elapsed, i in [times[0], *interesting, *times[-5:]]: |
| 210 |
print(f"Time for {i} : {elapsed}") |
| 211 |
|
| 212 |
|
| 213 |
if __name__ == "__main__": |
| 214 |
check = SpeedCheck() |
| 215 |
sys.exit(check.run()) |