-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
159 lines (142 loc) · 3.48 KB
/
main.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package main
import (
"crypto/rand"
"database/sql"
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"path"
"strconv"
"sync"
"time"
_ "github.com/mattn/go-sqlite3"
)
type Task struct {
Id string
Output string
Status string
Cmd string
Args []string
}
func genId() string {
b := make([]byte, 16)
if _, err := rand.Read(b); err != nil {
panic(err)
}
return hex.EncodeToString(b)
}
func simulate(fn func(action string, t *Task)) {
t := &Task{
Id: genId(),
Status: "pending",
Cmd: "example",
Output: "",
Args: []string{"a", "b", "c"},
}
fn("new", t)
t.Status = "processing"
fn("status", t)
for i := 0; i < 10; i++ {
t.Output = strconv.Itoa(i) + "Some kind of output and whatever"
fn("out", t)
}
fn("rm", t)
}
func logPanic(err error) {
if err != nil {
log.Panic(err)
}
}
func makeSQLSimulator() (*sql.DB, func(action string, t *Task)) {
fmt.Println("Opening...")
db, err := sql.Open("sqlite3", "./tmp.db?_timeout=5000&_journal=WAL&_sync=1")
if err != nil {
log.Panic(err)
}
fmt.Println("Migrations...")
_, err = db.Exec(`
BEGIN;
CREATE TABLE IF NOT EXISTS tasks (
id varchar(16) NOT NULL PRIMARY KEY,
status varchar(16) NOT NULL,
cmd varchar(16) NOT NULL,
args text NOT NULL,
output text NULL
);
CREATE INDEX IF NOT EXISTS task_status_idx ON tasks (status);
COMMIT;
`)
logPanic(err)
return db, func(action string, t *Task) {
switch action {
case "new":
args, err := json.Marshal(t.Args)
logPanic(err)
_, err = db.Exec(`INSERT INTO tasks
(id, status, cmd, args)
VALUES
(@id, @status, @cmd, @args)
`, sql.Named("id", t.Id), sql.Named("status", t.Status), sql.Named("cmd", t.Cmd), sql.Named("args", string(args)))
logPanic(err)
case "status":
_, err = db.Exec(`UPDATE tasks SET status=@status WHERE id=@id`, sql.Named("id", t.Id), sql.Named("status", t.Status))
logPanic(err)
case "out":
_, err = db.Exec(`UPDATE tasks SET output=@out WHERE id=@id`, sql.Named("id", t.Id), sql.Named("out", t.Output))
logPanic(err)
case "rm":
_, err = db.Exec(`DELETE FROM tasks WHERE id=@id`, sql.Named("id", t.Id))
logPanic(err)
}
}
}
func fileSimulator(action string, t *Task) {
dir := "./tmp"
if action == "rm" {
logPanic(os.Remove(path.Join(dir, t.Id)))
return
}
b, err := json.Marshal(t)
if err != nil {
logPanic(err)
}
filename := path.Join(dir, t.Id)
tmpfile := filename + ".tmp"
logPanic(ioutil.WriteFile(tmpfile, b, 0777))
logPanic(os.Rename(tmpfile, filename))
}
func simulateN(name string, concurrency, num int, handler func(action string, t *Task)) {
wg := new(sync.WaitGroup)
start := time.Now()
wg.Add(concurrency)
for i := 0; i < concurrency; i++ {
go func() {
defer wg.Done()
for i := 0; i < num; i++ {
simulate(handler)
}
}()
}
wg.Wait()
fmt.Println(name, "ran", num*concurrency, "tasks in ", time.Since(start).String())
}
func main() {
os.Mkdir("./tmp", 0777)
db, sqlHandler := makeSQLSimulator()
defer db.Close()
simulateN("SQL", 100, 100, sqlHandler)
simulateN("SQL", 100, 100, sqlHandler)
simulateN("SQL", 100, 100, sqlHandler)
simulateN("SQL", 100, 100, sqlHandler)
simulateN("SQL", 100, 100, sqlHandler)
simulateN("SQL", 100, 100, sqlHandler)
simulateN("FILE", 100, 100, fileSimulator)
simulateN("FILE", 100, 100, fileSimulator)
simulateN("FILE", 100, 100, fileSimulator)
simulateN("FILE", 100, 100, fileSimulator)
simulateN("FILE", 100, 100, fileSimulator)
simulateN("FILE", 100, 100, fileSimulator)
}