Skip to content

Commit 3bde28c

Browse files
committed
RFC: refactor benchmarks setups and customize pytest's discovery rule to enable running benchmarks through pytest
1 parent 81482cf commit 3bde28c

File tree

14 files changed

+88
-5
lines changed

14 files changed

+88
-5
lines changed

benchmarks/coordinates.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ def setup(self):
5151
self.array_rep = CartesianRepresentation(np.ones((3, 1000)) * u.kpc)
5252
self.array_dif = CartesianDifferential(np.ones((3, 1000)) * u.km / u.s)
5353

54+
# pytest compat
55+
setup_method = setup
56+
5457
def time_with_differentials_scalar(self):
5558
self.scalar_rep.with_differentials(self.scalar_dif)
5659

@@ -129,6 +132,9 @@ def setup(self):
129132
xyz_clustered2, representation_type=CartesianRepresentation
130133
)
131134

135+
# pytest compat
136+
setup_method = setup
137+
132138
def time_init_nodata(self):
133139
FK5()
134140

@@ -188,6 +194,9 @@ def setup(self):
188194
lat=self.array_q_dec, lon=self.array_q_ra
189195
)
190196

197+
# pytest compat
198+
setup_method = setup
199+
191200
def time_init_scalar(self):
192201
SkyCoord(1, 2, unit="deg", frame="icrs")
193202

benchmarks/io_ascii/core.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ def setup(self):
4141
for col, x in izip(self.cols, lst):
4242
col.str_vals = [str(s) for s in x]
4343

44+
# pytest compat
45+
setup_method = setup
46+
4447
def time_continuation_inputter(self):
4548
core.ContinuationLinesInputter().process_lines(self.lines)
4649

benchmarks/io_ascii/fixedwidth.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ def setup(self):
1818
self.splitter.cols = self.header.cols
1919
self.data = ascii.FixedWidthData()
2020

21+
# pytest compat
22+
setup_method = setup
23+
2124
def time_splitter(self):
2225
self.splitter(self.lines[1:])
2326

benchmarks/io_ascii/ipac.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ def setup(self):
2626
self.data.cols = list(self.table.columns.values())
2727
self.data._set_fill_values(self.data.cols)
2828

29+
# pytest compat
30+
setup_method = setup
31+
2932
def time_splitter(self):
3033
self.splitter.join(self.vals, self.widths)
3134

benchmarks/io_ascii/main.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ def setup(self):
4343
if self.file_format != "sextractor":
4444
self.table = self.read()
4545

46+
# pytest compat
47+
setup_method = setup
48+
4649
def read(self):
4750
return ascii.read(BytesIO(self.data), format=self.file_format, guess=False)
4851

benchmarks/io_ascii/rdb.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,8 @@ def setup(self):
1313
self.lines = f.read().split("\n")
1414
f.close()
1515

16+
# pytest compat
17+
setup_method = setup
18+
1619
def time_get_cols(self):
1720
self.header.get_cols(self.lines)

benchmarks/io_ascii/sextractor.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,8 @@ def setup(self):
2323
self.lines.append("# {} {} Description [pixel**2]".format(i, randword()))
2424
self.lines.append("Non-header line")
2525

26+
# pytest compat
27+
setup_method = setup
28+
2629
def time_header(self):
2730
self.header.get_cols(self.lines)

benchmarks/io_ascii/table.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ def setup(self):
2828
self.outputter = core.TableOutputter()
2929
self.table = table.Table()
3030

31+
# pytest compat
32+
setup_method = setup
33+
3134
def time_table_outputter(self):
3235
self.outputter(self.cols, {"table": {}})
3336

benchmarks/io_fits.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ def setup(self):
2525
t["booleans"] = t["floats"] > 0.5
2626
t.write(temp, format="fits")
2727

28+
# pytest compat
29+
setup_method = setup
30+
2831
def time_read_nommap(self):
2932
try:
3033
Table.read(self.table_file, format="fits", memmap=False)
@@ -42,7 +45,7 @@ def time_write(self):
4245
t.write(table_bytes, format="fits")
4346

4447

45-
class FITSBinTableHDU:
48+
class FITSBinTableHDUBenchmarks:
4649
def time_from_columns_bytes(self):
4750
x = np.repeat(b"a", 2_000_000)
4851
array = np.array(x, dtype=[("col", "S1")])
@@ -57,7 +60,7 @@ def make_header(ncards=1000):
5760
return fits.Header(cards)
5861

5962

60-
class FITSHeader:
63+
class FITSHeaderBenchmarks:
6164
"""
6265
Tests of the Header interface
6366
"""
@@ -66,6 +69,9 @@ def setup(self):
6669
self.hdr = make_header()
6770
self.hdr_string = self.hdr.tostring()
6871

72+
# pytest compat
73+
setup_method = setup
74+
6975
def time_get_int(self):
7076
self.hdr.get("INT999")
7177

@@ -85,21 +91,23 @@ def time_fromstring(self):
8591
fits.Header.fromstring(self.hdr_string)
8692

8793

88-
class FITSHDUList:
94+
class FITSHDUListBenchmarks:
8995
"""
9096
Tests of the HDUList interface
9197
"""
9298

93-
filename = "many_hdu.fits"
94-
9599
def setup_cache(self):
100+
self.filename = NamedTemporaryFile(delete=False).name
96101
hdr = make_header()
97102
hdul = fits.HDUList(
98103
[fits.PrimaryHDU(header=hdr)]
99104
+ [fits.ImageHDU(header=hdr) for _ in range(30)]
100105
)
101106
hdul.writeto(self.filename)
102107

108+
# pytest compat
109+
setup_method = setup_cache
110+
103111
def time_getheader(self):
104112
fits.getheader(self.filename)
105113

benchmarks/modeling/compound.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ def setup(self):
4747
| models.RotateNative2Celestial(5.6, -72.05, 180)
4848
)
4949

50+
# pytest compat
51+
setup_method = setup
52+
5053
def time_scalar(self):
5154
r, d = self.model(x_no_units_scalar, x_no_units_scalar)
5255

@@ -77,6 +80,9 @@ def setup(self):
7780
| models.RotateNative2Celestial(5.6 * u.deg, -72.05 * u.deg, 180 * u.deg)
7881
)
7982

83+
# pytest compat
84+
setup_method = setup
85+
8086
def time_scalar(self):
8187
r, d = self.model(x_no_units_scalar * u.pix, x_no_units_scalar * u.pix)
8288

0 commit comments

Comments
 (0)