压测

jmeter压测:html

工具准备:apache-jmeter-5.0.tgzjava

本地启动jmeter: nohup sh apache-jmeter-5.0/bin/jmeter & 修改语言:option(选项) - choose language(选择语言) 安装插件:选项 - Plugins Manager - Available Plugins - 搜索:jpgc - Standard Setgit

机器性能监控:
	下载ServerAgent-2.2.3.zip插件:
		下载地址:https://github.com/undera/perfmon-agent/blob/master/README.md
	修改startAgent.sh文件:
		修改前:java -jar $(dirname $0)/CMDRunner.jar --tool PerfMonAgent "$@"
		修改后:java -jar $(dirname $0)/CMDRunner.jar --tool PerfMonAgent --udp-port 6789 --tcp-port 6789 "$@"
	在被压机器上启动agent:
		nohup sh startAgent.sh &
	关闭agent:
		kill掉如下的进程:
		ps aux | grep startAgent.sh
		ps aux | grep CMDRunner

编写测试计划:github

添加'用户定义的变量':
	target-server-addr 目标服务器
	target-server-port 目标服务器端口
	...

测试计划右键 - 线程(用户) - 线程组
	线程数:指定发起请求的线程数。
	ramp-up period:在多长时间内启动全部的线程。
	循环次数:指定每一个线程循环调用的次数。
		1>指定固定循环的次数。注:若调度器的持续时间设置的太小,则会致使没法达到设定的循环的次数。
		2>不设置固定的次数,即选'永远'。注:并不会永远执行,由于下面还设置了持续时间。
	调度器:
		持续时间:发请求持续的时间。
		启动延迟:延迟发出请求的时间。若不设置,则执行jmeter命令后当即发出请求。


	在线程组上添加:
		取样器-HTTP请求:填写 服务器地址+端口、请求方法+地址+请求参数

			在HTTP请求上添加:配置元件-HTTP信息头管理器,并配置头信息。eg:Content-Type application/json

			在线程组上 或 在HTTP请求上 添加:
				定时器-Constant Throughput Timer(固定吞吐量定时器):
				Target throughput(in samples per minute):目标吞吐量。
					说明:目标吞吐量为10,则这里应该填600。若是"循环次数"或"持续时间"过小,则颇有可能达不到这里设置的吞吐量。
				Calculate Throughput based on(计算吞吐量的方式):

					this thread only:
						each thread will try to maintain the target throughput. The overall throughput will be proportional to the number of active threads.
						将每一个线程的吞吐量设为Target throughput。故:总吞吐量 = Target Throughput * 线程数。

					all active threads:
						the target throughput is divided amongst all the active threads in all Thread Groups. Each thread will delay as needed, based on when it last ran. In this case, each other Thread Group will need a Constant Throughput timer with the same settings.
						将全部活跃线程的总吞吐量设为Target throughput。
						活跃线程指同一时刻同时运行的线程,每一个活跃线程在 上一次运行结束后 等待合理的时间后再次运行。全部的线程组都应该使用相同的配置。

					all active threads in current thread group:
						the target throughput is divided amongst all the active threads in the group. Each thread will delay as needed, based on when it last ran.
						将该线程组中活跃线程的总吞吐量设为Target throughput。当测试计划中只有一个线程组时,该选项和all active threads效果相同。

					all active threads (shared):
						as above(all active threads); each thread is delayed based on when any thread last ran.
						与all active threads相似,不一样的是,每一个活跃线程会在 全部活跃线程上一次运行结束后 等待合理的时间后再次运行。

					all active threads in current thread group (shared) :
						as above(all active threads in current thread group), but each thread is delayed based on when any thread in the group last ran.
						与all active threads in current thread group相似,不一样的是,每一个活跃线程会在 该线程组中全部活跃线程上一次运行结束后 等待合理的时间后再次运行。

			在HTTP请求上添加:
				后置处理器-JSON Extractor(JSON提取器)
					Names of created variables				变量名,后面的HTTP请求能够经过${Variable names}获取该变量的值。
					JSON Path expressions					json path表达式,用于获取response中指定参数的值。eg:{"person":{"name":"jack"}} ==> $.person.name
					Match No.(0 for Random) 				0表明随机,1表明第一个,-1表明全部
					Compute concatenation var(suffix_ALL) 	是否将全部匹配到的值保存下来(格式:变量名=“变量名_ALL”、值=全部匹配到的值以逗号链接的字符串)。
					Default Values 							默认值,通常设为NOT FOUND

			在HTTP请求上添加:
				后置处理器-BeanShell PostProcessor
				1>引入jar包:apache-jmeter-5.0/lib/ext 下添加fastjson的jar包。
				2>编写Script:
					String response = prev.getResponseDataAsString();		// 获取response的内容(json串),prev变量是jmeter提供的。
					JSONObject responseObj = JSON.parseObject(response);	// 使用fastjson来解析json串。

					vars.put("key1", "根据response解析出来key1的value"); 		// 向vars集合中添加key-value对,后面的HTTP请求能够经过${key}来获取集合中的值,vars变量是jmeter提供的。

	在线程组上添加:
		逻辑控制器-若是(if)控制器
			条件:条件表达式。eg:"${count}"=="0"

		逻辑控制器-若是(ForEach)控制器
			输入变量前缀:变量的前缀。eg:level1_ ,则能够遍历全部以"level1_"开头的变量,好比level1_aa、level1_bb等。
			输出变量名称:遍历时使用的变量。
			说明:能够当作 for (String 输出变量名称 : 输入变量前缀)


	在线程组上添加:
		取样器-Debug Sampler
			在Debug Sampler上添加:后置处理器-BeanShell PreProcessor,咱们能够经过BeanShell PreProcessor中的srcipt来初始化一些信息,即向vars集合中添加key-value对。


	说明:Debug Sampler通常都在HTTP请求的前面,即先执行线程组中的Debug Sampler,而后再依次执行HTTP请求。


