org.apache.jmeter.threads.JMeterThread Java Examples

The following examples show how to use org.apache.jmeter.threads.JMeterThread. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AbstractSimpleThreadGroup.java    From jmeter-plugins with Apache License 2.0 6 votes vote down vote up
@Override
public void start(int groupNum, ListenerNotifier notifier, ListedHashTree threadGroupTree, StandardJMeterEngine engine) {
    running = true;

    int numThreads = getNumThreads();

    log.info("Starting thread group number " + groupNum + " threads " + numThreads);

    long now = System.currentTimeMillis(); // needs to be same time for all threads in the group
    final JMeterContext context = JMeterContextService.getContext();
    for (int i = 0; running && i < numThreads; i++) {
        JMeterThread jmThread = makeThread(groupNum, notifier, threadGroupTree, engine, i, context);
        scheduleThread(jmThread, now); // set start and end time
        Thread newThread = new Thread(jmThread, jmThread.getThreadName());
        registerStartedThread(jmThread, newThread);
        newThread.start();
    }

    log.info("Started thread group number " + groupNum);
}
 
Example #2
Source File: AbstractSimpleThreadGroup.java    From jmeter-plugins with Apache License 2.0 6 votes vote down vote up
private JMeterThread makeThread(int groupNum,
                                ListenerNotifier notifier, ListedHashTree threadGroupTree,
                                StandardJMeterEngine engine, int threadNum,
                                JMeterContext context) { // N.B. Context needs to be fetched in the correct thread
    boolean onErrorStopTest = getOnErrorStopTest();
    boolean onErrorStopTestNow = getOnErrorStopTestNow();
    boolean onErrorStopThread = getOnErrorStopThread();
    boolean onErrorStartNextLoop = getOnErrorStartNextLoop();

    String groupName = getName();
    String distributedPrefix = JMeterUtils.getPropDefault(THREAD_GROUP_DISTRIBUTED_PREFIX_PROPERTY_NAME, "");
    final String threadName = distributedPrefix + (distributedPrefix.isEmpty() ? "" : "-") + groupName + " " + groupNum + "-" + (threadNum + 1);

    final JMeterThread jmeterThread = new JMeterThread(cloneTree(threadGroupTree), this, notifier);
    jmeterThread.setThreadNum(threadNum);
    jmeterThread.setThreadGroup(this);
    jmeterThread.setInitialContext(context);
    jmeterThread.setThreadName(threadName);
    jmeterThread.setEngine(engine);
    jmeterThread.setOnErrorStopTest(onErrorStopTest);
    jmeterThread.setOnErrorStopTestNow(onErrorStopTestNow);
    jmeterThread.setOnErrorStopThread(onErrorStopThread);
    jmeterThread.setOnErrorStartNextLoop(onErrorStartNextLoop);
    return jmeterThread;
}
 
Example #3
Source File: ParallelSampler.java    From jmeter-bzm-plugins with Apache License 2.0 6 votes vote down vote up
private HashTree getSubTree(TestElement te) {
    try {
        Field field = JMeterThread.class.getDeclaredField("testTree");
        field.setAccessible(true);
        JMeterThread parentThread = JMeterContextService.getContext().getThread();
        if (parentThread == null) {
            log.error("Current thread is null.");
            throw new NullPointerException();
        }
        HashTree testTree = (HashTree) field.get(parentThread);
        SearchByClass<?> searcher = new SearchByClass<>(te.getClass());
        testTree.traverse(searcher);
        return searcher.getSubTree(te);
    } catch (ReflectiveOperationException ex) {
        log.warn("Can not get sub tree for Test element " + te.getName(), ex);
        return null;
    }
}
 
