lib/pattern: fix formatting
[fio.git] / t / jsonplus2csv_test.py
1 #!/usr/bin/env python3
2 # SPDX-License-Identifier: GPL-2.0-only
3 #
4 # Copyright (c) 2020 Western Digital Corporation or its affiliates.
5 #
6 """
7 jsonplus2csv-test.py
8
9 Do one basic test of tools/fio_jsonplus2csv
10
11 USAGE
12 python jsonplus2csv-test.py [-f fio-executable] [-s script-location]
13
14 EXAMPLES
15 python t/jsonplus2csv-test.py
16 python t/jsonplus2csv-test.py -f ./fio -s tools
17
18 REQUIREMENTS
19 Python 3.5+
20 """
21
22 import os
23 import sys
24 import platform
25 import argparse
26 import subprocess
27
28
29 def parse_args():
30     """Parse command-line arguments."""
31
32     parser = argparse.ArgumentParser()
33     parser.add_argument('-f', '--fio',
34                         help='path to fio executable (e.g., ./fio)')
35     parser.add_argument('-s', '--script',
36                         help='directory containing fio_jsonplus2csv script')
37     return parser.parse_args()
38
39
40 def run_fio(fio):
41     """Run fio to generate json+ data.
42
43     Parameters:
44         fio     path to fio executable.
45     """
46
47 # We need an async ioengine to get submission latencies
48     if platform.system() == 'Linux':
49         aio = 'libaio'
50     elif platform.system() == 'Windows':
51         aio = 'windowsaio'
52     else:
53         aio = 'posixaio'
54
55     fio_args = [
56         "--max-jobs=4",
57         "--output=fio-output.json",
58         "--output-format=json+",
59         "--filename=fio_jsonplus_clat2csv.test",
60         "--ioengine=" + aio,
61         "--time_based",
62         "--runtime=3s",
63         "--size=1M",
64         "--slat_percentiles=1",
65         "--clat_percentiles=1",
66         "--lat_percentiles=1",
67         "--thread=1",
68         "--name=test1",
69         "--rw=randrw",
70         "--name=test2",
71         "--rw=read",
72         "--name=test3",
73         "--rw=write",
74         ]
75
76     output = subprocess.run([fio] + fio_args, stdout=subprocess.PIPE,
77                             stderr=subprocess.PIPE)
78
79     return output
80
81
82 def check_output(fio_output, script_path):
83     """Run t/fio_jsonplus_clat2csv and validate the generated CSV files
84     against the original json+ fio output.
85
86     Parameters:
87         fio_output      subprocess.run object describing fio run.
88         script_path     path to fio_jsonplus_clat2csv script.
89     """
90
91     if fio_output.returncode != 0:
92         print("ERROR: fio run failed")
93         return False
94
95     if platform.system() == 'Windows':
96         script = ['python.exe', script_path]
97     else:
98         script = [script_path]
99
100     script_args = ["fio-output.json", "fio-output.csv"]
101     script_args_validate = script_args + ["--validate"]
102
103     script_output = subprocess.run(script + script_args)
104     if script_output.returncode != 0:
105         return False
106
107     script_output = subprocess.run(script + script_args_validate)
108     if script_output.returncode != 0:
109         return False
110
111     return True
112
113
114 def main():
115     """Entry point for this script."""
116
117     args = parse_args()
118
119     index = 1
120     passed = 0
121     failed = 0
122
123     if args.fio:
124         fio_path = args.fio
125     else:
126         fio_path = os.path.join(os.path.dirname(__file__), '../fio')
127         if not os.path.exists(fio_path):
128             fio_path = 'fio'
129     print("fio path is", fio_path)
130
131     if args.script:
132         script_path = args.script
133     else:
134         script_path = os.path.join(os.path.dirname(__file__), '../tools/fio_jsonplus_clat2csv')
135         if not os.path.exists(script_path):
136             script_path = 'fio_jsonplus_clat2csv'
137     print("script path is", script_path)
138
139     fio_output = run_fio(fio_path)
140     status = check_output(fio_output, script_path)
141     print("Test {0} {1}".format(index, ("PASSED" if status else "FAILED")))
142     if status:
143         passed = passed + 1
144     else:
145         failed = failed + 1
146     index = index + 1
147
148     print("{0} tests passed, {1} failed".format(passed, failed))
149
150     sys.exit(failed)
151
152 if __name__ == '__main__':
153     main()