📄 best-practices.html
字号:
<li>
Only save the data that you need
</li>
<li>
Use as few Assertions as possible
</li>
</ul>
</blockquote>
</p>
</td></tr>
<tr><td><br></td></tr>
</table>
<table border="0" cellspacing="0" cellpadding="2" width="100%">
<tr><td bgcolor="#525D76">
<font color="#ffffff" face="arial,helvetica,sanserif">
<a name="beanshell_server"><strong>16.7 BeanShell server</strong></a>
</font>
</td></tr>
<tr><td>
<blockquote>
<p>
The BeanShell interpreter has a very useful feature - it can act as a server,
which is accessible by telnet or http.
</p>
<p><table border="1" bgcolor="#bbbb00" width="50%" cellspacing="0" cellpadding="2">
<tr><td>
There is no security. Anyone who can connect to the port can issue any BeanShell commands.
These can provide unrestricted access to the JMeter application and the host.
<b>
Do not enable the server unless the ports are protected against access, e.g. by a firewall.
</b>
</td></tr>
</table></p>
<p>
If you do wish to use the server, define the following in jmeter.properties:
</p>
<pre>
beanshell.server.port=9000
beanshell.server.file=../extras/startup.bsh
</pre>
<p>
In the above example, the server will be started, and will listen on ports 9000 and 9001.
Port 9000 will be used for http access. Port 9001 will be used for telnet access.
The startup.bsh file will be processed by the server, and can be used to define various functions and set up variables.
The startup file defines methods for setting and printing JMeter and system properties.
This is what you should see in the JMeter console:
</p>
<pre>
Startup script running
Startup script completed
Httpd started on port: 9000
Sessiond started on port: 9001
</pre>
<p>
As a practical example, assume you have a long-running JMeter test running in non-GUI mode,
and you want to vary the throughput at various times during the test.
The test-plan includes a Constant Throughput Timer which is defined in terms of a property,
e.g. ${__P(throughput)}.
The following BeanShell commands could be used to change the test:
</p>
<pre>
printprop("throughput");
curr=Integer.decode(args[0]); // Start value
inc=Integer.decode(args[1]); // Increment
end=Integer.decode(args[2]); // Final value
secs=Integer.decode(args[3]); // Wait between changes
while(curr <= end){
setprop("throughput",curr.toString()); // Needs to be a string here
Thread.sleep(secs*1000);
curr += inc;
}
printprop("throughput");
</pre>
<p>
The script can be stored in a file (throughput.bsh, say), and sent to the server using bshclient.jar.
For example:
</p>
<pre>
java -jar ../lib/bshclient.jar localhost 9000 throughput.bsh 70 5 100 60
</pre>
</blockquote>
</p>
</td></tr>
<tr><td><br></td></tr>
</table>
<table border="0" cellspacing="0" cellpadding="2" width="100%">
<tr><td bgcolor="#525D76">
<font color="#ffffff" face="arial,helvetica,sanserif">
<a name="bsh_scripting"><strong>16.8 BeanShell scripting</strong></a>
</font>
</td></tr>
<tr><td>
<blockquote>
<table border="0" cellspacing="0" cellpadding="2" width="100%">
<tr><td bgcolor="#828DA6">
<font color="#ffffff" face="arial,helvetica,sanserif">
<a name="bsh_overview"><strong>16.8.1 Overview</strong></a>
</font>
</td></tr>
<tr><td>
<blockquote>
<p>
Each BeanShell test element has its own copy of the interpreter (for each thread).
If the test element is repeatedly called, e.g. within a loop, then the interpreter is retained
between invocations unless the "Reset bsh.Interpreter before each call" option is selected.
</p>
<p>
Some long-running tests may cause the interpreter to use lots of memory; if this is the case try using the reset option.
</p>
<p>
You can test BeanShell scripts outside JMeter by using the command-line interpreter:
<pre>
$ java -cp bsh-xxx.jar[;other jars as needed] bsh.Interperter file.bsh [parameters]
or
$ java -cp bsh-xxx.jar bsh.Interperter
bsh% source("file.bsh");
bsh% exit(); // or use EOF key (e.g. ^Z or ^D)
</pre>
</p>
</blockquote>
</td></tr>
<tr><td><br></td></tr>
</table>
<table border="0" cellspacing="0" cellpadding="2" width="100%">
<tr><td bgcolor="#828DA6">
<font color="#ffffff" face="arial,helvetica,sanserif">
<a name="bsh_variables"><strong>16.8.2 Sharing Variables</strong></a>
</font>
</td></tr>
<tr><td>
<blockquote>
<p>
Variables can be defined in startup (initialisation) scripts.
These will be retained across invocations of the test element, unless the reset option is used.\
</p>
<p>
Scripts can also access JMeter variables using the get() and put() methods of the "vars" variable,
for example:
<code>
vars.get("HOST"); vars.put("MSG","Successful");
</code>
.
The get() and put() methods only support variables with String values,
but there are also getObject() and putObject() methods which can be used for arbitrary objects.
JMeter variables are local to a thread, but can be used by all test elements (not just Beanshell).
</p>
<p>
If you need to share variables between threads, then JMeter properties can be used:
<pre>
import org.apache.jmeter.util.JMeterUtils;
String value=JMeterUtils.getPropDefault("name","");
JMeterUtils.setProperty("name", "value");
</pre>
The sample .bshrc files contain sample definitions of getprop() and setprop() methods.
</p>
<p>
Another possible method of sharing variables is to use the "bsh.shared" shared namespace.
For example:
<pre>
if (bsh.shared.myObj == void){
// not yet defined, so create it:
myObj=new AnyObject();
}
bsh.shared.myObj.process();
</pre>
Rather than creating the object in the test element, it can be created in the startup file
defined by the JMeter property "beanshell.init.file". This is only processed once.
</p>
</blockquote>
</td></tr>
<tr><td><br></td></tr>
</table>
</blockquote>
</p>
</td></tr>
<tr><td><br></td></tr>
</table>
<br>
<table>
<tr>
<td bgcolor="#525D76">
<div align="right"><a href="index.html"><font size=-1 color="#ffffff" face="arial,helvetica,sanserif">Index</font></a></div>
</td>
<td bgcolor="#525D76">
<div align="right"><a href="boss.html"><font size=-1 color="#ffffff" face="arial,helvetica,sanserif">Next</font></a></div>
</td>
<td bgcolor="#525D76">
<div align="right"><a href="remote-test.html"><font size=-1 color="#ffffff" face="arial,helvetica,sanserif">Prev</font></a></div>
</td>
</tr>
</table>
</td>
</tr>
<tr><td>
<hr noshade size="1">
</td></tr>
<tr>
<td>
<table width=100%>
<tr>
<td>
<font color="#525D76" size="-1"><em>
Copyright © 1999-2008, Apache Software Foundation
</em></font>
</td>
<td align="right">
<font color="#525D76" size="-1"><em>
Updated: $Date: 2008-05-16 16:02:40 +0100 (Fri, 16 May 2008) $
</em></font>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -