|
1 | | -from time import time, sleep |
2 | | -from diskcache import FanoutCache |
3 | | - |
4 | | -# Initialise the maximum delay reference |
5 | | -max_delay = 0 |
6 | | - |
7 | | -# Initialise the reference to current time |
8 | | -# Add a significant amount of time to ignore the first measurement due to other initialisations |
9 | | -current_time = time() + 100 |
10 | | - |
11 | | -# Initialise the Fanout Cache instance |
12 | | -data = FanoutCache('test', shards=8) |
13 | | - |
14 | | - |
15 | | -# Declare the function used to modify the data |
16 | | -def set_data(**kwargs): |
17 | | - """ |
18 | | -
|
19 | | - Function used to modify the cache. |
20 | | -
|
21 | | - :param kwargs: Key, value pairs of data to modify. |
22 | | -
|
23 | | - """ |
24 | | - |
25 | | - # Fetch the global variables |
26 | | - global max_delay |
27 | | - global current_time |
28 | | - global data |
29 | | - |
30 | | - # Update the data with the given keyword arguments |
31 | | - for key, value in kwargs.items(): |
32 | | - |
33 | | - # Measure the time taken to update it for each item |
34 | | - temp = time() |
35 | | - dif = temp - current_time |
36 | | - if dif > max_delay: |
37 | | - max_delay = dif |
38 | | - print("New max delay encountered: ", max_delay) |
39 | | - current_time = temp |
40 | | - |
41 | | - # Update the data |
42 | | - data[key] = value |
43 | | - |
44 | | - |
45 | | -# Initialise some test values |
46 | | -test_values = {str(key): value for key in range(25) for value in range(25)} |
47 | | - |
48 | | -# Keep writing the data in 0.1 sec intervals |
49 | | -while True: |
50 | | - sleep(0.1) |
51 | | - set_data(**test_values) |
| 1 | +"""Benchmark for Issue #109 |
| 2 | +
|
| 3 | +""" |
| 4 | + |
| 5 | +import time |
| 6 | +import diskcache as dc |
| 7 | + |
| 8 | + |
| 9 | +def main(): |
| 10 | + import argparse |
| 11 | + parser = argparse.ArgumentParser() |
| 12 | + parser.add_argument('--cache-dir', default='/tmp/test') |
| 13 | + parser.add_argument('--iterations', type=int, default=100) |
| 14 | + parser.add_argument('--sleep', type=float, default=0.1) |
| 15 | + parser.add_argument('--size', type=int, default=25) |
| 16 | + args = parser.parse_args() |
| 17 | + |
| 18 | + data = dc.FanoutCache(args.cache_dir) |
| 19 | + delays = [] |
| 20 | + values = {str(num): num for num in range(args.size)} |
| 21 | + iterations = args.iterations |
| 22 | + |
| 23 | + for i in range(args.iterations): |
| 24 | + print(f'Iteration {i + 1}/{iterations}', end='\r') |
| 25 | + time.sleep(args.sleep) |
| 26 | + for key, value in values.items(): |
| 27 | + start = time.monotonic() |
| 28 | + data[key] = value |
| 29 | + stop = time.monotonic() |
| 30 | + diff = stop - start |
| 31 | + delays.append(diff) |
| 32 | + |
| 33 | + # Discard warmup delays, first two iterations. |
| 34 | + del delays[:(len(values) * 2)] |
| 35 | + |
| 36 | + # Convert seconds to microseconds. |
| 37 | + delays = sorted(delay * 1e6 for delay in delays) |
| 38 | + |
| 39 | + # Display performance. |
| 40 | + print() |
| 41 | + print(f'Total #: {len(delays)}') |
| 42 | + print(f'Min delay (us): {delays[0]:>8.3f}') |
| 43 | + print(f'50th %ile (us): {delays[int(len(delays) * 0.50)]:>8.3f}') |
| 44 | + print(f'90th %ile (us): {delays[int(len(delays) * 0.90)]:>8.3f}') |
| 45 | + print(f'99th %ile (us): {delays[int(len(delays) * 0.99)]:>8.3f}') |
| 46 | + print(f'Max delay (us): {delays[-1]:>8.3f}') |
| 47 | + |
| 48 | + |
| 49 | +if __name__ == '__main__': |
| 50 | + main() |
0 commit comments