Example #4
Source File: ParallelSampler.java    From jmeter-bzm-plugins with Apache License 2.0 6 votes vote down vote up
@Override
public void threadFinished(JMeterThread thread) {
    JMeterContextServiceAccessorParallel.incrNumberOfThreads();
    try {
        Field field = AbstractTestElement.class.getDeclaredField("threadContext");
        field.setAccessible(true);
        if (thread instanceof JMeterThreadParallel) {
            JMeterThreadParallel pthr = (JMeterThreadParallel) thread;
            for (TestElement te : pthr.getParallelCompiler().getKnownElements()) {
                field.set(te, null);
            }
        }
    } catch (IllegalAccessException | NoSuchFieldException e) {
        log.warn("Failed to reset context", e);
    }
}
 
Example #5
Source File: AbstractSimpleThreadGroup.java    From jmeter-plugins with Apache License 2.0 6 votes vote down vote up
@Override
public boolean stopThread(String threadName, boolean now) {
    for (Entry<JMeterThread, Thread> entry : allThreads.entrySet()) {
        JMeterThread thrd = entry.getKey();
        if (thrd.getThreadName().equals(threadName)) {
            thrd.stop();
            thrd.interrupt();
            if (now) {
                Thread t = entry.getValue();
                if (t != null) {
                    t.interrupt();
                }
            }
            return true;
        }
    }
    return false;
}
 
Example #6
Source File: UltimateThreadGroupTest.java    From jmeter-plugins with Apache License 2.0 6 votes vote down vote up
@Test
public void testScheduleThreadAll() {
    System.out.println("scheduleThreadAll");
    HashTree hashtree = new HashTree();
    hashtree.add(new LoopController());

    CollectionProperty prop = JMeterPluginsUtils.tableModelRowsToCollectionProperty(dataModel, UltimateThreadGroup.DATA_PROPERTY);
    instance.setData(prop);
    instance.testStarted();

    for (int n = 0; n < instance.getNumThreads(); n++) {
        JMeterThread thread = new JMeterThread(hashtree, null, null);
        thread.setThreadNum(n);
        instance.scheduleThread(thread);
    }
}
 
Example #7
Source File: UltimateThreadGroupTest.java    From jmeter-plugins with Apache License 2.0 6 votes vote down vote up
@Test
public void testScheduleThread() {
    System.out.println("scheduleThread");
    HashTree hashtree = new HashTree();
    hashtree.add(new LoopController());
    JMeterThread thread = new JMeterThread(hashtree, null, null);

    CollectionProperty prop = JMeterPluginsUtils.tableModelRowsToCollectionProperty(dataModel, UltimateThreadGroup.DATA_PROPERTY);
    instance.setData(prop);
    instance.testStarted();

    instance.scheduleThread(thread);

    assertTrue(thread.getStartTime() > 0);
    assertTrue(thread.getEndTime() > thread.getStartTime());
}
 
Example #8
Source File: SteppingThreadGroupTest.java    From jmeter-plugins with Apache License 2.0 6 votes vote down vote up
@Test
public void testScheduleThreadIntegerOverflow() {
    System.out.println("scheduleThreadIntegerOverflow");
    HashTree hashtree = new HashTree();
    hashtree.add(new LoopController());
    JMeterThread thread = new JMeterThread(hashtree, null, null);
    SteppingThreadGroup instance = new SteppingThreadGroup();
    int numThreads = 3;
    instance.setNumThreads(numThreads);
    int inUserCount = 1;
    instance.setInUserCount("" + inUserCount);
    instance.setInUserCountBurst("0");
    int inUserPeriod = 224985600;
    instance.setInUserPeriod("" + inUserPeriod);
    instance.setRampUp("0");
    instance.setThreadGroupDelay("0");
    int flightTime = 33;
    instance.setFlightTime("" + flightTime);

    thread.setThreadNum(0);
    instance.scheduleThread(thread);

    assertEquals(1000L * ((inUserCount + 1) * inUserPeriod + inUserCount * flightTime), thread.getEndTime() - thread.getStartTime());

}
 
Example #9
Source File: AbstractDynamicThreadGroup.java    From jmeter-plugins with Apache License 2.0 5 votes vote down vote up
@Override
public void threadFinished(JMeterThread jMeterThread) {
    if(log.isDebugEnabled()) {
        log.debug("threadFinished: " + jMeterThread.getThreadName());
    }
    if (jMeterThread instanceof DynamicThread) {
        threads.remove(jMeterThread);
    }
}
 
