summaryrefslogtreecommitdiffstats
path: root/fbench/util/fbench-formatter.py
blob: 75ec43fe7b0b72fba04a7a0c2524e62215bd548f (plain) (blame)
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
#!/usr/bin/python
# Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
"""Usage: read.py [options] [vespa-fbench output file]

Will read from stdin if no file name is given

Wildcards:
    %d : any digits
     * : any string
     . : any char

Example:
    fbench-formatter.py file%d directory/file
    cat filename | fbench-formatter.py

Options:
    -h, --help              show this help
    -d, --dir=<string>      search directory [default: current directory]
    -n, --depth=<int>       search depth for subfolders [default: no limit]
    -f                      show file list
    
    -w                      give output as html
    -s                      give output as minimal tab seperated list
                            (headers is written to stderr)
    -c                      give output as comma seperated list
                            (headers is written to stderr)


    -t, --tag=<string>      set tag to output (use with -s)
"""
from math import sqrt
import os
import sys
import getopt
import re
from sets import Set

delimer = "[--xxyyzz--FBENCH_MAGIC_DELIMITER--zzyyxx--]"
urlFailStr = "FBENCH: URL FETCH FAILED!";
attributelist = ["NumHits", "NumFastHits", "TotalHitCount", "QueryHits", "QueryOffset", "NumErrors", "SearchTime", "AttributeFetchTime", "FillTime", "DocsSearched", "NodesSearched", "FullCoverage"]
timeAttributes = ['SearchTime', 'AttributeFetchTime', 'FillTime']


# Init
acc = {}
avg = {}
max_d = {}
min_d = {}

for i in attributelist:
    acc[i] = 0
    avg[i] = 0.0
    max_d[i] = 0
    min_d[i] = sys.maxint

entries = 0
fail   = 0

timeArray = list()
thisTime = 0
totalTime = 0

zeroHits = 0

# Global options
_filelist = 0
_output = 0
_dir = "."
_depth = 0

_tag = ""
_useTag = 0

def usage():
    print >> sys.stderr, __doc__

def abort(message):
    print >> sys.stderr, message + "\n"
    usage()
    sys.exit(2)

def main(argv):
    try:
        opts, args = getopt.getopt(argv, "h:d:n:t:fwsc", ["help", "dir=", "depth=", "tag="])
    except getopt.GetoptError:
        usage()
        sys.exit(2)

    global _output

    for opt, arg in opts:
        if opt in ("-h", "--help"):
            abort("")
        elif opt in ("-d", "--dir="):
            global _dir
            _dir = arg
        elif opt in ("-n", "--depth="):
            global _depth
            try:
                _depth = int(arg)
            except:
                abort("Depth must be an integer")
        elif opt == "-f":
            global _filelist
            _filelist = 1
        elif opt == "-w":
            _output = 1
        elif opt == "-s":
            _output = 2
        elif opt == "-c":
            _output = 3
        elif opt in ("-t", "--tag"):
            global _tag, _useTag
            _useTag = 1
            _tag = arg


    # Get file patterns
    files = Set()
    stdin = 1
    
    for argument in args:

        stdin = 0

        # Regex is translated into emacs-format
        filepattern = re.sub('[0-9]*%d', '[0-9]+', argument)

        # Get list of all matching files
        if (_depth == 0):
            cmd = "find %s -regex '.*/%s'" % (_dir, filepattern)
        else:
            cmd = "find %s -regex '.*/%s' -maxdepth %d" % (_dir, filepattern, _depth)
        fi = os.popen(cmd)

        list = fi.readlines()
        for i in list:
            files.add( i.strip() )
        if len(list) == 0:
            print >> sys.stderr, "\"%s\" does not match any files" % filepattern

    # Exit if no files or stdin
    if len(files) == 0 and stdin == 0:
        print >> sys.stderr, "No matching files found"
        sys.exit(1)

    # Print filenames
    if _filelist != 0:
        print "Files: "
        print files
        print ""

    # Print number of files
    if _filelist != 0:
        print >> sys.stderr, "Processing %d files..." % len(files)

    # Parse all files
    for file in files:
        parsefile(file)

    if stdin == 1:
        print >> sys.stderr, "Processing stdin..."
        parsefile("-")

    calculate()
    printResult()

def parsefile(filename):
    global zeroHits, entries, fail, timeArray, thisTime, acc, min_d, max_d

    if filename == "-":
        file = sys.stdin
    else:
        file = open(filename, "r")

    valid = 0

    for rawline in file:
        # Skip empty lines
        if (rawline == ""):
            continue
        
        line = rawline.strip()

        # Deliminer
        if (line == delimer):
            if valid == 1:
                entries += 1
                timeArray.append(thisTime)
                thisTime = 0
                valid = 0
                continue

        if (line == urlFailStr):
            fail += 1
            entries += 1
            continue

        # Split line at ':'
        match = line.split(':')
        if len(match) < 2:
            continue
        
        name = match[0].strip()
        valueStr = match[1].strip()

        if ( name in attributelist ):
            valid = 1
            print name

            # Extract info from header
            value = int(valueStr)
            acc[name] += value

            if (value == 0 and name == "TotalHitCount"):
                zeroHits += 1

            if (name in timeAttributes):
                thisTime += value

            # Find min/max
            if value < min_d[name]:
                min_d[name] = value

            if value > max_d[name]:
                max_d[name] = value

    file.close()

