~sschwarzer/ftputil

ref: c6d0136bf66260c302304a9c53848e08337d405d ftputil/sandbox/cache_benchmark.py -rwxr-xr-x 1.4 KiB
c6d0136bStefan Schwarzer Mention that `account` and `session_factory` normally aren't needed 6 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#! /usr/bin/env python
# Copyright (C) 2011, Stefan Schwarzer

import random
import sys
import time

import ftp_stat_cache


def print_statistics(task, old_time, new_time):
    """Print how long `task` (a string) took."""
    time_difference = new_time - old_time
    print "%40s took: %8.3f seconds" % (task, time_difference)


def main(max_size, new_entries):
    cache = ftp_stat_cache.StatCache()
    cache.resize(max_size)
    #cache = {}
    # Populate cache until it's full.
    t1 = time.time()
    L = range(max_size)
    random.shuffle(L)
    for i in xrange(max_size):
        rand = L[i]
        # The cache checks if entries start with "/".
        cache["/%d" % rand] = rand
    t2 = time.time()
    print_statistics("Filling cache with %d entries" % max_size, t1, t2)
    # Read the cache.
    for i in xrange(max_size):
        data = cache["/%d" % i]
    t3 = time.time()
    print_statistics("Reading the cache", t2, t3)
    # Now that the cache is full, try to add 100 more entries,
    # implicitly replacing old entries.
    L = range(new_entries)
    random.shuffle(L)
    for i in xrange(new_entries):
        rand = L[i]
        # Make sure to add entries, not replace them.
        cache["/%d" % (max_size+rand)] = rand
    t4 = time.time()
    print_statistics("Replacing %d entries" % new_entries, t3, t4)


if __name__ == '__main__':
    test_size = 10000
    main(max_size=test_size, new_entries=test_size)