t/steadystate_tests: better support automated testing
[fio.git] / t / steadystate_tests.py
1 #!/usr/bin/env python
2 # Note: this script is python2 and python3 compatible.
3 #
4 # steadystate_tests.py
5 #
6 # Test option parsing and functonality for fio's steady state detection feature.
7 #
8 # steadystate_tests.py --read file-for-read-testing --write file-for-write-testing ./fio
9 #
10 # REQUIREMENTS
11 # Python 2.6+
12 # SciPy
13 #
14 # KNOWN ISSUES
15 # only option parsing and read tests are carried out
16 # On Windows this script works under Cygwin but not from cmd.exe
17 # On Windows I encounter frequent fio problems generating JSON output (nothing to decode)
18 # min runtime:
19 # if ss attained: min runtime = ss_dur + ss_ramp
20 # if not attained: runtime = timeout
21
22 from __future__ import absolute_import
23 from __future__ import print_function
24 import os
25 import sys
26 import json
27 import pprint
28 import argparse
29 import subprocess
30 from scipy import stats
31
32 def parse_args():
33     parser = argparse.ArgumentParser()
34     parser.add_argument('fio',
35                         help='path to fio executable')
36     parser.add_argument('--read',
37                         help='target for read testing')
38     parser.add_argument('--write',
39                         help='target for write testing')
40     args = parser.parse_args()
41
42     return args
43
44
45 def check(data, iops, slope, pct, limit, dur, criterion):
46     measurement = 'iops' if iops else 'bw'
47     data = data[measurement]
48     mean = sum(data) / len(data)
49     if slope:
50         x = list(range(len(data)))
51         m, intercept, r_value, p_value, std_err = stats.linregress(x,data)
52         m = abs(m)
53         if pct:
54             target = (m / mean * 100) if mean != 0 else 0
55             criterion = criterion[:-1]
56         else:
57             target = m
58     else:
59         maxdev = 0
60         for x in data:
61             maxdev = max(abs(mean-x), maxdev)
62         if pct:
63             target = maxdev / mean * 100
64             criterion = criterion[:-1]
65         else:
66             target = maxdev
67
68     criterion = float(criterion)
69     if criterion == 0.0:
70         objsame = False
71     else:
72         objsame = abs(target - criterion) / criterion < 0.005
73     return (objsame, target < limit, mean, target)
74
75
76 if __name__ == '__main__':
77     args = parse_args()
78
79     pp = pprint.PrettyPrinter(indent=4)
80
81     passed = 0
82     failed = 0
83
84 #
85 # test option parsing
86 #
87     parsing = [ { 'args': ["--parse-only", "--debug=parse", "--ss_dur=10s", "--ss=iops:10", "--ss_ramp=5"],
88                   'output': "set steady state IOPS threshold to 10.000000" },
89                 { 'args': ["--parse-only", "--debug=parse", "--ss_dur=10s", "--ss=iops:10%", "--ss_ramp=5"],
90                   'output': "set steady state threshold to 10.000000%" },
91                 { 'args': ["--parse-only", "--debug=parse", "--ss_dur=10s", "--ss=iops:.1%", "--ss_ramp=5"],
92                   'output': "set steady state threshold to 0.100000%" },
93                 { 'args': ["--parse-only", "--debug=parse", "--ss_dur=10s", "--ss=bw:10%", "--ss_ramp=5"],
94                   'output': "set steady state threshold to 10.000000%" },
95                 { 'args': ["--parse-only", "--debug=parse", "--ss_dur=10s", "--ss=bw:.1%", "--ss_ramp=5"],
96                   'output': "set steady state threshold to 0.100000%" },
97                 { 'args': ["--parse-only", "--debug=parse", "--ss_dur=10s", "--ss=bw:12", "--ss_ramp=5"],
98                   'output': "set steady state BW threshold to 12" },
99               ]
100     for test in parsing:
101         output = subprocess.check_output([args.fio] + test['args'])
102         if test['output'] in output.decode():
103             print("PASSED '{0}' found with arguments {1}".format(test['output'], test['args']))
104             passed = passed + 1
105         else:
106             print("FAILED '{0}' NOT found with arguments {1}".format(test['output'], test['args']))
107             failed = failed + 1
108
109 #
110 # test some read workloads
111 #
112 # if ss active and attained,
113 #   check that runtime is less than job time
114 #   check criteria
115 #   how to check ramp time?
116 #
117 # if ss inactive
118 #   check that runtime is what was specified
119 #
120     reads = [ {'s': True, 'timeout': 100, 'numjobs': 1, 'ss_dur': 5, 'ss_ramp': 3, 'iops': True, 'slope': True, 'ss_limit': 0.1, 'pct': True},
121               {'s': False, 'timeout': 20, 'numjobs': 2},
122               {'s': True, 'timeout': 100, 'numjobs': 3, 'ss_dur': 10, 'ss_ramp': 5, 'iops': False, 'slope': True, 'ss_limit': 0.1, 'pct': True},
123               {'s': True, 'timeout': 10, 'numjobs': 3, 'ss_dur': 10, 'ss_ramp': 500, 'iops': False, 'slope': True, 'ss_limit': 0.1, 'pct': True},
124             ]
125
126     if args.read == None:
127         if os.name == 'posix':
128             args.read = '/dev/zero'
129             extra = [ "--size=128M" ]
130         else:
131             print("ERROR: file for read testing must be specified on non-posix systems")
132             sys.exit(1)
133     else:
134         extra = []
135
136     jobnum = 0
137     for job in reads:
138
139         tf = "steadystate_job{0}.json".format(jobnum)
140         parameters = [ "--name=job{0}".format(jobnum) ]
141         parameters.extend(extra)
142         parameters.extend([ "--thread",
143                             "--output-format=json",
144                             "--output={0}".format(tf),
145                             "--filename={0}".format(args.read),
146                             "--rw=randrw",
147                             "--rwmixread=100",
148                             "--stonewall",
149                             "--group_reporting",
150                             "--numjobs={0}".format(job['numjobs']),
151                             "--time_based",
152                             "--runtime={0}".format(job['timeout']) ])
153         if job['s']:
154            if job['iops']:
155                ss = 'iops'
156            else:
157                ss = 'bw'
158            if job['slope']:
159                ss += "_slope"
160            ss += ":" + str(job['ss_limit'])
161            if job['pct']:
162                ss += '%'
163            parameters.extend([ '--ss_dur={0}'.format(job['ss_dur']),
164                                '--ss={0}'.format(ss),
165                                '--ss_ramp={0}'.format(job['ss_ramp']) ])
166
167         output = subprocess.call([args.fio] + parameters)
168         with open(tf, 'r') as source:
169             jsondata = json.loads(source.read())
170             source.close()
171
172         for jsonjob in jsondata['jobs']:
173             line = "{0}".format(jsonjob['job options']['name'])
174             if job['s']:
175                 if jsonjob['steadystate']['attained'] == 1:
176                     # check runtime >= ss_dur + ss_ramp, check criterion, check criterion < limit
177                     mintime = (job['ss_dur'] + job['ss_ramp']) * 1000
178                     actual = jsonjob['read']['runtime']
179                     if mintime > actual:
180                         line = 'FAILED ' + line + ' ss attained, runtime {0} < ss_dur {1} + ss_ramp {2}'.format(actual, job['ss_dur'], job['ss_ramp'])
181                         failed = failed + 1
182                     else:
183                         line = line + ' ss attained, runtime {0} > ss_dur {1} + ss_ramp {2},'.format(actual, job['ss_dur'], job['ss_ramp'])
184                         objsame, met, mean, target = check(data=jsonjob['steadystate']['data'],
185                             iops=job['iops'],
186                             slope=job['slope'],
187                             pct=job['pct'],
188                             limit=job['ss_limit'],
189                             dur=job['ss_dur'],
190                             criterion=jsonjob['steadystate']['criterion'])
191                         if not objsame:
192                             line = 'FAILED ' + line + ' fio criterion {0} != calculated criterion {1} '.format(jsonjob['steadystate']['criterion'], target)
193                             failed = failed + 1
194                         else:
195                             if met:
196                                 line = 'PASSED ' + line + ' target {0} < limit {1}'.format(target, job['ss_limit'])
197                                 passed = passed + 1
198                             else:
199                                 line = 'FAILED ' + line + ' target {0} < limit {1} but fio reports ss not attained '.format(target, job['ss_limit'])
200                                 failed = failed + 1
201                 else:
202                     # check runtime, confirm criterion calculation, and confirm that criterion was not met
203                     expected = job['timeout'] * 1000
204                     actual = jsonjob['read']['runtime']
205                     if abs(expected - actual) > 10:
206                         line = 'FAILED ' + line + ' ss not attained, expected runtime {0} != actual runtime {1}'.format(expected, actual)
207                     else:
208                         line = line + ' ss not attained, runtime {0} != ss_dur {1} + ss_ramp {2},'.format(actual, job['ss_dur'], job['ss_ramp'])
209                         objsame, met, mean, target = check(data=jsonjob['steadystate']['data'],
210                             iops=job['iops'],
211                             slope=job['slope'],
212                             pct=job['pct'],
213                             limit=job['ss_limit'],
214                             dur=job['ss_dur'],
215                             criterion=jsonjob['steadystate']['criterion'])
216                         if not objsame:
217                             if actual > (job['ss_dur'] + job['ss_ramp'])*1000:
218                                 line = 'FAILED ' + line + ' fio criterion {0} != calculated criterion {1} '.format(jsonjob['steadystate']['criterion'], target)
219                                 failed = failed + 1
220                             else:
221                                 line = 'PASSED ' + line + ' fio criterion {0} == 0.0 since ss_dur + ss_ramp has not elapsed '.format(jsonjob['steadystate']['criterion'])
222                                 passed = passed + 1
223                         else:
224                             if met:
225                                 line = 'FAILED ' + line + ' target {0} < threshold {1} but fio reports ss not attained '.format(target, job['ss_limit'])
226                                 failed = failed + 1
227                             else:
228                                 line = 'PASSED ' + line + ' criterion {0} > threshold {1}'.format(target, job['ss_limit'])
229                                 passed = passed + 1
230             else:
231                 expected = job['timeout'] * 1000
232                 actual = jsonjob['read']['runtime']
233                 if abs(expected - actual) < 10:
234                     result = 'PASSED '
235                     passed = passed + 1
236                 else:
237                     result = 'FAILED '
238                     failed = failed + 1
239                 line = result + line + ' no ss, expected runtime {0} ~= actual runtime {1}'.format(expected, actual)
240             print(line)
241             if 'steadystate' in jsonjob:
242                 pp.pprint(jsonjob['steadystate'])
243         jobnum += 1
244
245     print("{0} test(s) PASSED, {1} test(s) FAILED".format(passed,failed))
246     sys.exit(failed)