forked from jlpadilla/benchmark-postgres
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
76 lines (56 loc) · 1.7 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
68
69
70
71
72
73
74
75
76
package main
import (
"context"
"fmt"
"strings"
"time"
"github.com/fatih/color"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/jlpadilla/benchmark-postgres/pkg/dbclient"
"github.com/jlpadilla/benchmark-postgres/pkg/fileutil"
)
var database *pgxpool.Pool
func main() {
database = dbclient.GetConnection()
fmt.Println("\nStarting setup...")
processFile("./data/setup.sql", false)
fmt.Println("Setup done successfully.")
color.Blue("\nSTARTING BENCHMARKS")
color.Blue("\nDESCRIPTION: Insert records in the database.\n")
processDir("./data/insert", false)
color.Blue("\nDESCRIPTION: Update records in the database.\n")
processDir("./data/update", true)
color.Blue("\nDESCRIPTION: Query records in the database.\n")
processDir("./data/query", true)
color.Blue("\nDESCRIPTION: Delete records in the database.\n")
processDir("./data/delete", true)
color.Blue("\nDONE.")
}
/*****************************
Helper functions
*****************************/
func processDir(directoryName string, print bool) {
insertFiles := fileutil.GetFilesOnDir(directoryName)
for _, filename := range insertFiles {
fmt.Println("\nSCRIPT:", filename)
processFile(filename, print)
}
}
func processFile(filename string, print bool) {
externalSql := fileutil.ReadFile(filename)
requests := strings.Split(string(externalSql), ";")
startRoutine := time.Now()
for _, request := range requests {
if len(strings.TrimSpace(request)) > 0 {
res, err := database.Exec(context.Background(), request)
if err != nil {
fmt.Println("ERROR:", err)
}
if print {
fmt.Println("RESULT:", res.RowsAffected())
}
}
}
totalTime := time.Since(startRoutine).Milliseconds()
fmt.Println("TIME: ", totalTime, "ms")
}