File: client_server.py

package info (click to toggle)
fio 3.41-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,012 kB
  • sloc: ansic: 82,290; python: 9,862; sh: 6,067; makefile: 813; yacc: 204; lex: 184
file content (505 lines) | stat: -rwxr-xr-x 15,977 bytes parent folder | download | duplicates (2)
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
#!/usr/bin/env python3
"""
# client_server.py
#
# Test fio's client/server mode.
#
# USAGE
# see python3 client_server.py --help
#
# EXAMPLES
# python3 t/client_server.py
# python3 t/client_server.py -f ./fio
#
# REQUIREMENTS
# Python 3.6
#
# This will start fio server instances listening on the interfaces below and
# will break if any ports are already occupied.
#
#
"""
import os
import sys
import time
import locale
import logging
import argparse
import tempfile
import subprocess
import configparser
from pathlib import Path
from fiotestlib import FioJobCmdTest, run_fio_tests


SERVER_LIST = [
            ",8765",
            ",8766",
            ",8767",
            ",8768",
        ]

PIDFILE_LIST = []

class ClientServerTest(FioJobCmdTest):
    """
    Client/sever test class.
    """

    def setup(self, parameters):
        """Setup a test."""

        fio_args = [
            f"--output={self.filenames['output']}",
            f"--output-format={self.fio_opts['output-format']}",
        ]
        for server in self.fio_opts['servers']:
            option = f"--client={server['client']}"
            fio_args.append(option)
            fio_args.append(server['jobfile'])

        super().setup(fio_args)



class ClientServerTestGlobalSingle(ClientServerTest):
    """
    Client/sever test class.
    One server connection only.
    The job file may or may not have a global section.
    """

    def check_result(self):
        super().check_result()

        config = configparser.ConfigParser(allow_no_value=True)
        config.read(self.fio_opts['servers'][0]['jobfile'])

        if not config.has_section('global'):
            if len(self.json_data['global options']) > 0:
                self.failure_reason = f"{self.failure_reason} non-empty 'global options' dictionary found with no global section in job file."
                self.passed = False
            return

        if len(self.json_data['global options']) == 0:
            self.failure_reason = f"{self.failure_reason} empty 'global options' dictionary found with no global section in job file."
            self.passed = False

        # Now make sure job file global section matches 'global options'
        # in JSON output
        job_file_global = dict(config['global'])
        for key, value in job_file_global.items():
            if value is None:
                job_file_global[key] = ""
        if job_file_global != self.json_data['global options']:
            self.failure_reason = f"{self.failure_reason} 'global options' dictionary does not match global section in job file."
            self.passed = False


class ClientServerTestGlobalMultiple(ClientServerTest):
    """
    Client/sever test class.
    Multiple server connections.
    Job files may or may not have a global section.
    """

    def check_result(self):
        super().check_result()

        #
        # For each job file, check if it has a global section
        # If so, make sure the 'global options' array has
        # as element for it.
        # At the end, make sure the total number of elements matches the number
        # of job files with global sections.
        #

        global_sections = 0
        for server in self.fio_opts['servers']:
            config = configparser.ConfigParser(allow_no_value=True)
            config.read(server['jobfile'])

            if not config.has_section('global'):
                continue

            global_sections += 1

            # this can only parse one server spec format
            [hostname, port] = server['client'].split(',')

            match = None
            for global_opts in self.json_data['global options']:
                if 'hostname' not in global_opts:
                    continue
                if 'port' not in global_opts:
                    continue
                if global_opts['hostname'] == hostname and int(global_opts['port']) == int(port):
                    match = global_opts
                    break

            if not match:
                self.failure_reason = f"{self.failure_reason} matching 'global options' element not found for {hostname}, {port}."
                self.passed = False
                continue

            del match['hostname']
            del match['port']

            # Now make sure job file global section matches 'global options'
            # in JSON output
            job_file_global = dict(config['global'])
            for key, value in job_file_global.items():
                if value is None:
                    job_file_global[key] = ""
            if job_file_global != match:
                self.failure_reason += " 'global options' dictionary does not match global section in job file."
                self.passed = False
            else:
                logging.debug("Job file global section matches 'global options' array element %s", server['client'])

        if global_sections != len(self.json_data['global options']):
            self.failure_reason = f"{self.failure_reason} mismatched number of elements in 'global options' array."
            self.passed = False
        else:
            logging.debug("%d elements in global options array as expected", global_sections)


