/
bench_oci8.go
71 lines (63 loc) · 1.46 KB
/
bench_oci8.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
// Copyright 2016 Tamás Gulácsi, Jia Lu. All rights reserved.
// Use of this source code is governed by The MIT License
// found in the accompanying LICENSE file.
package main
import (
"database/sql"
"flag"
"log"
"os"
"runtime/pprof"
"testing"
"time"
_ "github.com/mattn/go-oci8"
)
var (
DB *sql.DB
cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
N = flag.Int("N", 1000, "row count")
)
func init() {
flag.Parse()
drv, dsn := "oci8", os.ExpandEnv(flag.Arg(0))
var err error
if DB, err = sql.Open(drv, dsn); err != nil {
log.Fatalf("cannot connect with %q to %q: %v", drv, dsn, err)
}
if err = DB.Ping(); err != nil {
log.Fatalf("cannot connect with %q to %q: %v", drv, dsn, err)
}
}
func main() {
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal(err)
}
log.Printf("Write CPU profile to %q.", f.Name())
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
result := testing.Benchmark(BenchmarkIter)
log.Println(result)
}
func BenchmarkIter(b *testing.B) {
rows, err := DB.Query("SELECT A.object_name from all_objects, all_objects A")
if err != nil {
b.Fatal(err)
}
defer rows.Close()
b.ReportAllocs()
start := time.Now()
b.ResetTimer()
i := 0
N := *N
for rows.Next() && i < N {
i++
}
b.StopTimer()
d := time.Since(start)
b.SetBytes(int64(i))
log.Printf("Iterated %d rows in %s: %.3f row/s.",
i, d, float64(i)/(float64(d)/float64(time.Second)))
}