-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileDescriptors.go
88 lines (81 loc) · 1.89 KB
/
fileDescriptors.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
package main
import (
"flag"
"fmt"
"log"
"os"
"runtime/pprof"
"strconv"
"sync"
"sync/atomic"
"time"
)
var totalSuccess int64
var totalFail int64
func Do(where string, numIter int, numBytes int, wg *sync.WaitGroup) {
var success int64
var failure int64
for i := 0; i < numIter; i++ {
f, err := os.Open(where)
if err != nil {
failure++
} else {
success++
}
b := make([]byte, numBytes)
f.Read(b)
f.Close()
}
atomic.AddInt64(&totalSuccess, success)
atomic.AddInt64(&totalFail, failure)
wg.Done()
}
func check(e error) {
if e != nil {
panic(e)
}
}
func main() {
var wg sync.WaitGroup
itr := flag.Int("i", 1000, "Loop trip per go routine.")
b := flag.Int("n", 100, "File size to read.")
g := flag.Int("g", 1000, "Level of concurrency while reading files.")
p := flag.Bool("p", false, "Collect PMU profile.")
flag.Parse()
numIter := *itr
numBytes := *b
numGoRoutines := *g
pmu := *p
fmt.Printf("numIter=%v, numBytes=%v, numGoRoutines=%v, PMU=%v\n", numIter, numBytes, numGoRoutines, pmu)
for i := 0; i < numGoRoutines; i++ {
s := strconv.Itoa(i)
f, err := os.Create("/tmp/xx/" + s)
check(err)
d2 := make([]byte, numBytes, numBytes)
_, err = f.Write(d2)
check(err)
f.Sync()
f.Close()
}
file, err := os.Create("file_io.prof")
if err != nil {
log.Fatal(err)
}
if pmu {
if err = pprof.StartCPUProfileWithConfig(pprof.CPUCycles(file, 30000000)); err != nil {
log.Fatal(err)
}
}
start := time.Now()
for i := 0; i < numGoRoutines; i++ {
wg.Add(1)
go Do("/tmp/xx/"+strconv.Itoa(i), numIter, numBytes, &wg)
}
wg.Wait()
elapsed := time.Since(start)
if pmu {
pprof.StopCPUProfile()
}
file.Close()
fmt.Printf("FD open:\n Successful=%v (%e/sec)\n Failed=%v (%e/sec)\n %f %% failure rate\n", totalSuccess, float64(totalSuccess)/elapsed.Seconds(), totalFail, float64(totalFail)/elapsed.Seconds(), float64(totalFail*100)/float64(totalFail+totalSuccess))
}