File: benchmark.py

package info (click to toggle)
grass 7.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 135,976 kB
  • ctags: 44,148
  • sloc: ansic: 410,300; python: 166,939; cpp: 34,819; sh: 9,358; makefile: 6,618; xml: 3,551; sql: 769; lex: 519; yacc: 450; asm: 387; perl: 282; sed: 17; objc: 7
file content (438 lines) | stat: -rw-r--r-- 13,000 bytes parent folder | download
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
# -*- coding: utf-8 -*-
"""
Created on Sat Jun 16 20:24:56 2012

@author: soeren
"""
from __future__ import (nested_scopes, generators, division, absolute_import,
                        with_statement, print_function, unicode_literals)

import optparse
#import numpy as np
import time
import collections
import copy
import cProfile
import sys, os
from jinja2 import Template
sys.path.append(os.getcwd())
sys.path.append("%s/.."%(os.getcwd()))

import grass.lib.gis as libgis
import grass.lib.raster as libraster
import grass.script as core
import grass.pygrass
import ctypes


def test__RasterSegment_value_access__if():
    test_a = pygrass.RasterSegment(name="test_a")
    test_a.open(mode="r")

    test_c = pygrass.RasterSegment(name="test_c")
    test_c.open(mode="w", mtype="CELL", overwrite=True)

    buff_a = pygrass.Buffer(test_a.cols, test_a.mtype)

    for row in range(test_a.rows):
        test_a.get_row(row, buff_a)
        for col in range(test_a.cols):
            test_c.put(row, col, buff_a[col] > 50)

    test_a.close()
    test_c.close()

def test__RasterSegment_value_access__add():
    test_a = pygrass.RasterSegment(name="test_a")
    test_a.open(mode="r")

    test_b = pygrass.RasterSegment(name="test_b")
    test_b.open(mode="r")

    test_c = pygrass.RasterSegment(name="test_c")
    test_c.open(mode="w", mtype="DCELL", overwrite=True)

    buff_a = pygrass.Buffer(test_a.cols, test_a.mtype)
    buff_b = pygrass.Buffer(test_b.cols, test_b.mtype)

    for row in range(test_a.rows):
        test_a.get_row(row, buff_a)
        test_b.get_row(row,buff_b)
        for col in range(test_a.cols):
            test_c.put(row, col, buff_a[col] + buff_b[col])

    test_a.close()
    test_b.close()
    test_c.close()

def test__RasterSegment_row_access__if():
    test_a = pygrass.RasterSegment(name="test_a")
    test_a.open(mode="r")

    test_c = pygrass.RasterSegment(name="test_c")
    test_c.open(mode="w", mtype="CELL", overwrite=True)

    buff_a = pygrass.Buffer(test_a.cols, test_a.mtype)

    for row in range(test_a.rows):
        test_a.get_row(row, buff_a)
        test_c.put_row(row, buff_a > 50)

    test_a.close()
    test_c.close()

def test__RasterSegment_row_access__add():
    test_a = pygrass.RasterSegment(name="test_a")
    test_a.open(mode="r")

    test_b = pygrass.RasterSegment(name="test_b")
    test_b.open(mode="r")

    test_c = pygrass.RasterSegment(name="test_c")
    test_c.open(mode="w", mtype="DCELL", overwrite=True)

    buff_a = pygrass.Buffer(test_a.cols, test_a.mtype)
    buff_b = pygrass.Buffer(test_b.cols, test_b.mtype)

    for row in range(test_a.rows):
        test_a.get_row(row, buff_a)
        test_b.get_row(row,buff_b)
        test_c.put_row(row, buff_a + buff_b)

    test_a.close()
    test_b.close()
    test_c.close()

def test__RasterRow_value_access__add():
    test_a = pygrass.RasterRow(name="test_a")
    test_a.open(mode="r")

    test_b = pygrass.RasterRow(name="test_b")
    test_b.open(mode="r")

    test_c = pygrass.RasterRow(name="test_c")
    test_c.open(mode="w", mtype="FCELL", overwrite=True)

    buff_a = pygrass.Buffer(test_a.cols, test_a.mtype)
    buff_b = pygrass.Buffer(test_b.cols, test_b.mtype)
    buff_c = pygrass.Buffer(test_b.cols, test_b.mtype)

    for row in range(test_a.rows):
        test_a.get_row(row, buff_a)
        test_b.get_row(row,buff_b)

        for col in range(test_a.cols):
            buff_c[col] = buff_a[col] + buff_b[col]

        test_c.put_row(buff_c)

    test_a.close()
    test_b.close()
    test_c.close()

