jmeter 中jsr232 listener例子

Life is short , play more!
本文来自lihao's Blog,转载请注明。

目的,自己写listener是为了将测试结果格式化输出到指定的地方,便于后续阅读和整理。
此处只是简单的格式化到日志中,便于进一步整理,留有测试记录使用。

如何使用, 在jmeter中添加jsr223 listener, 语言选择javascript类型。 将如下代码加入到脚本框中。

当每次执行测试后, 打开右上角的日志(黄色的三角叹号),则可以看到日志的打印。便于上下滚动查看测试结果,比treeview 批量浏览数据时更方便。

var pre=ctx.getPreviousResult();

var result=prev.isSuccessful();

//var sampler= ctx.getCurrentSampler();

var samplerType=sampler.getClass().toString();

//output test result

     log.info("=================="+sampler.getName()+"==================");

     log.info("Comments: "+sampler.getComment());
     log.info("Request Content: "+pre.getSamplerData().replace(/\n/g, "; "));
     if(samplerType.match(/HTTPSampler/))
     {

		
		if(sampler.getMethod().match(/POST/)){
			log.info("MultipartPost: "+sampler.getUseMultipartForPost().toString());	
			if(!sampler.getUseMultipartForPost()){
					
			}

		}


     }
   
     log.info("Response: "+pre.getResponseDataAsString());
     
	log.info("************************************");
if(!result){
	log.info("***测试结果: !结果错误,请查证!×***");
}
else{
	log.info("********测试结果: 结果正确。✓ ******");
}
	log.info("************************************");
    log.info("");

 

如下是输出的日志示例。 还可以统计出错误的用例以及执行的用例等。自行添加即可。

2016/12/12 19:04:39 INFO  - jmeter.visualizers.JSR223Listener: ==================3-1 login================== 
2016/12/12 19:04:39 INFO  - jmeter.visualizers.JSR223Listener: Comments:  
2016/12/12 19:04:39 INFO  - jmeter.visualizers.JSR223Listener: Request Content: GET http://172.16.2.111/hissummer-admin/login/login?username=nighteblis#gmail.com&p=PkP8GHibpOtU4gw%2Fr0kIHsVqAyejF%2BvsFtO926HLLCl8I12xr36%2Bm4b6s0eYrlhCw1vA0S9miDx8XIaeDS%2BjaDjTL6%2BN7%2BHLw%2FEekwodAcFVxK4w374gc0Kv%2B1yRxcAiHMK6W1aGFRowFcYtIZeiPc9W8rn4S17aoKsx9%2Fjwz3w%3D; ; [no cookies];  
2016/12/12 19:04:39 INFO  - jmeter.visualizers.JSR223Listener: Response: {"data":{"auth":[{"code":"auth_manager","displayName":"权限管理"},{"actionUrl":"/authority/usr/list","code":"auth_usr_list","displayName":"用户列表"},{"actionUrl":"/authority/usr/add","code":"auth_usr_add","displayName":"添加账户"},{"actionUrl":"/authority/read/list","code":"auth_read_list","displayName":"获取读权限列表"},{"actionUrl":"/authority/read/save","code":"auth_read_save","displayName":"保存读权限列表"},{"actionUrl":"/authority/write/list","code":"auth_write_list","displayName":"获取写权限列表"},{"actionUrl":"/authority/write/save","code":"auth_write_save","displayName":"保存写权限"}],"isAdmin":false,"isSupperAdmin":true,"passwordNeedReset":false,"realName":"系统管理员"},"message":"","status":0} 
2016/12/12 19:04:39 INFO  - jmeter.visualizers.JSR223Listener: ************************************ 
2016/12/12 19:04:39 INFO  - jmeter.visualizers.JSR223Listener: ********测试结果: 结果正确。✓ ****** 
2016/12/12 19:04:39 INFO  - jmeter.visualizers.JSR223Listener: ************************************ 
2016/12/12 19:04:39 INFO  - jmeter.visualizers.JSR223Listener:  
2016/12/12 19:04:39 INFO  - jmeter.visualizers.JSR223Listener: ==================12 /restaurant/uploadImg2CRM================== 
2016/12/12 19:04:39 INFO  - jmeter.visualizers.JSR223Listener: Comments:  
2016/12/12 19:04:39 INFO  - jmeter.visualizers.JSR223Listener: Request Content: POST http://172.16.2.111/hissummer-admin/shop/restaurant/uploadImg2CRM; ; POST data:; -----------------------------7d159c1302d0y0
; Content-Disposition: form-data; name="shopType"
; Content-Type: text/plain; charset=UTF-8
; Content-Transfer-Encoding: 8bit
; 
; RESTAURANT
; -----------------------------7d159c1302d0y0
; Content-Disposition: form-data; name="file"; filename="why.jpg"
; Content-Type: image/jpeg
; Content-Transfer-Encoding: binary
; 
; <actual file content, not shown here>
; -----------------------------7d159c1302d0y0--
; ; ; Cookie Data:; JSESSIONID=8C05DF253ADBD1DA8D578F47EBF30405;  
2016/12/12 19:04:39 INFO  - jmeter.visualizers.JSR223Listener: MultipartPost: true 
2016/12/12 19:04:39 INFO  - jmeter.visualizers.JSR223Listener: Response: {"data":{"imgKey":"ed10b7e6-6e0a-4d0f-840b-f640deae5edc","imgUrl":"http://dev.zs.hissummer.com/res/restaurant/4137b4b8-1109-4a91-826b-bf26026ec95e.jpg","thumbnailImgUrl":"http://dev.zs.hissummer.com/res/restaurant/4137b4b8-1109-4a91-826b-bf26026ec95e.jpg"},"message":"","status":0} 
2016/12/12 19:04:39 INFO  - jmeter.visualizers.JSR223Listener: ************************************ 
2016/12/12 19:04:39 INFO  - jmeter.visualizers.JSR223Listener: ********测试结果: 结果正确。✓ ****** 
2016/12/12 19:04:39 INFO  - jmeter.visualizers.JSR223Listener: ************************************

 


发表评论

电子邮件地址不会被公开。 必填项已用*标注