Skip to content

Commit b3550a5

Browse files
Tofelgithub-actions[bot]
authored andcommitted
[Bot] Add automatically generated go documentation
1 parent a189b41 commit b3550a5

File tree

7 files changed

+143
-0
lines changed

7 files changed

+143
-0
lines changed

wasp/benchspy/basic.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ type BasicData struct {
2222
GeneratorConfigs map[string]*wasp.Config `json:"generator_configs"`
2323
}
2424

25+
// MustNewBasicData creates a new BasicData instance from a commit or tag.
26+
// It panics if the creation fails, ensuring that the caller receives a valid instance.
2527
func MustNewBasicData(commitOrTag string, generators ...*wasp.Generator) BasicData {
2628
b, err := NewBasicData(commitOrTag, generators...)
2729
if err != nil {
@@ -31,6 +33,9 @@ func MustNewBasicData(commitOrTag string, generators ...*wasp.Generator) BasicDa
3133
return *b
3234
}
3335

36+
// NewBasicData creates a new BasicData instance using the provided commit or tag and a list of generators.
37+
// It ensures that at least one generator is provided and that it is associated with a testing.T instance.
38+
// This function is essential for initializing data required for testing and reporting.
3439
func NewBasicData(commitOrTag string, generators ...*wasp.Generator) (*BasicData, error) {
3540
if len(generators) == 0 {
3641
return nil, errors.New("at least one generator is required")
@@ -58,6 +63,8 @@ func NewBasicData(commitOrTag string, generators ...*wasp.Generator) (*BasicData
5863
return b, nil
5964
}
6065

66+
// FillStartEndTimes calculates the earliest start time and latest end time from generator schedules.
67+
// It validates that all segments have valid start and end times, returning an error if any are missing.
6168
func (b *BasicData) FillStartEndTimes() error {
6269
earliestTime := time.Now()
6370
var latestTime time.Time
@@ -89,6 +96,9 @@ func (b *BasicData) FillStartEndTimes() error {
8996
return nil
9097
}
9198

99+
// Validate checks the integrity of BasicData by ensuring that the test start and end times are set,
100+
// and that at least one generator configuration is provided. It returns an error if any of these
101+
// conditions are not met, guiding users to correct their input before proceeding.
92102
func (b *BasicData) Validate() error {
93103
if b.TestStart.IsZero() {
94104
return errors.New("test start time is missing. We cannot query Loki without a time range. Please set it and try again")
@@ -104,6 +114,8 @@ func (b *BasicData) Validate() error {
104114
return nil
105115
}
106116

117+
// IsComparable checks if two BasicData instances have the same configuration settings.
118+
// It validates the count, presence, and equivalence of generator configurations, returning an error if discrepancies are found.
107119
func (b *BasicData) IsComparable(otherData BasicData) error {
108120
// are all configs present? do they have the same schedule type? do they have the same segments? is call timeout the same? is rate limit timeout the same?
109121
if len(b.GeneratorConfigs) != len(otherData.GeneratorConfigs) {

wasp/benchspy/generator.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ type GeneratorQueryExecutor struct {
2020
QueryResults map[string]interface{} `json:"query_results"`
2121
}
2222

23+
// NewStandardGeneratorQueryExecutor creates a new GeneratorQueryExecutor for standard queries.
24+
// It initializes the executor with generated standard queries based on the provided generator.
25+
// This function is useful for setting up query execution in a standardized manner.
2326
func NewStandardGeneratorQueryExecutor(generator *wasp.Generator) (*GeneratorQueryExecutor, error) {
2427
g := &GeneratorQueryExecutor{
2528
KindName: string(StandardQueryExecutor_Generator),
@@ -33,6 +36,9 @@ func NewStandardGeneratorQueryExecutor(generator *wasp.Generator) (*GeneratorQue
3336
return NewGeneratorQueryExecutor(generator, queries)
3437
}
3538

39+
// NewGeneratorQueryExecutor creates a new instance of GeneratorQueryExecutor.
40+
// It initializes the executor with a specified generator and a set of query functions,
41+
// enabling the execution of custom queries against the generator's data.
3642
func NewGeneratorQueryExecutor(generator *wasp.Generator, queries map[string]GeneratorQueryFn) (*GeneratorQueryExecutor, error) {
3743
g := &GeneratorQueryExecutor{
3844
KindName: string(StandardQueryExecutor_Generator),
@@ -44,14 +50,20 @@ func NewGeneratorQueryExecutor(generator *wasp.Generator, queries map[string]Gen
4450
return g, nil
4551
}
4652

53+
// Results returns the query results as a map of string keys to interface{} values.
54+
// It allows users to access the outcomes of executed queries, facilitating data retrieval and manipulation.
4755
func (g *GeneratorQueryExecutor) Results() map[string]interface{} {
4856
return g.QueryResults
4957
}
5058

59+
// Kind returns the type of the query executor as a string.
60+
// It is used to identify the specific kind of query executor in various operations.
5161
func (l *GeneratorQueryExecutor) Kind() string {
5262
return l.KindName
5363
}
5464

65+
// IsComparable checks if the current GeneratorQueryExecutor is comparable to another QueryExecutor.
66+
// It validates the type and configuration of both executors, returning an error if they are not compatible.
5567
func (g *GeneratorQueryExecutor) IsComparable(otherQueryExecutor QueryExecutor) error {
5668
otherType := reflect.TypeOf(otherQueryExecutor)
5769

@@ -83,6 +95,8 @@ func (l *GeneratorQueryExecutor) compareQueries(other map[string]GeneratorQueryF
8395
return nil
8496
}
8597

98+
// Validate checks if the generator is set and if at least one query is provided.
99+
// It returns an error if either condition is not met, ensuring that the query executor is properly configured before execution.
86100
func (g *GeneratorQueryExecutor) Validate() error {
87101
if g.Generator == nil {
88102
return errors.New("generator is not set")
@@ -95,6 +109,8 @@ func (g *GeneratorQueryExecutor) Validate() error {
95109
return nil
96110
}
97111

112+
// Execute runs the defined queries using the generator's data, storing results for each query.
113+
// It validates the generator and its data, ensuring responses are available before execution.
98114
func (g *GeneratorQueryExecutor) Execute(_ context.Context) error {
99115
if g.Generator == nil {
100116
return errors.New("generator is not set")
@@ -130,6 +146,9 @@ func (g *GeneratorQueryExecutor) Execute(_ context.Context) error {
130146
return nil
131147
}
132148

149+
// TimeRange sets the time range for the query executor.
150+
// It ensures that all responses are within the specified time range,
151+
// although no action is needed if the generator already contains the correct data.
133152
func (g *GeneratorQueryExecutor) TimeRange(_, _ time.Time) {
134153
// nothing to do here, since all responses stored in the generator are already in the right time range
135154
}
@@ -194,6 +213,9 @@ func (g *GeneratorQueryExecutor) standardQuery(standardMetric StandardLoadMetric
194213
}
195214
}
196215

216+
// MarshalJSON customizes the JSON encoding of the GeneratorQueryExecutor.
217+
// It serializes only the relevant fields, including query names and results,
218+
// making it suitable for efficient data transmission and storage in JSON format.
197219
func (g *GeneratorQueryExecutor) MarshalJSON() ([]byte, error) {
198220
// we need custom marshalling to only include query names, since the functions are not serializable
199221
type QueryExecutor struct {
@@ -222,6 +244,9 @@ func (g *GeneratorQueryExecutor) MarshalJSON() ([]byte, error) {
222244
})
223245
}
224246

247+
// UnmarshalJSON parses the JSON-encoded data and populates the GeneratorQueryExecutor fields.
248+
// It extracts generator configuration, queries, and query results, converting them to appropriate types.
249+
// This function is essential for initializing a GeneratorQueryExecutor from JSON data.
225250
func (g *GeneratorQueryExecutor) UnmarshalJSON(data []byte) error {
226251
// helper struct with QueryExecutors as json.RawMessage and QueryResults as map[string]interface{}
227252
// and as actual types

wasp/benchspy/loki.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import (
1515
"golang.org/x/sync/errgroup"
1616
)
1717

18+
// NewLokiQueryExecutor initializes a LokiQueryExecutor with specified queries and configuration.
19+
// It returns a pointer to the newly created LokiQueryExecutor, enabling efficient querying of logs in a Loki instance.
1820
func NewLokiQueryExecutor(queries map[string]string, lokiConfig *wasp.LokiConfig) *LokiQueryExecutor {
1921
return &LokiQueryExecutor{
2022
KindName: string(StandardQueryExecutor_Loki),
@@ -40,14 +42,20 @@ type LokiQueryExecutor struct {
4042
Config *wasp.LokiConfig `json:"-"`
4143
}
4244

45+
// Results returns the query results as a map of string keys to interface{} values.
46+
// It is useful for retrieving the output of executed queries in a structured format.
4347
func (l *LokiQueryExecutor) Results() map[string]interface{} {
4448
return l.QueryResults
4549
}
4650

51+
// Kind returns the type of the query executor as a string.
52+
// It is used to identify the specific kind of query executor in various operations.
4753
func (l *LokiQueryExecutor) Kind() string {
4854
return l.KindName
4955
}
5056

57+
// IsComparable checks if the current LokiQueryExecutor is of the same type as another QueryExecutor.
58+
// It ensures that the queries are structurally and semantically identical, returning an error if discrepancies are found.
5159
func (l *LokiQueryExecutor) IsComparable(otherQueryExecutor QueryExecutor) error {
5260
otherType := reflect.TypeOf(otherQueryExecutor)
5361

@@ -58,6 +66,9 @@ func (l *LokiQueryExecutor) IsComparable(otherQueryExecutor QueryExecutor) error
5866
return l.compareQueries(otherQueryExecutor.(*LokiQueryExecutor).Queries)
5967
}
6068

69+
// Validate checks if the LokiQueryExecutor has valid queries and configuration.
70+
// It returns an error if no queries are set or if the configuration is missing,
71+
// ensuring that the executor is ready for execution.
6172
func (l *LokiQueryExecutor) Validate() error {
6273
if len(l.Queries) == 0 {
6374
return errors.New("there are no Loki queries, there's nothing to fetch. Please set them and try again")
@@ -69,6 +80,9 @@ func (l *LokiQueryExecutor) Validate() error {
6980
return nil
7081
}
7182

83+
// Execute runs the configured Loki queries concurrently and collects the results.
84+
// It validates the configuration, handles basic authentication, and manages errors during execution.
85+
// This function is essential for retrieving log data from Loki based on specified queries.
7286
func (l *LokiQueryExecutor) Execute(ctx context.Context) error {
7387
var basicAuth client.LokiBasicAuth
7488

@@ -159,11 +173,16 @@ func (l *LokiQueryExecutor) compareQueries(other map[string]string) error {
159173
return nil
160174
}
161175

176+
// TimeRange sets the start and end time for the Loki query execution.
177+
// This function is essential for defining the time window of the data to be queried.
162178
func (l *LokiQueryExecutor) TimeRange(start, end time.Time) {
163179
l.StartTime = start
164180
l.EndTime = end
165181
}
166182

183+
// UnmarshalJSON parses the JSON-encoded data and populates the LokiQueryExecutor fields.
184+
// It converts the query results from a generic map to a specific type map, ensuring type safety.
185+
// This function is essential for handling JSON data in a structured manner.
167186
func (l *LokiQueryExecutor) UnmarshalJSON(data []byte) error {
168187
// helper struct with QueryResults map[string]interface{}
169188
type Alias LokiQueryExecutor
@@ -188,6 +207,9 @@ func (l *LokiQueryExecutor) UnmarshalJSON(data []byte) error {
188207
return nil
189208
}
190209

210+
// NewStandardMetricsLokiExecutor creates a LokiQueryExecutor configured with standard queries
211+
// based on the provided test and generator details. It facilitates querying metrics from Loki
212+
// for performance analysis within a specified time range.
191213
func NewStandardMetricsLokiExecutor(lokiConfig *wasp.LokiConfig, testName, generatorName, branch, commit string, startTime, endTime time.Time) (*LokiQueryExecutor, error) {
192214
lq := &LokiQueryExecutor{
193215
KindName: string(StandardQueryExecutor_Loki),

wasp/benchspy/metrics.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"strconv"
77
)
88

9+
// CalculatePercentile computes the specified percentile of a slice of numbers.
10+
// It is useful for statistical analysis, allowing users to determine the value below which a given percentage of observations fall.
911
func CalculatePercentile(numbers []float64, percentile float64) float64 {
1012
// Sort the slice
1113
sort.Float64s(numbers)
@@ -31,6 +33,8 @@ func CalculatePercentile(numbers []float64, percentile float64) float64 {
3133
return numbers[lowerIndex]*(1-weight) + numbers[upperIndex]*weight
3234
}
3335

36+
// StringSliceToFloat64Slice converts a slice of strings to a slice of float64 values.
37+
// It returns an error if any string cannot be parsed as a float64.
3438
func StringSliceToFloat64Slice(s []string) ([]float64, error) {
3539
numbers := make([]float64, len(s))
3640
for i, str := range s {

wasp/benchspy/prometheus.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ type PrometheusConfig struct {
2222

2323
const PrometheusUrlEnvVar = "PROMETHEUS_URL"
2424

25+
// NewPrometheusConfig creates a new PrometheusConfig instance with the specified name regex patterns.
26+
// It retrieves the Prometheus URL from the environment and is useful for configuring Prometheus query execution.
2527
func NewPrometheusConfig(nameRegexPatterns ...string) *PrometheusConfig {
2628
return &PrometheusConfig{
2729
Url: os.Getenv(PrometheusUrlEnvVar),
@@ -56,6 +58,8 @@ func NewPrometheusQueryExecutor(queries map[string]string, config PrometheusConf
5658
}, nil
5759
}
5860

61+
// NewStandardPrometheusQueryExecutor creates a PrometheusQueryExecutor with standard queries based on the provided time range and configuration.
62+
// It generates queries for each name regex pattern and returns the executor or an error if the generation fails.
5963
func NewStandardPrometheusQueryExecutor(startTime, endTime time.Time, config PrometheusConfig) (*PrometheusQueryExecutor, error) {
6064
p := &PrometheusQueryExecutor{}
6165

@@ -74,6 +78,8 @@ func NewStandardPrometheusQueryExecutor(startTime, endTime time.Time, config Pro
7478
return NewPrometheusQueryExecutor(standardQueries, config)
7579
}
7680

81+
// Execute runs a series of Prometheus queries concurrently, collecting results and warnings.
82+
// It returns an error if any query fails, allowing for efficient data retrieval in reporting tasks.
7783
func (r *PrometheusQueryExecutor) Execute(ctx context.Context) error {
7884
for name, query := range r.Queries {
7985
result, warnings, queryErr := r.client.Query(ctx, query, r.EndTime)
@@ -91,14 +97,22 @@ func (r *PrometheusQueryExecutor) Execute(ctx context.Context) error {
9197
return nil
9298
}
9399

100+
// Results returns the query results as a map of string to interface{}.
101+
// It allows users to access the results of executed queries, facilitating data retrieval and manipulation.
94102
func (r *PrometheusQueryExecutor) Results() map[string]interface{} {
95103
return r.QueryResults
96104
}
97105

106+
// Kind returns the type of the query executor as a string.
107+
// It is used to identify the specific implementation of a query executor,
108+
// enabling filtering and processing of results based on their executor type.
98109
func (l *PrometheusQueryExecutor) Kind() string {
99110
return l.KindName
100111
}
101112

113+
// Validate checks the Prometheus query executor's configuration for correctness.
114+
// It ensures that the Prometheus client is initialized and that at least one query is provided.
115+
// It returns an error if any validation checks fail, helping to prevent execution issues.
102116
func (r *PrometheusQueryExecutor) Validate() error {
103117
if r.client == nil {
104118
return errors.New("prometheus client is nil")
@@ -111,6 +125,8 @@ func (r *PrometheusQueryExecutor) Validate() error {
111125
return nil
112126
}
113127

128+
// IsComparable checks if the provided QueryExecutor is of the same type as the receiver.
129+
// It returns an error if the types do not match, ensuring type safety for query comparisons.
114130
func (r *PrometheusQueryExecutor) IsComparable(other QueryExecutor) error {
115131
otherType := reflect.TypeOf(other)
116132
if otherType != reflect.TypeOf(r) {
@@ -141,10 +157,15 @@ func (r *PrometheusQueryExecutor) compareQueries(other map[string]string) error
141157
return nil
142158
}
143159

160+
// Warnings returns a map of warnings encountered during query execution.
161+
// This function is useful for retrieving any issues that may have arisen,
162+
// allowing users to handle or log them appropriately.
144163
func (r *PrometheusQueryExecutor) Warnings() map[string]v1.Warnings {
145164
return r.warnings
146165
}
147166

167+
// MustResultsAsValue retrieves the query results as a map of metric names to their corresponding values.
168+
// It ensures that the results are in a format compatible with further processing or serialization.
148169
func (r *PrometheusQueryExecutor) MustResultsAsValue() map[string]model.Value {
149170
results := make(map[string]model.Value)
150171
for name, result := range r.QueryResults {
@@ -180,6 +201,8 @@ func (r *PrometheusQueryExecutor) MustResultsAsValue() map[string]model.Value {
180201
return results
181202
}
182203

204+
// TimeRange sets the start and end time for the Prometheus query execution.
205+
// This function is essential for defining the time window for data retrieval in monitoring and analytics tasks.
183206
func (r *PrometheusQueryExecutor) TimeRange(startTime, endTime time.Time) {
184207
r.StartTime = startTime
185208
r.EndTime = endTime
@@ -222,6 +245,9 @@ type TypedMetric struct {
222245
MetricType string `json:"metric_type"`
223246
}
224247

248+
// MarshalJSON customizes the JSON representation of the PrometheusQueryExecutor.
249+
// It includes only essential fields: Kind, Queries, and simplified QueryResults,
250+
// making it useful for efficient data transfer and storage in JSON format.
225251
func (g *PrometheusQueryExecutor) MarshalJSON() ([]byte, error) {
226252
// we need custom marshalling to only include some parts of the metrics
227253
type QueryExecutor struct {
@@ -248,6 +274,9 @@ func (g *PrometheusQueryExecutor) MarshalJSON() ([]byte, error) {
248274
return json.Marshal(q)
249275
}
250276

277+
// UnmarshalJSON decodes JSON data into a PrometheusQueryExecutor instance.
278+
// It populates the QueryResults field with the appropriate metric types,
279+
// enabling structured access to the results of Prometheus queries.
251280
func (r *PrometheusQueryExecutor) UnmarshalJSON(data []byte) error {
252281
// helper struct with QueryResults map[string]interface{}
253282
type Alias PrometheusQueryExecutor

0 commit comments

Comments
 (0)