我已经构建了一个应用程序。它的后端是vertx (java),我使用sockJs从客户端访问它。我想为这个应用程序做负载测试(基准测试/性能测试)。我之前没有做过负载测试。有人能帮我一下吗,怎么做?比如,我想并发地访问多个连接的服务器,看看它什么时候失败,我能做些什么来改进它。下面是一个基本代码
HttpVerticle.java
package sockjs;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.HttpServer;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.handler.sockjs.SockJSHandler;
import io.vertx.ext.web.handler.sockjs.SockJSHandlerOptions;
import io.vertx.ext.web.handler.sockjs.SockJSSocket;
public class HttpVerticle extends AbstractVerticle {
static int count = 0;
@Override
public void start() throws Exception {
HttpServer server = vertx.createHttpServer();
Router router = Router.router(Vertx.vertx());
SockJSHandlerOptions options = new SockJSHandlerOptions();
options.setHeartbeatInterval(20000);
SockJSHandler sockJSHandler = SockJSHandler.create(vertx, options);
sockJSHandler.socketHandler(this::myHandler);
router.route("/myapp/*").handler(sockJSHandler);
router.route("/myway/*").handler(sockJSHandler);
server.requestHandler(router::accept).listen(8080);
}
private void myHandler(SockJSSocket sockJSSocket) {
System.out.println("Connection established " + count); //Calls when the connection is established
count++;
System.out.println(sockJSSocket);
//sockJSSocket.handler(this::processMessage);
sockJSSocket.handler(buffer -> { //everytime a message is received, a reply is sent back in uppercase.
System.out.println(buffer.toString());
String reply = buffer.toString();
reply = reply.toUpperCase();
sockJSSocket.write((Buffer.buffer(reply)));
});
}
private void processMessage(Buffer buffer) {
System.out.println(buffer.toString());
}
}
Main.java
package sockjs;
import io.vertx.core.Vertx;
public class Main {
public static void main(String[] args) {
System.out.println("Vertx server started");
Vertx vertx = Vertx.vertx();
vertx.deployVerticle(new HttpVerticle());
}
}
客户端可以是这样的:
var sockJsUrl = "https://cdnjs.cloudflare.com/ajax/libs/sockjs-client/1.1.1/sockjs.min.js";
var head = document.querySelector("head");
var script = document.createElement("script");
script.type = "text/javascript";
script.src = sockJsUrl;
head.appendChild(script);
var sock = new SockJS("http://localhost:8080/myapp");
sock.onopen = function() {
console.log("Connection open", sock.readyState);
}
sock.onmessage = function(event) {
console.log("Message from server");
console.log(event);
}
sock.onclose = function() {
console.log("Connection closed", sock.readyState);
}
sock.onerror = function() {
console.log("Connection error", sock.readyState);
}
坦白说,我对性能测试一窍不通。所以如果有人告诉我细节,我会很感激的。
至少有3个独立的选项:
- Gatling tool - Websocket
- Tsung - Websocket
- Apache JMeter with WebSocket Sampler plugin
如果你更喜欢基于javascript的解决方案,你可以看看Artillery
和一些"通常"的建议:
- 不要忘记监控服务器端操作系统资源,即CPU, RAM, Swap,磁盘和网络使用情况-它们中的任何一个都可能成为瓶颈
- 在遥测分析器工具下运行您的应用程序-如果它与您的应用程序相关,它将允许更快地确定问题的根本原因
- 请记住,web/应用服务器的默认配置通常不适合高负载,您可能需要提前调整它
看看https://github.com/observing/thor
请注意,它仅用于websocket,因此您将无法测试模拟的性能(当websocket在您的浏览器或网络中不可用时)。