-
Notifications
You must be signed in to change notification settings - Fork 0
/
Write-Insert.go
65 lines (57 loc) · 1.67 KB
/
Write-Insert.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
package main
import (
"database/sql"
"fmt"
"time"
_ "github.com/mattn/go-sqlite3"
"os"
)
func main() {
ops:=10000
database, _ :=
sql.Open("sqlite3", "./bogo.db")
statement, _ :=
database.Prepare("CREATE TABLE IF NOT EXISTS store (key TEXT PRIMARY KEY, value TEXT)")
statement.Exec()
start_sql:= time.Now()
for i:=0; i<ops; i++ {
statement, _ =
database.Prepare("INSERT INTO store (key, value) VALUES (?, ?)")
statement.Exec("RANDOMVALUE", "ASDASKGHVASDKV")
}
duration_sql := time.Since(start_sql)
fmt.Println("IOPS: ops/sec")
fmt.Println("SQL insert:", float64(ops)/float64(duration_sql.Seconds()))
filename := "file.txt"
f, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
defer f.Close()
start_file := time.Now()
for i:=0; i<ops; i++ {
text:= "INSERT INTO store (key, value) VALUES (RANDOMVALUE, ASDASKGHVASDKV)\n"
if _, err = f.WriteString(text); err != nil {
panic(err)
}
f.Sync()
}
duration_file := time.Since(start_file)
//fmt.Println("File append:",duration_file)
fmt.Println("File appemdt:", float64(ops)/float64(duration_file.Seconds()))
}
/*
==== Prepare outside for loop and no fsync for file
IOPS: ops/sec
SQL insert: 84416.0415229677
File append: 703207.7807409391
=== Prepare inside for loop and no fsync for file
IOPS: ops/sec
SQL insert: 36916.05464893253
File appemdt: 693832.2338636478
== Prepare outside for loop and with fsync after each write
IOPS: ops/sec
SQL insert: 82598.394160016
File append: 9116.557306587609
== Prepare outside for loop and with fsync after all writes
IOPS: ops/sec
SQL insert: 73898.50764180772
File append: 386102.2942507206
*/