Example #10
Source File: AbstractSimpleThreadGroup.java    From jmeter-plugins with Apache License 2.0 5 votes vote down vote up
@Override
public void tellThreadsToStop() {
    running = false;
    for (Entry<JMeterThread, Thread> entry : allThreads.entrySet()) {
        JMeterThread item = entry.getKey();
        item.stop(); // set stop flag
        item.interrupt(); // interrupt sampler if possible
        Thread t = entry.getValue();
        if (t != null) { // Bug 49734
            t.interrupt(); // also interrupt JVM thread
        }
    }
}
 
Example #11
Source File: AbstractSimpleThreadGroup.java    From jmeter-plugins with Apache License 2.0 5 votes vote down vote up
@Override
public void stop() {
    running = false;
    for (JMeterThread item : allThreads.keySet()) {
        item.stop();
    }
}
 
Example #12
Source File: VirtualUserController.java    From jmeter-plugins with Apache License 2.0 5 votes vote down vote up
private boolean moveToPool(JMeterThread thread) {
    if (thread instanceof DynamicThread) {
        if (!owner.isLimitReached() && getOwnerAsArrivals().movedToPool((DynamicThread) thread)) {
            reInitialize();
            return true;
        }
    }
    return false;
}
 
Example #13
Source File: VirtualUserController.java    From jmeter-plugins with Apache License 2.0 5 votes vote down vote up
@Override
protected Sampler nextIsNull() throws NextIsNullException {
    JMeterThread thread = JMeterContextService.getContext().getThread();
    if (owner instanceof ArrivalsThreadGroup) {
        getOwnerAsArrivals().completionFact(thread, iterationNo);
    }

    long iLimit = owner.getIterationsLimitAsLong();

    if (owner.isLimitReached()) {
        log.info("Test limit reached, thread is done: " + thread.getThreadName());
        setDone(true);
        return null;
    } else if (iLimit > 0 && iterationNo >= iLimit) {
        log.info("Iteration limit reached, thread is done: " + thread.getThreadName());
        setDone(true);
        return null;
    } else if (owner instanceof ConcurrencyThreadGroup && ((ConcurrencyThreadGroup) owner).tooMuchConcurrency()) {
        log.info("Need to decrease concurrency, thread is done: " + thread.getThreadName());
        setDone(true);
        return null;
    } else if (owner instanceof ArrivalsThreadGroup) {
        moveToPool(thread);
        return super.nextIsNull();
    } else {
        reInitialize();
        return next();
    }
}
 
Example #14
Source File: VirtualUserController.java    From jmeter-plugins with Apache License 2.0 5 votes vote down vote up
public void startNextLoop() {
    JMeterThread thread = JMeterContextService.getContext().getThread();
    if (owner instanceof ArrivalsThreadGroup) {
        getOwnerAsArrivals().abandonFact(thread, iterationNo);

        if (!moveToPool(thread)) {
            setDone(true);
        }
    } else {
        reInitialize();
    }
}
 
Example #15
Source File: ConcurrencyThreadGroup.java    From jmeter-plugins with Apache License 2.0 5 votes vote down vote up
@Override
public void threadFinished(JMeterThread thread) {
    super.threadFinished(thread);
    saveLogRecord("FINISH", thread.getThreadName(), "");
    lock.lock();
    try {
        condition.signalAll();
    } finally {
        lock.unlock();
    }
}
 
Example #16
Source File: AbstractSimpleThreadGroupTest.java    From jmeter-plugins with Apache License 2.0 5 votes vote down vote up
@Test
public void testThreadFinished() {
    System.out.println("threadFinished");
    HashTree hashtree = new HashTree();
    hashtree.add(new LoopController());
    JMeterThread thread = new JMeterThread(hashtree, null, null);
    AbstractSimpleThreadGroup instance = new AbstractSimpleThreadGroupImpl();
    instance.threadFinished(thread);
}
 
