-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbenchmark.go
68 lines (47 loc) · 1.35 KB
/
benchmark.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
68
package main
import (
"database/sql"
"flag"
"fmt"
"os"
_ "github.com/lib/pq"
)
const (
dbname = "homework"
dbuser = "postgres"
maxWorker = 100
)
func main() {
numWorkers := flag.Int("numWorkers", 1, fmt.Sprintf("Number of parallel connections to the server (min 1, max %d)", maxWorker))
file := flag.String("file", "", "CSV file with column format \"hostname, starttime, endtime\" with header intact.")
flag.Parse()
if *numWorkers < 0 || *numWorkers > maxWorker || *file == "" {
flag.PrintDefaults()
os.Exit(1)
}
connStr := fmt.Sprintf("user=%s dbname=%s sslmode=disable", dbuser, dbname)
db, err := sql.Open("postgres", connStr)
checkError(err)
defer db.Close()
// db.Open does not open the connection. Hence, validate DSN.
err = db.Ping()
// check if this validation would work with channels
checkError(err)
queries := parseCSV(*file + ".csv")
work := make(chan Request, len(queries))
defer close(work)
done := make(chan struct{})
defer close(done)
go request(work, queries, done)
NewScheduler(*numWorkers, db).schedule(work, done)
}
func request(work chan Request, queries []Query, done chan struct{}) {
c := make(chan Result)
results := make([]Result, 0, len(queries))
for _, query := range queries {
work <- Request{query, c}
results = append(results, <-c)
}
GetStats(results).print()
done <- struct{}{}
}