class ClientServerTestAllClientsLat(ClientServerTest):
    """
    Client/sever test class.
    Make sure the "All clients" job has latency percentile data.
    Assumes that a job named 'test' is run with no global section.
    Only check read data.
    """

    def check_result(self):
        super().check_result()

        config = configparser.ConfigParser(allow_no_value=True)
        config.read(self.fio_opts['servers'][0]['jobfile'])

        lats = { 'clat': True, 'lat': False, 'slat': False }
        for key in lats:
            opt = f"{key}_percentiles"
            if opt in config.options('test'):
                lats[key] = config.getboolean('test', opt)
                logging.debug("%s set to %s", opt, lats[key])

        all_clients = None
        client_stats = self.json_data['client_stats']
        for client in client_stats:
            if client['jobname'] == "All clients":
                all_clients = client
                break

        if not all_clients:
            self.failure_reason = f"{self.failure_reason} Could not find 'All clients' output"
            self.passed = False

        for key, value in lats.items():
            if value:
                if 'percentile' not in all_clients['read'][f"{key}_ns"]:
                    self.failure_reason += f" {key} percentiles not found"
                    self.passed = False
                    break

                logging.debug("%s percentiles found as expected", key)
            else:
                if 'percentile' in all_clients['read'][f"{key}_ns"]:
                    self.failure_reason += f" {key} percentiles found unexpectedly"
                    self.passed = False
                    break

                logging.debug("%s percentiles appropriately not found", key)



TEST_LIST = [
    {   # Smoke test
        "test_id": 1,
        "fio_opts": {
            "output-format": "json",
            "servers": [
                    {
                        "client" : 0, # index into the SERVER_LIST array
                        "jobfile": "test01.fio",
                    },
                ]
            },
        "test_class": ClientServerTest,
    },
    {   # try another client
        "test_id": 2,
        "fio_opts": {
            "output-format": "json",
            "servers": [
                    {
                        "client" : 1,
                        "jobfile": "test01.fio",
                    },
                ]
            },
        "test_class": ClientServerTest,
    },
    {   # single client global section
        "test_id": 3,
        "fio_opts": {
            "output-format": "json",
            "servers": [
                    {
                        "client" : 2,
                        "jobfile": "test01.fio",
                    },
                ]
            },
        "test_class": ClientServerTestGlobalSingle,
    },
    {   # single client no global section
        "test_id": 4,
        "fio_opts": {
            "output-format": "json",
            "servers": [
                    {
                        "client" : 3,
                        "jobfile": "test04-noglobal.fio",
                    },
                ]
            },
        "test_class": ClientServerTestGlobalSingle,
    },
    {   # multiple clients, some with global, some without
        "test_id": 5,
        "fio_opts": {
            "output-format": "json",
            "servers": [
                    {
                        "client" : 0,
                        "jobfile": "test04-noglobal.fio",
                    },
                    {
                        "client" : 1,
                        "jobfile": "test01.fio",
                    },
                    {
                        "client" : 2,
                        "jobfile": "test04-noglobal.fio",
                    },
                    {
                        "client" : 3,
                        "jobfile": "test01.fio",
                    },
                ]
            },
        "test_class": ClientServerTestGlobalMultiple,
    },
    {   # multiple clients, all with global sections
        "test_id": 6,
        "fio_opts": {
            "output-format": "json",
            "servers": [
                    {
                        "client" : 0,
                        "jobfile": "test01.fio",
                    },
                    {
                        "client" : 1,
                        "jobfile": "test01.fio",
                    },
                    {
                        "client" : 2,
                        "jobfile": "test01.fio",
                    },
                    {
                        "client" : 3,
                        "jobfile": "test01.fio",
                    },
                ]
            },
        "test_class": ClientServerTestGlobalMultiple,
    },
    {   # Enable submission latency
        "test_id": 7,
        "fio_opts": {
            "output-format": "json",
            "servers": [
                    {
                        "client" : 0,
                        "jobfile": "test07-slat.fio",
                    },
                    {
                        "client" : 1,
                        "jobfile": "test07-slat.fio",
                    },
                ]
            },
        "test_class": ClientServerTestAllClientsLat,
    },
    {   # Enable completion latency
        "test_id": 8,
        "fio_opts": {
            "output-format": "json",
            "servers": [
                    {
                        "client" : 0,
                        "jobfile": "test08-clat.fio",
                    },
                    {
                        "client" : 1,
                        "jobfile": "test08-clat.fio",
                    },
                ]
            },
        "test_class": ClientServerTestAllClientsLat,
    },
    {   # Enable total latency
        "test_id": 9,
        "fio_opts": {
            "output-format": "json",
            "servers": [
                    {
                        "client" : 0,
                        "jobfile": "test09-lat.fio",
                    },
                    {
                        "client" : 1,
                        "jobfile": "test09-lat.fio",
                    },
                ]
            },
        "test_class": ClientServerTestAllClientsLat,
    },
    {   # Disable completion latency
        "test_id": 10,
        "fio_opts": {
            "output-format": "json",
            "servers": [
                    {
                        "client" : 0,
                        "jobfile": "test10-noclat.fio",
                    },
                    {
                        "client" : 1,
                        "jobfile": "test10-noclat.fio",
                    },
                ]
            },
        "test_class": ClientServerTestAllClientsLat,
    },
    {   # Enable submission, completion, total latency
        "test_id": 11,
        "fio_opts": {
            "output-format": "json",
            "servers": [
                    {
                        "client" : 0,
                        "jobfile": "test11-alllat.fio",
                    },
                    {
                        "client" : 1,
                        "jobfile": "test11-alllat.fio",
                    },
                ]
            },
        "test_class": ClientServerTestAllClientsLat,
    },
]