Example #17
Source File: AbstractSimpleThreadGroupTest.java    From jmeter-plugins with Apache License 2.0 5 votes vote down vote up
@Test
public void testFlow() {
    JMeterUtils.setProperty(AbstractSimpleThreadGroup.THREAD_GROUP_DISTRIBUTED_PREFIX_PROPERTY_NAME, "distprefix");
    final AbstractSimpleThreadGroupImpl tg = new AbstractSimpleThreadGroupImpl();
    tg.setName("TGName");
    LoopController looper = new LoopController();
    looper.setLoops(-1);
    tg.setSamplerController(looper);
    tg.setNumThreads(1);
    ListedHashTree listedHashTree = ArrivalsThreadGroupTest.getListedHashTree(tg, false);
    tg.start(0, null, listedHashTree, null);

    for (Map.Entry<JMeterThread, Thread> entry : tg.getAllThreads().entrySet()) {
        assertEquals("distprefix-TGName 0-1", entry.getValue().getName());
    }

    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                Thread.sleep(1000);
                tg.tellThreadsToStop();
                tg.waitThreadsStopped();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }).start();

    tg.verifyThreadsStopped();
}
 
Example #18
Source File: AbstractSimpleThreadGroupTest.java    From jmeter-plugins with Apache License 2.0 5 votes vote down vote up
@Test
public void testScheduleThread_JMeterThread_long() {
    System.out.println("scheduleThread");
    JMeterThread thread = null;
    long now = 0L;
    AbstractSimpleThreadGroup instance = new AbstractSimpleThreadGroupImpl();
    instance.scheduleThread(thread, now);
}
 
Example #19
Source File: AbstractSimpleThreadGroupTest.java    From jmeter-plugins with Apache License 2.0 5 votes vote down vote up
@Test
public void testScheduleThread_JMeterThread() {
    System.out.println("scheduleThread");
    JMeterThread thread = null;
    AbstractSimpleThreadGroup instance = new AbstractSimpleThreadGroupImpl();
    instance.scheduleThread(thread);
}
 
Example #20
Source File: UltimateThreadGroup.java    From jmeter-plugins with Apache License 2.0 5 votes vote down vote up
@Override
protected void scheduleThread(JMeterThread thread, long tgStartTime) {
    log.debug("Scheduling thread: " + thread.getThreadName());
    if (threadsToSchedule < 1) {
        if (!scheduleIT.hasNext()) {
            throw new RuntimeException("Not enough schedule records for thread #" + thread.getThreadName());
        }

        currentRecord = (CollectionProperty) scheduleIT.next();
        threadsToSchedule = currentRecord.get(0).getIntValue();
    }

    int numThreads = currentRecord.get(START_THREADS_CNT_FIELD_NO).getIntValue();
    int initialDelay = currentRecord.get(INIT_DELAY_FIELD_NO).getIntValue();
    int startRampUp = currentRecord.get(STARTUP_TIME_FIELD_NO).getIntValue();
    int flightTime = currentRecord.get(HOLD_LOAD_FOR_FIELD_NO).getIntValue();
    int endRampUp = currentRecord.get(SHUTDOWN_TIME_FIELD_NO).getIntValue();

    long ascentPoint = tgStartTime + 1000 * initialDelay;
    final int rampUpDelayForThread = (int) Math.floor(1000 * startRampUp * (double) threadsToSchedule / numThreads);
    long startTime = ascentPoint + rampUpDelayForThread;
    long descentPoint = startTime + 1000 * flightTime + 1000 * startRampUp - rampUpDelayForThread;

    thread.setStartTime(startTime);
    thread.setEndTime(descentPoint + (int) Math.floor(1000 * endRampUp * (double) threadsToSchedule / numThreads));

    thread.setScheduled(true);
    threadsToSchedule--;
}
 
