I wrote the same app in PHP, Go and Python to compare performance

I knew that Go is faster than PHP (they are 2 my main programming languages now), but how big the difference. And how about Python? The best way to find out (as a software engineer) is to compare them on a real example.

I wrote a simple REST API, URL shortener service, in 3 languages using popular frameworks:

  • PHP 8.2 with Symfony

  • Go 1.21

  • Python 3.12 with FastAPI

All applications work in Docker, interact with MySQL database and Redis. I use K6 tool from Graphana to measure each app API performance with the same scenario, also compared how fast they generate random 8-symbol codes.

Source code is available here: https://github.com/Sunsetboy/url_shortener

How the URL shortener works

Real life examples: bit.ly, TinyURL

  • The service should accept a URL, save it, and return a unique short link.

  • When accessing the short link, the service should redirect the user to the full URL.

  • If the short link does not correspond to any saved URL, the service should return a response with the code 404.

  • The same URL submitted to the service multiple times should be converted into different short links.

Traffic estimations

I estimate that the service is read-heavy, for every added link there will be 100 requests to get the long URL.

How I do load testing

There is a K6 container in the project with scripts to test each API with the same process:

  • Try to request a full URL with not incorrect short URL

  • Try to add a new long URL by POST request

  • Try to request a full URL using a short URL from the POST response 100 times

I used 10 concurrent virtual users.

I measure 90th percentile for requests duration, according to my traffic estimations the ratio between GET and POST requests should be around 100/1.

Results

Languagep90 request durationp95 request duration
PHP with Symfony28.2 ms34.9 ms
Python with FastAPI49 ms51 ms
Go980 microsecond1.4 ms

Go is a clear winner here!

Python is slower than PHP in my test, but I'm not a Python developer and could miss some optimisation tricks in my FastAPI app.

It would be interesting to try other languages like Ruby, NodeJS or even Rust. I will do it and share my results. Stay tuned!