def calculate():

    global avg, avgTime, Sn, totalTime, timeArray

    successes = entries - fail

    # Calculate average values
    if successes == 0:
        print "Could not find any successfully runned queries"
        print "Make sure benchmarkdata reporting is activated"
        sys.exit(1);
    
    for entry in acc.keys():
        avg[entry] = float(acc[entry]) / successes

    # Calculate average total time
    totalTime = 0
    for i in timeAttributes:
        totalTime += acc[i]
    avgTime = float(totalTime) / float(successes)

    # Calculate standard deviation
    Sn = 0.0
    for sample in timeArray[1:]:
        Sn += ( float(sample)-avgTime )**2
    Sn = sqrt( Sn / successes )

def printResult():
    if _output == 0:
        printDefault()
    elif _output == 1:
        printHtml()
    elif _output == 2:
        printSimple()
    else:
        printCommaSeperated()

def printDefault():
    # Ordinary printing
    print "%21s\t%14s\t%10s\t%6s\t%6s" % ("NAME", "TOTAL", "AVG", "MIN", "MAX")
    for entry in acc.keys():
        print "%21s:\t%14d\t%10.2f\t%6d\t%6d" % (entry, acc[entry], avg[entry], min_d[entry], max_d[entry])
    print ""
    print "%21s:\t%14.3f\t%10.2f\t%6d\t%6d" % ( "Search+Fill+AttrFetch", totalTime, avgTime, min(timeArray), max(timeArray) )
    print "%21s:\t%14.3f" % ( "Standard deviation", Sn)
    print "%21s:\t%14d" % ( "Number of requests", entries)
    print "%21s:\t%14d" % ( "successful requests", entries-fail)
    print "%21s:\t%14d" % ( "failed requests", fail)

    print "%21s:\t%14d" % ( "zero hit requests", zeroHits)

def printHtml():
    
        # HTML printing
        print "<html>"
        print "  <head>"
        print "    <title=\"Fbench\">"
        print "  </head>"
        print "  <body>"
        
        print "    <table>"
        print "      <tr>"
        print "        <th align='left'>Name</th>"
        print "        <th>Total</th>"
        print "        <th>Avg</th>"
        print "        <th>Min</th>"
        print "        <th>Max</th>"
        print "      </tr>"
        for entry in acc.keys():
            print "      <tr>"
            print "        <td>%s</td>" % entry
            print "        <td align='right'>%d</td>" % acc[entry]
            print "        <td align='right'>%.2f</td>" % avg[entry]
            print "        <td align='right'>%d</td>" % min_d[entry]
            print "        <td align='right'>%d</td>" % max_d[entry]
            print "      </tr>"
        print "    </table>"

        print "    <table>"
        print "      <tr>"
        print "        <th align='left'>Average time</th>"
        print "        <td align='right'>%.3f ms </td>" % avgTime
        print "      </tr>"
        print "        <th align='left'>Standard deviation</th>"
        print "        <td align='right'>%.3f</td>" % Sn
        print "      </tr>"
        print "      </tr>"
        print "        <th align='left'>Number of requests</th>"
        print "        <td align='right'>%d</td>" % entries
        print "      </tr>"
        print "      </tr>"
        print "        <th align='left'>Number of successful requests</th>"
        print "        <td align='right'>%d</td>" % entries - fail
        print "      </tr>"
        print "      </tr>"
        print "        <th align='left'>Number of failed requests</th>"
        print "        <td align='right'>%d</td>" % fail
        print "      </tr>"
        print "      </tr>"
        print "        <th align='left'>Number of zero hit requests</th>"
        print "        <td align='right'>%d</td>" % zeroHits
        print "      </tr>"
        print "    </table>"
        print "  </body>"

def printSimple():
    # Minimal print
    printHeader = ""
    for entry in acc.keys():
        printHeader += entry + '\t'
    printHeader += "NumRequests\t"
    printHeader += "NumSuccess\t"
    printHeader += "NumFailed\t"
    printHeader += "ZeroHitRequests\t"
    printHeader += "TotalTime\t"
    if _useTag:
        printHeader += "Tag"
    print >> sys.stderr, printHeader
        
    printtext = ""
    for entry in acc.keys():
        printtext += str(acc[entry]) + '\t'
    printtext += str(entries) + '\t'
    printtext += str(entries-fail) + '\t'
    printtext += str(fail) + '\t'
    printtext += str(zeroHits) + '\t'
    printtext += str(totalTime) + '\t'
    if _useTag:
        printtext += _tag
    print printtext

def printCommaSeperated():
    printHeader = ""
    for entry in acc.keys():
        printHeader += entry + ','
    printHeader += "NumRequests,"
    printHeader += "NumSuccess,"
    printHeader += "NumFailed,"
    printHeader += "ZeroHitRequests,"
    if _useTag:
        printHeader += "TotalTime,"
        printHeader += "Tag"
    else:
        printHeader += "TotalTime"
    print >> sys.stderr, printHeader
        
    printtext = ""
    for entry in acc.keys():
        printtext += str(acc[entry]) + ','
    printtext += str(entries) + ','
    printtext += str(entries-fail) + ','
    printtext += str(fail) + ','
    printtext += str(zeroHits) + ','
    if _useTag:
        printtext += str(totalTime) + ','
        printtext += _tag
    else:
        printtext += str(totalTime)
    print printtext

if __name__ == "__main__":
    main(sys.argv[1:])