Best way to use HttpClient to post a file in terms of effeciency

断了今生、忘了曾经 提交于 2019-12-11 20:25:47

问题


I have to build a fairly custom load testing tool, and because of this I don't want to use JMeter (lets assume for a moment that the level of customization cannot be done using jmeter).

One import part of this load testing tool is sending a XML for to a API endpoing.

How can I improve the below code to make this fast/efficient?

private boolean PostErrorToApi() throws IOException {
       File xmlSample = new File("/path/to/file/sample.xml");

       CloseableHttpClient httpClient = HttpClients.createDefault();

       HttpPost httpPost = new HttpPost("http://localhost:8090/some/api/method/get/");
       httpPost.setEntity(new FileEntity(xmlSample, "text/xml, application/xml"));

       CloseableHttpResponse response = httpClient.execute(httpPost);

       String line = "";

       try {
           BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

           while ((line = reader.readLine()) != null) {
               System.out.println(line);
           }
       } finally {
           response.close();
       }

       if (line.equals("200 OK"))
           return true;
       else
           return false;
   }

My goal is to get to a few thousand requests per second (http latency shouldn't be that much of an issue since this will be in a private network).

I'm not trying to get a real world scenerio here, the goal is to see if the services can stay up for 48 hours and just to monitor any issues that might come up etc.


回答1:


Mostly it's IO bound task, not CPU. You can try to cache file in memory, without reading it every time from disc. Cache instance of httpClient, use same connection to send file multiple time. Spawn multiple threads in one JVM, each thread will create separate connection. If it's not enough, create multiple processes, run it on different machines and collect results

Also readLine will block until data will be available, so you task will be stuck and you will never know if you have any problems. Set SO_TIMEOUT for reading response.



来源:https://stackoverflow.com/questions/19168944/best-way-to-use-httpclient-to-post-a-file-in-terms-of-effeciency

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!