Vertx:处理POST主体的速度很慢

时间:2018-04-08 12:45:05

标签: java vert.x

我正在尝试实施Http服务来上传文件。上传的文件通过原始请求传输。这是我用vertx 3实现的内容

public class MainVerticle extends AbstractVerticle {
    public static void main(String[] args) {
        Vertx vertx = Vertx.vertx();
        vertx.deployVerticle(new MainVerticle());
    }

    public static void log(String message) {
        System.out.printf("%s > %s\n", System.currentTimeMillis(), message);
    }

    @Override
    public void start(Future<Void> fut) {
        // Create a router object.
        Router router = Router.router(vertx);
        router.route().handler(BodyHandler.create());

        router.route("/api/v1.0/log/write")
                .handler(routingContext -> {
                    log("Processing... ");
                    JsonObject result = new JsonObject();
                    result.put("status", "ok");
                    routingContext.response()
                            .putHeader("content-type", "application/json")
                            .end(result.toString());
                });

        Handler<HttpServerRequest> handler = new Handler<HttpServerRequest>() {
            @Override
            public void handle(HttpServerRequest request) {
                request.handler(buffer -> log("Handling2...." + buffer.length()));
                request.endHandler(aVoid -> {
                    log("Done");
                    JsonObject result = new JsonObject();
                    result.put("status", "ok");
                    request.response()
                            .putHeader("content-type", "application/json")
                            .end(result.toString());
                });
            }
        };

        vertx
                .createHttpServer()
                .requestHandler(router::accept)
                .listen(9231, result -> {
                    if (result.succeeded()) {
                        fut.complete();
                    } else {
                        fut.fail(result.cause());
                    }
                });
    }
}

为了验证服务,我运行了以下脚本:

max=50
for i in `seq 2 $max`
do
    curl -H "Content-Type: application/json" \
        -X POST \
        --max-time 10 \
        --retry 5 \
        --retry-delay 0 \
        --retry-max-time 60 \
        --data-binary "@./512-kb.log" \
        http://localhost:9231/api/v1.0/log/write
    echo "\n"
done

令我惊讶的是,服务执行速度很慢。大致约1个请求/秒。以下是运行时日志:

text Apr 08, 2018 7:41:06 PM io.vertx.core.Starter INFO: Succeeded in deploying verticle 1523191273 > Processing... 1523191274 > Processing... 1523191275 > Processing... 1523191276 > Processing... 1523191277 > Processing... 1523191278 > Processing...

请您告诉我如何以正确的方式实施它?提前谢谢。

1 个答案:

答案 0 :(得分:2)

  

令我惊讶的是,服务执行速度很慢。大致约1个请求/秒。以下是运行时日志

您的运行时输出似乎并不是说您每秒只有一个请求,而是每毫秒一个请求;)

我已尝试使用以下代码:

public class MainVerticle extends AbstractVerticle {
    public static void log(String message) {
        System.out.printf("%s > %s\n", Instant.ofEpochMilli(System.currentTimeMillis()), message);
    }

    @Override
    public void start(Future<Void> fut) {
        Router router = Router.router(vertx);
        router.route().handler(BodyHandler.create());

        router.route("/api/v1.0/log/write").handler(routingContext -> {
            log("Processing... ");
            JsonObject result = new JsonObject();
            result.put("status", "ok");
            routingContext.response().putHeader("content-type", "application/json").end(result.toString());
        });

        vertx.createHttpServer().requestHandler(router::accept).listen(9231, result -> {
            if (result.succeeded()) {
                fut.complete();
            } else {
                fut.fail(result.cause());
            }
        });
    }
}

结果如下:

2018-04-09T06:39:49.154Z > Processing... 
2018-04-09T06:39:49.192Z > Processing... 
2018-04-09T06:39:49.203Z > Processing... 
2018-04-09T06:39:49.213Z > Processing... 
2018-04-09T06:39:49.224Z > Processing... 
2018-04-09T06:39:49.235Z > Processing...
2018-04-09T06:39:49.246Z > Processing... 
2018-04-09T06:39:49.257Z > Processing... 
...

我有几次同样的结果。

P.S:

1)您的处理程序似乎没有被使用

2)我使用io.vertx.core.Launcher来运行我的Verticle或vertx可执行命令。