From fe7b926773a7d6a7ddf84dc2a10df27e5de052d9 Mon Sep 17 00:00:00 2001 From: Matt Jaffee Date: Sun, 13 Jan 2019 16:35:42 -0600 Subject: [PATCH] make sure more tests and benchmarks can have their temp dir set by flag This is to allow the directory to be set to where a particular disk is mounted during benchmarking. --- cluster_internal_test.go | 2 +- executor_internal_test.go | 2 +- executor_test.go | 38 ++++++++++++++++++++++++++++++++++++-- field_internal_test.go | 2 +- fragment_internal_test.go | 11 ++++------- holder_internal_test.go | 2 +- index_internal_test.go | 2 +- server_internal_test.go | 2 +- translate_test.go | 2 +- utils_internal_test.go | 2 +- view_internal_test.go | 2 +- 11 files changed, 49 insertions(+), 18 deletions(-) diff --git a/cluster_internal_test.go b/cluster_internal_test.go index 50602d385..a6cf437b2 100644 --- a/cluster_internal_test.go +++ b/cluster_internal_test.go @@ -83,7 +83,7 @@ func TestFragCombos(t *testing.T) { // newIndexWithTempPath returns a new instance of Index. func newIndexWithTempPath(name string) *Index { - path, err := ioutil.TempDir("", "pilosa-index-") + path, err := ioutil.TempDir(*TempDir, "pilosa-index-") if err != nil { panic(err) } diff --git a/executor_internal_test.go b/executor_internal_test.go index 5b786a45e..4177968d5 100644 --- a/executor_internal_test.go +++ b/executor_internal_test.go @@ -14,7 +14,7 @@ func TestExecutor_TranslateGroupByCall(t *testing.T) { e := &executor{ Holder: NewHolder(), } - e.Holder.Path, _ = ioutil.TempDir("", "") + e.Holder.Path, _ = ioutil.TempDir(*TempDir, "") err := e.Holder.Open() if err != nil { t.Fatalf("opening holder: %v", err) diff --git a/executor_test.go b/executor_test.go index 110717971..ac349f333 100644 --- a/executor_test.go +++ b/executor_test.go @@ -16,7 +16,9 @@ package pilosa_test import ( "context" + "flag" "fmt" + "io/ioutil" "math/rand" "reflect" "strconv" @@ -33,6 +35,20 @@ import ( "github.com/pkg/errors" ) +var ( + TempDir *string +) + +func init() { + tdflag := flag.Lookup("temp-dir") + if tdflag == nil { + TempDir = flag.String("temp-dir", "", "Directory in which to place temporary data (e.g. for benchmarking). Useful if you are trying to benchmark different storage configurations.") + } else { + s := tdflag.Value.String() + TempDir = &s + } +} + // Ensure a row query can be executed. func TestExecutor_Execute_Row(t *testing.T) { t.Run("RowIDColumnID", func(t *testing.T) { @@ -2987,7 +3003,16 @@ func TestExecutor_Execute_SetRow(t *testing.T) { } func benchmarkExistence(nn bool, b *testing.B) { - c := test.MustRunCluster(b, 1) + c := test.MustNewCluster(b, 1) + var err error + c[0].Config.DataDir, err = ioutil.TempDir(*TempDir, "benchmarkExistence") + if err != nil { + b.Fatalf("getting temp dir: %v", err) + } + err = c.Start() + if err != nil { + b.Fatalf("starting cluster: %v", err) + } defer c.Close() hldr := test.Holder{Holder: c[0].Server.Holder()} @@ -3587,7 +3612,16 @@ func TestExecutor_Execute_GroupBy(t *testing.T) { } func BenchmarkGroupBy(b *testing.B) { - c := test.MustRunCluster(b, 1) + c := test.MustNewCluster(b, 1) + var err error + c[0].Config.DataDir, err = ioutil.TempDir(*TempDir, "benchmarkGroupBy") + if err != nil { + b.Fatalf("getting temp dir: %v", err) + } + err = c.Start() + if err != nil { + b.Fatalf("starting cluster: %v", err) + } defer c.Close() c.CreateField(b, "i", pilosa.IndexOptions{}, "a") c.CreateField(b, "i", pilosa.IndexOptions{}, "b") diff --git a/field_internal_test.go b/field_internal_test.go index c2495e9c0..0a26225bb 100644 --- a/field_internal_test.go +++ b/field_internal_test.go @@ -192,7 +192,7 @@ type TestField struct { // NewTestField returns a new instance of TestField d/0. func NewTestField(opts FieldOption) *TestField { - path, err := ioutil.TempDir("", "pilosa-field-") + path, err := ioutil.TempDir(*TempDir, "pilosa-field-") if err != nil { panic(err) } diff --git a/fragment_internal_test.go b/fragment_internal_test.go index afd04616f..548e5f40a 100644 --- a/fragment_internal_test.go +++ b/fragment_internal_test.go @@ -39,12 +39,9 @@ var ( // In order to generate the sample fragment file, // run an import and copy PILOSA_DATA_DIR/INDEX_NAME/FRAME_NAME/0 to testdata/sample_view FragmentPath = flag.String("fragment", "testdata/sample_view/0", "fragment path") - TempDir = "" -) -func init() { // nolint: gochecknoinits - flag.StringVar(&TempDir, "temp-dir", "", "Directory in which to place temporary data (e.g. for benchmarking). Useful if you are trying to benchmark different storage configurations.") -} + TempDir = flag.String("temp-dir", "", "Directory in which to place temporary data (e.g. for benchmarking). Useful if you are trying to benchmark different storage configurations.") +) // Ensure a fragment can set a bit and retrieve it. func TestFragment_SetBit(t *testing.T) { @@ -2069,7 +2066,7 @@ func BenchmarkFileWrite(b *testing.B) { b.Run(fmt.Sprintf("Rows%d", numRows), func(b *testing.B) { b.StopTimer() for i := 0; i < b.N; i++ { - f, err := ioutil.TempFile(TempDir, "") + f, err := ioutil.TempFile(*TempDir, "") if err != nil { b.Fatalf("getting temp file: %v", err) } @@ -2125,7 +2122,7 @@ func (f *fragment) CleanKeep(t testing.TB) { // mustOpenFragment returns a new instance of Fragment with a temporary path. func mustOpenFragment(index, field, view string, shard uint64, cacheType string) *fragment { - file, err := ioutil.TempFile(TempDir, "pilosa-fragment-") + file, err := ioutil.TempFile(*TempDir, "pilosa-fragment-") if err != nil { panic(err) } diff --git a/holder_internal_test.go b/holder_internal_test.go index 1c4dfad88..b13df7809 100644 --- a/holder_internal_test.go +++ b/holder_internal_test.go @@ -46,7 +46,7 @@ func (h *tHolder) Reopen() error { } func newHolder() *tHolder { - path, err := ioutil.TempDir("", "pilosa-") + path, err := ioutil.TempDir(*TempDir, "pilosa-") if err != nil { panic(err) } diff --git a/index_internal_test.go b/index_internal_test.go index fd83fd775..dc0296049 100644 --- a/index_internal_test.go +++ b/index_internal_test.go @@ -21,7 +21,7 @@ import ( // mustOpenIndex returns a new, opened index at a temporary path. Panic on error. func mustOpenIndex(opt IndexOptions) *Index { - path, err := ioutil.TempDir("", "pilosa-index-") + path, err := ioutil.TempDir(*TempDir, "pilosa-index-") if err != nil { panic(err) } diff --git a/server_internal_test.go b/server_internal_test.go index e22681764..a8af8186c 100644 --- a/server_internal_test.go +++ b/server_internal_test.go @@ -38,7 +38,7 @@ func TestCountOpenFiles(t *testing.T) { func TestMonitorAntiEntropyZero(t *testing.T) { - td, err := ioutil.TempDir("", "") + td, err := ioutil.TempDir(*TempDir, "") if err != nil { t.Fatalf("getting temp dir: %v", err) } diff --git a/translate_test.go b/translate_test.go index fe78108ff..2b5d35a49 100644 --- a/translate_test.go +++ b/translate_test.go @@ -803,7 +803,7 @@ type TranslateFile struct { } func NewTranslateFile() *TranslateFile { - f, err := ioutil.TempFile("", "") + f, err := ioutil.TempFile(*TempDir, "") if err != nil { panic(err) } diff --git a/utils_internal_test.go b/utils_internal_test.go index 21d8d0780..016d5276f 100644 --- a/utils_internal_test.go +++ b/utils_internal_test.go @@ -212,7 +212,7 @@ func (t *ClusterCluster) addCluster(i int, saveTopology bool) (*cluster, error) t.common.Nodes = append(t.common.Nodes, node) // create node-specific temp directory - path, err := ioutil.TempDir("", fmt.Sprintf("pilosa-cluster-node-%d-", i)) + path, err := ioutil.TempDir(*TempDir, fmt.Sprintf("pilosa-cluster-node-%d-", i)) if err != nil { return nil, err } diff --git a/view_internal_test.go b/view_internal_test.go index 4ca7667ad..6867b03ed 100644 --- a/view_internal_test.go +++ b/view_internal_test.go @@ -24,7 +24,7 @@ import ( // mustOpenView returns a new instance of View with a temporary path. func mustOpenView(index, field, name string) *view { - path, err := ioutil.TempDir("", "pilosa-view-") + path, err := ioutil.TempDir(*TempDir, "pilosa-view-") if err != nil { panic(err) }