Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example of using Apache Bench (ab) to POST JSON to an API
# post_loc.txt contains the json you want to post
# -p means to POST it
# -H adds an Auth header (could be Basic or Token)
# -T sets the Content-Type
# -c is concurrent clients
# -n is the number of requests to run in the test
ab -p post_loc.txt -T application/json -H 'Authorization: Token abcd1234' -c 10 -n 2000 http://example.com/api/v1/locations/
@vkotovv

This comment has been minimized.

Copy link

vkotovv commented Jul 23, 2015

I had to add -l key to accept dynamic response length (see this). Otherwise there were lots of failed requests.

@montaro

This comment has been minimized.

Copy link

montaro commented May 22, 2017

Thanks!

@explorer14

This comment has been minimized.

Copy link

explorer14 commented Nov 13, 2017

Hi, if my API endpoint gets all the info from the headers and doesn't need a post body, do I still need to specify a post file in this command? Thanks.

@govind22

This comment has been minimized.

Copy link

govind22 commented Apr 4, 2018

can someone share the actual post_loc.txt file. This command does not work with the json file used to post json data ?

I get Error : Could not stat POST data file (content.json/content.txt): Partial results are valid but processing is incomplete

@muhassinmusthafa

This comment has been minimized.

Copy link

muhassinmusthafa commented Jun 1, 2018

Do we first need to write JSON data to the file post_loc.txt ? If the above code works, share the code to write JSON data to text file in command prompt.

@kinger310

This comment has been minimized.

Copy link

kinger310 commented Jun 5, 2018

Yeah~it's just JSON data in the file.
something like {"userid": XXX,"appid": "XXX","option": "XXX"}

@muhassinmusthafa

This comment has been minimized.

Copy link

muhassinmusthafa commented Jul 5, 2018

Hai kinger310,
Do we need to specify the location of the same?What is its format ? (For example: D:/apacheload/post_loc.txt)

@muhassinmusthafa

This comment has been minimized.

Copy link

muhassinmusthafa commented Jul 5, 2018

How we can pass parameters? along with url / in a separate format in this command?

@muhassinmusthafa

This comment has been minimized.

Copy link

muhassinmusthafa commented Jul 5, 2018

I got the result ( different metrics to understand the various output values returned by ab) when I use the above with slight change. The command line statement should look like this : For example:

First install apache sever,then open bin folder inside apache,open command prompt and type the command as given below

ab -p C:\Users\Dell\Desktop\ab\smartinvoice.txt -T application/json -H '1ef632f7-02fe-41cd-beec-a1f9bb8cae03' -c 100 -n 1000 http://localhost:3036/app/smartinvoice/document/smartinvoice

where,
path of the text file --> C:\Users\Dell\Desktop\ab\smartinvoice.txt
URI path --> http://localhost:3036/app/smartinvoice/document/smartinvoice
token (different for each login) --> '1ef632f7-02fe-41cd-beec-a1f9bb8cae03'

The output is as follows:

Benchmarking localhost (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests

Server Software:
Server Hostname: localhost
Server Port: 3036

Document Path: /app/home#/smartinvoiceform//views//
Document Length: 5 bytes

Concurrency Level: 100
Time taken for tests: 0.216 seconds
Complete requests: 1000
Failed requests: 0
Non-2xx responses: 1000
Total transferred: 106000 bytes
Total body sent: 24679000
HTML transferred: 5000 bytes
Requests per second: 4631.09 [#/sec] (mean)
Time per request: 21.593 [ms] (mean)
Time per request: 0.216 [ms] (mean, across all concurrent requests)
Transfer rate: 479.39 [Kbytes/sec] received
111611.92 kb/s sent
112091.31 kb/s total

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.3 0 2
Processing: 1 20 11.1 17 72
Waiting: 0 15 6.7 13 44
Total: 1 21 11.1 17 72

Percentage of the requests served within a certain time (ms)
50% 17
66% 21
75% 23
80% 24
90% 31
95% 46
98% 64
99% 69
100% 72 (longest request)

Give small values for c and n. Then increase these values until we get failure cases to see different cases.

But records are not saved in the db.
If any one get the result..pls post here

@muhassinmusthafa

This comment has been minimized.

Copy link

muhassinmusthafa commented Jul 6, 2018

Is it possible to store data from text file(in our post_loc.txt) into db using apache benchmarking command mentioned below:

ab -p post_loc.txt -T application/json -H 'Authorization: Token abcd1234' -c 10 -n 2000 http://example.com/api/v1/locations/

Do we need to specify db name or db related information to store json data in the text file ?

@muhassinmusthafa

This comment has been minimized.

Copy link

muhassinmusthafa commented Jul 10, 2018

JSON data in the text file not saved into db. but it shows metrics in the output.Anyone knows about this issue? How to solve ?

@JeanOsorio

This comment has been minimized.

Copy link

JeanOsorio commented Sep 7, 2018

I trying to replicate this example but I always get:

ab: Could not stat POST data file (data.txt): Partial results are valid but processing is incomplete. I can't see the problem, any idea?

@claviering

This comment has been minimized.

Copy link

claviering commented Mar 31, 2019

NICE

@raul1991

This comment has been minimized.

Copy link

raul1991 commented Jun 8, 2019

Can Apache bench support dyamic values in post request ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.