You are viewing a plain text version of this content. The canonical link for it is here.
Posted to httpclient-users@hc.apache.org by S Ahmed <sa...@gmail.com> on 2013/10/03 23:19:35 UTC

simple load tester, tips on make my http post call fast and effecient

I want to create a simple load tester that posts a file to a service
endpoint.

Are there any existing samples that have all the best-practices in place
and that also does this in a group of threads?

As a starting point, can you guys critique my method that posts a file
please.

I am creating a single instance of the client that I will pass to this
function:

CloseableHttpClient httpClient = HttpClients.createDefault();


private static int PostToApi(CloseableHttpClient httpClient, File xmlFile)
throws IOException {

        HttpPost httpPost = new HttpPost("http://localhost:8090/a/b/c/");
        httpPost.setEntity(new FileEntity(xmlFile, "text/xml,
application/xml"));

        CloseableHttpResponse response = httpClient.execute(httpPost);

        int responseCode = 0;

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


            String line = "";
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
                if(line.equals("OK")) {
                    responseCode = 1;
                }
            }
        } finally {
            response.close();
        }

        return responseCode;
    }


What can I do to make this more stable and fast/effecient etc?

This is for load testing and it will be called thousands of times per
second hopefully so I want some advice on how to clean it up.

Re: simple load tester, tips on make my http post call fast and effecient

Posted by S Ahmed <sa...@gmail.com>.
Oleg, great thanks for your advice, httpcore-ab looks perfect (thought
looking at the source it doesn't seem to be using httpclient but it makes a
raw socket connection etc.)

BenchmarkWorker.java:
                    final int timeout = config.getSocketTimeout();
                    socket.setSoTimeout(timeout);
                    socket.connect(new InetSocketAddress(hostname, port),
timeout);

                    conn.bind(socket);

A few questions:

1. how do I set the timeout with httpclient?
    CloseableHttpClient httpClient = HttpClients.createDefault();

2.  I get a warning that says FileEntity is deprecated, what should I be
using instead?

 httpPost.setEntity(new FileEntity(xmlFile, "text/xml, application/xml"));


Thanks!


On Fri, Oct 4, 2013 at 4:23 AM, Oleg Kalnichevski <ol...@apache.org> wrote:

> On Thu, 2013-10-03 at 17:19 -0400, S Ahmed wrote:
> > I want to create a simple load tester that posts a file to a service
> > endpoint.
> >
> > Are there any existing samples that have all the best-practices in place
> > and that also does this in a group of threads?
> >
> > As a starting point, can you guys critique my method that posts a file
> > please.
> >
> > I am creating a single instance of the client that I will pass to this
> > function:
> >
>
> You code looks all right to me. However, you should probably consider
> using an exiting load tool instead of building it yourself.
>
> For instance, HttpCore (the component kit HttpClient is based upon)
> ships with a Apache Bench (AB) clone [1]. If you are familiar with the
> original 'ab' [2] you will find 'httpcore-ab' pretty straight-forward .
> If you want to study the source you can find it here [3] or in the
> source distribution of HttpCore.
>
> Oleg
>
> [1]
>
> http://hc.apache.org/httpcomponents-core-4.3.x/httpcore-ab/project-info.html
> [2]
> http://httpd.apache.org/docs/2.2/programs/ab.html
> [3]
>
> http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> For additional commands, e-mail: httpclient-users-help@hc.apache.org
>
>

Re: simple load tester, tips on make my http post call fast and effecient

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Thu, 2013-10-03 at 17:19 -0400, S Ahmed wrote:
> I want to create a simple load tester that posts a file to a service
> endpoint.
> 
> Are there any existing samples that have all the best-practices in place
> and that also does this in a group of threads?
> 
> As a starting point, can you guys critique my method that posts a file
> please.
> 
> I am creating a single instance of the client that I will pass to this
> function:
> 

You code looks all right to me. However, you should probably consider
using an exiting load tool instead of building it yourself.

For instance, HttpCore (the component kit HttpClient is based upon)
ships with a Apache Bench (AB) clone [1]. If you are familiar with the
original 'ab' [2] you will find 'httpcore-ab' pretty straight-forward .
If you want to study the source you can find it here [3] or in the
source distribution of HttpCore.

Oleg

[1]
http://hc.apache.org/httpcomponents-core-4.3.x/httpcore-ab/project-info.html
[2]
http://httpd.apache.org/docs/2.2/programs/ab.html
[3]
http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/httpcore-ab/src/main/java/org/apache/http/benchmark/


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org