add pg-spam
This commit is contained in:
55
cmd/pg-spam/main.go
Normal file
55
cmd/pg-spam/main.go
Normal 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()
|
||||||
|
})
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user