def test_sizing_multiprocess(self): counters = SharedCounters('OK', 'FAILED') _original = asyncio.sleep async def _slept(time): await _original(0) with patch('asyncio.sleep', _slept): @scenario() async def sizer(session): if random.randint(0, 10) == 1: counters['FAILED'] += 1 raise AssertionError() else: counters['OK'] += 1 stdout, stderr = self._test_molotov('--sizing', '-p', '2', '--sizing-tolerance', '5', '--console-update', '0', '-s', 'sizer', 'molotov.tests.test_run') ratio = (float(counters['FAILED'].value) / float(counters['OK'].value) * 100.) self.assertTrue(fmwk._RESULTS['REACHED'] == 1) self.assertEqual(counters['FAILED'].value, fmwk._RESULTS['FAILED'].value) self.assertEqual(counters['OK'].value, fmwk._RESULTS['OK'].value) self.assertTrue(ratio > 5., ratio)
def test_sizing_multiprocess_interrupted(self): counters = SharedCounters("OK", "FAILED") @scenario() async def sizer(session): if random.randint(0, 10) == 1: counters["FAILED"] += 1 raise AssertionError() else: counters["OK"] += 1 async def _stop(): await asyncio.sleep(2.0) os.kill(os.getpid(), signal.SIGINT) asyncio.ensure_future(_stop()) stdout, stderr, rc = self._test_molotov( "--sizing", "-p", "3", "--sizing-tolerance", "90", "--console-update", "0", "-s", "sizer", "molotov.tests.test_run", ) self.assertTrue("Sizing was not finished" in stdout)
def test_sizing_multiprocess(self): counters = SharedCounters('OK', 'FAILED') with catch_sleep(): @scenario() async def sizer(session): if random.randint(0, 10) == 1: counters['FAILED'] += 1 raise AssertionError() else: counters['OK'] += 1 with set_args('molotov', '--sizing', '-p', '2', '--sizing-tolerance', '5', '--console-update', '0', '-s', 'sizer', 'molotov.tests.test_run') as (stdout, stderr): try: main() except SystemExit: pass stdout, stderr = stdout.read().strip(), stderr.read().strip() # stdout, stderr, rc = self._test_molotov() ratio = (float(counters['FAILED'].value) / float(counters['OK'].value) * 100.) self.assertTrue(ratio >= 5., ratio)
def _async_test(*args, **kw): oldloop = asyncio.get_event_loop() loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) loop.set_debug(True) console = SharedConsole(interval=0) results = SharedCounters( "WORKER", "REACHED", "RATIO", "OK", "FAILED", "MINUTE_OK", "MINUTE_FAILED", "MAX_WORKERS", "SETUP_FAILED", "SESSION_SETUP_FAILED", ) kw["loop"] = loop kw["console"] = console kw["results"] = results fut = asyncio.ensure_future(func(*args, **kw)) try: loop.run_until_complete(fut) finally: loop.stop() loop.close() asyncio.set_event_loop(oldloop) return fut.result()
def test_mapping(self): # making sure it works like a defaultdict(int) data = SharedCounters('one', 'two') self.assertTrue(data['one'].value == 0) data['one'] += 10 data['one'] -= 1 data['two'] = 4 self.assertTrue(data['one'].value, 9) self.assertTrue(data['two'].value, 4)
def test_mapping(self): # making sure it works like a defaultdict(int) data = SharedCounters("one", "two") self.assertTrue(data["one"].value == 0) data["one"] += 10 data["one"] -= 1 data["two"] = 4 self.assertTrue(data["one"].value, 9) self.assertTrue(data["two"].value, 4)
def __init__(self, args, loop=None): self.args = args self.console = self.args.shared_console if loop is None: loop = asyncio.get_event_loop() self.loop = loop # the stastd client gets initialized after we fork # processes in case -p was used self.statsd = None self._tasks = [] self._procs = [] self._results = SharedCounters('WORKER', 'REACHED', 'RATIO', 'OK', 'FAILED', 'MINUTE_OK', 'MINUTE_FAILED') self.eventer = EventSender(self.console)
def _async_test(*args, **kw): cofunc = asyncio.coroutine(func) oldloop = asyncio.get_event_loop() loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) loop.set_debug(True) console = SharedConsole(interval=0) results = SharedCounters('WORKER', 'REACHED', 'RATIO', 'OK', 'FAILED', 'MINUTE_OK', 'MINUTE_FAILED') kw['loop'] = loop kw['console'] = console kw['results'] = results try: loop.run_until_complete(cofunc(*args, **kw)) finally: loop.stop() loop.close() asyncio.set_event_loop(oldloop)
def _async_test(*args, **kw): cofunc = asyncio.coroutine(func) oldloop = asyncio.get_event_loop() loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) loop.set_debug(True) console = SharedConsole(interval=0) results = SharedCounters("WORKER", "REACHED", "RATIO", "OK", "FAILED", "MINUTE_OK", "MINUTE_FAILED") kw["loop"] = loop kw["console"] = console kw["results"] = results try: loop.run_until_complete(cofunc(*args, **kw)) finally: loop.stop() loop.close() asyncio.set_event_loop(oldloop)
def test_sizing_multiprocess_interrupted(self): counters = SharedCounters('OK', 'FAILED') @scenario() async def sizer(session): if random.randint(0, 10) == 1: counters['FAILED'] += 1 raise AssertionError() else: counters['OK'] += 1 async def _stop(): await asyncio.sleep(2.) os.kill(os.getpid(), signal.SIGINT) asyncio.ensure_future(_stop()) stdout, stderr = self._test_molotov('--sizing', '-p', '3', '--sizing-tolerance', '90', '--console-update', '0', '-s', 'sizer', 'molotov.tests.test_run') self.assertTrue(fmwk._RESULTS['REACHED'] == 0) self.assertTrue("Sizing was not finished" in stdout)
def test_interface(self): data = SharedCounters("one", "two") self.assertTrue("one" in data) self.assertEqual(len(data.keys()), 2) for key in data: data[key] = 0 self.assertTrue(data[key], 0) for key, value in data.items(): data[key] = value self.assertTrue(data[key], value) data.values() repr(data) str(data) self.assertRaises(KeyError, data.__setitem__, "meh", 1) self.assertRaises(TypeError, data.__setitem__, "one", "1")
def test_interface(self): data = SharedCounters('one', 'two') self.assertTrue('one' in data) self.assertEqual(len(data.keys()), 2) for key in data: data[key] = 0 self.assertTrue(data[key], 0) for key, value in data.items(): data[key] = value self.assertTrue(data[key], value) data.values() repr(data) str(data) self.assertRaises(KeyError, data.__setitem__, 'meh', 1) self.assertRaises(TypeError, data.__setitem__, 'one', '1')
def test_sizing_multiprocess(self): counters = SharedCounters("OK", "FAILED") with catch_sleep(): @scenario() async def sizer(session): if random.randint(0, 10) == 1: counters["FAILED"] += 1 raise AssertionError() else: counters["OK"] += 1 with set_args( "molotov", "--sizing", "-p", "2", "--sizing-tolerance", "5", "--console-update", "0", "-s", "sizer", "molotov.tests.test_run", ) as (stdout, stderr): try: main() except SystemExit: pass stdout, stderr = stdout.read().strip(), stderr.read().strip() # stdout, stderr, rc = self._test_molotov() ratio = (float(counters["FAILED"].value) / float(counters["OK"].value) * 100.0) self.assertTrue(ratio >= 4.75, ratio)
def __init__(self, args, loop=None): self.args = args self.console = self.args.shared_console if loop is None: loop = asyncio.get_event_loop() self.loop = loop # the stastd client gets initialized after we fork # processes in case -p was used self.statsd = None self._tasks = [] self._procs = [] self._results = SharedCounters( "WORKER", "REACHED", "RATIO", "OK", "FAILED", "MINUTE_OK", "MINUTE_FAILED", "MAX_WORKERS", "SETUP_FAILED", "SESSION_SETUP_FAILED", ) self.eventer = EventSender(self.console)
import os import unittest import multiprocessing_on_dill as multiprocessing from molotov.sharedcounter import SharedCounters, SharedCounter # pre-forked variable _DATA = SharedCounters("test") def run_worker(value): _DATA["test"] += value _DATA["test"] -= value _DATA["test"] += value class TestSharedCounters(unittest.TestCase): def test_operators(self): c1 = SharedCounter("ok") c2 = SharedCounter("ok") c1.value = 4 c2.value = 5 self.assertTrue(c1 <= c2) self.assertTrue(c1 < c2) self.assertTrue(c1 >= 2) self.assertTrue(c1 > 2) self.assertTrue(c1 == 4) self.assertTrue(c1 != 5) c2.value = 4 c2 += SharedCounter("ok") self.assertTrue(c1 == c2) repr(c1)
import unittest import multiprocessing from molotov.sharedcounter import SharedCounters, SharedCounter # pre-forked variable _DATA = SharedCounters('test') def run_worker(value): _DATA['test'] += value _DATA['test'] -= value _DATA['test'] += value class TestSharedCounters(unittest.TestCase): def test_operators(self): c1 = SharedCounter('ok') c2 = SharedCounter('ok') c1.value = 4 c2.value = 5 self.assertTrue(c1 <= c2) self.assertTrue(c1 < c2) self.assertTrue(c1 >= 2) self.assertTrue(c1 > 2) self.assertTrue(c1 == 4) self.assertTrue(c1 != 5) c2.value = 4 c2 += SharedCounter('ok') self.assertTrue(c1 == c2) repr(c1)
import multiprocessing import asyncio import time import os from molotov.session import LoggedClientSession as Session from molotov.api import get_fixture, pick_scenario, get_scenario from molotov.stats import get_statsd_client from molotov.sharedcounter import SharedCounters _STOP = False _STARTED_AT = _TOLERANCE = None _REFRESH = .3 _HOWLONG = 0 _RESULTS = SharedCounters('WORKER', 'REACHED', 'RATIO', 'OK', 'FAILED', 'MINUTE_OK', 'MINUTE_FAILED') def display_results(): ok, fail = _RESULTS['OK'].value, _RESULTS['FAILED'].value workers = _RESULTS['WORKER'].value return 'SUCCESSES: %s | FAILURES: %s | WORKERS: %s' % (ok, fail, workers) def _now(): return int(time.time()) async def step(worker_id, step_id, session, quiet, verbose, console, scenario=None): """ single scenario call.