Commit 66333b56 authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent 617ea1af
...@@ -48,7 +48,8 @@ from glob import glob ...@@ -48,7 +48,8 @@ from glob import glob
from os.path import basename from os.path import basename
from time import time from time import time
from tcpu import B, readfile from golang.testing import B
from tcpu import readfile
# PipeChan is `chan float` which uses OS pipe for communication. # PipeChan is `chan float` which uses OS pipe for communication.
......
...@@ -26,10 +26,10 @@ import sys ...@@ -26,10 +26,10 @@ import sys
import hashlib import hashlib
import tzodb import tzodb
import zlib import zlib
from time import time
from math import ceil, log10
from os.path import dirname from os.path import dirname
from golang import testing
# fmtsize formats size in human readable form # fmtsize formats size in human readable form
_unitv = "BKMGT" # (2^10)^i represents by corresponding char suffix _unitv = "BKMGT" # (2^10)^i represents by corresponding char suffix
def fmtsize(size): def fmtsize(size):
...@@ -50,66 +50,19 @@ def prettyarg(arg): ...@@ -50,66 +50,19 @@ def prettyarg(arg):
return fmtsize(arg) return fmtsize(arg)
# ---- 8< ---- from wendelin.core/t/py.bench # benchit benchmarks benchf(bencharg)
def benchit(benchf, bencharg):
# B is benchmarking timer/request passed to benchmarks as fixture def _(b):
# similar to https://golang.org/pkg/testing/#B.
class B:
def __init__(self):
self.N = 1 # default when func does not accept `b` arg
self._t_start = None # t of timer started; None if timer is currently stopped
self.reset_timer()
def reset_timer(self):
self._t_total = 0.
def start_timer(self):
if self._t_start is not None:
return
self._t_start = time()
def stop_timer(self):
if self._t_start is None:
return
t = time()
self._t_total += t - self._t_start
self._t_start = None
def total_time(self):
return self._t_total
# benchit runs benchf auto-adjusting whole runing time to ttarget
def benchit(benchf, bencharg, ttarget = 1.):
b = B()
b.N = 0
t = 0.
while t < (ttarget * 0.9):
if b.N == 0:
b.N = 1
else:
n = b.N * (ttarget / t) # exact how to adjust b.N to reach ttarget
order = int(log10(n)) # n = k·10^order, k ∈ [1,10)
k = float(n) / (10**order)
k = ceil(k) # lift up k to nearest int
b.N = int(k * 10**order) # b.N = int([1,10))·10^order
b.reset_timer()
b.start_timer()
benchf(b, bencharg) benchf(b, bencharg)
b.stop_timer() r = testing.benchmark(_)
t = b.total_time()
benchname = benchf.__name__ benchname = benchf.__name__
if benchname.startswith('bench_'): if benchname.startswith('bench_'):
benchname = benchname[len('bench_'):] benchname = benchname[len('bench_'):]
print('Benchmark%s/py/%s %d\t%.3f µs/op' % print('Benchmark%s/py/%s %d\t%.3f µs/op' %
(benchname, prettyarg(bencharg), n, t * 1E6 / n)) (benchname, prettyarg(bencharg), r.N, r.T * 1E6 / r.N))
# ---- 8< ----
def _bench_hasher(b, h, blksize): def _bench_hasher(b, h, blksize):
blksize = int(blksize) blksize = int(blksize)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment