![7dbf86b80de2f89f7ed3dbf5e7e01a9c.gif](https://i-blog.csdnimg.cn/blog_migrate/065947ef7969be83e64b2de0a570aab3.gif)
谢谢打开这篇文章的每个你
关注我们 点击右上角 ··· 设为星标
![ae46d36ee32be91615762568bd04e820.gif](https://i-blog.csdnimg.cn/blog_migrate/5351bcc568b1275f8ee318901911da97.gif)
jmeter的BeanShell Sampler,可以直接引用java代码,有下面3种方式:
1、直接输入java代码
2、导入java文件
3、导入class文件
BeanShell脚本访问变量主要通过vars和props去访问,常用主要方法如下:
vars.get(变量名):获取定义变量对应的值,含BeanShell脚本生成变量;
vars.put(变量名,变量值):生成新的变量或修改已存在的变量;
props.get(变量名):获取jmeter.properties中定义变量对应值;
一
直接输入Java代码
1、在testplan中添加变量值
![b18f2dcf54e4112d235e68cbc508d694.png](https://i-blog.csdnimg.cn/blog_migrate/db65ff707eb3fd0caf4731789b0efd4c.jpeg)
添加BeanShell Sampler,注意在BeanShell 前后必须加一个Debug Sampler,这要方便调试看结果,添加的Debug Sampler不需要任何操作。
![7aaec1d024e88e936d8e38a0447e15a9.png](https://i-blog.csdnimg.cn/blog_migrate/f9c8c527b6468c928c78cdbb10be103e.jpeg)
运行查看执行的结果
![ed741f8d78fb8b0372640bf772074774.png](https://i-blog.csdnimg.cn/blog_migrate/ddad37bea7f5dfc9bae11168ce99232e.jpeg)
2、在jmeter中使用调用方法
![cdd07a5c0184c276d1e910786e2e9d83.png](https://i-blog.csdnimg.cn/blog_migrate/c89697c67861416ec73829bfbbbfcc34.jpeg)
查看结果
![77ad16dae3b7737b1b598d097b3c6384.png](https://i-blog.csdnimg.cn/blog_migrate/46095e0a73e3b6dcb6eb3f6766ffed8f.jpeg)
二
在BeanShell中引入Java文件
写一个Java文件保存到桌面
![219feb506c996d656f28bd7aec2f164b.png](https://i-blog.csdnimg.cn/blog_migrate/65ec448d35343c78f2a218fe34c7e896.png)
在BeanShell中引入该Java文件
![a32d0678ddaeaa7affa4d683835af17d.png](https://i-blog.csdnimg.cn/blog_migrate/3e0a5f372fd8b7763393f62c69124e5e.png)
查看执行的结果
![3e83ecf4ebd2c8fcdf52823eccf4213a.png](https://i-blog.csdnimg.cn/blog_migrate/b3264a98022f8063d18d286ab3dc605b.jpeg)
三
在BeanShell文件中引入class文件
![4ee1c67959046b12a878dc11b3119a51.png](https://i-blog.csdnimg.cn/blog_migrate/91690d7b16311a5d36c98522ac9e3fd1.jpeg)
查看生成的结果
![adb804123531fb67479b9829db2cbee2.png](https://i-blog.csdnimg.cn/blog_migrate/3f4dd85ecd7f3e8bec9c66c691784bd9.jpeg)
BeanShell实现加密解密功能
一、在IDEA中写好加密的脚本
![eb7745d4790e989ad1770875c1db5025.png](https://i-blog.csdnimg.cn/blog_migrate/943fc1485a201321d6181d436a24d278.jpeg)
二、然后将整个包文件导出,生成jar包
![bbbfd493a67a94b5ea2cd7e1b21d29b4.png](https://i-blog.csdnimg.cn/blog_migrate/87194f8e482963532f6ae323d41445b8.jpeg)
三、将jar包文件放到jmeter的lib/ext目录下
![dcb0e8d30373583d1870000012c7afb9.png](https://i-blog.csdnimg.cn/blog_migrate/d7209cd1bbc4d77839aa24c890fc26d1.jpeg)
然后在jmeter的BeanShell中引入该类,调用其中的加密方法
![de7801f85340f7210f1eddc1d28ee791.png](https://i-blog.csdnimg.cn/blog_migrate/ec8172e7b424cc1137d87fc5774b0a09.jpeg)
查看调试结果
![958f421bbd3715a181fe5bcf074b5aa3.png](https://i-blog.csdnimg.cn/blog_migrate/d61747707b1a6e2148813f97d480071a.jpeg)
![f6dc8b35c5b1e645a55a95e5629d509f.png](https://i-blog.csdnimg.cn/blog_migrate/a28e0469b8ccc95132036c97ae634fe4.jpeg)
把加密后的数据保存到mdm这个变量里,供在jmeter里使用。