Example #21
Source File: ParallelSampler.java    From jmeter-bzm-plugins with Apache License 2.0 5 votes vote down vote up
private void injectVariables(JMeterThread jmThread, JMeterContext threadContext) {
    if (threadContext != null && threadContext.getVariables() != null) {
        try {
            Class<JMeterThread> cls = JMeterThread.class;
            Field vars = cls.getDeclaredField("threadVars");
            vars.setAccessible(true);
            vars.set(jmThread, threadContext.getVariables());
        } catch (Throwable ex) {
            log.warn("Cannot inject variables into parallel thread ", ex);
        }
    }
}
 
Example #22
Source File: ParallelSamplerTest.java    From jmeter-bzm-plugins with Apache License 2.0 5 votes vote down vote up
@Test
public void sample() throws Exception {
    JMeterThread dummy = new JMeterThread(new HashTree(new GenericController()), null, null);
    JMeterContextService.getContext().setEngine(new StandardJMeterEngine());
    JMeterContextService.getContext().setThread(dummy);
    JMeterThread thr = JMeterContextService.getContext().getThread();
    for (int n = 0; n < 1000; n++) {// we're doing good check here because of multi-threads
        log.debug("\n\n\nTry #" + n);
        EmulSampler.instances = 0;
        EmulSampler.count.set(0);
        ParallelSampler obj = new ParallelSampler();
        obj.threadStarted();
        obj.setGenerateParent(true);

        obj.addTestElement(getContextedSampler(thr));
        obj.addTestElement(getContextedSampler(thr));
        obj.addTestElement(getContextedSampler(thr));
        obj.addTestElement(getContextedSampler(thr));
        obj.addTestElement(getContextedSampler(thr));

        SampleResult res = obj.sample(null);
        assertEquals(5, EmulSampler.count.get());
        if (res.getSubResults().length < 5) {
            throw new AssertionError();
        }

        assertEquals(5, res.getSubResults().length);
    }
}
 
Example #23
Source File: ParallelSamplerTest.java    From jmeter-bzm-plugins with Apache License 2.0 5 votes vote down vote up
@Test
public void underLoop() throws Exception {
    EmulSampler payload = new EmulSampler();
    payload.setName("payload");

    ParallelSampler sam = new ParallelSampler();
    sam.threadStarted();
    sam.setName("Parallel Sampler");
    sam.addTestElement(payload);

    LoopController ctl = getLoopController(5);
    ctl.addTestElement(sam);

    JMeterThread thr = new JMeterThread(new HashTree(ctl), sam, sam.notifier);
    thr.setThreadName("root");
    thr.setThreadGroup(new DummyThreadGroup());
    JMeterContextService.getContext().setThread(thr);

    addToContext(sam, thr);
    addToContext(payload, thr);

    sam.setRunningVersion(true);
    ctl.setRunningVersion(true);
    payload.setRunningVersion(true);
    thr.run();
    assertEquals(5, EmulSampler.count.get());
}
 
Example #24
Source File: ParallelSamplerTest.java    From jmeter-bzm-plugins with Apache License 2.0 5 votes vote down vote up
public void addToContext(TestElement te, JMeterThread parentThread) throws NoSuchFieldException, IllegalAccessException {
    Field field = JMeterThread.class.getDeclaredField("compiler");
    field.setAccessible(true);
    TestCompiler parentCompiler = (TestCompiler) field.get(parentThread);
    parentCompiler.addNode(te, null);
    parentCompiler.subtractNode();
}
 
Example #25
Source File: SteppingThreadGroupTest.java    From jmeter-plugins with Apache License 2.0 4 votes vote down vote up
/**
 * Test of scheduleThread method, of class SteppingThreadGroup.
 */
