forked from theplant/batchputs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
81 lines (68 loc) · 1.93 KB
/
example_test.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
package batchputs_test
import (
"fmt"
"os"
"time"
"github.com/theplant/batchputs"
)
/*
With this example, We created 30k records with 3 columns each, and inserts it into database in batch. and then we updates 20k records.
semaphoreci.com runs this example for inserting 30k records and updates 20k records totally less than 2 seconds.
```
=== RUN ExamplePut_perf
--- PASS: ExamplePut_perf (1.73s)
```
[![Build Status](https://semaphoreci.com/api/v1/theplant/batchputs/branches/master/badge.svg)](https://semaphoreci.com/theplant/batchputs)
*/
func ExamplePut_perf() {
db := openAndMigrate()
// with table
// CREATE TABLE countries
// (
// code VARCHAR(50) PRIMARY KEY NOT NULL,
// short_name TEXT,
// special_notes TEXT,
// region TEXT,
// income_group TEXT,
// count INTEGER,
// avg_age NUMERIC
// );
rows := [][]interface{}{}
for i := 0; i < 30000; i++ {
rows = append(rows, []interface{}{
fmt.Sprintf("CODE_%d", i),
fmt.Sprintf("short name %d", i),
i,
})
}
columns := []string{"code", "short_name", "count"}
dialect := os.Getenv("DB_DIALECT")
if len(dialect) == 0 {
dialect = "postgres"
}
start := time.Now()
err := batchputs.Put(db.DB(), dialect, "countries", "code", columns, rows)
if err != nil {
panic(err)
}
duration := time.Since(start)
fmt.Println("Inserts 30000 records using less than 3 seconds:", duration.Seconds() < 3)
rows = [][]interface{}{}
for i := 0; i < 20000; i++ {
rows = append(rows, []interface{}{
fmt.Sprintf("CODE_%d", i),
fmt.Sprintf("short name %d", i),
i + 1,
})
}
start = time.Now()
err = batchputs.Put(db.DB(), dialect, "countries", "code", columns, rows)
if err != nil {
panic(err)
}
duration = time.Since(start)
fmt.Println("Updates 20000 records using less than 3 seconds:", duration.Seconds() < 3)
//Output:
// Inserts 30000 records using less than 3 seconds: true
// Updates 20000 records using less than 3 seconds: true
}