📄 jmeterthread.java
字号:
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.apache.jmeter.threads;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import org.apache.jmeter.assertions.Assertion;
import org.apache.jmeter.assertions.AssertionResult;
import org.apache.jmeter.control.Controller;
import org.apache.jmeter.control.TransactionSampler;
import org.apache.jmeter.engine.StandardJMeterEngine;
import org.apache.jmeter.engine.event.LoopIterationEvent;
import org.apache.jmeter.engine.event.LoopIterationListener;
import org.apache.jmeter.gui.GuiPackage;
import org.apache.jmeter.processor.PostProcessor;
import org.apache.jmeter.samplers.SampleEvent;
import org.apache.jmeter.samplers.SampleListener;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.samplers.Sampler;
import org.apache.jmeter.testbeans.TestBeanHelper;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jmeter.testelement.TestListener;
import org.apache.jmeter.testelement.ThreadListener;
import org.apache.jmeter.timers.Timer;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.collections.HashTree;
import org.apache.jorphan.collections.HashTreeTraverser;
import org.apache.jorphan.collections.SearchByClass;
import org.apache.jorphan.logging.LoggingManager;
import org.apache.jorphan.util.JMeterStopTestException;
import org.apache.jorphan.util.JMeterStopThreadException;
import org.apache.log.Logger;
/**
* The JMeter interface to the sampling process, allowing JMeter to see the
* timing, add listeners for sampling events and to stop the sampling process.
*
*/
public class JMeterThread implements Runnable, Serializable {
private static final Logger log = LoggingManager.getLoggerForClass();
private static final long serialVersionUID = 23L; // Remember to change this when the class changes ...
// NOT USED private static Map samplers = new HashMap();
private int initialDelay = 0;
private Controller controller;
private boolean running;
private HashTree testTree;
private transient TestCompiler compiler;
private JMeterThreadMonitor monitor;
private String threadName;
private transient JMeterContext threadContext;
private transient JMeterVariables threadVars;
private Collection testListeners;
private transient ListenerNotifier notifier;
private int threadNum = 0;
private long startTime = 0;
private long endTime = 0;
private boolean scheduler = false;
// based on this scheduler is enabled or disabled
private ThreadGroup threadGroup; // Gives access to parent thread
// threadGroup
private StandardJMeterEngine engine = null; // For access to stop methods.
private boolean onErrorStopTest;
private boolean onErrorStopThread;
public static final String PACKAGE_OBJECT = "JMeterThread.pack"; // $NON-NLS-1$
public static final String LAST_SAMPLE_OK = "JMeterThread.last_sample_ok"; // $NON-NLS-1$
public JMeterThread() {
}
public JMeterThread(HashTree test, JMeterThreadMonitor monitor, ListenerNotifier note) {
this.monitor = monitor;
threadVars = new JMeterVariables();
testTree = test;
compiler = new TestCompiler(testTree, threadVars);
controller = (Controller) testTree.getArray()[0];
SearchByClass threadListenerSearcher = new SearchByClass(TestListener.class);
test.traverse(threadListenerSearcher);
testListeners = threadListenerSearcher.getSearchResults();
notifier = note;
running = true;
}
public void setInitialContext(JMeterContext context) {
threadVars.putAll(context.getVariables());
}
/**
* Checks whether the JMeterThread is Scheduled. author
* T.Elanjchezhiyan(chezhiyan@siptech.co.in)
*/
public boolean isScheduled() {
return this.scheduler;
}
/**
* Enable the scheduler for this JMeterThread. author
* T.Elanjchezhiyan(chezhiyan@siptech.co.in)
*/
public void setScheduled(boolean sche) {
this.scheduler = sche;
}
/**
* Set the StartTime for this Thread.
*
* @param stime
* the StartTime value. author
* T.Elanjchezhiyan(chezhiyan@siptech.co.in)
*/
public void setStartTime(long stime) {
startTime = stime;
}
/**
* Get the start time value.
*
* @return the start time value. author
* T.Elanjchezhiyan(chezhiyan@siptech.co.in)
*/
public long getStartTime() {
return startTime;
}
/**
* Set the EndTime for this Thread.
*
* @param etime
* the EndTime value. author
* T.Elanjchezhiyan(chezhiyan@siptech.co.in)
*/
public void setEndTime(long etime) {
endTime = etime;
}
/**
* Get the end time value.
*
* @return the end time value. author
* T.Elanjchezhiyan(chezhiyan@siptech.co.in)
*/
public long getEndTime() {
return endTime;
}
/**
* Check the scheduled time is completed.
*
* author T.Elanjchezhiyan(chezhiyan@siptech.co.in)
*/
private void stopScheduler() {
long delay = System.currentTimeMillis() - endTime;
if ((delay >= 0)) {
running = false;
}
}
/**
* Wait until the scheduled start time if necessary
*
* Author T.Elanjchezhiyan(chezhiyan@siptech.co.in)
*/
private void startScheduler() {
long delay = (startTime - System.currentTimeMillis());
if (delay > 0) {
try {
Thread.sleep(delay);
} catch (Exception e) {
}
}
}
public void setThreadName(String threadName) {
this.threadName = threadName;
}
/*
* See below for reason for this change. Just in case this causes problems,
* allow the change to be backed out
*/
private static final boolean startEarlier =
JMeterUtils.getPropDefault("jmeterthread.startearlier", true); // $NON-NLS-1$
private static final boolean reversePostProcessors =
JMeterUtils.getPropDefault("jmeterthread.reversePostProcessors",false); // $NON-NLS-1$
static {
if (startEarlier) {
log.info("jmeterthread.startearlier=true (see jmeter.properties)");
} else {
log.info("jmeterthread.startearlier=false (see jmeter.properties)");
}
if (reversePostProcessors) {
log.info("Running PostProcessors in reverse order");
} else {
log.info("Running PostProcessors in forward order");
}
}
public void run() {
try {
initRun();
while (running) {
Sampler sam;
while (running && (sam = controller.next()) != null) {
try {
threadContext.setCurrentSampler(sam);
// Check if we are running a transaction
TransactionSampler transactionSampler = null;
if(sam instanceof TransactionSampler) {
transactionSampler = (TransactionSampler) sam;
}
// Find the package for the transaction
SamplePackage transactionPack = null;
if(transactionSampler != null) {
transactionPack = compiler.configureTransactionSampler(transactionSampler);
// Check if the transaction is done
if(transactionSampler.isTransactionDone()) {
// Get the transaction sample result
SampleResult transactionResult = transactionSampler.getTransactionResult();
transactionResult.setThreadName(threadName);
transactionResult.setGroupThreads(threadGroup.getNumberOfThreads());
transactionResult.setAllThreads(JMeterContextService.getNumberOfThreads());
// Check assertions for the transaction sample
checkAssertions(transactionPack.getAssertions(), transactionResult);
// Notify listeners with the transaction sample result
notifyListeners(transactionPack.getSampleListeners(), transactionResult);
compiler.done(transactionPack);
// Transaction is done, we do not have a sampler to sample
sam = null;
}
else {
// It is the sub sampler of the transaction that will be sampled
sam = transactionSampler.getSubSampler();
}
}
// Check if we have a sampler to sample
if(sam != null) {
// Get the sampler ready to sample
SamplePackage pack = compiler.configureSampler(sam);
// Hack: save the package for any transaction
// controllers
threadContext.getVariables().putObject(PACKAGE_OBJECT, pack);
delay(pack.getTimers());
Sampler sampler = pack.getSampler();
sampler.setThreadContext(threadContext);
sampler.setThreadName(threadName);
TestBeanHelper.prepare(sampler);
// Perform the actual sample
SampleResult result = sampler.sample(null);
// TODO: remove this useless Entry parameter
// If we got any results, then perform processing on the result
if (result != null) {
result.setGroupThreads(threadGroup.getNumberOfThreads());
result.setAllThreads(JMeterContextService.getNumberOfThreads());
result.setThreadName(threadName);
threadContext.setPreviousResult(result);
runPostProcessors(pack.getPostProcessors());
checkAssertions(pack.getAssertions(), result);
// Do not send subsamples to listeners which receive the transaction sample
List sampleListeners = getSampleListeners(pack, transactionPack, transactionSampler);
notifyListeners(sampleListeners, result);
compiler.done(pack);
// Add the result as subsample of transaction if we are in a transaction
if(transactionSampler != null) {
transactionSampler.addSubSamplerResult(result);
}
// Check if thread or test should be stopped
if (result.isStopThread() || (!result.isSuccessful() && onErrorStopThread)) {
stopThread();
}
if (result.isStopTest() || (!result.isSuccessful() && onErrorStopTest)) {
stopTest();
}
} else {
compiler.done(pack); // Finish up
}
}
if (scheduler) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -