背景
Jmeter 平时性能测试工作一般都是通过命令行在 Linux 下执行,为了锻炼自己代码与逻辑能力,想 Jmeter 是否可以通过 springboot 工程启动,周末在家尝试写一写,一写原来需要处理很多事情,才可以启动起来,起来还是有很问题需要处理,下面是相应的代码,其实网上也有,但关键的是自己有意识收集知识,到用的时候能拿来改一改就用。
前置条件
需要在 linux 中配置 Jmeter,并且配置 Java 环境变量:
vi ~/.bash_profile
JMETER_HOME=/root/tools/apache-jmeter-5.1.1
PATH=$PATH:$HOME/bin:$JMETER_HOME/bin:
export PATH
source ~/.bash_profile
页面设计
运行效果
点击上传脚本,弹出对话框,点击上传,后台日志显示上传成功。
点击启动,并且读取启动日志。
点击停止。
示意图说明:
通过访问 -> 调用 JAVA 代码-> 启动 shell命令-> 启动 Jmeter -> 获取启动日志
参考代码
前端代码
以下参考代码:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="Mark Otto, Jacob Thornton, and Bootstrap contributors">
<meta name="generator" content="Jekyll v3.8.6">
<title>Jmeter启动</title>
<link rel="canonical" href="https://v4ing.bootcss.com/docs/4.3/examples/checkout/">
<link href="asserts/css/bootstrap.min.css" th:href="@{/webjars/bootstrap/4.3.1/css/bootstrap.css}" rel="stylesheet">
<meta name="theme-color" content="#563d7c">
<style>
.bd-placeholder-img {
font-size: 1.125rem;
text-anchor: middle;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
@media (min-width: 768px) {
.bd-placeholder-img-lg {
font-size: 3.5rem;
}
}
</style>
<link href="https://v4.bootcss.com/docs/4.3/examples/checkout/form-validation.css" rel="stylesheet">
</head>
<body class="bg-light">
<div class="container">
<div class="py-5 text-center">
<img src="https://jmeter.apache.org/images/logo.svg" class="d-block mx-auto mb-4" alt="Apache JMeter">
<p class="lead">The Apache JMeter? application is open source software, a 100% pure Java application designed to
load test functional behavior and measure performance. It was originally designed for testing Web
Applications but has since expanded to other test functions.</p>
</div>
<div class="col-md-8 order-md-1">
<h4 class="mb-3">上传脚本</h4>
<form>
<input id="jmeterId" type="file"/>
<a class="btn btn-lg btn-primary btn-block" value="上传脚本" onclick="submitupload()">上传脚本</a>
</form>
<form>
<input id="jmeterParam" type="file"/>
<a class="btn btn-lg btn-primary btn-block" value="上传参数" onclick="submitParm()">上传参数文件</a>
</form>
<h4 class="mb-3">运行</h4>
<form class="needs-validation" novalidate>
<div class="row">
<div class="col-md-6 mb-3">
<label for="jmeterName">压测脚本</label>
<input type="text" class="form-control" id="jmeterName" name="jmeterName" placeholder="jmx file"
value="" required>
<div class="invalid-feedback">
jmx file
</div>
</div>
<div class="col-md-6 mb-3">
<label for="numberName">并发数</label>
<input type="text" class="form-control" id="numberName" name="num" placeholder="并发数" value=""
required>
<div class="invalid-feedback">
并发数
</div>
</div>
<div class="col-md-6 mb-3">
<label for="duration">执行时间</label>
<input type="text" class="form-control" id="duration" name="duration" placeholder="执行时间" value=""
required>
<div class="invalid-feedback">
并发数
</div>
</div>
</div>
<hr class="mb-4">
<a class="btn btn-success" onclick="JmeterRun()" type="submit">运行</a>
<a class="btn btn-danger" onclick="Jmeterstop()" type="submit">停止</a>
<a class="btn btn-info" onclick="JmeterInfo()" data-toggle="modal" data-target="#myModal">查看信息</a>
</form>
</div>
</div>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Jmeter运行日志</h4>
</div>
<div class="modal-body">
<textarea rows="30" cols="20" id="JmeterMsg"></textarea>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<footer class="my-5 pt-5 text-muted text-center text-small">
<p class="mb-1">© 2017-2019 Company Name</p>
<ul class="list-inline">
<li class="list-inline-item"><a href="#">Privacy</a></li>
<li class="list-inline-item"><a href="#">Terms</a></li>
<li class="list-inline-item"><a href="#">Support</a></li>
</ul>
</footer>
</div>
<script type="text/javascript" th:src="@{/webjars/jquery/3.3.1/jquery.js}"></script>
<script th:src="@{/asserts/js/layer/layer.js}"></script>
<script>window.jQuery || document.write('<script src="/docs/4.3.1/assets/js/vendor/jquery.slim.min.js"><\/script>')</script>
<script src="/docs/4.3/dist/js/bootstrap.bundle.js"
th:src="@{/webjars/bootstrap/4.3.1/js/bootstrap.bundle.js}"></script>
<script src="https://v4.bootcss.com/docs/4.3/examples/checkout/form-validation.js"></script>
</body>
<script>
function submitupload() {
var type = "file";
var id = "jmeterId";
var formData = new FormData();
var jmeterId = $("#jmeterId").val();
if (jmeterId == "") {
layer.msg("Jmeter文件不能为空,请输入", {time: 2000, icon: 5, shift: 6}, function () {
});
return;
}
formData.append(type, $("#" + id)[0].files[0]);
$.ajax({
type: "POST",
url: '/jmeter/upload',
data: formData,
processData: false,
contentType: false,
success: function (data) {
if (data.code == 100) {
layer.msg("用户信息保存成功", {time: 1000, icon: 6}, function () {
$("#jmeterName").val(data.extend.file);
});
} else {
layer.msg("信息保存失败,请重新操作" + data.err, {time: 2000, icon: 5, shift: 6}, function () {
});
}
}
});
}
function submitParm() {
var type = "file";
var id = "jmeterParam";
var formData = new FormData();
var jmeterPara = $("#jmeterParam").val();
if (jmeterPara == "") {
layer.msg("Jmeter文件不能为空,请输入", {time: 2000, icon: 5, shift: 6}, function () {
});
return;
}
formData.append(type, $("#" + id)[0].files[0]);
$.ajax({
type: "POST",
url: '/jmeter/Paramupload',
data: formData,
processData: false,
contentType: false,
success: function (data) {
if (data.code == 100) {
layer.msg("参数文件保存成功", {time: 1000, icon: 6}, function () {
});
} else {
layer.msg("信息保存失败,请重新操作" + data.err, {time: 2000, icon: 5, shift: 6}, function () {
});
}
}
});
}
function JmeterRun() {
let JmeterName = $("#jmeterName").val();
let number = $("#numberName").val();
let duration = $("#duration").val();
console.log(JmeterName);
console.log(number);
$.ajax({
type: "POST",
url: '/jmeter/JmeterRun',
data: {
"jmeterName": JmeterName,
"numberName": number,
"duration": duration
},
success: function (result) {
if (result.code == 100) {
layer.msg("启动成功成功", {time: 1000, icon: 6}, function () {
});
} else {
layer.msg("启动失败,请重新操作", {time: 2000, icon: 5, shift: 6}, function () {
});
}
}
})
}
function Jmeterstop() {
$.ajax({
type: "Get",
url: '/jmeter/JmeterStop',
processData: false,
contentType: false,
success: function (result) {
if (result.code==100) {
layer.msg("停止成功", {time: 1000, icon: 6}, function () {
});
} else {
layer.msg("停止失败,请重新操作", {time: 2000, icon: 5, shift: 6}, function () {
});
}
}
})
}
function JmeterInfo() {
$.ajax({
type: "Get",
url: '/jmeter/Jmeterinfo',
processData: false,
contentType: false,
success: function (result) {
if (result.code == 100) {
layer.msg("启动成功成功", {time: 1000, icon: 6}, function () {
$("#JmeterMsg").val(data.extend.infopage);
});
} else {
layer.msg("启动失败,请重新操作", {time: 2000, icon: 5, shift: 6}, function () {
});
}
}
})
}
</script>
</html>
服务端接口
参考代码如下:
package com.sevendays.controller;
import com.sevendays.pojo.Msg;
import com.sevendays.service.JmerterScriptService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
@Controller
@RequestMapping("/jmeter")
public class JmeterController {
private static final Logger logger = LoggerFactory.getLogger(JmeterController.class);
@Autowired
JmerterScriptService jmerterScriptService;
@GetMapping("/jmeterIndex")
public String jmeterIndex() {
return "jmeter/jmterIndex";
}
@PostMapping("/upload")
@ResponseBody
public Msg upload(@RequestParam("file") MultipartFile file) {
if (file.isEmpty()) {
return Msg.fail().add("err", "上传失败");
}
String fileName = file.getOriginalFilename();
logger.info("路径" + fileName);
String filePath = "/home/7d/";
if (!fileName.endsWith(".jmx")) {
return Msg.fail().add("err", "脚本上传失败");
}
File dest = new File(filePath + fileName);
String jmxName = fileName.substring(0, fileName.lastIndexOf("."));
try {
file.transferTo(dest);
logger.info("上传成功:" + jmxName);
return Msg.success().add("file", jmxName);
} catch (IOException e) {
logger.error(e.toString(), e);
}
return Msg.fail();
}
@PostMapping("/Paramupload")
@ResponseBody
public Msg uploadParam(@RequestParam("file") MultipartFile file) {
if (file.isEmpty()) {
return Msg.fail().add("err", "上传失败");
}
String fileName = file.getOriginalFilename();
logger.info("路径" + fileName);
String filePath = "/home/7d";
File dest = new File(filePath + fileName);
String jmxName = fileName.substring(0, fileName.lastIndexOf("."));
try {
file.transferTo(dest);
logger.info("上传成功:" + jmxName);
return Msg.success().add("file", jmxName);
} catch (IOException e) {
logger.error(e.toString(), e);
}
return Msg.fail();
}
@PostMapping("/JmeterRun")
@ResponseBody
public Msg run(@RequestParam("jmeterName") String jmeterName, @RequestParam("numberName") String numberName, @RequestParam("duration") String duration) {
logger.info(jmeterName);
if (!jmeterName.isEmpty() && !numberName.isEmpty()) {
jmerterScriptService.runCommand(jmeterName.trim(), numberName.trim(), duration);
return Msg.success();
} else {
return Msg.fail();
}
}
@GetMapping("/JmeterStop")
@ResponseBody
public Msg stop() {
jmerterScriptService.stopCommand();
return Msg.success();
}
@GetMapping("/Jmeterinfo")
@ResponseBody
public Msg info() {
String info = jmerterScriptService.selectInfo();
return Msg.success().add("infopage", info);
}
}
interface层代码
package com.sevendays.service;
public interface JmerterScriptService {
void execCommand(String cmd);
void runCommand(String script, String num,String seconds);
void stopCommand();
String selectInfo();
}
接口实现层
package com.sevendays.service.impl;
import com.sevendays.controller.JmeterController;
import com.sevendays.service.JmerterScriptService;
import com.sevendays.utils.LogSvrReadInput;
import com.sevendays.utils.execCmd;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Date;
@Service
public class JmerterScriptServiceImpl implements JmerterScriptService {
private static final Logger logger = LoggerFactory.getLogger(JmerterScriptServiceImpl.class);
@Override
public void execCommand(String cmd) {
try {
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(cmd, null, null);
InputStream stderr = proc.getInputStream();
InputStreamReader isr = new InputStreamReader(stderr, "GBK");
BufferedReader br = new BufferedReader(isr);
String line = "";
while ((line = br.readLine()) != null) {
logger.info(line);
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void runCommand(String script, String num, String seconds) {
String numThread = "<stringProp name=\"ThreadGroup.num_threads\">#numThread</stringProp>";
String time = "<stringProp name=\"ThreadGroup.duration\">#timeDuration</stringProp>";
String bak = "cp /home/7d/" + script + ".jmx /home/7d/" + script + "bak.jmx";
String old = "/home/7d/" + script + ".jmx";
execCmd.execCmd(bak);
logger.info("路径:{}", old);
execCmd.replacTextContent(old, "#numThread", num);
execCmd.replacTextContent(old, "#timeDuration", seconds);
String runcmd = "nohup jmeter -n -t /home/7d/#scriptName.jmx -l /home/7d/#scriptName.jtl -j /home/7d/jmeter.log > /home/7d/jmeterlog.log&".replaceAll("#scriptName", script);
logger.info("运行命令{}", runcmd);
execCmd.execCmd(runcmd);
}
@Override
public void stopCommand() {
String stoprunm = "/root/tools/apache-jmeter-5.1.1/bin/shutdown.sh";
execCmd.execCmd(stoprunm);
}
@Override
public String selectInfo() {
String tail = "tail -f /home/7d/jmeterlog.log";
File file = new File("/home/7d/jmeterlog.log");
String s = LogSvrReadInput.realtimeShowLog(file);
logger.info("输出日志:--》{}",s);
return s;
}
}
工具类
package com.sevendays.utils;
import com.sevendays.service.impl.JmerterScriptServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
public class execCmd {
private static final Logger logger = LoggerFactory.getLogger(execCmd.class);
public execCmd() {
}
public static void execCmd(String cmd) {
try {
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(cmd, null, null);
InputStream stderr = proc.getInputStream();
InputStreamReader isr = new InputStreamReader(stderr, "GBK");
BufferedReader br = new BufferedReader(isr);
String line = "";
while ((line = br.readLine()) != null) {
logger.info(line);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void replacTextContent(String path, String srcStr, String newStr) {
File file = new File(path);
if (!file.exists() && !file.isFile()) {
logger.info("file{},不存在:", path);
return;
}
try {
FileReader in = new FileReader(file);
BufferedReader bufIn = new BufferedReader(in);
CharArrayWriter tempStream = new CharArrayWriter();
String line = null;
while ((line = bufIn.readLine()) != null) {
line = line.replaceAll(srcStr, newStr);
tempStream.write(line);
tempStream.append(System.getProperty("line.separator"));
}
bufIn.close();
FileWriter out = new FileWriter(file);
tempStream.writeTo(out);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
logger.info("====path:" + path);
}
}
jmeter脚本
脚本其实也没有什么东西,只有定义好规则,这样方便替换。
小结
上面 Demo 中还是一个问题没有解决就是在页面实时参看日志,目前还没实现,不过总体上实现自己想的功能。
源码地址:
- https://github.com/zuozewei/blog-example/tree/master/Performance-testing/01-test-tool/jmeter/seven_days
|