@cryptotaxi247 / kubo / commits / f1333d641

bench: offline add

Brian Tiger Chow committed Jan 20, 2015 at 04:26 UTC f1333d64193887fb36d01b131ab4d0eeac75319e
1 file changed +79
test/bench/offline_add/main.go new
+79
@@ -0,0 +1,79 @@
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/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-random"
13 + "github.com/jbenet/go-ipfs/repo/config"
14 + "github.com/jbenet/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, ".go-ipfs")))
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 +}