-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
85 lines (67 loc) · 1.76 KB
/
client.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
77
78
79
80
81
82
83
84
85
// Please set the database constants in this file before running.
package main
import (
"database/sql"
"fmt"
"log"
"math/rand"
"os"
"runtime"
"strconv"
"time"
"github.com/smorz/k-challenge-client/challenge"
)
const (
// database info
DB_USER = "postgres"
DB_PASSWORD = "123456"
DB_NAME = "ktest"
)
func main() {
// just for studying program efficiency
startMoment := time.Now()
// setup log
log.SetFlags(log.LstdFlags | log.Lshortfile)
// Check the accuracy of the input
if len(os.Args) < 2 {
log.Fatal("The command must have a positive integer input next to it")
}
if len(os.Args) > 2 {
log.Fatal("The command must have only one input. no more")
}
count, err := strconv.Atoi(os.Args[1])
if err != nil || count <= 0 {
log.Fatal("The input must be a positive integer")
}
// used in TradeGenerator
rand.Seed(time.Now().UnixNano())
firstDay := time.Now().AddDate(-1, 0, 0)
// Creating an instance of generator
tg, _ := challenge.NewTradeGenerator(firstDay, count)
// connecting to database
dbinfo := fmt.Sprintf("user=%s password=%s dbname=%s sslmode=disable", DB_USER, DB_PASSWORD, DB_NAME)
db, err := sql.Open("postgres", dbinfo)
if err != nil {
log.Fatal(err)
}
defer db.Close()
fmt.Println("connected to postgresql")
// setup database
db.SetMaxIdleConns(10)
db.SetMaxOpenConns(10)
db.SetConnMaxLifetime(10)
// Creating an instance of Copier
c, err := challenge.NewCopier(db, tg)
if err != nil {
log.Fatal(err)
}
// Obtain the number of CPUs
cores := runtime.GOMAXPROCS(-1)
// Start Generating and inserting
// Each row is generate just before the generation of the copy statement.
if err := c.Start(cores); err != nil {
log.Fatal(err)
}
// showing Result
fmt.Printf("elapsed time: %v\n", time.Since(startMoment))
}