jmeter.java
来自「测试工具」· Java 代码 · 共 958 行 · 第 1/3 页
JAVA
958 行
/*
* 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;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.Authenticator;
import java.net.MalformedURLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Properties;
import java.util.StringTokenizer;
import javax.swing.JLabel;
import org.apache.commons.cli.avalon.CLArgsParser;
import org.apache.commons.cli.avalon.CLOption;
import org.apache.commons.cli.avalon.CLOptionDescriptor;
import org.apache.commons.cli.avalon.CLUtil;
import org.apache.jmeter.control.ReplaceableController;
import org.apache.jmeter.engine.ClientJMeterEngine;
import org.apache.jmeter.engine.JMeterEngine;
import org.apache.jmeter.engine.RemoteJMeterEngineImpl;
import org.apache.jmeter.engine.StandardJMeterEngine;
import org.apache.jmeter.engine.event.LoopIterationEvent;
import org.apache.jmeter.exceptions.IllegalUserActionException;
import org.apache.jmeter.gui.GuiPackage;
import org.apache.jmeter.gui.MainFrame;
import org.apache.jmeter.gui.action.ActionNames;
import org.apache.jmeter.gui.action.ActionRouter;
import org.apache.jmeter.gui.action.Load;
import org.apache.jmeter.gui.tree.JMeterTreeListener;
import org.apache.jmeter.gui.tree.JMeterTreeNode;
import org.apache.jmeter.gui.tree.JMeterTreeModel;
import org.apache.jmeter.plugin.JMeterPlugin;
import org.apache.jmeter.plugin.PluginManager;
import org.apache.jmeter.reporters.ResultCollector;
import org.apache.jmeter.reporters.Summariser;
import org.apache.jmeter.samplers.Remoteable;
import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.services.FileServer;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jmeter.testelement.TestListener;
import org.apache.jmeter.util.BeanShellInterpreter;
import org.apache.jmeter.util.BeanShellServer;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.collections.HashTree;
import org.apache.jorphan.gui.ComponentUtil;
import org.apache.jorphan.logging.LoggingManager;
import org.apache.jorphan.collections.SearchByClass;
import org.apache.jorphan.reflect.ClassTools;
import org.apache.jorphan.util.JMeterException;
import org.apache.jorphan.util.JOrphanUtils;
import org.apache.log.Logger;
import com.thoughtworks.xstream.converters.ConversionException;
/**
* Main JMeter class; processes options and starts the GUI, non-GUI or server as appropriate.
*/
public class JMeter implements JMeterPlugin {
private static final Logger log = LoggingManager.getLoggerForClass();
public static final String HTTP_PROXY_PASS = "http.proxyPass"; // $NON-NLS-1$
public static final String HTTP_PROXY_USER = "http.proxyUser"; // $NON-NLS-1$
private static final int PROXY_PASSWORD = 'a';// $NON-NLS-1$
private static final int JMETER_HOME_OPT = 'd';// $NON-NLS-1$
private static final int HELP_OPT = 'h';// $NON-NLS-1$
// jmeter.log
private static final int JMLOGFILE_OPT = 'j';// $NON-NLS-1$
// sample result log file
private static final int LOGFILE_OPT = 'l';// $NON-NLS-1$
private static final int NONGUI_OPT = 'n';// $NON-NLS-1$
private static final int PROPFILE_OPT = 'p';// $NON-NLS-1$
private static final int PROPFILE2_OPT = 'q';// $NON-NLS-1$
private static final int REMOTE_OPT = 'r';// $NON-NLS-1$
private static final int SERVER_OPT = 's';// $NON-NLS-1$
private static final int TESTFILE_OPT = 't';// $NON-NLS-1$
private static final int PROXY_USERNAME = 'u';// $NON-NLS-1$
private static final int VERSION_OPT = 'v';// $NON-NLS-1$
private static final int SYSTEM_PROPERTY = 'D';// $NON-NLS-1$
private static final int JMETER_GLOBAL_PROP = 'G';// $NON-NLS-1$
private static final int PROXY_HOST = 'H';// $NON-NLS-1$
private static final int JMETER_PROPERTY = 'J';// $NON-NLS-1$
private static final int LOGLEVEL = 'L';// $NON-NLS-1$
private static final int NONPROXY_HOSTS = 'N';// $NON-NLS-1$
private static final int PROXY_PORT = 'P';// $NON-NLS-1$
private static final int REMOTE_OPT_PARAM = 'R';// $NON-NLS-1$
private static final int SYSTEM_PROPFILE = 'S';// $NON-NLS-1$
private static final int REMOTE_STOP = 'X';// $NON-NLS-1$
/**
* Define the understood options. Each CLOptionDescriptor contains:
* <ul>
* <li>The "long" version of the option. Eg, "help" means that "--help"
* will be recognised.</li>
* <li>The option flags, governing the option's argument(s).</li>
* <li>The "short" version of the option. Eg, 'h' means that "-h" will be
* recognised.</li>
* <li>A description of the option.</li>
* </ul>
*/
private static final CLOptionDescriptor[] options = new CLOptionDescriptor[] {
new CLOptionDescriptor("help", CLOptionDescriptor.ARGUMENT_DISALLOWED, HELP_OPT,
"print usage information and exit"),
new CLOptionDescriptor("version", CLOptionDescriptor.ARGUMENT_DISALLOWED, VERSION_OPT,
"print the version information and exit"),
new CLOptionDescriptor("propfile", CLOptionDescriptor.ARGUMENT_REQUIRED, PROPFILE_OPT,
"the jmeter property file to use"),
new CLOptionDescriptor("addprop", CLOptionDescriptor.ARGUMENT_REQUIRED
| CLOptionDescriptor.DUPLICATES_ALLOWED, PROPFILE2_OPT,
"additional JMeter property file(s)"),
new CLOptionDescriptor("testfile", CLOptionDescriptor.ARGUMENT_REQUIRED, TESTFILE_OPT,
"the jmeter test(.jmx) file to run"),
new CLOptionDescriptor("logfile", CLOptionDescriptor.ARGUMENT_REQUIRED, LOGFILE_OPT,
"the file to log samples to"),
new CLOptionDescriptor("jmeterlogfile", CLOptionDescriptor.ARGUMENT_REQUIRED, JMLOGFILE_OPT,
"jmeter run log file (jmeter.log)"),
new CLOptionDescriptor("nongui", CLOptionDescriptor.ARGUMENT_DISALLOWED, NONGUI_OPT,
"run JMeter in nongui mode"),
new CLOptionDescriptor("server", CLOptionDescriptor.ARGUMENT_DISALLOWED, SERVER_OPT,
"run the JMeter server"),
new CLOptionDescriptor("proxyHost", CLOptionDescriptor.ARGUMENT_REQUIRED, PROXY_HOST,
"Set a proxy server for JMeter to use"),
new CLOptionDescriptor("proxyPort", CLOptionDescriptor.ARGUMENT_REQUIRED, PROXY_PORT,
"Set proxy server port for JMeter to use"),
new CLOptionDescriptor("nonProxyHosts", CLOptionDescriptor.ARGUMENT_REQUIRED, NONPROXY_HOSTS,
"Set nonproxy host list (e.g. *.apache.org|localhost)"),
new CLOptionDescriptor("username", CLOptionDescriptor.ARGUMENT_REQUIRED, PROXY_USERNAME,
"Set username for proxy server that JMeter is to use"),
new CLOptionDescriptor("password", CLOptionDescriptor.ARGUMENT_REQUIRED, PROXY_PASSWORD,
"Set password for proxy server that JMeter is to use"),
new CLOptionDescriptor("jmeterproperty", CLOptionDescriptor.DUPLICATES_ALLOWED
| CLOptionDescriptor.ARGUMENTS_REQUIRED_2, JMETER_PROPERTY,
"Define additional JMeter properties"),
new CLOptionDescriptor("globalproperty", CLOptionDescriptor.DUPLICATES_ALLOWED
| CLOptionDescriptor.ARGUMENTS_REQUIRED_2, JMETER_GLOBAL_PROP,
"Define Global properties (sent to servers)"),
new CLOptionDescriptor("systemproperty", CLOptionDescriptor.DUPLICATES_ALLOWED
| CLOptionDescriptor.ARGUMENTS_REQUIRED_2, SYSTEM_PROPERTY,
"Define additional system properties"),
new CLOptionDescriptor("systemPropertyFile", CLOptionDescriptor.DUPLICATES_ALLOWED
| CLOptionDescriptor.ARGUMENT_REQUIRED, SYSTEM_PROPFILE,
"additional system property file(s)"),
new CLOptionDescriptor("loglevel", CLOptionDescriptor.DUPLICATES_ALLOWED
| CLOptionDescriptor.ARGUMENTS_REQUIRED_2, LOGLEVEL,
"[category=]level e.g. jorphan=INFO or jmeter.util=DEBUG"),
new CLOptionDescriptor("runremote", CLOptionDescriptor.ARGUMENT_DISALLOWED, REMOTE_OPT,
"Start remote servers (as defined in remote_hosts)"),
new CLOptionDescriptor("remotestart", CLOptionDescriptor.ARGUMENT_REQUIRED, REMOTE_OPT_PARAM,
"Start these remote servers (overrides remote_hosts)"),
new CLOptionDescriptor("homedir", CLOptionDescriptor.ARGUMENT_REQUIRED, JMETER_HOME_OPT,
"the jmeter home directory to use"),
new CLOptionDescriptor("remoteexit", CLOptionDescriptor.ARGUMENT_DISALLOWED, REMOTE_STOP,
"Exit the remote servers at end of test (non-GUI)"),
};
public JMeter() {
}
// Hack to allow automated tests to find when test has ended
//transient boolean testEnded = false;
private JMeter parent;
private Properties remoteProps; // Properties to be sent to remote servers
private boolean remoteStop; // should remote engines be stopped at end of GUI test?
/**
* Starts up JMeter in GUI mode
*/
private void startGui(CLOption testFile) {
PluginManager.install(this, true);
JMeterTreeModel treeModel = new JMeterTreeModel();
JMeterTreeListener treeLis = new JMeterTreeListener(treeModel);
treeLis.setActionHandler(ActionRouter.getInstance());
// NOTUSED: GuiPackage guiPack =
GuiPackage.getInstance(treeLis, treeModel);
MainFrame main = new MainFrame(ActionRouter.getInstance(), treeModel, treeLis);
ComponentUtil.centerComponentInWindow(main, 80);
main.show();
ActionRouter.getInstance().actionPerformed(new ActionEvent(main, 1, ActionNames.ADD_ALL));
String arg;
if (testFile != null && (arg = testFile.getArgument()) != null) {
FileInputStream reader = null;
try {
File f = new File(arg);
log.info("Loading file: " + f);
reader = new FileInputStream(f);
HashTree tree = SaveService.loadTree(reader);
GuiPackage.getInstance().setTestPlanFile(f.getAbsolutePath());
Load.insertLoadedTree(1, tree);
} catch (ConversionException e) {
log.error("Failure loading test file", e);
JMeterUtils.reportErrorToUser(SaveService.CEtoString(e));
} catch (Exception e) {
log.error("Failure loading test file", e);
JMeterUtils.reportErrorToUser(e.toString());
} finally {
JOrphanUtils.closeQuietly(reader);
}
}
}
/**
* Takes the command line arguments and uses them to determine how to
* startup JMeter.
*/
public void start(String[] args) {
CLArgsParser parser = new CLArgsParser(args, options);
if (null != parser.getErrorString()) {
System.err.println("Error: " + parser.getErrorString());
System.out.println("Usage");
System.out.println(CLUtil.describeOptions(options).toString());
return;
}
try {
initializeProperties(parser); // Also initialises JMeter logging
/*
* The following is needed for HTTPClient.
* (originally tried doing this in HTTPSampler2,
* but it appears that it was done too late when running in GUI mode)
* Set the commons logging default to Avalon Logkit, if not already defined
*/
if (System.getProperty("org.apache.commons.logging.Log") == null) { // $NON-NLS-1$
System.setProperty("org.apache.commons.logging.Log" // $NON-NLS-1$
, "org.apache.commons.logging.impl.LogKitLogger"); // $NON-NLS-1$
}
log.info(JMeterUtils.getJMeterCopyright());
log.info("Version " + JMeterUtils.getJMeterVersion());
logProperty("java.version"); //$NON-NLS-1$
logProperty("os.name"); //$NON-NLS-1$
logProperty("os.arch"); //$NON-NLS-1$
logProperty("os.version"); //$NON-NLS-1$
logProperty("file.encoding"); // $NON-NLS-1$
log.info("Default Locale=" + Locale.getDefault().getDisplayName());// $NON-NLS-1$
log.info("JMeter Locale=" + JMeterUtils.getLocale().getDisplayName());// $NON-NLS-1$
log.info("JMeterHome=" + JMeterUtils.getJMeterHome());// $NON-NLS-1$
logProperty("user.dir"," ="); //$NON-NLS-1$
log.info("PWD ="+new File(".").getCanonicalPath());//$NON-NLS-1$
setProxy(parser);
updateClassLoader();
if (log.isDebugEnabled())
{
String jcp=System.getProperty("java.class.path");// $NON-NLS-1$
String bits[] =jcp.split(File.pathSeparator);
log.debug("ClassPath");
for(int i = 0; i<bits.length ;i++){
log.debug(bits[i]);
}
log.debug(jcp);
}
// Set some (hopefully!) useful properties
long now=System.currentTimeMillis();
JMeterUtils.setProperty("START.MS",Long.toString(now));
Date today=new Date(now); // so it agrees with above
// TODO perhaps should share code with __time() function for this...
JMeterUtils.setProperty("START.YMD",new SimpleDateFormat("yyyyMMdd").format(today));
JMeterUtils.setProperty("START.HMS",new SimpleDateFormat("HHmmss").format(today));
if (parser.getArgumentById(VERSION_OPT) != null) {
System.out.println(JMeterUtils.getJMeterCopyright());
System.out.println("Version " + JMeterUtils.getJMeterVersion());
} else if (parser.getArgumentById(HELP_OPT) != null) {
System.out.println(JMeterUtils.getResourceFileAsText("org/apache/jmeter/help.txt"));// $NON-NLS-1$
} else if (parser.getArgumentById(SERVER_OPT) != null) {
// We need to check if the JMeter home contains spaces in the path,
// because then we will not be able to bind to RMI registry, see
// Java bug id 4496398
final String jmHome = JMeterUtils.getJMeterHome();
if(jmHome.indexOf(" ") > -1) {// $NON-NLS-1$
// Just warn user, and exit, no reason to continue, since we will
// not be able to bind to RMI registry, until Java bug 4496398 is fixed
log.error("JMeter path cannot contain spaces when run in server mode : " + jmHome);
throw new RuntimeException("JMeter path cannot contain spaces when run in server mode: "+jmHome);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?