Merge branch 'jsonplus2csv' of https://github.com/vincentkfu/fio
[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     if platform.system() == 'Linux':
48         aio = 'libaio'
49     elif platform.system() == 'Windows':
50         aio = 'windowsaio'
51     else:
52         aio = 'posixaio'
53
54     fio_args = [
55         "--output=fio-output.json",
56         "--output-format=json+",
57         "--filename=fio_jsonplus_clat2csv.test",
58         "--ioengine=" + aio,
59         "--time_based",
60         "--runtime=3s",
61         "--size=1G",
62         "--slat_percentiles=1",
63         "--clat_percentiles=1",
64         "--lat_percentiles=1",
65         "--thread=1",
66         "--name=test1",
67         "--rw=randrw",
68         "--name=test2",
69         "--rw=read",
70         "--name=test3",
71         "--rw=write",
72         ]
73
74     output = subprocess.run([fio] + fio_args, stdout=subprocess.PIPE,
75                             stderr=subprocess.PIPE)
76
77     return output
78
79
80 def check_output(fio_output, script_path):
81     """Run t/fio_jsonplus_clat2csv and validate the generated CSV files
82     against the original json+ fio output.
83
84     Parameters:
85         fio_output      subprocess.run object describing fio run.
86         script_path     path to fio_jsonplus_clat2csv script.
87     """
88
89     if fio_output.returncode != 0:
90         return False
91
92     if platform.system() == 'Windows':
93         script = ['python.exe', script_path]
94     else:
95         script = [script_path]
96
97     script_args = ["fio-output.json", "fio-output.csv"]
98     script_args_validate = script_args + ["--validate"]
99
100     script_output = subprocess.run(script + script_args)
101     if script_output.returncode != 0:
102         return False
103
104     script_output = subprocess.run(script + script_args_validate)
105     if script_output.returncode != 0:
106         return False
107
108     return True
109
110
111 def main():
112     """Entry point for this script."""
113
114     args = parse_args()
115
116     index = 1
117     passed = 0
118     failed = 0
119
120     if args.fio:
121         fio_path = args.fio
122     else:
123         fio_path = os.path.join(os.path.dirname(__file__), '../fio')
124         if not os.path.exists(fio_path):
125             fio_path = 'fio'
126     print("fio path is", fio_path)
127
128     if args.script:
129         script_path = args.script
130     else:
131         script_path = os.path.join(os.path.dirname(__file__), '../tools/fio_jsonplus_clat2csv')
132         if not os.path.exists(script_path):
133             script_path = 'fio_jsonplus_clat2csv'
134     print("script path is", script_path)
135
136     fio_output = run_fio(fio_path)
137     status = check_output(fio_output, script_path)
138     print("Test {0} {1}".format(index, ("PASSED" if status else "FAILED")))
139     if status:
140         passed = passed + 1
141     else:
142         failed = failed + 1
143     index = index + 1
144
145     print("{0} tests passed, {1} failed".format(passed, failed))
146
147     sys.exit(failed)
148
149 if __name__ == '__main__':
150     main()