github.com/keltia/go-ipfs@v0.3.8-0.20150909044612-210793031c63/test/bench/offline_add/main.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "log" 7 "os" 8 "os/exec" 9 "path" 10 "testing" 11 12 "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-random" 13 "github.com/ipfs/go-ipfs/repo/config" 14 "github.com/ipfs/go-ipfs/thirdparty/unit" 15 ) 16 17 func main() { 18 if err := compareResults(); err != nil { 19 log.Fatal(err) 20 } 21 } 22 23 func compareResults() error { 24 var amount unit.Information 25 for amount = 10 * unit.MB; amount > 0; amount = amount * 2 { 26 if results, err := benchmarkAdd(int64(amount)); err != nil { // TODO compare 27 return err 28 } else { 29 log.Println(amount, "\t", results) 30 } 31 } 32 return nil 33 } 34 35 func benchmarkAdd(amount int64) (*testing.BenchmarkResult, error) { 36 results := testing.Benchmark(func(b *testing.B) { 37 b.SetBytes(amount) 38 for i := 0; i < b.N; i++ { 39 b.StopTimer() 40 tmpDir, err := ioutil.TempDir("", "") 41 if err != nil { 42 b.Fatal(err) 43 } 44 defer os.RemoveAll(tmpDir) 45 46 env := append(os.Environ(), fmt.Sprintf("%s=%s", config.EnvDir, path.Join(tmpDir, config.DefaultPathName))) 47 setupCmd := func(cmd *exec.Cmd) { 48 cmd.Env = env 49 } 50 51 cmd := exec.Command("ipfs", "init", "-f", "-b=1024") 52 setupCmd(cmd) 53 if err := cmd.Run(); err != nil { 54 b.Fatal(err) 55 } 56 57 const seed = 1 58 f, err := ioutil.TempFile("", "") 59 if err != nil { 60 b.Fatal(err) 61 } 62 defer os.Remove(f.Name()) 63 64 random.WritePseudoRandomBytes(amount, f, seed) 65 if err := f.Close(); err != nil { 66 b.Fatal(err) 67 } 68 69 b.StartTimer() 70 cmd = exec.Command("ipfs", "add", f.Name()) 71 setupCmd(cmd) 72 if err := cmd.Run(); err != nil { 73 b.Fatal(err) 74 } 75 b.StopTimer() 76 } 77 }) 78 return &results, nil 79 }