mirror of
https://github.com/featurebasedb/featurebase.git
synced 2026-08-28 02:44:59 +00:00
We centralize the creation paths for test indexes, fields, etcetera so they all have a common path, all using standard test holders. There's still two versions, one for test.* functions and one for internal. They do share a TestHolderConfig though. Large hunks of the related APIs are simplified/streamlined. * Fragments are always created with a Field and don't need a workaround in case they don't have it. * Creation of test fragments, etc., use optional FieldOptions but don't specify names because they're all using new holders for each thing created anyway. This dramatically reduces the complexity of the calls. * test fragments are created inside test views which are created inside test fields, etcetera, so everything is using the same logic; test views aren't bypassing the other layers, they're creating themselves normally within a field. * Quite a few things now use the standard runtime/production logic instead of being custom workarounds; for instance, instead of `mustOpenMutexFragment` creating a fragment and then creating a mutex vector for it, we just create a mutex-typed field and have the normal runtime code do this. * Similarly, we now use the same field creation logic that production does, instead of having our own test-only thing that validates field names directly, so our test that we're validating field names is actually testing the runtime code. Yay. * fragSpec goes away. it was a replacement for fragProxy which existed to solve memory allocation problems but replaced them with interface overhead problems. Now we just have pointers to things and maintain valid data structures. * Many panics are now Fatal or Fatalf calls. * Some specific bugs fixed, like a cluster which was requested and then had its first node directly overwritten, which isn't valid with shared clusters. * Drop the temp-dir test flag and TempDir variable, we can just use $TMPDIR. * Drop a benchmark of "write file to disk" that was purely a benchmark of file write speed, not a benchmark of rendering the data that needs to be written. * Drop the unused "flags" parameter to fragment creation, which was only used back when we changed the BSI format. * Use holder.Txf() rather than index.Txf(). The TxFactory has to be holder-level anyway, referring to it via the index is misleading. * Test holders automatically close themselves and delete themselves, we remove various other things that thought they were responsible for deleting themselves.
257 lines
7.8 KiB
Go
257 lines
7.8 KiB
Go
// Copyright 2021 Molecula Corp. All rights reserved.
|
|
package stats_test
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
pilosa "github.com/molecula/featurebase/v3"
|
|
"github.com/molecula/featurebase/v3/logger"
|
|
"github.com/molecula/featurebase/v3/stats"
|
|
"github.com/molecula/featurebase/v3/test"
|
|
)
|
|
|
|
// TestMultiStatClient_Expvar run the multistat client with exp var
|
|
// since the EXPVAR data is stored in a global we should run these in one test function
|
|
func TestMultiStatClient_Expvar(t *testing.T) {
|
|
hldr := test.MustOpenHolder(t)
|
|
|
|
c := stats.NewExpvarStatsClient()
|
|
ms := make(stats.MultiStatsClient, 1)
|
|
ms[0] = c
|
|
hldr.Stats = ms
|
|
|
|
hldr.SetBit("d", "f", 0, 0)
|
|
hldr.SetBit("d", "f", 0, 1)
|
|
hldr.SetBit("d", "f", 0, pilosa.ShardWidth)
|
|
hldr.SetBit("d", "f", 0, pilosa.ShardWidth+2)
|
|
hldr.ClearBit("d", "f", 0, 1)
|
|
|
|
indexStats := fmt.Sprintf(`{"%s": %d, "%s": %d}`, pilosa.MetricClearBit, 1, pilosa.MetricSetBit, 4)
|
|
|
|
if stats.Expvar.String() != `{"index:d": `+indexStats+`}` {
|
|
t.Fatalf("unexpected expvar : %s", stats.Expvar.String())
|
|
}
|
|
|
|
hldr.Stats.CountWithCustomTags("cc", 1, 1.0, []string{"foo:bar"})
|
|
if stats.Expvar.String() != `{"cc": 1, "index:d": `+indexStats+`}` {
|
|
t.Fatalf("unexpected expvar : %s", stats.Expvar.String())
|
|
}
|
|
|
|
// Gauge creates a unique key, subsequent Gauge calls will overwrite
|
|
hldr.Stats.Gauge("g", 5, 1.0)
|
|
hldr.Stats.Gauge("g", 8, 1.0)
|
|
if stats.Expvar.String() != `{"cc": 1, "g": 8, "index:d": `+indexStats+`}` {
|
|
t.Fatalf("unexpected expvar : %s", stats.Expvar.String())
|
|
}
|
|
|
|
// Set creates a unique key, subsequent sets will overwrite
|
|
hldr.Stats.Set("s", "4", 1.0)
|
|
hldr.Stats.Set("s", "7", 1.0)
|
|
if stats.Expvar.String() != `{"cc": 1, "g": 8, "index:d": `+indexStats+`, "s": "7"}` {
|
|
t.Fatalf("unexpected expvar : %s", stats.Expvar.String())
|
|
}
|
|
|
|
// Record timing duration and a uniquely Set key/value
|
|
dur, _ := time.ParseDuration("123us")
|
|
hldr.Stats.Timing("tt", dur, 1.0)
|
|
if stats.Expvar.String() != `{"cc": 1, "g": 8, "index:d": `+indexStats+`, "s": "7", "tt": 123µs}` {
|
|
t.Fatalf("unexpected expvar : %s", stats.Expvar.String())
|
|
}
|
|
|
|
// Expvar histogram is implemented as a gauge
|
|
hldr.Stats.Histogram("hh", 3, 1.0)
|
|
if stats.Expvar.String() != `{"cc": 1, "g": 8, "hh": 3, "index:d": `+indexStats+`, "s": "7", "tt": 123µs}` {
|
|
t.Fatalf("unexpected expvar : %s", stats.Expvar.String())
|
|
}
|
|
|
|
// Expvar should ignore earlier set tags from setbit
|
|
if hldr.Stats.Tags() != nil {
|
|
t.Fatalf("unexpected tag")
|
|
}
|
|
}
|
|
|
|
func TestStatsCount_TopN(t *testing.T) {
|
|
c := test.MustRunCluster(t, 1)
|
|
defer c.Close()
|
|
hldr := test.Holder{Holder: c.GetNode(0).Server.Holder()}
|
|
|
|
// Execute query.
|
|
called := false
|
|
hldr.Holder.Stats = &MockStats{
|
|
mockCountWithTags: func(name string, value int64, rate float64, tags []string) {
|
|
if name != "query_topn_total" {
|
|
t.Errorf("Expected query_topn_total, Results %s", name)
|
|
}
|
|
|
|
if tags[0] != "index:d" {
|
|
t.Errorf("Expected index, Results %s", tags[0])
|
|
}
|
|
|
|
called = true
|
|
},
|
|
}
|
|
|
|
hldr.SetBit("d", "f", 0, 0)
|
|
hldr.SetBit("d", "f", 0, 1)
|
|
hldr.SetBit("d", "f", 0, pilosa.ShardWidth)
|
|
hldr.SetBit("d", "f", 0, pilosa.ShardWidth+2)
|
|
|
|
if _, err := c.GetNode(0).API.Query(context.Background(), &pilosa.QueryRequest{Index: "d", Query: `TopN(field=f, n=2)`}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !called {
|
|
t.Error("CountWithCustomTags name isn't called")
|
|
}
|
|
}
|
|
|
|
func TestStatsCount_Bitmap(t *testing.T) {
|
|
// Cluster has to be unhsared because we're mocking the stats which writes
|
|
// to a holder in use by other tests.
|
|
c := test.MustRunUnsharedCluster(t, 1)
|
|
defer c.Close()
|
|
hldr := test.Holder{Holder: c.GetNode(0).Server.Holder()}
|
|
called := false
|
|
hldr.Holder.Stats = &MockStats{
|
|
mockCountWithTags: func(name string, value int64, rate float64, tags []string) {
|
|
if name != pilosa.MetricRow {
|
|
t.Errorf("Expected %s, Results %s", pilosa.MetricRow, name)
|
|
}
|
|
|
|
if tags[0] != "index:d" {
|
|
t.Errorf("Expected index, Results %s", tags[0])
|
|
}
|
|
|
|
called = true
|
|
},
|
|
}
|
|
|
|
hldr.SetBit("d", "f", 0, 0)
|
|
hldr.SetBit("d", "f", 0, 1)
|
|
|
|
if _, err := c.GetNode(0).API.Query(context.Background(), &pilosa.QueryRequest{Index: "d", Query: `Row(f=0)`}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !called {
|
|
t.Error("CountWithCustomTags name isn't called")
|
|
}
|
|
}
|
|
|
|
func TestStatsCount_APICalls(t *testing.T) {
|
|
// We can't share a cluster when we're modifying its stats counter.
|
|
cluster := test.MustRunUnsharedCluster(t, 1)
|
|
defer cluster.Close()
|
|
cmd := cluster.GetNode(0)
|
|
h := cmd.Handler.(*pilosa.Handler).Handler
|
|
holder := cmd.Server.Holder()
|
|
hldr := test.Holder{Holder: holder}
|
|
|
|
t.Run("create index", func(t *testing.T) {
|
|
called := false
|
|
hldr.Stats = &MockStats{
|
|
mockCount: func(name string, value int64, rate float64) {
|
|
if name != pilosa.MetricCreateIndex {
|
|
t.Errorf("Expected %v, Results %s", pilosa.MetricCreateIndex, name)
|
|
}
|
|
called = true
|
|
},
|
|
}
|
|
w := httptest.NewRecorder()
|
|
h.ServeHTTP(w, test.MustNewHTTPRequest("POST", "/index/i", strings.NewReader("")))
|
|
if !called {
|
|
t.Error("Count isn't called")
|
|
}
|
|
})
|
|
|
|
t.Run("create field", func(t *testing.T) {
|
|
called := false
|
|
hldr.Stats = &MockStats{
|
|
mockCountWithTags: func(name string, value int64, rate float64, index []string) {
|
|
if name != pilosa.MetricCreateField {
|
|
t.Errorf("Expected %v, Results %s", pilosa.MetricCreateField, name)
|
|
}
|
|
if index[0] != "index:i" {
|
|
t.Errorf("Expected index:i, Results %s", index)
|
|
}
|
|
|
|
called = true
|
|
},
|
|
}
|
|
w := httptest.NewRecorder()
|
|
h.ServeHTTP(w, test.MustNewHTTPRequest("POST", "/index/i/field/f", strings.NewReader("")))
|
|
if !called {
|
|
t.Error("Count isn't called")
|
|
}
|
|
})
|
|
|
|
t.Run("delete field", func(t *testing.T) {
|
|
called := false
|
|
hldr.Stats = &MockStats{
|
|
mockCountWithTags: func(name string, value int64, rate float64, index []string) {
|
|
if name != pilosa.MetricDeleteField {
|
|
t.Errorf("Expected %v, Results %s", pilosa.MetricDeleteField, name)
|
|
}
|
|
if index[0] != "index:i" {
|
|
t.Errorf("Expected index:i, Results %s", index)
|
|
}
|
|
|
|
called = true
|
|
},
|
|
}
|
|
w := httptest.NewRecorder()
|
|
h.ServeHTTP(w, test.MustNewHTTPRequest("DELETE", "/index/i/field/f", strings.NewReader("")))
|
|
if !called {
|
|
t.Error("Count isn't called")
|
|
}
|
|
})
|
|
|
|
t.Run("delete index", func(t *testing.T) {
|
|
called := false
|
|
hldr.Stats = &MockStats{
|
|
mockCount: func(name string, value int64, rate float64) {
|
|
if name != pilosa.MetricDeleteIndex {
|
|
t.Errorf("Expected %v, Results %s", pilosa.MetricDeleteIndex, name)
|
|
}
|
|
|
|
called = true
|
|
},
|
|
}
|
|
w := httptest.NewRecorder()
|
|
h.ServeHTTP(w, test.MustNewHTTPRequest("DELETE", "/index/i", strings.NewReader("")))
|
|
if !called {
|
|
t.Error("Count isn't called")
|
|
}
|
|
})
|
|
|
|
}
|
|
|
|
type MockStats struct {
|
|
mockCount func(name string, value int64, rate float64)
|
|
mockCountWithTags func(name string, value int64, rate float64, tags []string)
|
|
}
|
|
|
|
func (s *MockStats) Count(name string, value int64, rate float64) {
|
|
if s.mockCount != nil {
|
|
s.mockCount(name, value, rate)
|
|
}
|
|
}
|
|
|
|
func (s *MockStats) CountWithCustomTags(name string, value int64, rate float64, tags []string) {
|
|
if s.mockCountWithTags != nil {
|
|
s.mockCountWithTags(name, value, rate, tags)
|
|
}
|
|
}
|
|
|
|
func (c *MockStats) Tags() []string { return nil }
|
|
func (c *MockStats) WithTags(tags ...string) stats.StatsClient { return c }
|
|
func (c *MockStats) Gauge(name string, value float64, rate float64) {}
|
|
func (c *MockStats) Histogram(name string, value float64, rate float64) {}
|
|
func (c *MockStats) Set(name string, value string, rate float64) {}
|
|
func (c *MockStats) Timing(name string, value time.Duration, rate float64) {}
|
|
func (c *MockStats) SetLogger(logger logger.Logger) {}
|
|
func (c *MockStats) Open() {}
|
|
func (c *MockStats) Close() error { return nil }
|