github.com/keltia/go-ipfs@v0.3.8-0.20150909044612-210793031c63/test/bench/bench_cli_ipfs_add/main.go (about)

     1  package main
     2  
     3  import (
     4  	"flag"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"log"
     8  	"os"
     9  	"os/exec"
    10  	"path"
    11  	"testing"
    12  
    13  	"github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-random"
    14  	"github.com/ipfs/go-ipfs/repo/config"
    15  	"github.com/ipfs/go-ipfs/thirdparty/unit"
    16  )
    17  
    18  var (
    19  	debug  = flag.Bool("debug", false, "direct IPFS output to console")
    20  	online = flag.Bool("online", false, "run the benchmarks with a running daemon")
    21  )
    22  
    23  func main() {
    24  	flag.Parse()
    25  	if err := compareResults(); err != nil {
    26  		log.Fatal(err)
    27  	}
    28  }
    29  
    30  func compareResults() error {
    31  	var amount unit.Information
    32  	for amount = 10 * unit.MB; amount > 0; amount = amount * 2 {
    33  		if results, err := benchmarkAdd(int64(amount)); err != nil { // TODO compare
    34  			return err
    35  		} else {
    36  			log.Println(amount, "\t", results)
    37  		}
    38  	}
    39  	return nil
    40  }
    41  
    42  func benchmarkAdd(amount int64) (*testing.BenchmarkResult, error) {
    43  	var benchmarkError error
    44  	results := testing.Benchmark(func(b *testing.B) {
    45  		b.SetBytes(amount)
    46  		for i := 0; i < b.N; i++ {
    47  			b.StopTimer()
    48  			tmpDir, err := ioutil.TempDir("", "")
    49  			if err != nil {
    50  				benchmarkError = err
    51  				b.Fatal(err)
    52  			}
    53  			defer os.RemoveAll(tmpDir)
    54  
    55  			env := append(
    56  				[]string{fmt.Sprintf("%s=%s", config.EnvDir, path.Join(tmpDir, config.DefaultPathName))}, // first in order to override
    57  				os.Environ()...,
    58  			)
    59  			setupCmd := func(cmd *exec.Cmd) {
    60  				cmd.Env = env
    61  				if *debug {
    62  					cmd.Stdout = os.Stdout
    63  					cmd.Stderr = os.Stderr
    64  				}
    65  			}
    66  
    67  			initCmd := exec.Command("ipfs", "init", "-f", "-b=1024")
    68  			setupCmd(initCmd)
    69  			if err := initCmd.Run(); err != nil {
    70  				benchmarkError = err
    71  				b.Fatal(err)
    72  			}
    73  
    74  			const seed = 1
    75  			f, err := ioutil.TempFile("", "")
    76  			if err != nil {
    77  				benchmarkError = err
    78  				b.Fatal(err)
    79  			}
    80  			defer os.Remove(f.Name())
    81  
    82  			random.WritePseudoRandomBytes(amount, f, seed)
    83  			if err := f.Close(); err != nil {
    84  				benchmarkError = err
    85  				b.Fatal(err)
    86  			}
    87  
    88  			func() {
    89  				// FIXME online mode isn't working. client complains that it cannot open leveldb
    90  				if *online {
    91  					daemonCmd := exec.Command("ipfs", "daemon")
    92  					setupCmd(daemonCmd)
    93  					if err := daemonCmd.Start(); err != nil {
    94  						benchmarkError = err
    95  						b.Fatal(err)
    96  					}
    97  					defer daemonCmd.Wait()
    98  					defer daemonCmd.Process.Signal(os.Interrupt)
    99  				}
   100  
   101  				b.StartTimer()
   102  				addCmd := exec.Command("ipfs", "add", f.Name())
   103  				setupCmd(addCmd)
   104  				if err := addCmd.Run(); err != nil {
   105  					benchmarkError = err
   106  					b.Fatal(err)
   107  				}
   108  				b.StopTimer()
   109  			}()
   110  		}
   111  	})
   112  	if benchmarkError != nil {
   113  		return nil, benchmarkError
   114  	}
   115  	return &results, nil
   116  }