| 1 |
#!/usr/bin/python3 |
| 2 |
|
| 3 |
# --- BEGIN COPYRIGHT BLOCK --- |
| 4 |
# Copyright (C) 2021 Red Hat, Inc. |
| 5 |
# All rights reserved. |
| 6 |
# |
| 7 |
# License: GPL (version 3 or any later version). |
| 8 |
# See LICENSE for details. |
| 9 |
# --- END COPYRIGHT BLOCK --- |
| 10 |
# |
| 11 |
# PYTHON_ARGCOMPLETE_OK |
| 12 |
|
| 13 |
import argcomplete |
| 14 |
import argparse |
| 15 |
import pathlib |
| 16 |
import signal |
| 17 |
import sys |
| 18 |
import os |
| 19 |
|
| 20 |
perf_dir = os.path.dirname(os.path.abspath(__file__)) |
| 21 |
tests_dir = os.path.dirname(perf_dir) |
| 22 |
dirsrvtests_dir = os.path.dirname(tests_dir) |
| 23 |
sys.path.insert(0, os.path.join(dirsrvtests_dir, 'lib')) |
| 24 |
|
| 25 |
from test389.perftools import * |
| 26 |
|
| 27 |
|
| 28 |
parser_description=""" |
| 29 |
Handle basic performance test |
| 30 |
""" |
| 31 |
|
| 32 |
parser_epilog=""" |
| 33 |
Handle basic performance test. |
| 34 |
""" |
| 35 |
|
| 36 |
list_description=""" |
| 37 |
List available tests. |
| 38 |
""" |
| 39 |
|
| 40 |
csv_description=""" |
| 41 |
Generate performance result summary in csv format. |
| 42 |
""" |
| 43 |
|
| 44 |
run_description=""" |
| 45 |
Generate a local test instance if it does not already exist and run tests. |
| 46 |
""" |
| 47 |
|
| 48 |
runall_description=""" |
| 49 |
Run all tests for 100,1K,10K,100K,1M users with both db lib with 1, 4 and 8 threads. |
| 50 |
""" |
| 51 |
|
| 52 |
tests = PerformanceTools.listTests() |
| 53 |
|
| 54 |
def convArg(options, args, optkey, argkey): |
| 55 |
# Convert one ParseArgs arg in PerformaceTools option |
| 56 |
try: |
| 57 |
options[optkey] = getattr(args,argkey) |
| 58 |
except AttributeError: |
| 59 |
pass |
| 60 |
|
| 61 |
def convArgs(args): |
| 62 |
# Convert ParseArgs args in PerformaceTools options |
| 63 |
options = {} |
| 64 |
for k in ( 'resultDir', 'seed', 'suffix'): |
| 65 |
convArg(options, args, k, k) |
| 66 |
convArg(options, args, "db_lib", "db") |
| 67 |
convArg(options, args, "nbUsers", "users") |
| 68 |
convArg(options, args, "nb_threads", "threads") |
| 69 |
return options |
| 70 |
|
| 71 |
|
| 72 |
def csvSubCmd(args): |
| 73 |
perftools=PerformanceTools(convArgs(args)) |
| 74 |
PerformanceTools.log2Csv(perftools.getFilePath("log"), args.out) |
| 75 |
|
| 76 |
def justifyText(left, right, margin): |
| 77 |
pos = 0; |
| 78 |
l = len(right) |
| 79 |
while (pos < l): |
| 80 |
spacepos = -1 |
| 81 |
pos0 = pos |
| 82 |
|
| 83 |
while (pos < l and right[pos] != '\n' and pos-pos0 <= margin): |
| 84 |
if (right[pos] == ' '): |
| 85 |
spacepos=pos |
| 86 |
pos += 1 |
| 87 |
if (pos==l): |
| 88 |
print (f"{left:<20}{right[pos0:pos]}") |
| 89 |
break; |
| 90 |
if pos > margin and spacepos > 0: |
| 91 |
pos = spacepos |
| 92 |
print (f"{left:<20}{right[pos0:pos]}") |
| 93 |
spacepos = -1 |
| 94 |
pos += 1 |
| 95 |
left="" |
| 96 |
|
| 97 |
|
| 98 |
def listSubCmd(args): |
| 99 |
for name,test in tests.items(): |
| 100 |
justifyText(name+':', test.description(), 40) |
| 101 |
print ("") |
| 102 |
|
| 103 |
def runSubCmd(args): |
| 104 |
options = convArgs(args) |
| 105 |
perftools = PerformanceTools.Tester.initTester(options) |
| 106 |
for test in args.test: |
| 107 |
tests[test].run(perftools, options) |
| 108 |
|
| 109 |
def runallSubCmd(args): |
| 110 |
PerformanceTools.runAllTests(convArgs(args)) |
| 111 |
|
| 112 |
|
| 113 |
# handle a control-c gracefully |
| 114 |
def signal_handler(signal, frame): |
| 115 |
print('\n\nExiting...') |
| 116 |
sys.exit(0) |
| 117 |
|
| 118 |
warningAboutUser="Note that this command creates new 389ds instances cwand must be run by an user having the permission to do so." |
| 119 |
|
| 120 |
# create the top-level parser |
| 121 |
parser = argparse.ArgumentParser(epilog=warningAboutUser) |
| 122 |
subparsers = parser.add_subparsers(help='sub-command help') |
| 123 |
prefix = os.path.join(os.environ.get('PREFIX', "")) |
| 124 |
perfdir= f"{prefix}/var/log/dirsrv/perfdir" |
| 125 |
parser.add_argument('--resultDir', '-r', type=pathlib.Path, default=perfdir, help=f'results directory (default is {perfdir})') |
| 126 |
|
| 127 |
# create the parser for the "csv" command |
| 128 |
parser_csv = subparsers.add_parser('csv', help=csv_description, description=csv_description) |
| 129 |
parser_csv.set_defaults(func=csvSubCmd) |
| 130 |
parser_csv.add_argument('--out', '-o', type=pathlib.Path, default=None, help='csv file (default is stdout)') |
| 131 |
|
| 132 |
# create the parser for the "list" command |
| 133 |
parser_list = subparsers.add_parser('list', help=list_description, description=list_description) |
| 134 |
parser_list.set_defaults(func=listSubCmd) |
| 135 |
|
| 136 |
testnames = ( tests.keys() ) |
| 137 |
|
| 138 |
# create the parser for the "run" command |
| 139 |
parser_run = subparsers.add_parser('run', help=run_description, description=run_description, epilog=warningAboutUser) |
| 140 |
parser_run.set_defaults(func=runSubCmd) |
| 141 |
parser_run.add_argument('--db', '-d', choices=['bdb','mdb'], default='mdb', help='db library (default is mdb)') |
| 142 |
parser_run.add_argument('--users', '-u', type=int, default=10000, help='number of users in test instance') |
| 143 |
parser_run.add_argument('--threads', '-t', type=int, default=1, help='number of threads in client tester') |
| 144 |
parser_run.add_argument('test', nargs='+', choices=testnames, help='test(s) to run') |
| 145 |
|
| 146 |
parser_run = subparsers.add_parser('runall', help=runall_description, description=runall_description, epilog=warningAboutUser) |
| 147 |
parser_run.set_defaults(func=runallSubCmd) |
| 148 |
|
| 149 |
signal.signal(signal.SIGINT, signal_handler) |
| 150 |
argcomplete.autocomplete(parser) |
| 151 |
args = parser.parse_args() |
| 152 |
#print(f"DBG args = {args}") |
| 153 |
if 'func' in args: |
| 154 |
args.func(args) |
| 155 |
else: |
| 156 |
parser.print_help() |