Skip to content

Commit c56cbd7

Browse files
committed
compose Reporter interface of smaller ones
1 parent d574af5 commit c56cbd7

File tree

5 files changed

+37
-25
lines changed

5 files changed

+37
-25
lines changed

wasp/benchspy/loki.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@ import (
1414
"golang.org/x/sync/errgroup"
1515
)
1616

17-
func NewLokiQuery(queries map[string]string, lokiConfig *wasp.LokiConfig) *LokiQuery {
18-
return &LokiQuery{
17+
func NewLokiQueryExecutor(queries map[string]string, lokiConfig *wasp.LokiConfig) *LokiQueryExecutor {
18+
return &LokiQueryExecutor{
1919
Kind: "loki",
2020
Queries: queries,
2121
LokiConfig: lokiConfig,
2222
QueryResults: make(map[string][]string),
2323
}
2424
}
2525

26-
type LokiQuery struct {
26+
type LokiQueryExecutor struct {
2727
Kind string `json:"kind"`
2828
// Test metrics
2929
StartTime time.Time `json:"start_time"`
@@ -35,27 +35,27 @@ type LokiQuery struct {
3535
// Performance queries results
3636
// can be anything, avg RPS, amount of errors, 95th percentile of CPU utilization, etc
3737
QueryResults map[string][]string `json:"query_results"`
38-
// In case something went wrong
38+
// In case something went wrong, but sure we need it
3939
Errors []error `json:"errors"`
4040

4141
LokiConfig *wasp.LokiConfig `json:"-"`
4242
}
4343

44-
func (l *LokiQuery) Results() map[string][]string {
44+
func (l *LokiQueryExecutor) Results() map[string][]string {
4545
return l.QueryResults
4646
}
4747

48-
func (l *LokiQuery) IsComparable(otherQueryExecutor QueryExecutor) error {
48+
func (l *LokiQueryExecutor) IsComparable(otherQueryExecutor QueryExecutor) error {
4949
otherType := reflect.TypeOf(otherQueryExecutor)
5050

5151
if otherType != reflect.TypeOf(l) {
5252
return fmt.Errorf("expected type %s, got %s", reflect.TypeOf(l), otherType)
5353
}
5454

55-
return l.compareLokiQueries(otherQueryExecutor.(*LokiQuery).Queries)
55+
return l.compareLokiQueries(otherQueryExecutor.(*LokiQueryExecutor).Queries)
5656
}
5757

58-
func (l *LokiQuery) Validate() error {
58+
func (l *LokiQueryExecutor) Validate() error {
5959
if len(l.Queries) == 0 {
6060
return errors.New("there are no Loki queries, there's nothing to fetch. Please set them and try again")
6161
}
@@ -66,7 +66,7 @@ func (l *LokiQuery) Validate() error {
6666
return nil
6767
}
6868

69-
func (l *LokiQuery) Execute(ctx context.Context) error {
69+
func (l *LokiQueryExecutor) Execute(ctx context.Context) error {
7070
splitAuth := strings.Split(l.LokiConfig.BasicAuth, ":")
7171
var basicAuth client.LokiBasicAuth
7272
if len(splitAuth) == 2 {
@@ -130,7 +130,7 @@ func (l *LokiQuery) Execute(ctx context.Context) error {
130130
return nil
131131
}
132132

133-
func (l *LokiQuery) compareLokiQueries(other map[string]string) error {
133+
func (l *LokiQueryExecutor) compareLokiQueries(other map[string]string) error {
134134
this := l.Queries
135135
if len(this) != len(other) {
136136
return fmt.Errorf("queries count is different. Expected %d, got %d", len(this), len(other))
@@ -155,7 +155,7 @@ func (l *LokiQuery) compareLokiQueries(other map[string]string) error {
155155
return nil
156156
}
157157

158-
func (l *LokiQuery) TimeRange(start, end time.Time) {
158+
func (l *LokiQueryExecutor) TimeRange(start, end time.Time) {
159159
l.StartTime = start
160160
l.EndTime = end
161161
}

wasp/benchspy/report.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@ import (
1111
// StandardReport is a report that contains all the necessary data for a performance test
1212
type StandardReport struct {
1313
BasicData
14-
LocalReportStorage
14+
LocalStorage
1515
ResourceReporter
1616
QueryExecutors []QueryExecutor `json:"query_executors"`
1717
}
1818

1919
func (b *StandardReport) Store() (string, error) {
20-
return b.LocalReportStorage.Store(b.TestName, b.CommitOrTag, b)
20+
return b.LocalStorage.Store(b.TestName, b.CommitOrTag, b)
2121
}
2222

2323
func (b *StandardReport) Load() error {
24-
return b.LocalReportStorage.Load(b.TestName, b.CommitOrTag, b)
24+
return b.LocalStorage.Load(b.TestName, b.CommitOrTag, b)
2525
}
2626

2727
func (b *StandardReport) Fetch(ctx context.Context) error {
@@ -107,7 +107,7 @@ func (s *StandardReport) UnmarshalJSON(data []byte) error {
107107
var executor QueryExecutor
108108
switch typeIndicator.Kind {
109109
case "loki":
110-
executor = &LokiQuery{}
110+
executor = &LokiQueryExecutor{}
111111
default:
112112
return fmt.Errorf("unknown query executor type: %s", typeIndicator.Kind)
113113
}

wasp/benchspy/storage.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@ import (
1616

1717
const DEFAULT_DIRECTORY = "performance_reports"
1818

19-
type LocalReportStorage struct {
19+
type LocalStorage struct {
2020
Directory string `json:"directory"`
2121
}
2222

23-
func (l *LocalReportStorage) defaultDirectoryIfEmpty() {
23+
func (l *LocalStorage) defaultDirectoryIfEmpty() {
2424
if l.Directory == "" {
2525
l.Directory = DEFAULT_DIRECTORY
2626
}
2727
}
2828

29-
func (l *LocalReportStorage) Store(testName, commitOrTag string, report interface{}) (string, error) {
29+
func (l *LocalStorage) Store(testName, commitOrTag string, report interface{}) (string, error) {
3030
l.defaultDirectoryIfEmpty()
3131
asJson, err := json.MarshalIndent(report, "", " ")
3232
if err != nil {
@@ -60,7 +60,7 @@ func (l *LocalReportStorage) Store(testName, commitOrTag string, report interfac
6060
return abs, nil
6161
}
6262

63-
func (l *LocalReportStorage) Load(testName, commitOrTag string, report interface{}) error {
63+
func (l *LocalStorage) Load(testName, commitOrTag string, report interface{}) error {
6464
l.defaultDirectoryIfEmpty()
6565
if testName == "" {
6666
return errors.New("test name is empty. Please set it and try again")

wasp/benchspy/types.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,27 @@ import (
55
"time"
66
)
77

8-
type Report interface {
8+
type Storer interface {
99
// Store stores the report in a persistent storage and returns the path to it, or an error
1010
Store() (string, error)
1111
// Load loads the report from a persistent storage and returns it, or an error
1212
Load() error
13+
}
14+
15+
type Fetcher interface {
1316
// Fetch populates the report with the data from the test
1417
Fetch() error
18+
}
19+
20+
type Comparator interface {
1521
// IsComparable checks whether both reports can be compared (e.g. test config is the same, app's resources are the same, queries or metrics used are the same, etc.), and an error if any difference is found
16-
IsComparable(otherReport Report) error
22+
IsComparable(otherReport Reporter) error
23+
}
24+
25+
type Reporter interface {
26+
Storer
27+
Fetcher
28+
Comparator
1729
}
1830

1931
type QueryExecutor interface {

wasp/benchspy_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func TestBenchSpyWithLokiQuery(t *testing.T) {
5252
},
5353
}
5454

55-
lokiQueryExecutor := benchspy.NewLokiQuery(
55+
lokiQueryExecutor := benchspy.NewLokiQueryExecutor(
5656
map[string]string{
5757
"vu_over_time": fmt.Sprintf("max_over_time({branch=~\"%s\", commit=~\"%s\", go_test_name=~\"%s\", test_data_type=~\"stats\", gen_name=~\"%s\"} | json | unwrap current_instances [10s]) by (node_id, go_test_name, gen_name)", label, label, t.Name(), gen.Cfg.GenName),
5858
},
@@ -77,7 +77,7 @@ func TestBenchSpyWithLokiQuery(t *testing.T) {
7777
TestName: t.Name(),
7878
CommitOrTag: "e7fc5826a572c09f8b93df3b9f674113372ce924",
7979
},
80-
LocalReportStorage: benchspy.LocalReportStorage{
80+
LocalStorage: benchspy.LocalStorage{
8181
Directory: "test_performance_reports",
8282
},
8383
}
@@ -154,7 +154,7 @@ func TestBenchSpyWithTwoLokiQueries(t *testing.T) {
154154
},
155155
}
156156

157-
lokiQueryExecutor := benchspy.NewLokiQuery(
157+
lokiQueryExecutor := benchspy.NewLokiQueryExecutor(
158158
map[string]string{
159159
"vu_over_time": fmt.Sprintf("max_over_time({branch=~\"%s\", commit=~\"%s\", go_test_name=~\"%s\", test_data_type=~\"stats\", gen_name=~\"%s\"} | json | unwrap current_instances [10s]) by (node_id, go_test_name, gen_name)", label, label, t.Name(), gen.Cfg.GenName),
160160
"responses_over_time": fmt.Sprintf("sum(count_over_time({branch=~\"%s\", commit=~\"%s\", go_test_name=~\"%s\", test_data_type=~\"responses\", gen_name=~\"%s\"} [1s])) by (node_id, go_test_name, gen_name)", label, label, t.Name(), gen.Cfg.GenName),
@@ -180,7 +180,7 @@ func TestBenchSpyWithTwoLokiQueries(t *testing.T) {
180180
TestName: t.Name(),
181181
CommitOrTag: "e7fc5826a572c09f8b93df3b9f674113372ce924",
182182
},
183-
LocalReportStorage: benchspy.LocalReportStorage{
183+
LocalStorage: benchspy.LocalStorage{
184184
Directory: "test_performance_reports",
185185
},
186186
}

0 commit comments

Comments
 (0)