1. 在IDEA中新建一个maven工程。
      创建Jmeter中使用的jar包中的工具类方法 - 图1
      创建Jmeter中使用的jar包中的工具类方法 - 图2
      创建Jmeter中使用的jar包中的工具类方法 - 图3
      创建Jmeter中使用的jar包中的工具类方法 - 图4
      2. 编写工具类方法,如加密算法。此处以加法为例。
      package DemoTest;

    public class DemoClass
    {
    public int add(int a, int b){
    return a+b;
    }

    }
    3. 在maven中配置将依赖包打包至某固定位置。



    org.apache.maven.plugins
    maven-compiler-plugin

    1.8
    1.8

    1. <plugin><br /> <groupId>org.apache.maven.plugins</groupId><br /> <artifactId>maven-dependency-plugin</artifactId><br /> <version>2.1</version><br /> <executions><br /> <execution><br /> <id>copy-dependencies</id><br /> <phase>package</phase><br /> <goals><br /> <goal>copy-dependencies</goal><br /> </goals><br /> <configuration><br /> <outputDirectory>${basedir}/target/lib</outputDirectory><br /> </configuration><br /> </execution><br /> </executions><br /> </plugin><br /> </plugins><br /></build><br />以上3步可以直接获取<br />4. 执行mvn package. 生成自己写的jar包和所依赖的jar包。<br />![](https://cdn.nlark.com/yuque/0/2022/png/306561/1647067490851-72c429eb-4e7c-4f5c-8e44-5e1f6df5b939.png#clientId=udd98c8ad-1b44-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=ue66e857d&margin=%5Bobject%20Object%5D&originHeight=370&originWidth=300&originalType=url&ratio=1&rotation=0&showTitle=false&status=done&style=none&taskId=u38005f9e-5337-4938-a9d8-fad047b3e04&title=)<br />5. 将自己项目的jar包和lib目录下的所有jar包拷贝到jmeter的lib目录下。(也可能没有第三方依赖包,根据你的项目而定)<br />![](https://cdn.nlark.com/yuque/0/2022/png/306561/1647067491443-49680511-bec1-475f-9cba-3b48562f3b15.png#clientId=udd98c8ad-1b44-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=u615db0bd&margin=%5Bobject%20Object%5D&originHeight=156&originWidth=339&originalType=url&ratio=1&rotation=0&showTitle=false&status=done&style=none&taskId=u78cd6784-308d-4c36-99ca-fca53ce7f17&title=)<br />6. 到jmeter官网下载最新的jmeter包,解压。将第5步的jar包粘贴至jmeter的lib目录下。<br />https://jmeter.apache.org/download_jmeter.cgi<br />![](https://cdn.nlark.com/yuque/0/2022/png/306561/1647067491556-76004211-3681-494c-a517-01e00898db48.png#clientId=udd98c8ad-1b44-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=u7fe8dd77&margin=%5Bobject%20Object%5D&originHeight=239&originWidth=258&originalType=url&ratio=1&rotation=0&showTitle=false&status=done&style=none&taskId=uf8db80bd-87ce-484c-8c23-4b39233f83a&title=)<br />7. 双击jmeter的bin目录下的jmeter.bat, 打开jmeter。<br />8. 在测试计划上右击,添加一个线程组。<br />![](https://cdn.nlark.com/yuque/0/2022/png/306561/1647067491693-98736c94-0097-4e10-a1bd-8afd09a02718.png#clientId=udd98c8ad-1b44-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=uce77dc8e&margin=%5Bobject%20Object%5D&originHeight=365&originWidth=560&originalType=url&ratio=1&rotation=0&showTitle=false&status=done&style=none&taskId=u9e4e6ec6-5e58-4ec9-93bb-805eb0dbf40&title=)<br />9. 在线程组上右击,添加一个Bean Shell Sampler.<br />![](https://cdn.nlark.com/yuque/0/2022/png/306561/1647067491651-69086fdd-a504-4086-a396-f49d0f8facac.png#clientId=udd98c8ad-1b44-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=u0f7507c5&margin=%5Bobject%20Object%5D&originHeight=681&originWidth=560&originalType=url&ratio=1&rotation=0&showTitle=false&status=done&style=none&taskId=u21219ca4-412e-48bb-b0f8-2da4223c6f1&title=)<br />10. 在脚本框中写入以下代码,然后保存执行。<br />import DemoTest.DemoClass;<br />int sum = new DemoClass().add(2,6);<br />vars.put("sum",sum.toString());<br />_log.info("sum = " + sum);_<br />![](https://cdn.nlark.com/yuque/0/2022/png/306561/1647067491967-fb68585f-eb8e-4df8-bae7-7e4cf38074ce.png#clientId=udd98c8ad-1b44-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=u97fcdeed&margin=%5Bobject%20Object%5D&originHeight=591&originWidth=904&originalType=url&ratio=1&rotation=0&showTitle=false&status=done&style=none&taskId=uf028d088-256c-4fe1-bed0-98f5b7ffa73&title=)<br />11. 也可以在Bean Shell PreProcessor中写java代码<br />首先添加HTTP请求<br />![](https://cdn.nlark.com/yuque/0/2022/png/306561/1647067492105-64b53e9b-2965-4809-884d-3fa1624164cf.png#clientId=udd98c8ad-1b44-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=u221e400b&margin=%5Bobject%20Object%5D&originHeight=666&originWidth=556&originalType=url&ratio=1&rotation=0&showTitle=false&status=done&style=none&taskId=u9d59b7b4-421a-4c6e-899f-4f2d074642f&title=)<br />添加Bean Shell PreProcessor<br />![](https://cdn.nlark.com/yuque/0/2022/png/306561/1647067492192-12d8d34c-09c7-424b-9c16-8625010b5f35.png#clientId=udd98c8ad-1b44-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=u56972b24&margin=%5Bobject%20Object%5D&originHeight=458&originWidth=548&originalType=url&ratio=1&rotation=0&showTitle=false&status=done&style=none&taskId=u7ce54f2d-6489-4c36-9f7d-7495edde6d2&title=)<br />![](https://cdn.nlark.com/yuque/0/2022/png/306561/1647067492380-39839ce8-554d-4fab-b5e1-3e3809d0160a.png#clientId=udd98c8ad-1b44-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=u123c4043&margin=%5Bobject%20Object%5D&originHeight=559&originWidth=849&originalType=url&ratio=1&rotation=0&showTitle=false&status=done&style=none&taskId=u50e397e9-fd7b-49a0-83c6-f390df91ca0&title=)<br />![](https://cdn.nlark.com/yuque/0/2022/png/306561/1647067492706-0c6b192e-530e-42ae-ac9c-3b9ad22f871a.png#clientId=udd98c8ad-1b44-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=u360814ac&margin=%5Bobject%20Object%5D&originHeight=559&originWidth=849&originalType=url&ratio=1&rotation=0&showTitle=false&status=done&style=none&taskId=u8f201d39-daaf-4735-aa66-95bef7eb0c6&title=)<br />![](https://cdn.nlark.com/yuque/0/2022/png/306561/1647067492822-bfb7e592-a7f9-4778-8e1d-b6b502f680f8.png#clientId=udd98c8ad-1b44-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=u79b01a05&margin=%5Bobject%20Object%5D&originHeight=559&originWidth=849&originalType=url&ratio=1&rotation=0&showTitle=false&status=done&style=none&taskId=u36448976-6c7e-4271-8061-69df4ba9bb1&title=)