def test_timer(): tmp = doctable.TempFolder('logs') def test_func(sec=0.02): time.sleep(sec) # return sum(i for i in range(n)) with doctable.Timer('trying out enter and exit', logfile=tmp.path.joinpath('0.log')): test_func() timer = doctable.Timer('testing verbose stepping') timer.step('running one thing') test_func() timer.step() test_func() timer.step('running last thing') timer = doctable.Timer('testing non-verbose stepping', verbose=False, logfile=tmp.path.joinpath('2.log')) test_func() timer.step('whatever this step is') test_func() timer.step('next step') test_func() timer.step('that\'s all folks.') timer = doctable.Timer(verbose=False) for i in range(10): time.sleep(0.01) timer.step() mean = timer.get_diff_stat(stat='mean', as_str=False) assert (mean >= 0.01 and mean < 0.011) med = timer.get_diff_stat(stat='median', as_str=False) assert (mean >= 0.01 and mean < 0.011) stdev = timer.get_diff_stat(stat='stdev', as_str=False) assert (stdev > 0 and stdev <= 0.001) print(mean, med, stdev) print(doctable.Timer.time_call(lambda: time.sleep(0.001), num_calls=10)) print( doctable.Timer.time_call(lambda: time.sleep(0.001), num_calls=10, as_str=True))
def run_benchmark(num_vals=10): tmp = doctable.TempFolder('tmp') timer = doctable.Timer('creating databases', logfile=tmp.joinpath('log.txt')) db1 = doctable.DocTable(schema=TestObj1, target=tmp.joinpath('1.db'), new_db=True) db2 = doctable.DocTable(schema=TestObj2, target=tmp.joinpath('2.db'), new_db=True) db2.clean_col_files('data') timer.step('creating synthetic data') data1 = [TestObj1(i) for i in range(num_vals)] data2 = [TestObj2(i) for i in range(num_vals)] timer.step('insert into table directly') db1.insert(data1) timer.step('insert into a column file') db2.insert(data2) timer.step('finished inserting') print(f'===========================================') print(f'===== Total took: {timer.total_diff()} =================') print(f'===========================================')
def test_result_containers(): t = doctable.Timer() t.step('constructing from range') seq = MySequence(range(10)) t.step('') print(seq.sum())
def time_call(func, *args, num_calls=10, **kwargs): timer = doctable.Timer(verbose=False) for i in (range(num_calls)): with timer: func(*args, **kwargs) #print([t._ts for t in timer[1:]]) return av_time(timer)
def test_workerpool(n=100): pool = doctable.WorkerPool(1, verbose=False) assert (not pool.any_alive()) pool.start() assert (pool.any_alive()) print(pool) print(f'average efficiency: {pool.av_efficiency()}') pool.join() assert (not pool.any_alive()) with pytest.raises(doctable.NoWorkersAvailable): pool.av_efficiency() with doctable.WorkerPool(3, verbose=False) as pool: assert (pool.any_alive()) print(f'av efficiency: {pool.av_efficiency()}') # test most basic map function elements = list(range(100)) assert (pool.map(example_func, elements) == [example_func(e) for e in elements]) print(f'av efficiency: {pool.av_efficiency()}') elements = list(range(100)) with doctable.Timer(): with doctable.WorkerPool(10, verbose=False) as pool: pool.map(example_sleep_func, elements) print(f'av efficiency: {pool.av_efficiency()}')
def speed_benchmark(folder='tmp'): timer = doctable.Timer('starting timer') settings = itertools.product([10, 50, 100], [1000, 5000], [1000, 2000]) for s1, s2, s3 in settings: benchmark_insert_read(timer, folder, save_every=s1, num_records=s2, record_size=s3) timer.step('deleting all data') fs = doctable.FSStore(folder) fs.delete_all_completely(force=True) timer.step('finished!')
def test_basic(): timer = doctable.Timer('starting tests') timer.step('init fsstore') fs = doctable.FSStore('tmp', save_every=100) timer.step('clearing all settings') fs.clear_settings() timer.step(f'current settings data: {fs.read_settings()}') timer.step(f'inserting unknown settings data.') with pytest.raises(KeyError): fs.write_settings(whateve='plz_don\'t_work') timer.step(f'current settings data: {fs.read_settings()}') # test resetting of seed timer.step('checking seed change') seed1 = fs.seed fs.set_seed() assert (seed1 != fs.seed) timer.step(f'deleting old records') fs.delete_records() timer.step(f'seed={fs.seed}; inserting records') records = [i for i in range(1000)] for r in records: fs.insert(r) fs.dump_file() timer.step('finished inserting; now checking integrity') assert (sum(records) == sum(fs.select_records(loadbar=True))) timer.step('asserting deletion of records') fs.delete_records() assert (len(glob.glob(f'{fs.folder}/*.pic')) == 0) timer.step('asserting deleted all completely!') fs.delete_all_completely() assert (not os.path.exists(fs.folder))
def time_call(cls, func: Callable, *args, num_calls=1, as_str=False, **kwargs): ''' Time function call with 0.05 ms latency per call. ''' timer = cls(verbose=False) timer = doctable.Timer(verbose=False) for i in range(10): func(*args, **kwargs) timer.step() if as_str: mean = timer.get_diff_stat(stat='mean', as_str=True) med = timer.get_diff_stat(stat='median', as_str=True) stdev = timer.get_diff_stat(stat='stdev', as_str=True) return f'{mean} ({med}) ± {stdev}' else: return timer.get_diff_stat(stat='mean', as_str=False)
def test_io(): timer = doctable.Timer('starting io tests') obj = list(range(10000)) fname = 'tmpfile.json_or_pic' timer.step('writing pickle') doctable.write_pickle(obj, fname) assert (os.path.exists(fname)) timer.step('reading pickle') assert (obj == doctable.read_pickle(fname)) os.remove(fname) timer.step('writing json') doctable.write_json(obj, fname) assert (os.path.exists(fname)) timer.step('reading json') assert (obj == doctable.read_json(fname)) os.remove(fname) timer.step('finished')
sys.path.append('..') import doctable @doctable.schema class Sample: __slots__ = [] id: int vec: torch.Tensor if __name__ == '__main__': #device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") #device = torch.device('cuda:0') device = torch.device('cpu') print(device) timer = doctable.Timer() if True: timer.step('creating matrix') nsamp = int(100e3) V = torch.rand((nsamp, 300)).to(device) timer.step('creating objects to encapsulate') samps = list() for i in range(V.shape[0]): samps.append(Sample(i, V[i,:])) a_vec = torch.rand((300)).to(device) a_mat = torch.rand((10000, 300)).to(device) timer.step('executing single vectors')
def test_play(n=1000): '''Tests ability to solve tasks when tasks take an unequal ammount of time to execute. ''' timer = doctable.Timer(logfile='logs/parallel_primefinder.log') timer.step('making elements') elements = list(range(3)) with multiprocessing.Pool(24) as p: out = p.map(TestWorker, elements) print(out) exit() if False: test_func = find_prime_long elements = list(range(n)) random.shuffle(elements) elif False: test_func = array_test import numpy as np elements = [np.ones((int(5e7) * (i + 1), )) for i in range(10)] for a in elements: a[0] = 0 print(len(elements), elements[0].shape) elif True: test_func = timed_step elements = list(range(n)) random.shuffle(elements) timer.step('check ram') if False: timer.step('single-core eval') prime_single = list(map(test_func, elements)) timer.step('multiprocessing.Pool') with multiprocessing.Pool(24) as p: prime_multi = p.map(test_func, elements) if False: timer.step('map_async') with multiprocessing.Pool(6) as p: prime_async = list(p.map_async(test_func, elements).get()) timer.step('imap') with multiprocessing.Pool(6) as p: prime_imap = list(p.imap(test_func, elements, 100)) timer.step('imap_unordered') with multiprocessing.Pool(6) as p: prime_unordered = list(p.imap_unordered(test_func, elements, 100)) if False: timer.step('doctable.Distribute') with doctable.Distribute(24) as d: prime_distribute = d.map_chunk(test_func, elements) timer.step('doctable.WorkerPool') with doctable.WorkerPool(24) as p: prime_async = p.map(test_func, elements) print(f'av efficiency: {p.av_efficiency()}') timer.step('annnnndddd time!') assert (prime_multi == prime_async) timer.step('done')
def read(self): return self.value def test_thread(ms): time.sleep(20) return [len(m) for m in ms] if __name__ == '__main__': # 10 mil / 512 MB # 20 mil / 900 MB # 30 mil / 1285 MB # 40 mil / 1672 MB # 50 mil / 20158 MB with doctable.Timer('Making big array'): m = [i for i in range(30000000)] time.sleep(20) # Create an actor from this class print('the ray way') m = ray.put(m) counters = [Counter.remote(m) for _ in range(5)] print(ray.get([c.shape.remote() for c in counters])) del m del counters exit() # alternatively, use doctable print('old school way') mats = [m.copy() for _ in range(5)]