Skip to content

Instantly share code, notes, and snippets.

@kelvinn
Created July 24, 2014 02:55
Show Gist options
  • Save kelvinn/6a1c51b8976acf25bd78 to your computer and use it in GitHub Desktop.
Save kelvinn/6a1c51b8976acf25bd78 to your computer and use it in GitHub Desktop.
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/
@govind22
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
Copy link

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
Copy link

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

@muhassinmusthafa
Copy link

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

@muhassinmusthafa
Copy link

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

@muhassinmusthafa
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
Copy link

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
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
Copy link

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
Copy link

NICE

@raul1991
Copy link

raul1991 commented Jun 8, 2019

Can Apache bench support dyamic values in post request ?

@darrylyoung
Copy link

Thanks for sharing.

@bala-codenatives
Copy link

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

This means it's unable to find the POST data file. specify the absolute path

@xuyuji9000
Copy link

xuyuji9000 commented Oct 18, 2021

Can Apache bench support dynamic values in post request ?

I have the same need .

According to this Changing POST data used by Apache Bench per iteration .

Maybe more specialized tool like Jmeter is a better option.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment