File: test_all_images.py

package info (click to toggle)
python-fabio 0.11.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 9,092 kB
  • sloc: python: 19,244; ansic: 1,085; makefile: 219; sh: 215
file content (103 lines) | stat: -rw-r--r-- 2,794 bytes parent folder | download | duplicates (3)
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103

"""
Check we can read all the test images
"""

import glob
import os
import time
import fabio.openimage
import gzip
import bz2
import pstats
import sys

try:
    import cProfile
except ImportError:
    import profile as cProfile

times = {}
images = []

for fname in glob.glob(os.path.join("testimages", "*")):
    if fname.find("header_only") == -1:
        images.append(fname)

images.sort()


def shellbench(cmd, imname):
    """
    The shell appears to be lying about it's performance. It claims
    zero time to gunzip a file when it actually takes 200 ms. This is
    cheating via a cache I suspect. We shall try to avoid this problem
    """
    if sys.platform != "win32":
        os.system("touch " + imname)
    astart = time.time()
    dummy_file = os.popen(cmd + " " + imname, "rb").read()
    return time.time() - astart


print("I/O 1  : Time to read the image")
print("I/O 2  : Time to read the image (repeat")
print("Fabio  : Time for fabio to read the image")
print("Shell  : Time for shell to do decompression")
print("Python : Time for python to do decompression\n")

print("I/O 1  I/O 2  Fabio  Shell  Python   Size/MB")
for im in images:
    # Network/disk io time first
    start = time.clock()
    the_file = open(im, "rb").read()
    times[im] = [time.clock() - start]
    start = time.clock()
    # Network/disk should be cached
    the_file = open(im, "rb").read()
    times[im].append(time.clock() - start)
    start = time.clock()
    try:
        fim = fabio.openimage.openimage(im)
    except KeyboardInterrupt:
        raise
    except Exception:
        print("Problem with image %s" % im)
        continue
    times[im].append(time.clock() - start)
    nt = 3
    ns = 2
    # Now check for a fabio slowdown effect
    if im[-3:] == '.gz':
        times[im].append(shellbench("gzip -cd ", im))
        nt += 1
        ns -= 1
        start = time.clock()
        the_file = gzip.GzipFile(im, "rb").read()
        times[im].append(time.clock() - start)
        nt += 1
        ns -= 1
    if im[-4:] == '.bz2':
        times[im].append(shellbench("bzip2 -cd ", im))
        nt += 1
        ns -= 1
        start = time.clock()
        the_file = bz2.BZ2File(im, "rb").read()
        times[im].append(time.clock() - start)
        nt += 1
        ns -= 1
    # Speed ratings in megabytes per second (for fabio)
    MB = len(the_file) / 1024.0 / 1024.0
    try:
        print(("%.4f " * nt + " " * 7 * ns) % tuple(times[im]), "%8.3f" % (MB), im)
    except Exception:
        print(times[im], MB, im)
        raise

    cProfile.run("fabio.openimage.openimage(im)", "stats")
    p = pstats.Stats("stats")
    # Hack around python2.4
    s = sys.stdout
    sys.stdout = open("profile.txt", "a")
    p.strip_dirs().sort_stats(-1).print_stats()
    sys.stdout = s