File: image_encoding_speed_test.py

package info (click to toggle)
python-mapnik 1%3A0.0~20200224-7da019cf9-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 10,784 kB
  • sloc: python: 12,085; cpp: 5,717; sh: 101; makefile: 18
file content (129 lines) | stat: -rw-r--r-- 3,631 bytes parent folder | download | duplicates (4)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
from timeit import Timer, time

import mapnik

from .utilities import execution_path, run_all


def setup():
    # All of the paths used are relative, if we run the tests
    # from another directory we need to chdir()
    os.chdir(execution_path('.'))

combinations = ['png',
                'png8',
                'png8:m=o',
                'png8:m=h',
                'png8:m=o:t=0',
                'png8:m=o:t=1',
                'png8:m=o:t=2',
                'png8:m=h:t=0',
                'png8:m=h:t=1',
                'png8:m=h:t=2',
                'png:z=1',
                'png:z=1:t=0',  # forces rbg, no a
                'png8:z=1',
                'png8:z=1:m=o',
                'png8:z=1:m=h',
                'png8:z=1:c=1',
                'png8:z=1:c=24',
                'png8:z=1:c=64',
                'png8:z=1:c=128',
                'png8:z=1:c=200',
                'png8:z=1:c=255',
                'png8:z=9:c=64',
                'png8:z=9:c=128',
                'png8:z=9:c=200',
                'png8:z=1:c=50:m=h',
                'png8:z=1:c=1:m=o',
                'png8:z=1:c=1:m=o:s=filtered',
                'png:z=1:s=filtered',
                'png:z=1:s=huff',
                'png:z=1:s=rle',
                'png8:m=h:g=2.0',
                'png8:m=h:g=1.0'
                ]

tiles = [
    'blank',
    'solid',
    'many_colors',
    'aerial_24'
]

iterations = 10


def do_encoding():

    global image

    results = {}
    sortable = {}

    def run(func, im, format, t):
        global image
        image = im
        start = time.time()
        set = t.repeat(iterations, 1)
        elapsed = (time.time() - start)
        min_ = min(set) * 1000
        avg = (sum(set) / len(set)) * 1000
        name = func.__name__ + ' ' + format
        results[name] = [min_, avg, elapsed * 1000, name, len(func())]
        sortable[name] = [min_]

    if 'blank' in tiles:
        def blank():
            return eval('image.tostring("%s")' % c)
        blank_im = mapnik.Image(512, 512)
        for c in combinations:
            t = Timer(blank)
            run(blank, blank_im, c, t)

    if 'solid' in tiles:
        def solid():
            return eval('image.tostring("%s")' % c)
        solid_im = mapnik.Image(512, 512)
        solid_im.fill(mapnik.Color("#f2efe9"))
        for c in combinations:
            t = Timer(solid)
            run(solid, solid_im, c, t)

    if 'many_colors' in tiles:
        def many_colors():
            return eval('image.tostring("%s")' % c)
        # lots of colors: http://tile.osm.org/13/4194/2747.png
        many_colors_im = mapnik.Image.open('../data/images/13_4194_2747.png')
        for c in combinations:
            t = Timer(many_colors)
            run(many_colors, many_colors_im, c, t)

    if 'aerial_24' in tiles:
        def aerial_24():
            return eval('image.tostring("%s")' % c)
        aerial_24_im = mapnik.Image.open('../data/images/12_654_1580.png')
        for c in combinations:
            t = Timer(aerial_24)
            run(aerial_24, aerial_24_im, c, t)

    for key, value in sorted(sortable.items(), key=lambda i: (i[1], i[0])):
        s = results[key]
        min_ = str(s[0])[:6]
        avg = str(s[1])[:6]
        elapsed = str(s[2])[:6]
        name = s[3]
        size = s[4]
        print(
            'min: %sms | avg: %sms | total: %sms | len: %s <-- %s' %
            (min_, avg, elapsed, size, name))


if __name__ == "__main__":
    setup()
    do_encoding()
    exit(run_all(eval(x) for x in dir() if x.startswith("test_")))