github.com/hanwen/go-fuse@v1.0.0/example/benchmark-read-throughput/readbench.go (about)

     1  // Copyright 2017 the Go-FUSE Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // readbench is a benchmark helper for measuring throughput on
     6  // single-file reads out of a FUSE filesystem.
     7  package main
     8  
     9  import (
    10  	"flag"
    11  	"fmt"
    12  	"log"
    13  	"os"
    14  	"time"
    15  )
    16  
    17  func gulp(fn string, bs int) (int, error) {
    18  	f, err := os.Open(fn)
    19  	if err != nil {
    20  		return 0, err
    21  	}
    22  	defer f.Close()
    23  
    24  	var tot int
    25  	buf := make([]byte, bs)
    26  	for {
    27  		n, _ := f.Read(buf[:])
    28  		tot += n
    29  		if n < len(buf) {
    30  			break
    31  		}
    32  	}
    33  
    34  	return tot, nil
    35  }
    36  
    37  func main() {
    38  	bs := flag.Int("bs", 32, "blocksize in kb")
    39  	mbLimit := flag.Int("limit", 1000, "amount of data to read in mb")
    40  	flag.Parse()
    41  	if len(flag.Args()) < 1 {
    42  		log.Fatal("readbench [-bs BLOCKSIZE -limit SIZE] file")
    43  	}
    44  	blocksize := *bs * 1024
    45  	totMB := 0.0
    46  	var totDT time.Duration
    47  
    48  	for totMB < float64(*mbLimit) {
    49  		t := time.Now()
    50  		n, err := gulp(flag.Arg(0), blocksize)
    51  		if err != nil {
    52  			log.Fatal(err)
    53  		}
    54  		dt := time.Now().Sub(t)
    55  		mb := float64(n) / (1 << 20)
    56  
    57  		totMB += mb
    58  		totDT += dt
    59  	}
    60  	fmt.Printf("block size %d kb: %.1f MB in %v: %.2f MBs/s\n", *bs, totMB, totDT, totMB/float64(totDT)*1e9)
    61  }