Skip to content

Instantly share code, notes, and snippets.

@rayyildiz
Last active March 3, 2021 12:15
Show Gist options
  • Save rayyildiz/aaf81ce8a593ba87fff1cfbf6282a17c to your computer and use it in GitHub Desktop.
Save rayyildiz/aaf81ce8a593ba87fff1cfbf6282a17c to your computer and use it in GitHub Desktop.
Performance test in go

Go perf test

create table names (
   id serial primary key,
   name text
);

Install wrk and run perf test.

wrk -t 6 -c100 -d20s -s post.lua -H "Content-Type: application/json" http://localhost:8080/add

result

  Running 20s test @ http://localhost:8080/add
    6 threads and 100 connections
    Thread Stats   Avg      Stdev       Max        +/- Stdev
      Latency     7.41ms    7.46ms      142.38ms   91.14%      
      Req/Sec     2.62k      317.10     5.67k      92.68%      
    314096 requests in 20.08s, 19.17MB read  
  Requests/sec:    15638.87  
  Transfer/sec:      0.95MB
version: "3"
services:
postgresql:
image: postgres:11
ports:
- "5432:5432"
environment:
POSTGRES_PASSWORD: "123456"
POSTGRES_USER: "postgres"
POSTGRES_DB: "postgres"
volumes:
- test_data:/var/lib/postgresql/data
volumes:
test_data: { }
package main
import (
"database/sql"
"encoding/json"
"log"
"net/http"
_ "github.com/lib/pq"
)
type Person struct {
Name string `json:"name"`
}
func main() {
dsn := "host=localhost port=5432 user=postgres password=123456 dbname=postgres sslmode=disable"
db, err := sql.Open("postgres", dsn)
if err != nil {
log.Fatalf("db connection ,%v", err)
}
db.SetMaxIdleConns(50)
db.SetMaxOpenConns(50)
http.HandleFunc("/add", func(w http.ResponseWriter, req *http.Request) {
var p Person
if err := json.NewDecoder(req.Body).Decode(&p); err != nil {
http.Error(w, "unmarshall", http.StatusBadRequest)
return
}
defer req.Body.Close()
_, err := db.Exec(`insert into names(name) values($1)`, p.Name)
if err != nil {
http.Error(w, "insert record", http.StatusBadRequest)
return
}
w.WriteHeader(http.StatusNoContent)
})
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Fatalf("coudl no start server, %v", err)
}
}
wrk.method = "POST"
wrk.body = '{"name":"test"}'
wrk.headers["Content-Type"] = "application/json"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment