|
| 1 | +#!/usr/bin/python |
| 2 | +# |
| 3 | +# fio-json-prs.py - example script to parse distributed workload generation result |
| 4 | +# produced by fio in JSON format |
| 5 | +# |
| 6 | +# input parameters: |
| 7 | +# 1 - JSON file - file containing fio JSON output |
| 8 | +# 2 - JSON path - path through JSON tree to a leaf node |
| 9 | +# |
| 10 | +# assumption: json output of non-leaf nodes consists of either |
| 11 | +# - dictionary - key field selects sub-value |
| 12 | +# - sequence - key field syntax is name=value, where |
| 13 | +# name is a dictionary key of sequence elements, and |
| 14 | +# value is the desired value to select a sequence element |
| 15 | +# example: |
| 16 | +# python fio-parse-json.py r.fiojob.json.log 'jobs/jobname=randread/read/iops' |
| 17 | +# |
| 18 | + |
| 19 | +import os, sys |
| 20 | +from pprint import pprint |
| 21 | +import json |
| 22 | + |
| 23 | +NOTOK=1 |
| 24 | + |
| 25 | +if len(sys.argv) < 3: |
| 26 | + print('usage: fio-parse-json.py fio-json.log path-to-leaf') |
| 27 | + print('path-to-leaf is a slash-separated list of key names in JSON tree') |
| 28 | + print('see instructions at top of this script') |
| 29 | + sys.exit(NOTOK) |
| 30 | + |
| 31 | + |
| 32 | +def filter_json_node(next_branch, node_list_in): |
| 33 | + #print next_branch, json.dumps(node, indent=4) |
| 34 | + #print '' |
| 35 | + #sys.stdout.flush() |
| 36 | + next_node_list = [] |
| 37 | + for n in node_list_in: |
| 38 | + dotlist = next_branch.split('=') |
| 39 | + if len(dotlist) > 2: |
| 40 | + print('unrecognized syntax at %s'%str(node)) |
| 41 | + sys.exit(NOTOK) |
| 42 | + elif len(dotlist) == 1: |
| 43 | + next_node_list.append(n[next_branch]) |
| 44 | + assert(isinstance(n, dict)) |
| 45 | + else: # must be a sequence, take any element with key matching value |
| 46 | + select_key = dotlist[0] |
| 47 | + select_value = dotlist[1] |
| 48 | + for e in n: # node is a seq |
| 49 | + #print 'select with key %s value %s sequence element %s'%(select_key, select_value, e) |
| 50 | + if select_value == '*': |
| 51 | + next_node_list.append(e) |
| 52 | + else: |
| 53 | + v = e[select_key] |
| 54 | + if v == select_value: |
| 55 | + next_node_list.append(e) |
| 56 | + |
| 57 | + if len(next_node_list) == 0: |
| 58 | + print('no list member in %s has key %s value %s'%(str(node), select_key, select_value)) |
| 59 | + sys.exit(NOTOK) |
| 60 | + return next_node_list |
| 61 | + |
| 62 | + |
| 63 | +fn = sys.argv[1] |
| 64 | +json_tree_path = sys.argv[2].split('/') |
| 65 | +with open(fn, 'r') as json_data: |
| 66 | + |
| 67 | + # check for empty file |
| 68 | + |
| 69 | + f_info = os.fstat(json_data.fileno()) |
| 70 | + if f_info.st_size == 0: |
| 71 | + print('JSON input file %s is empty'%fn) |
| 72 | + sys.exit(NOTOK) |
| 73 | + |
| 74 | + # find start of JSON object and position file handle right before that |
| 75 | + |
| 76 | + lines = json_data.readlines() |
| 77 | + start_of_json_data=0 |
| 78 | + for l in lines: |
| 79 | + if l[0] == '{': break |
| 80 | + start_of_json_data += 1 |
| 81 | + json_data.seek(0, os.SEEK_SET) |
| 82 | + for j in range(0,start_of_json_data): |
| 83 | + l = json_data.readline() |
| 84 | + |
| 85 | + # parse the JSON object |
| 86 | + |
| 87 | + node = json.load(json_data) |
| 88 | + current_branch = None |
| 89 | + next_node_list = [node] |
| 90 | + for next_branch in json_tree_path: |
| 91 | + next_node_list = filter_json_node(next_branch, next_node_list) |
| 92 | + for n in next_node_list: print(n) |
| 93 | + |
0 commit comments