github.com/cloud-foundations/dominator@v0.0.0-20221004181915-6e4fee580046/cmd/fsreadslow/main.go (about) 1 package main 2 3 import ( 4 "bufio" 5 "fmt" 6 "io" 7 "os" 8 "time" 9 10 "github.com/Cloud-Foundations/Dominator/lib/format" 11 "github.com/Cloud-Foundations/Dominator/lib/fsbench" 12 "github.com/Cloud-Foundations/Dominator/lib/fsrateio" 13 ) 14 15 // Benchmark the read speed of the underlying block device for a given file. 16 func main() { 17 pathname := "/" 18 if len(os.Args) == 2 { 19 pathname = os.Args[1] 20 } 21 bytesPerSecond, blocksPerSecond, err := fsbench.GetReadSpeed(pathname) 22 if err != nil { 23 fmt.Fprintf(os.Stderr, "Error! %s\n", err) 24 return 25 } 26 ctx := fsrateio.NewReaderContext(bytesPerSecond, blocksPerSecond, 0) 27 fmt.Println(ctx) 28 var file *os.File 29 file, err = os.Open(pathname) 30 if err != nil { 31 fmt.Fprintf(os.Stderr, "Error! %s\n", err) 32 return 33 } 34 rd := bufio.NewReader(ctx.NewReader(file)) 35 buffer := make([]byte, 65536) 36 timeStart := time.Now() 37 tread := 0 38 for { 39 n := 0 40 n, err = rd.Read(buffer) 41 if n < 1 && err == io.EOF { 42 break 43 } 44 if err != nil { 45 fmt.Fprintf(os.Stderr, "Error! %s\n", err) 46 return 47 } 48 tread += n 49 } 50 bytesPerSecond = uint64(float64(tread) / time.Since(timeStart).Seconds()) 51 fmt.Printf("%s/s\n", format.FormatBytes(bytesPerSecond)) 52 }