繁体   English   中英

如何在Java中发送并行HTTP POST请求

[英]How to send parallel HTTP POST requests in Java

我正在尝试生成多个请求,并根据并发因素将其并行发送到服务器。 我是多线程领域的新手,所以我想出了一些可以完成工作的代码。 我看到完成此操作的总时间太长,在服务器端进行任何优化之前,我想看看我编写的代码是否足够优化。

public class Scheduler {

    public Scheduler(AppConfig appConfig) {
        this.appConfig = appConfig;
    }

    private Collection<CustomRequests> customRequests;

    public void initiateScheduler() {
        Long currTime = System.currentTimeMillis();
        ExecutorService pool = Executors.newFixedThreadPool(appConfig.concurrency);

        Generator generator = new Generator();
        customRequests = generator.generate();

        Long totalTimeReq = 0L;
        long maxTime = Long.MIN_VALUE;
        long minTime = Long.MAX_VALUE;

        Collection<Future<Long>> futures = new ArrayList<>();
        for (CustomRequest customRequest : customRequests) {
            futures.add(pool.submit(submitRequest(customRequests)));
        }

        try {
            for (Future f : futures) {
                while (f.isDone() == false) {
                    try {
                        Thread.sleep(1000L);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                Long time = (Long) f.get();
                totalTimeReq += time;
                maxTime = maxTime < time ? time : maxTime;
                minTime = minTime > time ? time : minTime;
            }
        } catch (Exception e) {
            log.error("Exception while calculating responses");

        }

        log.info("Total time taken for WRs in ms {}", totalTimeReq);
        log.info("Total time taken: {} in ms", System.currentTimeMillis() - currTime);
        log.info("Avg time per req: {} in ms", totalTimeReq / customRequests.size());
        pool.shutdownNow();
    }

    private Callable submitRequest(CustomRequest customRequest) {
        return new Callable() {

            @Override
            public Long call() {

                HttpURLConnection connection = null;
                long queryTime = System.currentTimeMillis();
                long doneTime = 0L;
                try {
                    //Create connection
                    URL url = new URL(appConfig.endpointUrl);
                    connection = (HttpURLConnection) url.openConnection();
                    connection.setCustomRequestMethod("POST");
                    connection.setCustomRequestProperty("Content-Type",
                            "application/x-protobuf");

                    connection.setCustomRequestProperty("Authorization", "bearer " + appConfig.bearerToken);
                    connection.setCustomRequestProperty("Content-Encoding", "snappy");
                    connection.setUseCaches(false);
                    connection.setDoOutput(true);
                    final SnappyOutputStream snappyOutputStream = new SnappyOutputStream(connection.getOutputStream());

                    customRequest.writeTo(snappyOutputStream);
                    snappyOutputStream.close();

                    //Get Response
                    InputStream is = connection.getInputStream();
                    BufferedReader rd = new BufferedReader(new InputStreamReader(is));
                    rd.close();
                    doneTime = System.currentTimeMillis();

                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (connection != null) {
                        connection.disconnect();
                    }
                }
                return (doneTime - queryTime);
            }
        };
    }
}

任何帮助在组织或优化上将不胜感激。

您有一个正确的想法,但是与其将期货添加到列表中并一一执行它们,您要做的就是这样做,您将获得返回的Future对象列表。

List<Callable<Long>> callables = new ArrayList<>();
for (CustomRequests request : customRequests)
    callables.add(submitRequest(request));

List<Future<Long>> results = pool.invokeAll(callables);
//Do as you wish with results

使用Apache Jmeter

https://jmeter.apache.org

使用jmeter您可以生成并发负载和最大请求​​等

如果不使用工具有任何限制,则可以使用Junit生成并发负载以进行测试。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM