@Override public int startThread(final Object testRunner) throws InvalidContextException { throw new InvalidContextException("You should not start worker threads until the main thread has " + "initialised the script engine, or after all other threads have " + "shut down. Typically, you should only call startWorkerThread() " + "from another worker thread."); } }
/** * Instrument the supplied {@code target} object's method which has the given name. Subsequent * calls to {@code target}'s given method will be recorded against the statistics for this * {@code Test}. * * @param target Object to instrument. * @param methodName method name to instrument * @throws NonInstrumentableTypeException If {@code target} could not be instrumented. * @since 3.2.1 */ public final void record(Object target, String methodName) throws NonInstrumentableTypeException { if (StringUtils.isNotEmpty(context)) { record(target, new MethodNameFilter(methodName)); } }
private static InternalScriptContext getGrinderInstance() { InternalScriptContext grinder = Grinder.grinder; if (grinder == null || grinder.getThreadNumber() == -1) { throw new RuntimeException("This method should be called in the worker thread context."); } return grinder; }
@Override public void testFailure(Failure failure) throws Exception { // Skip Generic Shutdown... It's not failure. Throwable rootCause = exceptionProcessor.getRootCause(failure.getException()); if (exceptionProcessor.isGenericShutdown(rootCause)) { return; } Grinder.grinder.getLogger().error(failure.getMessage(), exceptionProcessor.filterException(rootCause)); // In case of exception, set test failed. try { StatisticsForTest forLastTest = Grinder.grinder.getStatistics().getForLastTest(); if (forLastTest != null) { forLastTest.setSuccess(false); } } catch (Throwable t) { noOp(); } } });
@Test @RunRate(10) public void doTest2() throws Exception { grinder.getStatistics().setDelayReports(true); HTTPResponse result = request.GET("http://www.google.co.kr"); if (result.getStatusCode() != 200) { grinder.getStatistics().getForLastTest().setSuccess(false); } else { grinder.getStatistics().getForLastTest().setSuccess(true); } }
private void instrumentInstance(Object target, Recorder recorder, InstrumentationFilter filter) throws NonInstrumentableTypeException { Class<?> c = target.getClass(); if (c.isArray()) { throw new NonInstrumentableTypeException("Can't instrument arrays"); } do { for (Method method : c.getDeclaredMethods()) { if (!Modifier.isStatic(method.getModifiers()) && filter.matches(method)) { getContext().add(target, method, TargetSource.FIRST_PARAMETER, recorder); } } c = c.getSuperclass(); } while (getContext().isInstrumentable(c)); } }
private void instrumentClass(Class<?> targetClass, Recorder recorder, InstrumentationFilter filter) throws NonInstrumentableTypeException { if (targetClass.isArray()) { throw new NonInstrumentableTypeException("Can't instrument arrays"); } for (Constructor<?> constructor : targetClass.getDeclaredConstructors()) { getContext().add(targetClass, constructor, recorder); } // Instrument the static methods declared by the target class. Ignore // any parent class. for (Method method : targetClass.getDeclaredMethods()) { if (Modifier.isStatic(method.getModifiers()) && filter.matches(method)) { getContext().add(targetClass, method, TargetSource.CLASS, recorder); } } }
/** * Get this thread unique id among all threads in the all agents. * * @return unique id b/w from 0 to total thread count. * @since 3.2.3 */ public static int getThreadUniqId() { InternalScriptContext grinder = getGrinderInstance(); GrinderProperties properties = grinder.getProperties(); if (properties != null) { int totalProcessCount = properties.getInt("grinder.processes", 1); int totalThreadCount = properties.getInt("grinder.threads", 1); int agentNumber = grinder.getAgentNumber(); int processNumber = grinder.getProcessNumber(); int threadNumber = grinder.getThreadNumber(); // Calc the current thread's unique id return (agentNumber * totalProcessCount * totalThreadCount) + (processNumber * totalThreadCount) + threadNumber; } return 0; }
@Override public void awaitStart() { int waitingTime = doRampUp(); int threadNumber = 0; if (Grinder.grinder != null) { threadNumber = Math.max(Grinder.grinder.getThreadNumber(), 0); } synchronized (m_threadEventCondition) { m_numberAwaitingStart++; m_numberRunning++; m_threadEventCondition.notifyAll(); } if (Grinder.grinder != null) { Grinder.grinder.getLogger().info("thread-{} is invoked after {} ms sleep", threadNumber, waitingTime); } // m_started.await(true); }
@BeforeThread public void beforeThread() throws InvalidContextException { grinder.getStatistics().setDelayReports(true); }
private boolean isScriptValidation() { try { return Grinder.grinder.getProperties().getBoolean("grinder.script.validation", false); } catch (Exception e) { return false; } }
@Override public void evaluate() throws Throwable { if (checkRun(Grinder.grinder.getRunNumber())) { statement.evaluate(); } }
@BeforeClass public static void beforeProcess() { request = new HTTPRequest(); try { test.record(request); } catch (NonInstrumentableTypeException e) { } }
/** * Creates a new <code>Test</code> instance. * * @param number Test number. * @param description Test description. */ public Test(int number, String description) { m_number = number; m_description = description; m_registeredTest = Grinder.grinder.getTestRegistry().register(this); }
@RunRate(50) @Test public void doTest() throws Exception { HTTPResponse result = request.GET("http://www.naver.com"); if (result.getStatusCode() != 200) { grinder.getStatistics().getForLastTest().setSuccess(false); } else { grinder.getStatistics().getForLastTest().setSuccess(true); } }
private int doRampUp(int rampUpInterval, int rampUpStep, int rampUpInitialThread, int rampUpInitialSleep) { int threadNumber = 0; int waitingTime; if (Grinder.grinder != null) { threadNumber = Math.max(Grinder.grinder.getThreadNumber(), 0); } try { waitingTime = getWaitingTime(rampUpInterval, rampUpStep, rampUpInitialThread, rampUpInitialSleep, threadNumber); if (waitingTime != 0) { if (Grinder.grinder != null) { Grinder.grinder.getLogger().info("thread-{} is sleeping {} ms for ramp-up", threadNumber, waitingTime); } sleeper.sleepNormal(waitingTime, 0); } return waitingTime; } catch (Sleeper.ShutdownException e) { throw new RuntimeException(e); } }
/** * Get the total agent count. * * @return agent count. */ public static int getAgentCount() { return getGrinderInstance().getProperties().getInt("grinder.agents", 1); }
/** * Get the thread count per a process. * * @return thread count assigned per a process */ public static int getThreadCount() { return getGrinderInstance().getProperties().getInt("grinder.threads", 1); } }
/** * Get the process count per an agent. * * @return process assigned per an agent */ public static int getProcessCount() { return getGrinderInstance().getProperties().getInt("grinder.processes", 1); }
protected int doRampUp() { InternalScriptContext grinder = Grinder.grinder; if (grinder != null) { GrinderProperties properties = grinder.getProperties(); int rampUpInterval = properties.getInt(GRINDER_PROP_THREAD_INCREMENT_INTERVAL, 0); int rampUpStep = properties.getInt(GRINDER_PROP_THREAD_INCREMENT, 0); int rampUpInitialThread = properties.getInt(GRINDER_PROP_INITIAL_PROCESS, 0); int rampUpInitialSleep = properties.getInt(GRINDER_PROP_INITIAL_THREAD_SLEEP_TIME, 0); return doRampUp(rampUpInterval, rampUpStep, rampUpInitialThread, rampUpInitialSleep); } return 0; }