测试计划右键 - 线程(用户) - jp[@gc](https://my.oschina.net/gccx) - Ultimate Thread Group
	说明:Ultimate Thread Group 是用来实现阶梯式压测的。

	Start Threads Count 	当前行启动的线程数
	Initial Delay, sec 		延时启动当前行的线程。单位秒
	Startup Time, sec 		启动当前行全部线程达峰值所需时间,单位秒
	Hold Load For, sec 		当前行线程达到峰值后持续的时间 单位秒
	Shutdown Time 			中止当前行全部线程所需时间,单位秒

在施压机器上以非GUI的方式来压测: jmeter -n -t jmx文件 -l 结果文件路径 -e -o Web报告保存路径 参数: -n run JMeter in nongui mode -t the jmeter test(.jmx) file to run -l the file to log samples to -e generate report dashboard after load test -o output folder for report dashboard eg: 脚本启动:start.sh 0808-getjob-200-10 # 日期-接口名-tps-压测持续时间 start.sh mkdir -p ~/data/jmeter/$1 apache-jmeter-5.0/bin/jmeter -n -t ~/data/jmeter/plan/my-plan.jmx -l ~/data/jmeter/$1/log -e -o ~/data/jmeter/$1/my-web-reportweb

报告查看:~/data/jmeter/0807/my-web-report/index.html

常见问题: jmeter导入jmx文件报错:missing class com.thoughtworks.xstream.converters.ConversionException 解决:jmeter插件下载地址:https://jmeter-plugins.org/downloads/all/ ,将下载的jmeter-plugins-manager-0.11.jar文件放置到jmeter目录下\lib\ext目录下,而后重启JMeter便可。express

参考资料:https://www.cnblogs.com/richered/category/1151707.htmlapache

吞吐量 吞吐量是指单位时间内处理完请求的数量。json

qps: qps是指每秒处理完请求的数量 qps = 并发量 / 平均响应时间。服务器

经常使用的统计脚本:并发

统计qps的最大值

echo "2019-07-25 22:57:32.117" | awk '{print $2}' | awk -F "." '{print$1}' | sort | uniq -c | awk '{print $1}' | sort -nr | head -1

统计qps最高的时刻

echo "2019-07-25 22:57:32.117" | awk '{print $2}' | awk -F "." '{print$1}' | sort | uniq -c | grep qps的最大值

统计每分钟请求数量的最大值

echo "2019-07-25 22:57:32.117" | awk '{print $2}' | awk -F ":" '{print $1":"$2}' | sort | uniq -c | awk '{print $1}' | sort -nr | head -1

统计前一分钟请求的数量

minute_time=$(date -d '-1 minute' +"%Y-%m-%d %H:%M") grep "${now_date}" xxx.log | grep -c "请求uri"

split函数:The awk function split(s,a,sep) splits a string s into an awk array a using the delimiter sep 举例:echo "2019-07-25" | awk '{len=split($0,a,"-");print "length="len; for(i=1;i<=len;i++)print a[i];}'

相关文章
相关标签/搜索