Veröffentlicht am Schreib einen Kommentar

Benchmark Go vs Rust

NOTE This article is in progress and will be updated frequently.

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 duration51015202530405060
go226083323241656483475423259190684647680383686
rust3051444125547196349871136774238883198697108258