一、下载jython-standalone-2.7.2.jar包放到jmeter安装目录lib下,重启jmeter
二、python安装pymongo
三、在线程组里添加 JSR223 Sampler,注意选择Language
代码 如下:注意鞋面是调试代码,最后往jmeter里放的时候记得在代码最后面加 SampleResult.setResponseData(res) 才能在响应中看到
import sys import json sys.path.append("D:\Python\Lib\site-packages") from pymongo import MongoClient client = MongoClient("IP",27017)#MongoDb数据库的IP地址 db = client.testdb#testdb是数据库的集合名称 db.authenticate("username", "password")#连接数据库的用户名和密码 res = db.test_user.find_one({"_id" : "90000036598"})#test_user集合名称 res = str(json.dumps(res, ensure_ascii=True))#编码后转换成str
四、在jmeter中添加后置处理器,用来处理中文显示
代码如下:
prev.setDataEncoding("UTF-8"); String s=new String(prev.getResponseData(),"UTF-8"); char aChar; int len= s.length(); StringBuffer outBuffer=new StringBuffer(len); for(int x =0; x <len;){ aChar= s.charAt(x++); if(aChar=='\\'){ aChar= s.charAt(x++); if(aChar=='u'){ int value =0; for(int i=0;i<4;i++){ aChar= s.charAt(x++); switch(aChar){ case'0': case'1': case'2': case'3': case'4': case'5': case'6': case'7': case'8': case'9': value=(value <<4)+aChar-'0'; break; case'a': case'b': case'c': case'd': case'e': case'f': value=(value <<4)+10+aChar-'a'; break; case'A': case'B': case'C': case'D': case'E': case'F': value=(value <<4)+10+aChar-'A'; break; default: throw new IllegalArgumentException( "Malformed \\uxxxx encoding.");}} outBuffer.append((char) value);}else{ if(aChar=='t') aChar='\t'; else if(aChar=='r') aChar='\r'; else if(aChar=='n') aChar='\n'; else if(aChar=='f') aChar='\f'; outBuffer.append(aChar);}}else outBuffer.append(aChar);} prev.setResponseData(outBuffer.toString());
五、添加查看结果树