@Test
public void testScheduleThread() {
    System.out.println("scheduleThread");
    HashTree hashtree = new HashTree();
    hashtree.add(new LoopController());
    JMeterThread thread = new JMeterThread(hashtree, null, null);
    SteppingThreadGroup instance = new SteppingThreadGroup();
    instance.setNumThreads(15);
    instance.setInUserCount("5");
    instance.setInUserCountBurst("10");
    instance.setInUserPeriod("30");
    instance.setRampUp("10");
    instance.setThreadGroupDelay("5");
    instance.setFlightTime("60");

    long s1 = -1, s2;
    for (int n = 0; n < 10; n++) {
        thread.setThreadNum(n);
        instance.scheduleThread(thread);
        s2 = thread.getStartTime();
        if (s1 >= 0) {
            assertEquals(1000, s2 - s1);
        }
        s1 = s2;
    }

    thread.setThreadNum(10);
    instance.scheduleThread(thread);
    s2 = thread.getStartTime();
    assertEquals(31000, s2 - s1);
    s1 = s2;

    for (int n = 11; n < 15; n++) {
        thread.setThreadNum(n);
        instance.scheduleThread(thread);
        s2 = thread.getStartTime();
        if (s1 >= 0) {
            assertEquals(2000, s2 - s1);
        }
        s1 = s2;
    }
}
 
Example #26
Source File: ArrivalsThreadGroup.java    From jmeter-plugins with Apache License 2.0 4 votes vote down vote up
public void completionFact(JMeterThread thread, long arrivalID) {
    completionsCount.incrementAndGet();
    saveLogRecord("COMPLETION", thread.getThreadName(), thread.getThreadNum() + "." + arrivalID);
}
 
Example #27
Source File: ArrivalsThreadGroup.java    From jmeter-plugins with Apache License 2.0 4 votes vote down vote up
public void abandonFact(JMeterThread thread, long arrivalID) {
    abandonsCount.incrementAndGet();
    saveLogRecord("ABANDONMENT", thread.getThreadName(), thread.getThreadNum() + "." + arrivalID);
}
 
Example #28
Source File: ConcurrencyThreadGroup.java    From jmeter-plugins with Apache License 2.0 4 votes vote down vote up
public void threadStarted(JMeterThread thread) {
    saveLogRecord("START", thread.getThreadName(), "");
}
 
Example #29
Source File: ParallelSamplerTest.java    From jmeter-bzm-plugins with Apache License 2.0 4 votes vote down vote up
@Test
public void testThreadSafeCookieManager() throws Exception {
    CookieManager cookieManager = new CookieManager();
    Cookie c = new Cookie();
    c.setName("name");
    c.setValue("value");
    c.setDomain("blazedemo.com");
    c.setPath("");
    cookieManager.add(c);
    HashTree hashtree = createTestTree(cookieManager);

    TestPlan testPlan = new TestPlan();
    HashTree testPlanHashTree = new HashTree();
    testPlanHashTree.put(testPlan, hashtree);

    StandardJMeterEngine engine = new StandardJMeterEngine();
    engine.configure(testPlanHashTree);

    EmulatorThreadMonitor monitor = new EmulatorThreadMonitor();
    JMeterThread thread = new JMeterThread(hashtree, monitor, null);
    thread.setThreadName("test thread");
    JMeterContextService.getContext().setThread(thread);

    ParallelSampler parallel = new ParallelSampler();
    parallel.testStarted();

    Field field = StandardJMeterEngine.class.getDeclaredField("test");
    field.setAccessible(true);
    HashTree testTree = (HashTree) field.get(engine);

    assertTrue("CookieManager should be changed to ThreadSafeCookieManager", testTree.toString().contains("ThreadSafeCookieManager"));
    ListedHashTree loop = (ListedHashTree) (testTree.values().toArray()[0]);
    ListedHashTree threadSafeManager = ((ListedHashTree) (loop.values().toArray()[0]));

    CookieManager mgr = (CookieManager) threadSafeManager.getArray()[0];
    assertTrue(mgr instanceof ThreadSafeCookieManager);
    assertEquals(1, mgr.getCookieCount());
    JMeterProperty property = mgr.getCookies().get(0);
    assertEquals("name", property.getName());
    assertEquals("blazedemo.com\tTRUE\t\tFALSE\t0\tname\tvalue", property.getStringValue());
}
 
Example #30
Source File: AbstractSimpleThreadGroupImpl.java    From jmeter-plugins with Apache License 2.0 4 votes vote down vote up
public void scheduleThread(JMeterThread thread, long now) {
    // just dummy impl
}