[7541] | 1 | #!/usr/bin/env python |
---|
| 2 | # |
---|
| 3 | # Copyright (c) 2010-2012 Corey Goldberg (corey@goldb.org) |
---|
| 4 | # License: GNU LGPLv3 |
---|
| 5 | # |
---|
| 6 | # This file is part of Multi-Mechanize | Performance Test Framework |
---|
| 7 | # |
---|
| 8 | |
---|
| 9 | |
---|
| 10 | |
---|
| 11 | import ConfigParser |
---|
| 12 | import multiprocessing |
---|
| 13 | import optparse |
---|
| 14 | import os |
---|
| 15 | import Queue |
---|
| 16 | import shutil |
---|
| 17 | import subprocess |
---|
| 18 | import sys |
---|
| 19 | import time |
---|
| 20 | |
---|
| 21 | import multimechanize.core as core |
---|
| 22 | import multimechanize.results as results |
---|
| 23 | import multimechanize.resultswriter as resultswriter |
---|
| 24 | import multimechanize.progressbar as progressbar |
---|
| 25 | |
---|
| 26 | |
---|
| 27 | usage = 'Usage: %prog <project name> [options]' |
---|
| 28 | parser = optparse.OptionParser(usage=usage) |
---|
| 29 | parser.add_option('-p', '--port', dest='port', type='int', help='rpc listener port') |
---|
| 30 | parser.add_option('-r', '--results', dest='results_dir', help='results directory to reprocess') |
---|
| 31 | parser.add_option('-b', '--bind-addr', dest='bind_addr', help='rpc bind address', default='localhost') |
---|
| 32 | parser.add_option('-d', '--directory', dest='projects_dir', help='directory containing project folder', default='.') |
---|
| 33 | cmd_opts, args = parser.parse_args() |
---|
| 34 | |
---|
| 35 | try: |
---|
| 36 | project_name = args[0] |
---|
| 37 | except IndexError: |
---|
| 38 | sys.stderr.write('\nERROR: no project specified\n\n') |
---|
| 39 | sys.stderr.write('%s\n' % usage) |
---|
| 40 | sys.stderr.write('example: %s my_project\n\n' % __file__) |
---|
| 41 | sys.exit(1) |
---|
| 42 | |
---|
| 43 | core.init(cmd_opts.projects_dir, project_name) |
---|
| 44 | |
---|
| 45 | |
---|
| 46 | |
---|
| 47 | |
---|
| 48 | def main(): |
---|
| 49 | if cmd_opts.results_dir: # don't run a test, just re-process results |
---|
| 50 | rerun_results(cmd_opts.results_dir) |
---|
| 51 | elif cmd_opts.port: |
---|
| 52 | import multimechanize.rpcserver |
---|
| 53 | multimechanize.rpcserver.launch_rpc_server(cmd_opts.bind_addr, cmd_opts.port, project_name, run_test) |
---|
| 54 | else: |
---|
| 55 | run_test() |
---|
| 56 | return |
---|
| 57 | |
---|
| 58 | |
---|
| 59 | |
---|
| 60 | def run_test(remote_starter=None): |
---|
| 61 | if remote_starter is not None: |
---|
| 62 | remote_starter.test_running = True |
---|
| 63 | remote_starter.output_dir = None |
---|
| 64 | |
---|
| 65 | run_time, rampup, results_ts_interval, console_logging, progress_bar, results_database, post_run_script, xml_report, user_group_configs = configure(project_name) |
---|
| 66 | |
---|
| 67 | run_localtime = time.localtime() |
---|
| 68 | output_dir = '%s/%s/results/results_%s' % (cmd_opts.projects_dir, project_name, time.strftime('%Y.%m.%d_%H.%M.%S/', run_localtime)) |
---|
| 69 | |
---|
| 70 | # this queue is shared between all processes/threads |
---|
| 71 | queue = multiprocessing.Queue() |
---|
| 72 | rw = resultswriter.ResultsWriter(queue, output_dir, console_logging) |
---|
| 73 | rw.daemon = True |
---|
| 74 | rw.start() |
---|
| 75 | |
---|
| 76 | user_groups = [] |
---|
| 77 | for i, ug_config in enumerate(user_group_configs): |
---|
| 78 | ug = core.UserGroup(queue, i, ug_config.name, ug_config.num_threads, ug_config.script_file, run_time, rampup) |
---|
| 79 | user_groups.append(ug) |
---|
| 80 | for user_group in user_groups: |
---|
| 81 | user_group.start() |
---|
| 82 | |
---|
| 83 | start_time = time.time() |
---|
| 84 | |
---|
| 85 | if console_logging: |
---|
| 86 | for user_group in user_groups: |
---|
| 87 | user_group.join() |
---|
| 88 | else: |
---|
| 89 | print '\n user_groups: %i' % len(user_groups) |
---|
| 90 | print ' threads: %i\n' % (ug_config.num_threads * len(user_groups)) |
---|
| 91 | |
---|
| 92 | if progress_bar: |
---|
| 93 | p = progressbar.ProgressBar(run_time) |
---|
| 94 | elapsed = 0 |
---|
| 95 | while elapsed < (run_time + 1): |
---|
| 96 | p.update_time(elapsed) |
---|
| 97 | if sys.platform.startswith('win'): |
---|
| 98 | print '%s transactions: %i timers: %i errors: %i\r' % (p, rw.trans_count, rw.timer_count, rw.error_count), |
---|
| 99 | else: |
---|
| 100 | print '%s transactions: %i timers: %i errors: %i' % (p, rw.trans_count, rw.timer_count, rw.error_count) |
---|
| 101 | sys.stdout.write(chr(27) + '[A' ) |
---|
| 102 | time.sleep(1) |
---|
| 103 | elapsed = time.time() - start_time |
---|
| 104 | |
---|
| 105 | print p |
---|
| 106 | |
---|
| 107 | while [user_group for user_group in user_groups if user_group.is_alive()] != []: |
---|
| 108 | if progress_bar: |
---|
| 109 | if sys.platform.startswith('win'): |
---|
| 110 | print 'waiting for all requests to finish...\r', |
---|
| 111 | else: |
---|
| 112 | print 'waiting for all requests to finish...\r' |
---|
| 113 | sys.stdout.write(chr(27) + '[A' ) |
---|
| 114 | time.sleep(.5) |
---|
| 115 | |
---|
| 116 | if not sys.platform.startswith('win'): |
---|
| 117 | print |
---|
| 118 | |
---|
| 119 | # all agents are done running at this point |
---|
| 120 | time.sleep(.2) # make sure the writer queue is flushed |
---|
| 121 | print '\n\nanalyzing results...\n' |
---|
| 122 | results.output_results(output_dir, 'results.csv', run_time, rampup, results_ts_interval, user_group_configs, xml_report) |
---|
| 123 | print 'created: %sresults.html\n' % output_dir |
---|
| 124 | if xml_report: |
---|
| 125 | print 'created: %sresults.jtl' % output_dir |
---|
| 126 | print 'created: last_results.jtl\n' |
---|
| 127 | |
---|
| 128 | # copy config file to results directory |
---|
| 129 | project_config = os.sep.join([cmd_opts.projects_dir, project_name, 'config.cfg']) |
---|
| 130 | saved_config = os.sep.join([output_dir, 'config.cfg']) |
---|
| 131 | shutil.copy(project_config, saved_config) |
---|
| 132 | |
---|
| 133 | if results_database is not None: |
---|
| 134 | print 'loading results into database: %s\n' % results_database |
---|
| 135 | import multimechanize.resultsloader |
---|
| 136 | multimechanize.resultsloader.load_results_database(project_name, run_localtime, output_dir, results_database, |
---|
| 137 | run_time, rampup, results_ts_interval, user_group_configs) |
---|
| 138 | |
---|
| 139 | if post_run_script is not None: |
---|
| 140 | print 'running post_run_script: %s\n' % post_run_script |
---|
| 141 | subprocess.call(post_run_script) |
---|
| 142 | |
---|
| 143 | print 'done.\n' |
---|
| 144 | |
---|
| 145 | if remote_starter is not None: |
---|
| 146 | remote_starter.test_running = False |
---|
| 147 | remote_starter.output_dir = output_dir |
---|
| 148 | |
---|
| 149 | return |
---|
| 150 | |
---|
| 151 | |
---|
| 152 | |
---|
| 153 | def rerun_results(results_dir): |
---|
| 154 | output_dir = '%s/%s/results/%s/' % (cmd_opts.projects_dir, project_name, results_dir) |
---|
| 155 | saved_config = '%s/config.cfg' % output_dir |
---|
| 156 | run_time, rampup, results_ts_interval, console_logging, progress_bar, results_database, post_run_script, xml_report, user_group_configs = configure(project_name, config_file=saved_config) |
---|
| 157 | print '\n\nanalyzing results...\n' |
---|
| 158 | results.output_results(output_dir, 'results.csv', run_time, rampup, results_ts_interval, user_group_configs, xml_report) |
---|
| 159 | print 'created: %sresults.html\n' % output_dir |
---|
| 160 | if xml_report: |
---|
| 161 | print 'created: %sresults.jtl' % output_dir |
---|
| 162 | print 'created: last_results.jtl\n' |
---|
| 163 | |
---|
| 164 | |
---|
| 165 | |
---|
| 166 | def configure(project_name, config_file=None): |
---|
| 167 | user_group_configs = [] |
---|
| 168 | config = ConfigParser.ConfigParser() |
---|
| 169 | if config_file is None: |
---|
| 170 | config_file = '%s/%s/config.cfg' % (cmd_opts.projects_dir, project_name) |
---|
| 171 | config.read(config_file) |
---|
| 172 | for section in config.sections(): |
---|
| 173 | if section == 'global': |
---|
| 174 | run_time = config.getint(section, 'run_time') |
---|
| 175 | rampup = config.getint(section, 'rampup') |
---|
| 176 | results_ts_interval = config.getint(section, 'results_ts_interval') |
---|
| 177 | try: |
---|
| 178 | console_logging = config.getboolean(section, 'console_logging') |
---|
| 179 | except ConfigParser.NoOptionError: |
---|
| 180 | console_logging = False |
---|
| 181 | try: |
---|
| 182 | progress_bar = config.getboolean(section, 'progress_bar') |
---|
| 183 | except ConfigParser.NoOptionError: |
---|
| 184 | progress_bar = True |
---|
| 185 | try: |
---|
| 186 | results_database = config.get(section, 'results_database') |
---|
| 187 | if results_database == 'None': results_database = None |
---|
| 188 | except ConfigParser.NoOptionError: |
---|
| 189 | results_database = None |
---|
| 190 | try: |
---|
| 191 | post_run_script = config.get(section, 'post_run_script') |
---|
| 192 | if post_run_script == 'None': post_run_script = None |
---|
| 193 | except ConfigParser.NoOptionError: |
---|
| 194 | post_run_script = None |
---|
| 195 | try: |
---|
| 196 | xml_report = config.getboolean(section, 'xml_report') |
---|
| 197 | except ConfigParser.NoOptionError: |
---|
| 198 | xml_report = False |
---|
| 199 | else: |
---|
| 200 | threads = config.getint(section, 'threads') |
---|
| 201 | script = config.get(section, 'script') |
---|
| 202 | user_group_name = section |
---|
| 203 | ug_config = UserGroupConfig(threads, user_group_name, script) |
---|
| 204 | user_group_configs.append(ug_config) |
---|
| 205 | |
---|
| 206 | return (run_time, rampup, results_ts_interval, console_logging, progress_bar, results_database, post_run_script, xml_report, user_group_configs) |
---|
| 207 | |
---|
| 208 | |
---|
| 209 | |
---|
| 210 | class UserGroupConfig(object): |
---|
| 211 | def __init__(self, num_threads, name, script_file): |
---|
| 212 | self.num_threads = num_threads |
---|
| 213 | self.name = name |
---|
| 214 | self.script_file = script_file |
---|
| 215 | |
---|
| 216 | |
---|
| 217 | |
---|
| 218 | if __name__ == '__main__': |
---|
| 219 | main() |
---|