由于项目需要,对mongoDB进行压测。
我有两种思路去搞,分别如下:
1.使用Python操作MongoDB,然后使用Jmeter调用Python压测;
2.使用Shell操作MongoDB,然后使用Jmeter调用Shell压测;
下面简要写一下部分要点:
1.shell 操作 mongoDB:
sql="db.c_device.find(
{
enterpriseId:"ab808081623e258801624136fdad001c",
$or:[
{deviceDisplayName:/1356/i},
{deviceSn:/1356/i},
{number:/1356/i},
{'departments.deptName':/1356/i}
]
}
).sort({joinTime:-1}).skip(70070).limit(10)"
time echo $sql|mongo -u'haochuang' --authenticationDatabase "haochuang" --host '127.0.0.1' -p'haochuang123' 'haochuang'
2.python操作 mongoDB:
#!/usr/bin/env python
# encoding: utf-8
'''
@author: haochuang
@license: (C) Copyright 2018-2019, MIT Corporation Limited.
@contact: nianhuaiju@qq.com
@software: api test
@file: python_mongodb_ok.py
@time: 2019/9/12 11:22
@desc: python-mongodb
'''
import pymongo
from pymongo import MongoClient
'''
python mongodb test
'''
host = '127.0.0.1'
username = 'haotest'
password = 'haotest123'
port = '27017'
db = 'haotest'
mongo_url = 'mongodb://{0}:{1}@{2}:{3}/?authSource={4}&authMechanism=SCRAM-SHA-1'.format(username, password, host, port,db)
# 进行连接
client = MongoClient(mongo_url)
# 指定数据库
db = client.haotest
# 指定集合
collection = db.hao_test
print "EXEC START..."
# 获取时间
# sql_get_time = collection.find({"enterpriseId":"aasdaasdad321313123sdfsfsdfsdfs","$or":"[{deviceDisplayName:/1356/i},{deviceSn:/1356/i},{number:/1356/i},{'departments.deptName':/1356/i}]"}).sort({"joinTime":"-1"}).skip(70070).limit(10);
sql_get_time = collection.find({"enterpriseId":"wqweqeqeqsf1231313dsferwrew",'$or':[{"deviceDisplayName":"/123/i"},{"deviceSn":"/1356/i"},{"number":"/1356/i"},{"departments.deptName":"/1356/i"}]}).count();
print "sql_get_time:" + str(sql_get_time)
# -- 统计enterpriseId中的device数量
sql_result = collection.find({"enterpriseId":"a2234dsffsdfasdasda23131"}).count();
print "enterpriseId_count:" + str(sql_result)
queue_result=sql_result
print queue_result
print "EXEC END..."
# print(next(sql))
client.close()
也可以使用 BeanShell,在里面用JAVA代码调用shell命令
log.error("开始");
String[] cmd = new String[]{"/bin/sh", "-c", "pwd"};
Process ps = Runtime.getRuntime().exec(cmd);
BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream()));
StringBuffer sb = new StringBuffer();
String line;
while ( (line = br.readLine()) != null ) {
sb.append(line).append("\n");
}
String result = sb.toString();
log.error("结果:"+result);
补充,python调用JMeter
import os
import sys
currpath = os.path.dirname(os.path.realpath(__file__)) # 当前文件目录
sys.path.insert(0, currpath)
# # print(currpath)
JmxTemlFileName = r'/Users/admin/Documents/jmeter/stu_tea_test.jmx' # 要执行的文件
#
JMETER_Home = r'''"/Users/admin/Downloads/apache-jmeter-5.1.1/bin/jmeter.bat"''' # jmeter执行文件
Jmeter_Out = currpath + '/result.txt'
def runCmd(cmd):
print(f"command={cmd}")
os.system(cmd)
########### 下面使用subprocess.Popen来调用shell############
# res = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# print('sys',sys.path)
# stdoutinfo,stderrinfo = res.communicate()
# print(f"stderrinfo={stderrinfo}")
# print(f"stdoutinfo={stdoutinfo}")
# print("returncode={0}".format(res.returncode))
exec_str = f"jmeter -n -t {JmxTemlFileName} -l {Jmeter_Out}"
if __name__ == '__main__':
runCmd(exec_str)
Jmeter执行python脚本函数方法1
如下摘自:https://blog.csdn.net/q13554515812/article/details/85252149
一、下载地址
插件百度网盘下载地址:https://pan.baidu.com/s/1SvJjyThsXYryXuEEg9rm3g
提取码:q9hd
二、使用说明
1.将下载的 jmeter-functions-execute-python-script-1.0.jar 包放到目录%JMETER_HOME%\lib\ext下
2.启动Jmeter,进入【选项】-【函数助手对话框】找到函数【__ExecutePythonScript】如下图:
参数1:脚本所在路径,示例:G:\测试代码\cloudhua\interfaceTest\pythonScripts\fileBlockScript.py
参数2:parameter1,python脚本接收的第一个参数;(可不填)
参数3:parameter2,python脚本接收的第二个参数;(可不填)
参数4:parameter3,python脚本接收的第三个参数;(可不填)
Jmeter执行Python脚本2:
jmeter 可以通过Jython 执:行python代码
1、下载Jython jar包:http://www.jython.org/downloads.html
2、把下载的Jython 的jar包放到 jmeter的lib文件夹。重启jmeter
3、线程组中添加 JSR223 Sampler
4、设置JSR223 Sampler
python代码
#获取jmeter 参数
param =vars.get("param")
print "hello"
#设置响应信息
SampleResult.setResponseData("message");
SampleResult.setResponseCode("502")
SampleResult.setSuccessful(False)
5、执行,由于代码中标记502 和执行失败,执行结果如下
想要熟悉更多mongoDB相关命令,请参考这篇文章:https://www.runoob.com/mongodb/mongodb-query.html
参考了如下几篇文章:
Python操作mongodb的9个步骤:https://www.jb51.net/article/141395.htm
Python连接MongoDB操作:https://www.yiibai.com/mongodb/mongodb_python.html
使用pymongo连接mongodb时报错:pymongo.errors.OperationFailure: not authorized:https://www.cnblogs.com/benben-wu/p/10312689.html
待续...
来源:oschina
链接:https://my.oschina.net/u/4314526/blog/4065974