-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
136 lines (108 loc) · 2.33 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
package main
import (
"context"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"sync"
"time"
)
//noinspection GoUnusedConst
const (
kb = 1024
mb = kb * 1024
gb = mb * 1024
)
func main() {
os.Exit(run())
}
func run() int {
var (
numberOfConcurrent int
blockSize int
)
var (
wg = sync.WaitGroup{}
rootCtx = context.Background()
mainCtx, cancel = context.WithCancel(rootCtx)
)
flag.IntVar(&numberOfConcurrent, "g", 100, "goroutine count")
flag.IntVar(&blockSize, "b", 1024, "block size per write")
flag.Parse()
log.Printf("goroutine count: %d\tblock size: %d\n", numberOfConcurrent, blockSize)
dir, err := ioutil.TempDir("", "diskio")
if err != nil {
log.Fatal(err)
}
log.Println("[Dir ] ", dir)
wg.Add(numberOfConcurrent)
for gc := 0; gc < numberOfConcurrent; gc++ {
go func(index int, ctx context.Context) {
defer wg.Done()
var (
fname = fmt.Sprintf("disk-io-data.%04d", index)
fpath = filepath.Join(dir, fname)
)
// https://qiita.com/kamol/items/fae07e8533b36f553714
file, err := os.OpenFile(fpath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0744)
if err != nil {
log.Fatal(err)
}
//noinspection GoUnhandledErrorResult
defer file.Close()
for i := 0; true; i++ {
data := make([]byte, blockSize)
for j := 0; j < blockSize; j++ {
data[j] = 1
}
_, _ = file.Write(data)
_ = file.Sync()
select {
case <-ctx.Done():
return
default:
}
}
}(gc, mainCtx)
}
wg.Add(1)
go func(ctx context.Context) {
defer wg.Done()
var (
tick = time.Tick(15 * time.Second)
)
for {
select {
case <-ctx.Done():
return
case <-tick:
// https://stackoverflow.com/questions/32482673/how-to-get-directory-total-size
var totalSize uint64 = 0
files, _ := ioutil.ReadDir(dir)
for _, f := range files {
fpath := filepath.Join(dir, f.Name())
stat, _ := os.Stat(fpath)
totalSize += uint64(stat.Size())
}
log.Printf("[Size] %04d MB\n", int(totalSize/mb))
}
}
}(mainCtx)
fmt.Println("Press ENTER to stop...")
s := ""
_, _ = fmt.Scanln(&s)
cancel()
wg.Wait()
fmt.Println("Remove temporary directory: ", dir)
err = os.RemoveAll(dir)
if err != nil {
log.Println(err)
}
fmt.Println("DONE")
fmt.Println("Press ENTER to exit...")
_, _ = fmt.Scanln(&s)
return 0
}