def test__RasterRow_value_access__if():
    test_a = pygrass.RasterRow(name="test_a")
    test_a.open(mode="r")

    test_c = pygrass.RasterRow(name="test_c")
    test_c.open(mode="w", mtype="CELL", overwrite=True)

    buff_a = pygrass.Buffer(test_a.cols, test_a.mtype)
    buff_c = pygrass.Buffer(test_a.cols, test_a.mtype)

    for row in range(test_a.rows):
        test_a.get_row(row, buff_a)

        for col in range(test_a.cols):
            buff_c[col] = buff_a[col] > 50

        test_c.put_row(buff_c)

    test_a.close()
    test_c.close()

def test__RasterRowIO_row_access__add():
    test_a = pygrass.RasterRowIO(name="test_a")
    test_a.open(mode="r")

    test_b = pygrass.RasterRowIO(name="test_b")
    test_b.open(mode="r")

    test_c = pygrass.RasterRowIO(name="test_c")
    test_c.open(mode="w", mtype="FCELL", overwrite=True)

    buff_a = pygrass.Buffer(test_a.cols, test_a.mtype)
    buff_b = pygrass.Buffer(test_b.cols, test_b.mtype)

    for row in range(test_a.rows):
        test_a.get_row(row, buff_a)
        test_b.get_row(row,buff_b)
        test_c.put_row(buff_a + buff_b)

    test_a.close()
    test_b.close()
    test_c.close()

def test__RasterRowIO_row_access__if():
    test_a = pygrass.RasterRowIO(name="test_a")
    test_a.open(mode="r")

    test_c = pygrass.RasterRowIO(name="test_c")
    test_c.open(mode="w", mtype="CELL", overwrite=True)

    buff_a = pygrass.Buffer(test_a.cols, test_a.mtype)

    for row in range(test_a.rows):
        test_a.get_row(row, buff_a)
        test_c.put_row(buff_a > 50)

    test_a.close()
    test_c.close()

def test__RasterRow_row_access__add():
    test_a = pygrass.RasterRow(name="test_a")
    test_a.open(mode="r")

    test_b = pygrass.RasterRow(name="test_b")
    test_b.open(mode="r")

    test_c = pygrass.RasterRow(name="test_c")
    test_c.open(mode="w", mtype="FCELL", overwrite=True)

    buff_a = pygrass.Buffer(test_a.cols, test_a.mtype)
    buff_b = pygrass.Buffer(test_b.cols, test_b.mtype)

    for row in range(test_a.rows):
        test_a.get_row(row, buff_a)
        test_b.get_row(row,buff_b)
        test_c.put_row(buff_a + buff_b)

    test_a.close()
    test_b.close()
    test_c.close()

def test__RasterRow_row_access__if():
    test_a = pygrass.RasterRow(name="test_a")
    test_a.open(mode="r")

    test_c = pygrass.RasterRow(name="test_c")
    test_c.open(mode="w", mtype="CELL", overwrite=True)

    buff_a = pygrass.Buffer(test_a.cols, test_a.mtype)

    for row in range(test_a.rows):
        test_a.get_row(row, buff_a)
        test_c.put_row(buff_a > 50)

    test_a.close()
    test_c.close()

def test__mapcalc__add():
    core.mapcalc("test_c = test_a + test_b", quite=True, overwrite=True)

def test__mapcalc__if():
    core.mapcalc("test_c = if(test_a > 50, 1, 0)", quite=True, overwrite=True)

def mytimer(func, runs=1):
    times = []
    t = 0.0
    for _ in range(runs):
        start = time.time()
        func()
        end = time.time()
        times.append(end - start)
        t = t + end - start

    return t/runs, times



def run_benchmark(resolution_list, runs, testdict, profile):
    regions = []
    for resolution in resolution_list:
        core.use_temp_region()
        core.run_command('g.region', e=50, w=-50, n=50, s=-50, res=resolution, flags='p')

        # Adjust the computational region for this process
        region = libgis.Cell_head()
        libraster.Rast_get_window(ctypes.byref(region))
        region.e = 50
        region.w = -50
        region.n = 50
        region.s = -50
        region.ew_res = resolution
        region.ns_res = resolution

        libgis.G_adjust_Cell_head(ctypes.byref(region), 0, 0)

        libraster.Rast_set_window(ctypes.byref(region))
        libgis.G_set_window(ctypes.byref(region))

        # Create two raster maps with random numbers
        core.mapcalc("test_a = rand(0, 100)", quite=True, overwrite=True)
        core.mapcalc("test_b = rand(0.0, 1.0)", quite=True, overwrite=True)
        result = collections.OrderedDict()
        result['res'] = resolution
        result['cols'] = region.cols
        result['rows'] = region.rows
        result['cells'] = region.rows * region.cols
        result['results'] = copy.deepcopy(testdict)
        for execmode, operation in result['results'].items():
            print(execmode)
            for oper, operdict in operation.items():
                operdict['time'], operdict['times'] = mytimer(operdict['func'],runs)
                if profile:
                    filename = '{}_{}_{}'.format(execmode, oper, profile)
                    cProfile.runctx(operdict['func'].__name__ + '()',
                                    globals(), locals(), filename = filename)
                print(('    {0}: {1: 40.6f}s'.format(oper, operdict['time'])))
                del(operdict['func'])

        regions.append(result)
        core.del_temp_region()

    return regions

