Skip to content

Instantly share code, notes, and snippets.

@cn007b
Last active December 18, 2023 10:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cn007b/6083c25407e1f19317f3a513e7ae2a28 to your computer and use it in GitHub Desktop.
Save cn007b/6083c25407e1f19317f3a513e7ae2a28 to your computer and use it in GitHub Desktop.
Plain Go vs Gin

Plain Go vs Gin

The aim of this benchmark is to answer the question do you need framework for your tiny microservice.

Prerequisites

Let's consider simple example where we need framework only to parse URL
(someone may decide that it's sufficient reason to start use framework for tiny microservice).

For gin example will use this implementation, and for plain go example will use this implementation.
Code looks in next way:

code

Prepare

Please run next command with purpose to make go web-server available from one docker container to another docker container and especially for apachebench.

# OSX
sudo ifconfig lo0 alias 10.254.254.254

# Ubuntu
sudo ifconfig lo 10.254.254.254

Prepare Gin

mkdir -p /tmp/benchmark/src/gin

curl -o /tmp/benchmark/src/gin/main.go \
  "https://gist.githubusercontent.com/cn007b/318180624ee1bbe1c444439004cae761/raw/a9bceec28eb48b32551d595a1991b573b6ec6b48/gin.go"

docker run -it --rm -v /tmp/benchmark:/app -w /app -e GOPATH='/app' \
    cn007b/go sh -c 'go get github.com/gin-gonic/gin && go install gin'

Prepare Plain Go

mkdir -p /tmp/benchmark/src/plain

curl -o /tmp/benchmark/src/plain/main.go \
  "https://gist.githubusercontent.com/cn007b/677973fc5f9d3fc75869fffff7c22bb8/raw/ebb63bd7d49ccab10a5b9a3735c7c4d80686763a/plain.go"

docker run -it --rm -v /tmp/benchmark:/app -w /app -e GOPATH='/app' \
    cn007b/go sh -c 'go install plain'

Benchmark

# run gin
docker run -it --rm -p 8080:8080 -v /tmp/benchmark:/app -w /app -e GOPATH='/app' \
    cn007b/go sh -c './bin/gin'

# run apachebench
docker run -ti --rm cn007b/ubuntu ab -k -n 5000 -c 100 -t 2 "http://10.254.254.254:8080/v1/id/7"

# run plain go
docker run -it --rm -p 8080:8080 -v /tmp/benchmark:/app -w /app -e GOPATH='/app' \
    cn007b/go sh -c './bin/plain'

# run apachebench
docker run -ti --rm cn007b/ubuntu ab -k -n 5000 -c 100 -t 2 "http://10.254.254.254:8080/v1/id/7"

Result Gin

Finished 7637 requests


Server Software:
Server Hostname:        10.254.254.254
Server Port:            8080

Document Path:          /v1/id/7
Document Length:        10 bytes

Concurrency Level:      100
Time taken for tests:   2.000 seconds
Complete requests:      7637 ‼️
Failed requests:        0
Keep-Alive requests:    7637
Total transferred:      1199009 bytes
HTML transferred:       76370 bytes
Requests per second:    3818.42 [#/sec] (mean) ‼️
Time per request:       26.189 [ms] (mean)
Time per request:       0.262 [ms] (mean, across all concurrent requests)
Transfer rate:          585.44 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1   8.3      0     129
Processing:     1   25  15.1     24     155
Waiting:        1   25  15.1     24     155
Total:          1   26  17.3     24     196

Percentage of the requests served within a certain time (ms)
  50%     24
  66%     28
  75%     31
  80%     33
  90%     41
  95%     56
  98%     73
  99%     90
 100%    196 (longest request)

Result Plain Go

Finished 15349 requests


Server Software:
Server Hostname:        10.254.254.254
Server Port:            8080

Document Path:          /v1/id/7
Document Length:        11 bytes

Concurrency Level:      100
Time taken for tests:   2.000 seconds
Complete requests:      15349 ‼️
Failed requests:        0
Keep-Alive requests:    15349
Total transferred:      2333048 bytes
HTML transferred:       168839 bytes
Requests per second:    7672.79 [#/sec] (mean) ‼️
Time per request:       13.033 [ms] (mean)
Time per request:       0.130 [ms] (mean, across all concurrent requests)
Transfer rate:          1138.93 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   5.9      0     123
Processing:     1   13   2.9     12      49
Waiting:        1   13   2.9     12      49
Total:          1   13   6.7     12     142

Percentage of the requests served within a certain time (ms)
  50%     12
  66%     13
  75%     14
  80%     15
  90%     16
  95%     17
  98%     20
  99%     23
 100%    142 (longest request)

Conclusion

100% sure you've answered the main question of this benchmark! 🙂
And now you have clue how much will you pay for syntax sugar which performs URL parsing.

PS

You can find more stuff like this in my demo repo.
Source code, examples, explanation info, etc. just go and check it out!

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