加载sockjs和vertx应用程序的测试

时间:2016-10-25 15:31:22

标签: performance benchmarking load-testing vert.x sockjs

我已经构建了一个应用程序。它在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);
}
坦率地说,我根本不了解性能测试。所以如果有人给我详细信息,我会很感激。

2 个答案:

答案 0 :(得分:1)

至少有3个独立选项:

  1. Gatling工具 - Websocket
  2. Tsung - Websocket
  3. Apache JMeterWebSocket Sampler插件
  4. 如果您更喜欢基于JavaScript的解决方案,可以查看Artillery

    以及一些“通常”建议:

    1. 不要忘记监控服务器端操作系统资源,即CPU,RAM,交换,磁盘和网络使用情况 - 任何一个都可能成为瓶颈
    2. profiler tool遥测下运行您的应用程序 - 如果它与您的应用程序相关,它将允许更快地找出问题的根本原因
    3. 请记住,Web /应用程序服务器默认配置通常不适合高负载,您可能需要提前以某种方式进行调整

答案 1 :(得分:0)

查看https://github.com/observing/thor

请注意它仅适用于websocket,因此您将无法测试仿真的性能(当您的浏览器或网络中没有websocket时)。