seaweedfs/unmaintained/stress_filer_upload/write_files/write_files.go

55 lines
1,019 B
Go
Raw Permalink Normal View History

2020-10-26 02:28:11 +00:00
package main
import (
"flag"
"fmt"
"math/rand"
"os"
2020-10-26 05:03:37 +00:00
"time"
2020-10-26 02:28:11 +00:00
)
var (
minSize = flag.Int("minSize", 1024, "min file size")
2020-10-26 05:03:37 +00:00
maxSize = flag.Int("maxSize", 3*1024*1024, "max file size")
fileCount = flag.Int("n", 1, "number of files to write")
2020-10-26 02:28:11 +00:00
blockSize = flag.Int("blockSizeKB", 4, "write block size")
toDir = flag.String("dir", ".", "destination directory")
)
func check(e error) {
if e != nil {
panic(e)
}
}
func main() {
flag.Parse()
2020-10-26 05:03:37 +00:00
block := make([]byte, *blockSize*1024)
2020-10-26 02:28:11 +00:00
for i := 0; i < *fileCount; i++ {
f, err := os.Create(fmt.Sprintf("%s/file%05d", *toDir, i))
check(err)
fileSize := *minSize + rand.Intn(*maxSize-*minSize)
2020-10-26 05:03:37 +00:00
startTime := time.Now()
fmt.Printf("write %s %d bytes: ", f.Name(), fileSize)
2020-10-26 02:28:11 +00:00
for x := 0; x < fileSize; {
rand.Read(block)
_, err = f.Write(block)
check(err)
2020-10-26 05:03:37 +00:00
x += len(block)
2020-10-26 02:28:11 +00:00
}
err = f.Close()
check(err)
2020-10-26 05:03:37 +00:00
fmt.Printf("%.02f MB/sec\n", float64(fileSize)*float64(time.Second)/float64(time.Now().Sub(startTime)*1024*1024))
2020-10-26 02:28:11 +00:00
}
}