- 浏览: 118512 次
- 性别:
- 来自: 杭州
文章分类
最新评论
编写获取本地和远程图片流的Jmeter自定义函数
代码如下:
将生成的jar包放置到Jmeter的lib\ext目录下。重启Jmeter后再运行--函数助手模块能够看到自定义的两个函数。
使用时可以用如下方式引用:
${__ImageRemoteBase64(${photoPath},)
其中${photoPath}通过CSV Data Set Config参数化得到的远端图片路径。
心得:
1、httpclient 可以自动对URL字符串中的中文编码进行转码,减少了URL包含中文导致的图片获取不到问题。
2、Jmeter自定义函数的包名必须要包含“functions”,否则Jmeter发现不了该自定义函数。建议包名:package org.apache.functions
代码如下:
package org.apache.functions; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.Collection; import java.util.LinkedList; import java.util.List; import org.apache.jmeter.engine.util.CompoundVariable; import org.apache.jmeter.functions.AbstractFunction; import org.apache.jmeter.functions.InvalidVariableException; import org.apache.jmeter.samplers.SampleResult; import org.apache.jmeter.samplers.Sampler; import org.apache.commons.codec.binary.Base64; public class ImageLocalBase64 extends AbstractFunction { // 显示的参数名字 private static final List<String> desc = new LinkedList(); // 显示的函数名字 private static final String KEY = "__ImageLocalBase64"; // 参数值 private Object[] values; static { desc.add("local path for image"); desc.add("Name of variable in which to store the result (optional)"); } public String execute(SampleResult paramSampleResult, Sampler paramSampler) throws InvalidVariableException { String s = null; try { String url = ((CompoundVariable) this.values[0]).execute(); url = new String(url.getBytes("UTF-8"), "UTF-8"); InputStream inStream = new FileInputStream(url); byte[] b = ImageUtil.readInputStream(inStream); byte[] bs = Base64.encodeBase64(b); s = new String(bs); return s; } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } return s; } // 获取参数值 public synchronized void setParameters(Collection<CompoundVariable> paramCollection) throws InvalidVariableException { // 检查参数,我也搞不懂是毛线意思,NND checkMinParameterCount(paramCollection, 1); this.values = paramCollection.toArray(); } // 返回函数名字 public String getReferenceKey() { return KEY; } // 返回参数名字 public List<String> getArgumentDesc() { return desc; } }
package org.apache.functions; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.util.Collection; import java.util.LinkedList; import java.util.List; import org.apache.jmeter.engine.util.CompoundVariable; import org.apache.jmeter.functions.AbstractFunction; import org.apache.jmeter.functions.InvalidVariableException; import org.apache.jmeter.samplers.SampleResult; import org.apache.jmeter.samplers.Sampler; import org.apache.commons.codec.binary.Base64; public class ImageRemoteBase64 extends AbstractFunction { //显示的参数名字 private static final List<String> desc = new LinkedList(); //显示的函数名字 private static final String KEY = "__ImageRemoteBase64"; //参数值 private Object[] values; static { desc.add("URL for remote Image"); desc.add("Name of variable in which to store the result (optional)"); } public String execute(SampleResult paramSampleResult, Sampler paramSampler) throws InvalidVariableException { String s = null; try { String url = ((CompoundVariable)this.values[0]).execute(); s = ImageUtil.getImageFromNetByURL1(url); return s; } catch (Exception e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } return s; } //获取参数值 public synchronized void setParameters(Collection<CompoundVariable> paramCollection) throws InvalidVariableException { //检查参数,我也搞不懂是毛线意思,NND checkMinParameterCount(paramCollection, 1); this.values = paramCollection.toArray(); } //返回函数名字 public String getReferenceKey() { return KEY; } //返回参数名字 public List<String> getArgumentDesc() { return desc; } /** * 从输入流中获取数据 * * @param inStream * 输入流 * @return * @throws Exception */ public static byte[] readInputStream(InputStream inStream) throws Exception { ByteArrayOutputStream outStream = new ByteArrayOutputStream(); byte[] buffer = new byte[inStream.available()]; int len = 0; while ((len = inStream.read(buffer)) != -1) { outStream.write(buffer, 0, len); } inStream.close(); return outStream.toByteArray(); } }
package org.apache.functions; import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import org.apache.commons.codec.binary.Base64; import org.apache.commons.codec.binary.StringUtils; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; public class ImageUtil { /** * 根据地址获得数据的字节流 * * @param strUrl * 网络连接地址 * @return */ public static String getImageFromNetByURL1(String strUrl) { HttpClient httpclient = new DefaultHttpClient(); HttpGet httpget = new HttpGet(strUrl); HttpResponse response; String s = null; try { response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); if (entity != null) { InputStream instream = entity.getContent(); byte[] b = readInputStream(instream); byte[] bs = Base64.encodeBase64(b); s = new String(bs); return s; } } catch (ClientProtocolException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } catch (IOException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } catch (Exception e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } return s; } /** * 根据地址获得数据的字节流 * * @param strUrl * 本地连接地址 * @return */ public static byte[] getImageFromLocalByUrl(String strUrl) { try { File imageFile = new File(strUrl); InputStream inStream = new FileInputStream(imageFile); byte[] btImg = readInputStream(inStream);// 得到图片的二进制数据 return btImg; } catch (Exception e) { e.printStackTrace(); } return null; } /** * 从输入流中获取数据 * * @param inStream * 输入流 * @return * @throws Exception */ public static byte[] readInputStream(InputStream inStream) throws Exception { ByteArrayOutputStream outStream = new ByteArrayOutputStream(); byte[] buffer = new byte[inStream.available()]; int len = 0; while ((len = inStream.read(buffer)) != -1) { outStream.write(buffer, 0, len); } inStream.close(); return outStream.toByteArray(); } /** * 将图片写入到磁盘 * * @param img * 图片数据流 * @param fileName * 文件保存时的名称 */ public static void writeImageToDisk(byte[] img, String zipImageUrl) { try { File file = new File(zipImageUrl); FileOutputStream fops = new FileOutputStream(file); fops.write(img); fops.flush(); fops.close(); System.out.println("图片已经写入" + zipImageUrl); } catch (Exception e) { e.printStackTrace(); } } }
将生成的jar包放置到Jmeter的lib\ext目录下。重启Jmeter后再运行--函数助手模块能够看到自定义的两个函数。
使用时可以用如下方式引用:
${__ImageRemoteBase64(${photoPath},)
其中${photoPath}通过CSV Data Set Config参数化得到的远端图片路径。
心得:
1、httpclient 可以自动对URL字符串中的中文编码进行转码,减少了URL包含中文导致的图片获取不到问题。
2、Jmeter自定义函数的包名必须要包含“functions”,否则Jmeter发现不了该自定义函数。建议包名:package org.apache.functions
发表评论
-
软件测试好帖子整理
2014-06-06 22:28 0junit 4 使用 http://www.ibm.com ... -
软件测试分类
2014-06-05 11:06 7451、按是否查看程序内部结构分为: (1)黑盒测试(black ... -
NetPerf中Netserver在windows7windows2008下启动失败
2014-03-13 15:53 962netperf 工具的netserver在win7和win ... -
ubuntu离线安装包制作
2013-11-28 20:20 0当重装系统或者安装虚拟机时没有必要重新在网上下载,可以按以下 ... -
Eclipse+Junit4+ant构建单元测试框架
2013-11-26 15:25 3366目录: Junit4 使用 Ant结合Junit4 ... -
【转】场景法设计测试用例
2013-11-20 22:02 5633在面向对象的软件开 ... -
HTTP常见错误(转)
2011-03-18 08:58 724HTTP 400 - 请求无效 HTTP 401 - 未被授权 ... -
QTP英文问答
2011-03-08 19:43 843第一部分:QTP 1. What are the Featu ... -
Netstat命令详解
2011-02-28 11:15 1078Netstat用于显示与IP、TCP、UDP和ICM ... -
Linux常用命令大全
2011-02-24 22:40 770Linux常用命令大全 系统信息 arc ... -
测试常用教程整理
2011-02-24 13:55 7741、SQL教程: http://www.w3school.c ... -
使用QTP对报表中的webtable内容进行正确性验证
2011-02-12 13:41 1022网络转载:http://developer.51cto.com ... -
QTP之excel操作函数整理
2010-12-02 09:09 4537******************************* ... -
32Windows 上Oracle突破1.7G内存限制(一)
2010-11-11 18:12 3463系统环境:Windows 2003 32位CPU 8核内存 ... -
性能测试概述
2009-10-12 19:26 0性能测试51Testing软件测 ... -
菜鸟进阶--QTP 10破解笔记
2009-09-13 22:01 7178破解步骤: 1.安装qtp, ... -
windows系统中设置进程为开机启动
2009-07-30 10:58 1175windows系统中设置进程为开机启动 可以通过控制面 ... -
使用计划任务 备份Mysql数据库脚本
2009-07-30 10:49 2262使用计划任务 备份Mysql数据库脚本 ...
相关推荐
主要介绍了Jmeter自定义函数base64加密实现过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
jar包放在jmeter-lib-ext目录下,打开jmeter在函数助手中可以看到。CarId.jar为车牌号生成,需要输入(省会简称(京津冀),市级代码(ABCD));
jmeter参数化5种方式脚本:用户自定义、函数、csv、数据库、正则提取。还包含post请求,get请求
(二)Jmeter在请求中使用自定义变量 (三)Jmeter读取CSV文件中的内容作为请求参数 (四)Jmeter参数化函数助手/使用随机函数/随机参数 (五)Jmeter串联请求 / 使用上一个请求的结果作为下一个请求的参数 (六)...
jmeter函数自定义
jmeter_extra_tool是一个专为Jmeter服务的常用工具类和自定义函数集。为日常的测试工作带来方便。开源地址: https://coding.net/u/tomoya_chen/p/jmeter_extra_tool/git
jmeter_extra_tool是一个专为Jmeter服务的常用工具类和自定义函数集。为日常的测试工作带来方便。开源地址: https://coding.net/u/tomoya_chen/p/jmeter_extra_tool-v1.0/git
jmeter函数,自定义封装,不错的一个jar包
14. 配置元件之用户自定义的变量? ?15. 配置元件之CSV Data Set Config参数化? ?16. 函数助手参数化??17. HTTP请求之xml格式数据请求??18. HTTP请求之json格式数据请求? ?19. 配置元件之HTTP信息头管理器??20. 配置...
支持类似jmeter的函数助手, 可以通过脚本进行接口录制,辅助编写yaml测试数据文件, 支持数据库断言, 支持分布式,jenkins持续集成 涉及框架和技术栈:requests+pytest+allure+yaml+redis 支持测试报告生成,接口...
本软件可以模拟不同类型的交易报文,可以对交易测试案例进行统一管理,并可以进行简单时间统计和成功率统计。 使用本软件可以减轻传统测试过程中的修改-编译-测试-的循环等待时间,...采用脚本处理,支持自定义函数。
概述 Jenkins共享库用于执行测试 ... 此函数假定我们在/opt/jmeter安装了JMeter的Jenkins代理上运行 一旦配置了所有内容,就可以在Jenkins Pipeline中使用它,如下所示 // Import Dynatrace library -- t