-
Notifications
You must be signed in to change notification settings - Fork 1
/
taos_bench_test.go
92 lines (87 loc) · 2.38 KB
/
taos_bench_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
package taos_test
import (
"database/sql"
"fmt"
"testing"
"time"
)
var (
driverName = "taosSql"
user = "root"
password = "taosdata"
host = "localhost"
port = 6041
dataSourceName = fmt.Sprintf("%s:%s@/tcp(%s:%d)/%s?interpolateParams=true", user, password, host, port, "log")
)
func TestMain(m *testing.M) {
b := testing.B{}
db, err := sql.Open(driverName, dataSourceName)
if err != nil {
b.Fatalf("error on: sql.open %s", err.Error())
}
defer db.Close()
_, err = db.Exec("create database if not exists bench_test")
if err != nil {
b.Fatalf("create database banch_test error %s", err.Error())
}
_, err = db.Exec("drop table IF EXISTS bench_test.test_insert")
if err != nil {
b.Fatalf("drop table bench_test.test_insert error %s", err.Error())
}
_, err = db.Exec("create table bench_test.test_insert (ts timestamp ,value double)")
if err != nil {
b.Fatalf("create table bench_test.test_insert error %s", err.Error())
}
_, err = db.Exec("drop table IF EXISTS bench_test.test_select")
if err != nil {
b.Fatalf("drop table bench_test.test_select error %s", err.Error())
}
_, err = db.Exec("create table bench_test.test_select (ts timestamp ,value double)")
if err != nil {
b.Fatalf("create table bench_test.test_select error %s", err.Error())
}
for i := 0; i < 1; i++ {
_, err = db.Exec(fmt.Sprintf("insert into bench_test.test_select values ( now + %ds ,123.456)", i))
if err != nil {
b.Fatalf("insert data error %s", err)
}
}
m.Run()
}
func BenchmarkInsert(b *testing.B) {
db, err := sql.Open(driverName, dataSourceName)
if err != nil {
b.Fatalf("error on: sql.open %s", err.Error())
return
}
for i := 0; i < b.N; i++ {
_, err = db.Exec("insert into bench_test.test_insert values (now,123.456)")
if err != nil {
b.Fatalf("insert data error %s", err)
}
}
}
func BenchmarkSelect(b *testing.B) {
db, err := sql.Open(driverName, dataSourceName)
if err != nil {
b.Fatalf("error on: sql.open %s", err.Error())
return
}
for i := 0; i < b.N; i++ {
rows, err := db.Query("select * from bench_test.test_select")
if err != nil {
b.Fatalf("select data error %s", err.Error())
}
var t time.Time
var s float64
for rows.Next() {
err := rows.Scan(&t, &s)
if err != nil {
b.Fatalf("scan error %s", err.Error())
}
if s != 123.456 {
b.Fatalf("result error expect 123.456 got %f", s)
}
}
}
}