forked from spiceai/sping
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
67 lines (52 loc) · 1.33 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main
import (
"context"
"flag"
"fmt"
"log"
"os"
"os/signal"
"runtime"
"strconv"
"syscall"
"time"
"github.com/spicehq/spar/pkg/spar"
"golang.org/x/sync/errgroup"
)
func main() {
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
flag.Parse()
if flag.NArg() < 1 {
fmt.Println("Usage: spar <throws> <sql>")
fmt.Println()
fmt.Println(`Example: spar "SELECT * FROM eth.recent_blocks ORDER BY number DESC LIMIT 10"`)
return
}
throws, err := strconv.Atoi(flag.Arg(0))
if err != nil {
log.Fatalf("invalid throws: %s", err.Error())
}
sql := flag.Arg(1)
sparClient := spar.NewSparClient()
if err := sparClient.Init(); err != nil {
log.Fatalf("failed to initialize spar client: %s", err.Error())
}
defer sparClient.Close()
log.Printf("throwing %d times using %d CPUs.\n", throws, runtime.NumCPU())
errGroup, errGroupCtx := errgroup.WithContext(ctx)
errGroup.SetLimit(runtime.NumCPU())
startTime := time.Now()
for j := 0; j < throws; j++ {
errGroup.Go(func() error {
return sparClient.Throw(errGroupCtx, sql)
})
}
if err := errGroup.Wait(); err != nil {
log.Fatalf("failed to throw: %s", err.Error())
}
duration := time.Since(startTime)
fmt.Printf("Done in %s\n\n", duration.Round(time.Microsecond))
sparClient.PrintStats()
fmt.Println()
}