def test_json_output(self): results = RunResults() results.status_code_counter['200'].extend([0, 0.1, 0.2]) results.total_time = 9 old_stdout = sys.stdout old_stderr = sys.stderr try: sys.stdout = StringIO() sys.stderr = StringIO() _boom.print_json(results) sys.stdout.seek(0) output = sys.stdout.read() finally: sys.stdout = old_stdout sys.stderr = old_stderr actual = json.loads(output) self.assertEqual(3, actual['count']) self.assertAlmostEqual(0.333, actual['rps'], delta=0.1) self.assertEqual(0, actual['min']) self.assertEqual(0.2, actual['max']) self.assertAlmostEqual(0.1, actual['avg'], delta=0.1) self.assertEqual(0.2, actual['amp'])
def test_json_output(self): results = RunResults() results.status_code_counter["200"].extend([0, 0.1, 0.2]) results.total_time = 9 old_stdout = sys.stdout old_stderr = sys.stderr try: sys.stdout = StringIO.StringIO() sys.stderr = StringIO.StringIO() _boom.print_json(results) sys.stdout.seek(0) output = sys.stdout.read() finally: sys.stdout = old_stdout sys.stderr = old_stderr actual = json.loads(output) self.assertEqual(3, actual["count"]) self.assertAlmostEqual(0.333, actual["rps"], delta=0.1) self.assertEqual(0, actual["min"]) self.assertEqual(0.2, actual["max"]) self.assertAlmostEqual(0.1, actual["avg"], delta=0.1) self.assertEqual(0.2, actual["amp"])
def test_json_output(self): results = RunResults() results.status_code_counter['200'].extend([0, 0.1, 0.2]) results.total_time = 9 old_stdout = sys.stdout old_stderr = sys.stderr try: sys.stdout = StringIO.StringIO() sys.stderr = StringIO.StringIO() _boom.print_json(results) sys.stdout.seek(0) output = sys.stdout.read() finally: sys.stdout = old_stdout sys.stderr = old_stderr actual = json.loads(output) self.assertEqual(3, actual['count']) self.assertAlmostEqual(0.333, actual['rps'], delta=0.1) self.assertEqual(0, actual['min']) self.assertEqual(0.2, actual['max']) self.assertAlmostEqual(0.1, actual['avg'], delta=0.1) self.assertEqual(0.2, actual['amp'])
def run( url, num=1, duration=None, method='GET', data=None, ct='text/plain', auth=None, concurrency=1, headers=None, pre_hook=None, post_hook=None, quiet=False): if headers is None: headers = {} if 'content-type' not in headers: headers['Content-Type'] = ct if data is not None and data.startswith('py:'): callable = data[len('py:'):] data = resolve_name(callable) method = getattr(requests, method.lower()) options = {'headers': headers} if pre_hook is not None: options['pre_hook'] = resolve_name(pre_hook) if post_hook is not None: options['post_hook'] = resolve_name(post_hook) if data is not None: options['data'] = data if auth is not None: options['auth'] = tuple(auth.split(':', 1)) pool = Pool(concurrency) start = time.time() jobs = None res = RunResults(num, quiet) try: if num is not None: jobs = [pool.spawn(onecall, method, url, res, **options) for i in range(num)] pool.join() else: with gevent.Timeout(duration, False): jobs = [] while True: jobs.append(pool.spawn(onecall, method, url, res, **options)) pool.join() except KeyboardInterrupt: # In case of a keyboard interrupt, just return whatever already got # put into the result object. pass finally: res.total_time = time.time() - start return res