Use pytest-benchmark for benchmarking

This commit is contained in:
Jan Petykiewicz 2020-03-15 23:15:30 -07:00
commit cd0ca442b2
2 changed files with 60 additions and 92 deletions

View file

@ -1,92 +0,0 @@
"""
Benchmarking, run with `python3 -m adze.test.benchmark`
TODO: Consider using pytest-benchmark?
"""
import warnings
from time import perf_counter
import random
import logging
import numpy
from numpy import sin, cos, pi
from numpy.linalg import norm
import adze
import adze.util
from .utils import TOL_FAIL, TOL_WARN, PRNG, MIN_VOLUME, flip_plane, tetrahedron_volume
from .utils import build_rand_tetrahedron, plane_generators, rand_tetrahedron_vertices
logger = logging.getLogger(__name__)
try:
# Check if running with line_profiler
profile
except NameError:
def profile(f):
return f
@profile
def time_split():
NUM_TRIALS = 10_000
logger.info(f'Intersecting {NUM_TRIALS} pairs of tetrahedra.')
polys = [build_rand_tetrahedron() for _ in range(NUM_TRIALS)]
facesets = [adze.planes_from_tetrahedron(rand_tetrahedron_vertices()) for _ in range(NUM_TRIALS)]
t0 = perf_counter()
for t in range(NUM_TRIALS):
# clip the first tet against the faces of the second
for face in facesets[t]:
polys[t] = adze.clip(polys[t], face)
if (t + 1) % (NUM_TRIALS // 10) == 0:
t_each = (perf_counter() - t0) / (t + 1)
logger.info(f'time per split for {t+1} splits: {t_each}')
logger.info(f'done timing, time per split: {(perf_counter() - t0) / NUM_TRIALS}')
polys_bulk = [build_rand_tetrahedron() for _ in range(NUM_TRIALS)]
bulk = {
'vertices': numpy.vstack([p['vertices'] for p in polys_bulk]),
'neighbors': numpy.vstack([p['neighbors'] for p in polys_bulk]) + numpy.arange(NUM_TRIALS).repeat(4)[:, None] * 4,
}
t1 = perf_counter()
poly = adze.clip(bulk, facesets[0][0])
logger.info(f'bulked split time per tet: {(perf_counter() - t1) / NUM_TRIALS}')
@profile
def time_intersection(poly_order=0):
"""
Benchmark intersection between pairs of tetrahedra
"""
NUM_TRIALS = 1000
t0 = perf_counter()
logger.info(f'Intersecting {NUM_TRIALS} pairs of tetrahedra.')
for t in range(NUM_TRIALS):
poly = build_rand_tetrahedron()
faces = adze.planes_from_tetrahedron(rand_tetrahedron_vertices())
# Clip the first tetrahedron using the faces of the second
for face in faces:
poly = adze.clip(poly, face)
# find the moments (up to quadratic order) of the clipped poly
_om = adze.get_moments(poly, poly_order)
if (t+1) % 10000 == 0:
t_each = (perf_counter() - t0) / (t + 1)
logger.info(f'time per split+get_moments for {t+1} iterations: {t_each}')
logger.info(f'done timing, time per split+get_moments: {(perf_counter() - t0) / NUM_TRIALS}')
if __name__ == '__main__':
logging.basicConfig(level=logging.INFO)
logger.info('time_intersection')
time_intersection()
logger.info('time_split')
time_split()

View file

@ -0,0 +1,60 @@
"""
Benchmarking, run with `python3 -m adze.test.benchmark`
TODO: Consider using pytest-benchmark?
"""
import warnings
from time import perf_counter
import random
import logging
import numpy
from numpy import sin, cos, pi
from numpy.linalg import norm
import adze
import adze.util
from .utils import TOL_FAIL, TOL_WARN, PRNG, MIN_VOLUME, flip_plane, tetrahedron_volume
from .utils import build_rand_tetrahedron, plane_generators, rand_tetrahedron_vertices
logger = logging.getLogger(__name__)
def test_split(benchmark):
poly = build_rand_tetrahedron()
faceset = adze.planes_from_tetrahedron(rand_tetrahedron_vertices())
benchmark(adze.clip, poly, faceset[0])
def test_split_bulk_1000(benchmark):
NUM_TRIALS = 1000
polys_bulk = [build_rand_tetrahedron() for _ in range(NUM_TRIALS)]
faceset = adze.planes_from_tetrahedron(rand_tetrahedron_vertices())
bulk = {
'vertices': numpy.vstack([p['vertices'] for p in polys_bulk]),
'neighbors': numpy.vstack([p['neighbors'] for p in polys_bulk]) + numpy.arange(NUM_TRIALS).repeat(4)[:, None] * 4,
}
benchmark(adze.clip, bulk, faceset[0])
def get_intersection_moments(poly, faces, poly_order):
# Clip the first tetrahedron using the faces of the second
for face in faces:
poly = adze.clip(poly, face)
# find the moments (up to quadratic order) of the clipped poly
om = adze.get_moments(poly, poly_order)
return om
def test_intersection(benchmark, poly_order=0):
"""
Benchmark intersection between pairs of tetrahedra
"""
poly = build_rand_tetrahedron()
faces = adze.planes_from_tetrahedron(rand_tetrahedron_vertices())
benchmark(get_intersection_moments, poly, faces, poly_order)