main.go 1.62 KB
Newer Older
Brian Tiger Chow's avatar
Brian Tiger Chow committed
1 2 3 4 5 6 7 8 9 10 11
package main

import (
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"os/exec"
	"path"
	"testing"

tavit ohanian's avatar
tavit ohanian committed
12
	"gitlab.dms3.io/dms3/go-dms3/thirdparty/unit"
13

Jakub Sztandera's avatar
Jakub Sztandera committed
14
	random "github.com/jbenet/go-random"
tavit ohanian's avatar
tavit ohanian committed
15
	config "gitlab.dms3.io/dms3/go-dms3-config"
Brian Tiger Chow's avatar
Brian Tiger Chow committed
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
)

func main() {
	if err := compareResults(); err != nil {
		log.Fatal(err)
	}
}

func compareResults() error {
	var amount unit.Information
	for amount = 10 * unit.MB; amount > 0; amount = amount * 2 {
		if results, err := benchmarkAdd(int64(amount)); err != nil { // TODO compare
			return err
		} else {
			log.Println(amount, "\t", results)
		}
	}
	return nil
}

func benchmarkAdd(amount int64) (*testing.BenchmarkResult, error) {
	results := testing.Benchmark(func(b *testing.B) {
		b.SetBytes(amount)
		for i := 0; i < b.N; i++ {
			b.StopTimer()
			tmpDir, err := ioutil.TempDir("", "")
			if err != nil {
				b.Fatal(err)
			}
			defer os.RemoveAll(tmpDir)

47
			env := append(os.Environ(), fmt.Sprintf("%s=%s", config.EnvDir, path.Join(tmpDir, config.DefaultPathName)))
Brian Tiger Chow's avatar
Brian Tiger Chow committed
48 49 50 51
			setupCmd := func(cmd *exec.Cmd) {
				cmd.Env = env
			}

tavit ohanian's avatar
tavit ohanian committed
52
			cmd := exec.Command("dms3", "init", "-b=2048")
Brian Tiger Chow's avatar
Brian Tiger Chow committed
53 54 55 56 57 58 59 60 61 62 63 64
			setupCmd(cmd)
			if err := cmd.Run(); err != nil {
				b.Fatal(err)
			}

			const seed = 1
			f, err := ioutil.TempFile("", "")
			if err != nil {
				b.Fatal(err)
			}
			defer os.Remove(f.Name())

65 66 67 68
			err = random.WritePseudoRandomBytes(amount, f, seed)
			if err != nil {
				b.Fatal(err)
			}
Brian Tiger Chow's avatar
Brian Tiger Chow committed
69 70 71 72 73
			if err := f.Close(); err != nil {
				b.Fatal(err)
			}

			b.StartTimer()
tavit ohanian's avatar
tavit ohanian committed
74
			cmd = exec.Command("dms3", "add", f.Name())
Brian Tiger Chow's avatar
Brian Tiger Chow committed
75 76 77 78 79 80 81 82 83
			setupCmd(cmd)
			if err := cmd.Run(); err != nil {
				b.Fatal(err)
			}
			b.StopTimer()
		}
	})
	return &results, nil
}