def parse_args():
    """Parse command-line arguments."""

    parser = argparse.ArgumentParser()
    parser.add_argument('-d', '--debug', help='Enable debug messages', action='store_true')
    parser.add_argument('-f', '--fio', help='path to file executable (e.g., ./fio)')
    parser.add_argument('-a', '--artifact-root', help='artifact root directory')
    parser.add_argument('-s', '--skip', nargs='+', type=int,
                        help='list of test(s) to skip')
    parser.add_argument('-o', '--run-only', nargs='+', type=int,
                        help='list of test(s) to run, skipping all others')
    args = parser.parse_args()

    return args


def start_servers(fio_path, servers=SERVER_LIST):
    """Start servers for our tests."""

    for server in servers:
        tmpfile = tempfile.mktemp()
        cmd = [fio_path, f"--server={server}", f"--daemonize={tmpfile}"]
        cmd_result = subprocess.run(cmd, capture_output=True, check=False,
                                   encoding=locale.getpreferredencoding())
        if cmd_result.returncode != 0:
            logging.error("Unable to start server on %s: %s", server, cmd_result.stderr)
            return False

        logging.debug("Started server %s", server)
        PIDFILE_LIST.append(tmpfile)

    return True


def stop_servers(pidfiles=PIDFILE_LIST):
    """Stop running fio server invocations."""

    for pidfile in pidfiles:
        with open(pidfile, "r", encoding=locale.getpreferredencoding()) as file:
            pid = file.read().strip()

        cmd = ["kill", f"{pid}"]
        cmd_result = subprocess.run(cmd, capture_output=True, check=False,
                                   encoding=locale.getpreferredencoding())
        if cmd_result.returncode != 0:
            logging.error("Unable to kill server with PID %s: %s", pid, cmd_result.stderr)
            return False
        logging.debug("Sent stop signal to PID %s", pid)

    return True


def main():
    """Run tests for fio's client/server mode."""

    args = parse_args()

    if args.debug:
        logging.basicConfig(level=logging.DEBUG)
    else:
        logging.basicConfig(level=logging.INFO)

    artifact_root = args.artifact_root if args.artifact_root else \
        f"client_server-test-{time.strftime('%Y%m%d-%H%M%S')}"
    os.mkdir(artifact_root)
    print(f"Artifact directory is {artifact_root}")

    if args.fio:
        fio_path = str(Path(args.fio).absolute())
    else:
        fio_path = os.path.join(os.path.dirname(__file__), '../fio')
    print(f"fio path is {fio_path}")

    if not start_servers(fio_path):
        sys.exit(1)
    print("Servers started")

    job_path = os.path.join(os.path.dirname(__file__), "client_server")
    for test in TEST_LIST:
        opts = test['fio_opts']
        for server in opts['servers']:
            server['client'] = SERVER_LIST[server['client']]
            server['jobfile'] = os.path.join(job_path, server['jobfile'])

    test_env = {
              'fio_path': fio_path,
              'fio_root': str(Path(__file__).absolute().parent.parent),
              'artifact_root': artifact_root,
              'basename': 'client_server',
              }

    _, failed, _ = run_fio_tests(TEST_LIST, test_env, args)

    stop_servers()
    sys.exit(failed)

if __name__ == '__main__':
    main()