-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
94 lines (76 loc) · 1.85 KB
/
main_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
82
83
84
85
86
87
88
89
90
91
92
93
94
package main
import (
"database/sql"
"fmt"
"github.com/jmoiron/sqlx"
_ "github.com/lib/pq"
"os"
"testing"
)
var (
dbNative *sql.DB
dbSqlx *sqlx.DB
)
func init() {
var err error
dbNative, err = sql.Open("postgres", os.Getenv("DATABASE_URL"))
if err != nil {
panic(err)
}
dbSqlx = sqlx.NewDb(dbNative, "postgres")
fmt.Printf("Connected to database at %s", os.Getenv("DATABASE_URL"))
}
func cleandb() {
var err error
if _, err = dbNative.Exec("TRUNCATE TABLE users CASCADE"); err != nil {
panic(err)
}
if _, err = dbNative.Exec("TRUNCATE TABLE addresses CASCADE"); err != nil {
panic(err)
}
}
func validate(user User) bool {
if user.FirstName != "Tharsan" {
return false
}
return true
}
func BenchmarkNative(b *testing.B) {
// cleandb()
for i := 0; i < b.N; i++ {
rows, err := dbNative.Query("SELECT user_id, first_name, last_name, addresses.address_id, street, city, region, zip, country FROM users INNER JOIN addresses USING (address_id)")
if err != nil {
panic(err)
}
for rows.Next() {
user := User{}
err = rows.Scan(&user.UserId, &user.FirstName, &user.LastName, &user.Address.AddressId, &user.Address.Street, &user.Address.City, &user.Address.Region, &user.Address.Zip, &user.Address.Country)
if err != nil {
panic(err)
}
// if !validate(user) {
// fmt.Println("Sorry user not valid")
// b.Fail()
// }
}
}
}
func BenchmarkSqlx(b *testing.B) {
for i := 0; i < b.N; i++ {
rows, err := dbSqlx.Queryx("SELECT user_id, first_name, last_name, addresses.address_id, street, city, region, zip, country FROM users INNER JOIN addresses USING (address_id)")
if err != nil {
panic(err)
}
for rows.Next() {
user := User{}
err = rows.StructScan(&user)
if err != nil {
panic(err)
}
// if !validate(user) {
// fmt.Println("Sorry user not valid")
// b.Fail()
// }
}
}
}