github.com/hanwen/go-fuse@v1.0.0/benchmark/bulkstat/main.go (about)

     1  // Copyright 2016 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  package main
     6  
     7  import (
     8  	"bufio"
     9  	"flag"
    10  	"log"
    11  	"os"
    12  	"path/filepath"
    13  	"sync"
    14  )
    15  
    16  func BulkStat(parallelism int, files []string) {
    17  	todo := make(chan string, len(files))
    18  	var wg sync.WaitGroup
    19  	wg.Add(parallelism)
    20  	for i := 0; i < parallelism; i++ {
    21  		go func() {
    22  			for {
    23  				fn := <-todo
    24  				if fn == "" {
    25  					break
    26  				}
    27  				_, err := os.Lstat(fn)
    28  				if err != nil {
    29  					log.Fatal("All stats should succeed:", err)
    30  				}
    31  			}
    32  			wg.Done()
    33  		}()
    34  	}
    35  
    36  	for _, v := range files {
    37  		todo <- v
    38  	}
    39  	close(todo)
    40  	wg.Wait()
    41  }
    42  
    43  func ReadLines(name string) []string {
    44  	f, err := os.Open(name)
    45  	if err != nil {
    46  		log.Fatal("ReadLines: ", err)
    47  	}
    48  	defer f.Close()
    49  	r := bufio.NewReader(f)
    50  
    51  	l := []string{}
    52  	for {
    53  		line, _, err := r.ReadLine()
    54  		if line == nil || err != nil {
    55  			break
    56  		}
    57  
    58  		fn := string(line)
    59  		l = append(l, fn)
    60  	}
    61  	if len(l) == 0 {
    62  		log.Fatal("no files added")
    63  	}
    64  
    65  	return l
    66  }
    67  
    68  func main() {
    69  	N := flag.Int("N", 1000, "how many files to stat")
    70  	cpu := flag.Int("cpu", 1, "how many threads to use")
    71  	prefix := flag.String("prefix", "", "mount point")
    72  	quiet := flag.Bool("quiet", false, "be quiet")
    73  	flag.Parse()
    74  
    75  	f := flag.Arg(0)
    76  	files := ReadLines(f)
    77  	for i, f := range files {
    78  		files[i] = filepath.Join(*prefix, f)
    79  	}
    80  	if !*quiet {
    81  		log.Printf("statting %d with %d threads; first file %s (%d names)", *N, *cpu, files[0], len(files))
    82  	}
    83  	todo := *N
    84  	for todo > 0 {
    85  		if len(files) > todo {
    86  			files = files[:todo]
    87  		}
    88  		BulkStat(*cpu, files)
    89  		todo -= len(files)
    90  	}
    91  }