Since it has become necessary that I have to deal with Go, my interest in Rust has also grown again. That’s why I set up a benchmark to test the performance of both languages with relation to web services.
First, I have given a very simple API which the two languages have to implement:
openapi: 3.0.2
info:
title: TODO
version: 0.1.0
servers:
- url: '127.0.0.1:3000'
description: app
components:
parameters:
state:
name: state
in: query
required: true
schema:
type: string
enum:
- open
- closed
schemas:
Task:
type: object
properties:
id:
type: integer
content:
type: string
state:
type: string
enum:
- open
- closed
Tasks:
type: array
items:
$ref: '#/components/schemas/Task'
paths:
/tasks:
get:
description: get tasks by state
responses:
'200':
$ref: '#/components/schemas/Tasks'
parameters:
- $ref: '#/components/parameters/state'
post:
description: add task
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/Task'
responses:
'200':
$ref: '#/components/schemas/Task'
The second part is a lua script for wrk benchmarking tool:
id = 0
counter = 0
request = function()
id = id + 1
if counter > 10 then
counter = 0
path = "/tasks?state=open"
wrk.method = "GET"
else
counter = counter + 1
path = "/tasks"
wrk.method = "POST"
wrk.body = "{\"id\":" .. id .. ",\"content\":\"task\",\"state\":\"open\"}"
wrk.headers["Content-Type"] = "application/json"
end
return wrk.format(nil, path)
end
The last (and hardest) part was the implementation of the api in rust and go.
request count / benchmark duration | 5 | 10 | 15 | 20 | 25 | 30 | 40 | 50 | 60 |
---|---|---|---|---|---|---|---|---|---|
go | 22608 | 33232 | 41656 | 48347 | 54232 | 59190 | 68464 | 76803 | 83686 |
rust | 30514 | 44125 | 54719 | 63498 | 71136 | 77423 | 88831 | 98697 | 108258 |