def get_testlist(loc):
    testlist = [test for test in list(loc.keys()) if 'test' in test[:5]]
    testlist.sort()
    return testlist

def get_testdict(testlist):
    testdict = collections.OrderedDict()
    for testfunc in testlist:
        #import pdb; pdb.set_trace()
        dummy, execmode, operation = testfunc.split('__')
        if execmode in list(testdict.keys()):
            testdict[execmode][operation] = collections.OrderedDict()
            testdict[execmode][operation]['func'] = loc[testfunc]
        else:
            testdict[execmode] = collections.OrderedDict()
            testdict[execmode][operation] = collections.OrderedDict()
            testdict[execmode][operation]['func'] = loc[testfunc]
    return testdict

def print_test(testdict):
    for execmode, operation in testdict.items():
        print(execmode)
        for oper, operdict in operation.items():
            print('    ', oper)
            for key, value in operdict.items():
                print('        ', key)

TXT = """
{% for region in regions %}
{{ '#'*60 }}
### Benchmark cols = {{ region.cols }} rows = {{ region.rows}} cells = {{ region.cells }}
{{ '#'*60 }}

    # equation: c = a + b
    {% for execmode, operation in region.results.iteritems() %}
        {{ "%-30s - %5s % 12.6fs"|format(execmode, 'add', operation.add.time) }}
    {%- endfor %}

    # equation: c = if a > 50 then 1 else 0
    {% for execmode, operation in region.results.iteritems() %}
        {{ "%-30s - %5s % 12.6fs"|format(execmode, 'if', operation.if.time) }}
    {%- endfor %}
{%- endfor %}
"""


CSV = """Class; Mode; Operation;

"""

RST = """
"""
#>>> txt = Template(TxT)
#>>> txt.render(name='John Doe')


def get_txt(results):
    txt = Template(TXT)
    return txt.render(regions = results)


#classes for required options
strREQUIRED = 'required'

class OptionWithDefault(optparse.Option):
    ATTRS = optparse.Option.ATTRS + [strREQUIRED]

    def __init__(self, *opts, **attrs):
        if attrs.get(strREQUIRED, False):
            attrs['help'] = '(Required) ' + attrs.get('help', "")
        optparse.Option.__init__(self, *opts, **attrs)

class OptionParser(optparse.OptionParser):
    def __init__(self, **kwargs):
        kwargs['option_class'] = OptionWithDefault
        optparse.OptionParser.__init__(self, **kwargs)

    def check_values(self, values, args):
        for option in self.option_list:
            if hasattr(option, strREQUIRED) and option.required:
                if not getattr(values, option.dest):
                    self.error("option %s is required".format(str(option)))
        return optparse.OptionParser.check_values(self, values, args)


def main(testdict):
    """Main function"""
    #usage
    usage = "usage: %prog [options] raster_map"
    parser = OptionParser(usage=usage)
    # ntime
    parser.add_option("-n", "--ntimes", dest="ntime",default=5, type="int",
                      help="Number of run for each test.")
    # res
    parser.add_option("-r", "--resolution", action="store", type="string",
                      dest="res", default = '1,0.25',
                      help="Resolution list separate by comma.")
    # fmt
    parser.add_option("-f", "--fmt", action="store", type="string",
                      dest="fmt", default = 'txt',
                      help="Choose the output format: 'txt', 'csv', 'rst'.")

    # output
    parser.add_option("-o", "--output", action="store", type="string",
                      dest="output", help="The output filename.")

    # store
    parser.add_option("-s", "--store", action="store", type="string",
                      dest="store", help="The filename of pickle obj.")

    # profile
    parser.add_option("-p", "--profile", action="store", type="string",
                      dest="profile", help="The filename of the profile results.")

    #return options and argument
    options, args = parser.parse_args()
    res = [float(r) for r in options.res.split(',')]
    #res = [1, 0.25, 0.1, 0.05]

    results = run_benchmark(res, options.ntime, testdict, options.profile)

    if options.store:
        import pickle
        output = open(options.store, 'wb')
        pickle.dump(results, output)
        output.close()
    #import pdb; pdb.set_trace()
    print(get_txt(results))


#add options
if __name__ == "__main__":
    #import pdb; pdb.set_trace()
    loc = locals()
    testlist = get_testlist(loc)
    testdict = get_testdict(testlist)
    #print_test(testdict)



    #import pdb; pdb.set_trace()

    main(testdict)