Skip to content

Instantly share code, notes, and snippets.

@ifraixedes
Last active April 25, 2023 22:50
Show Gist options
  • Save ifraixedes/2b25ceaec4fa5b9e4533 to your computer and use it in GitHub Desktop.
Save ifraixedes/2b25ceaec4fa5b9e4533 to your computer and use it in GitHub Desktop.
Go PosgreSQL UUID as string and UUID (byte[16]) benchmark

This file exists to give to this gist a proper name.

DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE Version 2, December 2004
(http://www.wtfpl.net/about/)
Copyright (C) 2015 Ivan Fraixedes (https://ivan.fraixed.es)
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
DOCKER_IP := $(shell ifconfig docker0 | grep "inet addr" | cut -d ":" -f2 | cut -d " " -f1)
DB_USER := test
DB_PWD := test
DB_NAME := test
DB_PORT := 5432
start-dev-env:
@docker run -d --name postgres -p $(DB_PORT):5432 -e POSTGRES_PASSWORD=$(DB_PWD) -e POSTGRES_USER=$(DB_USER) -e POSTGRES_DB=$(DB_NAME) postgres > /dev/null && echo "Posgress started up"
@docker run -it --link postgres:postgres -v $(realpath .):/db --rm -e DB_USER=$(DB_USER) -e DB_NAME=$(DB_NAME) postgres sh -c '/db/pg-ready.sh'
stop-dev-env:
@docker rm -f postgres > /dev/null || true
sql-cli:
@docker run -it --link postgres:postgres --rm postgres sh -c 'PGPASSWORD=$(DB_PWD) exec psql -h "$$POSTGRES_PORT_5432_TCP_ADDR" -p "$$POSTGRES_PORT_5432_TCP_PORT" -U $(DB_USER) -d $(DB_NAME)'
go-run:
@go run main.go -pg="postgres://$(DB_USER):$(DB_PWD)@$(DOCKER_IP):$(DB_PORT)/$(DB_NAME)?sslmode=disable"
go-bench:
@go test -v -bench . -benchmem -pg="postgres://$(DB_USER):$(DB_PWD)@$(DOCKER_IP):$(DB_PORT)/$(DB_NAME_TEST)?sslmode=disable"
deps:
@go get github.com/lib/pq github.com/satori/go.uuid
#!/bin/bash
ready=1
until [ $ready == 0 ]; do
pg_isready -q -h $POSTGRES_PORT_5432_TCP_ADDR -p $POSTGRES_PORT_5432_TCP_PORT -U $DB_USER -d $DB_NAME
ready=$?
sleep 0.5s
done
PASS
BenchmarkUUIDAsString_Insert-2 2000 502895 ns/op 656 B/op 18 allocs/op
BenchmarkUUIDAsBytes_Insert-2 3000 553112 ns/op 720 B/op 20 allocs/op
BenchmarkUUIDAsString_Retrieve-2 3000 575590 ns/op 952 B/op 26 allocs/op
BenchmarkUUIDAsBytes_Retrieve-2 3000 875052 ns/op 904 B/op 25 allocs/op
ok gist.github.com/ifraixedes/2b25ceaec4fa5b9e4533 7.322s
package uuidt_test
import (
"database/sql"
"flag"
"log"
"os"
"strings"
"testing"
_ "github.com/lib/pq"
"github.com/satori/go.uuid"
)
var db *sql.DB
func TestMain(m *testing.M) {
ta, tp := splitTestAndProgArgs()
fs := flag.NewFlagSet("", flag.ExitOnError)
pgURL := fs.String("pg", "", "PG String connection")
fs.Parse(tp)
var err error
db, err = sql.Open("postgres", *pgURL)
if err != nil {
log.Fatalf("Error opening db connection: %v", err)
}
err = db.Ping()
if err != nil {
log.Fatalf("Error establishing db connection: %v", err)
}
_, err = db.Exec("DROP TABLE IF EXISTS users")
if err != nil {
log.Fatalf("Error dropping table: %v", err)
}
db.Exec("CREATE TABLE users (id uuid NOT NULL);")
if err != nil {
log.Fatalf("Error creating table: %v", err)
}
os.Args = ta
flag.Parse()
os.Exit(m.Run())
}
func BenchmarkUUIDAsString_Insert(b *testing.B) {
id := uuid.NewV4()
sid := id.String()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := db.Exec("INSERT INTO users(id) VALUES($1)", sid)
if err != nil {
log.Fatalf("Error inserting UUID as string: %v", err)
}
}
}
func BenchmarkUUIDAsBytes_Insert(b *testing.B) {
id := uuid.NewV4()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := db.Exec("INSERT INTO users(id) VALUES($1)", id)
if err != nil {
log.Fatalf("Error inserting UUID as byte array: %v", err)
}
}
}
func BenchmarkUUIDAsString_Retrieve(b *testing.B) {
id := uuid.NewV4()
_, err := db.Exec("INSERT INTO users(id) VALUES($1)", id)
if err != nil {
log.Fatalf("Error inserting UUID as byte array: %v", err)
}
var pid string
b.ResetTimer()
for i := 0; i < b.N; i++ {
err := db.QueryRow("SELECT id FROM users WHERE id = $1", id).Scan(&pid)
if err != nil {
log.Fatalf("Error retrieving UUID as string: %v", err)
}
}
}
func BenchmarkUUIDAsBytes_Retrieve(b *testing.B) {
id := uuid.NewV4()
_, err := db.Exec("INSERT INTO users(id) VALUES($1)", id)
if err != nil {
log.Fatalf("Error inserting UUIDs as byte array: %v", err)
}
var pid uuid.UUID
b.ResetTimer()
for i := 0; i < b.N; i++ {
err := db.QueryRow("SELECT id FROM users WHERE id = $1", id).Scan(&pid)
if err != nil {
log.Fatalf("Error retrieving UUID as byte array: %v", err)
}
}
}
func splitTestAndProgArgs() (test []string, prog []string) {
test = make([]string, 1)
prog = make([]string, 0)
test[0] = os.Args[0]
for _, v := range os.Args[1:] {
if strings.HasPrefix(v, "-test.") {
test = append(test, v)
} else {
prog = append(prog, v)
}
}
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment