vertx速度减慢

1bqhqjot  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(276)

我试图创建一个大规模的文件服务器(200万并发),经过大量的搜索,我发现vertx是最好的这样的任务。所以我想出了这段代码

public void uploadFile(Vertx vertx,RoutingContext ctx,String targetFilePath,FileUploadListener mListener) {
         ctx.request().pause();

         new File(targetFilePath).getParentFile().mkdirs();

          vertx.fileSystem().open(targetFilePath, new OpenOptions(), new Handler<AsyncResult<AsyncFile>>() {

            @Override
            public void handle(AsyncResult<AsyncFile> arg0) {
                try {
                    AsyncFile file = arg0.result();
                    if(file == null) {
                        Logger.Log("file null");
                        mListener.onFail();
                        return;
                    }
                    Pump pump = Pump.pump(ctx.request(), file);

                    ctx.request().endHandler(v1 -> file.close(v2 -> {
                                mListener.onSuccess(new File(targetFilePath));
                    }));
                    pump.start();
                    ctx.request().resume();

                }catch (Exception e) {
                    e.printStackTrace();
                    Logger.Log(e);
                    mListener.onFail();
                    return;
                }
            }
        });
    }

然而,当多个请求尝试使用此方法同时上载一个文件时(9mb文件需要1秒,但9mb文件中的100个文件需要1分钟),上载过程会变慢。我是否缺少一些改进并发性的东西,或者因为我在windows 10上运行此方法,所以套接字有这样的速度限制?谢谢
这是我的主旨

public class MainDeployment extends AbstractVerticle{

private Router router = Router.router(vertx);

  @Override
  public void start() throws Exception {

      //GUI.display("Sub Verticle Has Deployed");

    // Different ways of deploying verticles

    // Deploy a verticle and don't wait for it to start

   for(Entry<String, MyHttpHandler> entry : MyVertxServer.map.entrySet()){
       router.route(entry.getKey()).handler(new Handler<RoutingContext>() {

            @Override
            public void handle(RoutingContext ctx) {
                System.out.println(ctx.request().uri());

                String[] handlerID = ctx.request().uri().split(ctx.currentRoute().getPath());

                String suffix = handlerID.length > 1 ? handlerID[1] : null;
                entry.getValue().Handle(ctx, new VertxUtils(), suffix);

            }
        });
   }

   MyVertxServer.server.requestHandler(router::accept);

  }

}
zpf6vheq

zpf6vheq1#

你的代码看起来不错。你的测试方法不是。实际上,这与vertx无关。首先,从您用于下载的同一台机器上载(从vertx的Angular 来看,下载您上载的文件)会将vertx可用的资源量减少一半。你大概有4个CPU?其中两个将忙于上传,只有另外两个将服务于您的请求。
然后是网络。事实上,我很惊讶你能在一秒钟内上传9mb。那是一个严肃的网络。
当然还有硬盘。我希望为了你的实验,你上传一个文件,你缓存在内存中。否则,现在有100个进程一次又一次地读取这个文件,而其他100个进程则尝试写入它。即使你有一流的固态硬盘,这也是一个相当大的挑战。
所以,说了这么多,你现在需要做数学了。首先,将代码部署到与客户端不同的计算机上。

相关问题