main.go 2.58 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
package main

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

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

Jakub Sztandera's avatar
Jakub Sztandera committed
15
	random "github.com/jbenet/go-random"
tavit ohanian's avatar
tavit ohanian committed
16
	config "gitlab.dms3.io/dms3/go-dms3-config"
17 18 19
)

var (
tavit ohanian's avatar
tavit ohanian committed
20
	debug  = flag.Bool("debug", false, "direct dms3 output to console")
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 47 48 49 50 51 52 53 54 55 56
	online = flag.Bool("online", false, "run the benchmarks with a running daemon")
)

func main() {
	flag.Parse()
	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) {
	var benchmarkError 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 {
				benchmarkError = err
				b.Fatal(err)
			}
			defer os.RemoveAll(tmpDir)

			env := append(
57
				[]string{fmt.Sprintf("%s=%s", config.EnvDir, path.Join(tmpDir, config.DefaultPathName))}, // first in order to override
58 59 60 61 62 63 64 65 66 67
				os.Environ()...,
			)
			setupCmd := func(cmd *exec.Cmd) {
				cmd.Env = env
				if *debug {
					cmd.Stdout = os.Stdout
					cmd.Stderr = os.Stderr
				}
			}

tavit ohanian's avatar
tavit ohanian committed
68
			initCmd := exec.Command("dms3", "init", "-b=2048")
69 70 71 72 73 74 75 76 77 78 79 80 81 82
			setupCmd(initCmd)
			if err := initCmd.Run(); err != nil {
				benchmarkError = err
				b.Fatal(err)
			}

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

83 84 85 86
			if err := random.WritePseudoRandomBytes(amount, f, seed); err != nil {
				benchmarkError = err
				b.Fatal(err)
			}
87 88 89 90 91 92 93 94
			if err := f.Close(); err != nil {
				benchmarkError = err
				b.Fatal(err)
			}

			func() {
				// FIXME online mode isn't working. client complains that it cannot open leveldb
				if *online {
tavit ohanian's avatar
tavit ohanian committed
95
					daemonCmd := exec.Command("dms3", "daemon")
96 97 98 99 100
					setupCmd(daemonCmd)
					if err := daemonCmd.Start(); err != nil {
						benchmarkError = err
						b.Fatal(err)
					}
101 102 103 104
					defer func() {
						_ = daemonCmd.Process.Signal(os.Interrupt)
						_ = daemonCmd.Wait()
					}()
105 106 107
				}

				b.StartTimer()
tavit ohanian's avatar
tavit ohanian committed
108
				addCmd := exec.Command("dms3", "add", f.Name())
109 110 111 112 113 114 115 116 117 118 119 120 121 122
				setupCmd(addCmd)
				if err := addCmd.Run(); err != nil {
					benchmarkError = err
					b.Fatal(err)
				}
				b.StopTimer()
			}()
		}
	})
	if benchmarkError != nil {
		return nil, benchmarkError
	}
	return &results, nil
}