Slim 4 - Performance Testing using Apache Bench

20 Dec 2019

Apache Bench (ab) is a great tool to test the performance of your API.

The problem is that your first test might give this strange result:

$ ab -n 1 'http://localhost/my-slim-app'
> 5.008 seconds

Of course the loading time in the browser is extremely fast, but when testing the loading time with AB you should not get a loading time < 5 seconds.

Using the switch -k (Use Keep-Alive) helps to solve the problem.

$ ab -n 1 -k 'http://localhost/my-slim-app'
> 0.100 seconds

There are two other options:

Add Connection: close to the response, e.g.

$response = $response->withHeader('Connection', 'close');

Change the HTTP version to 1.0, e.g.

$response = $response->withProtocolVersion('1.0');

Read more