add pg-spam

This commit is contained in:
Bel LaPointe
2026-03-09 09:45:21 -06:00
parent 39bd53b7f1
commit 82400ca0b2

55
cmd/pg-spam/main.go Normal file
View File

@@ -0,0 +1,55 @@
package main
import (
"context"
"database/sql"
"flag"
"fmt"
"io"
"log"
"os"
"sync/atomic"
"time"
"gitea.bel.blue/bel/with"
_ "github.com/lib/pq"
)
func main() {
if err := with.Context(run); err != nil {
panic(err)
}
}
func run(ctx context.Context) error {
fs := flag.NewFlagSet(os.Args[0], flag.ContinueOnError)
c := fs.String("c", "sqlite://", "conn string")
rps := fs.Int("rps", 1, "connections per second")
if err := fs.Parse(os.Args[1:]); err != nil {
panic(err)
}
return with.SQL(ctx, *c, func(pg *sql.DB) error {
stderr := log.Writer()
log.SetOutput(io.Discard)
var okays atomic.Uint64
var errs atomic.Uint64
d := time.Second / time.Duration(*rps)
go with.Every(ctx, 1*time.Second, func() {
fmt.Fprintf(stderr, "%v connections, %v failures\n", okays.Load(), errs.Load())
})
with.GoEvery(ctx, d, func() {
if err := with.SQL(ctx, *c, func(pg *sql.DB) error {
return nil
}); err != nil {
fmt.Fprintf(stderr, "err: %v\n", err)
errs.Add(1)
} else {
okays.Add(1)
}
})
return ctx.Err()
})
}