Skip to content

Instantly share code, notes, and snippets.

@threadedstream
Created March 13, 2024 20:03
Show Gist options
  • Save threadedstream/c85722e27cd4fed87c56753a56c541ab to your computer and use it in GitHub Desktop.
Save threadedstream/c85722e27cd4fed87c56753a56c541ab to your computer and use it in GitHub Desktop.
Comparison of clickhouse and pgx poolers.
package main
import (
"context"
"fmt"
"log"
"sync"
"github.com/ClickHouse/clickhouse-go/v2"
"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgxpool"
)
const (
clickhouseDatabaseHost = "0.0.0.0"
clickhouseDatabasePort = 9010
clickhouseDatabaseName = "my_database"
clickhouseDatabaseUsername = "username"
clickhouseDatabasePassword = "password"
)
func getConn() clickhouse.Conn {
conn, err := clickhouse.Open(&clickhouse.Options{
Addr: []string{fmt.Sprintf("%s:%d", clickhouseDatabaseHost, clickhouseDatabasePort)},
Auth: clickhouse.Auth{
Database: clickhouseDatabaseName,
Username: clickhouseDatabaseUsername,
Password: clickhouseDatabasePassword,
},
MaxIdleConns: 5,
MaxOpenConns: 5,
})
if err != nil {
log.Fatal(err)
}
if err = conn.Ping(context.Background()); err != nil {
log.Fatal(err)
}
return conn
}
func doInsert(ctx context.Context, conn clickhouse.Conn) error {
query := "INSERT INTO test VALUES ($1, $2, $3)"
return conn.Exec(ctx, query, uuid.New().String(), "name", 23)
}
const (
postgresDSN = "postgres://username:password@localhost:5432/postgres"
)
func getPgPool() *pgxpool.Pool {
config, err := pgxpool.ParseConfig(postgresDSN)
if err != nil {
log.Fatal(err)
}
config.MaxConns = 5
config.MinConns = 1
pool, err := pgxpool.NewWithConfig(context.Background(), config)
if err != nil {
log.Fatal(err)
}
return pool
}
func doInsertPg(ctx context.Context, pool *pgxpool.Pool) error {
query := "INSERT INTO test VALUES ($1, $2, $3)"
_, err := pool.Exec(ctx, query, uuid.New().String(), "name", 23)
return err
}
func main() {
conn := getConn()
ctx := context.Background()
wg := new(sync.WaitGroup)
for i := 0; i < 1000; i++ {
wg.Add(1)
go func() {
defer wg.Done()
if err := doInsert(ctx, conn); err != nil {
log.Println("error: ", err.Error())
}
}()
}
wg.Wait()
log.Println("open: ", conn.Stats().Open)
log.Println("idle: ", conn.Stats().Idle)
pool := getPgPool()
for i := 0; i < 1000; i++ {
wg.Add(1)
go func() {
defer wg.Done()
if err := doInsertPg(ctx, pool); err != nil {
log.Println("error: ", err.Error())
}
}()
}
wg.Wait()
log.Println("acquired: ", pool.Stat().AcquiredConns())
log.Println("idle: ", pool.Stat().IdleConns())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment