Skip to content

Commit 2c56a03

Browse files
[Bot] Add automatically generated go documentation (#1474)
Co-authored-by: Tofel <[email protected]>
1 parent b847867 commit 2c56a03

File tree

7 files changed

+141
-0
lines changed

7 files changed

+141
-0
lines changed

wasp/benchspy/basic.go

Lines changed: 13 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 test data configurations in a structured manner.
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 updates the BasicData instance with these times, ensuring all segments have valid start and end times.
6168
func (b *BasicData) FillStartEndTimes() error {
6269
earliestTime := time.Now()
6370
var latestTime time.Time
@@ -89,6 +96,8 @@ func (b *BasicData) FillStartEndTimes() error {
8996
return nil
9097
}
9198

99+
// Validate checks the integrity of the BasicData fields, 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 conditions are not met.
92101
func (b *BasicData) Validate() error {
93102
if b.TestStart.IsZero() {
94103
return errors.New("test start time is missing. We cannot query Loki without a time range. Please set it and try again")
@@ -104,6 +113,10 @@ func (b *BasicData) Validate() error {
104113
return nil
105114
}
106115

116+
// IsComparable checks if two BasicData instances have the same configuration settings.
117+
// It validates the count, presence, and equivalence of generator configurations,
118+
// returning an error if any discrepancies are found. This function is useful for ensuring
119+
// consistency between data reports before processing or comparison.
107120
func (b *BasicData) IsComparable(otherData BasicData) error {
108121
// 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?
109122
if len(b.GeneratorConfigs) != len(otherData.GeneratorConfigs) {

wasp/benchspy/direct.go

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

23+
// NewStandardDirectQueryExecutor creates a new DirectQueryExecutor configured for standard queries.
24+
// It initializes the executor and generates the necessary queries, returning the executor or an error if the process fails.
2325
func NewStandardDirectQueryExecutor(generator *wasp.Generator) (*DirectQueryExecutor, error) {
2426
g := &DirectQueryExecutor{
2527
KindName: string(StandardQueryExecutor_Direct),
@@ -33,6 +35,8 @@ func NewStandardDirectQueryExecutor(generator *wasp.Generator) (*DirectQueryExec
3335
return NewDirectQueryExecutor(generator, queries)
3436
}
3537

38+
// NewDirectQueryExecutor creates a new DirectQueryExecutor with the specified generator and query functions.
39+
// It initializes the executor with a kind name and prepares a map for query results, enabling efficient query execution.
3640
func NewDirectQueryExecutor(generator *wasp.Generator, queries map[string]DirectQueryFn) (*DirectQueryExecutor, error) {
3741
g := &DirectQueryExecutor{
3842
KindName: string(StandardQueryExecutor_Direct),
@@ -44,14 +48,20 @@ func NewDirectQueryExecutor(generator *wasp.Generator, queries map[string]Direct
4448
return g, nil
4549
}
4650

51+
// Results returns the query results as a map of string keys to interface{} values.
52+
// It allows users to access the outcomes of executed queries, facilitating further processing or type assertions.
4753
func (g *DirectQueryExecutor) Results() map[string]interface{} {
4854
return g.QueryResults
4955
}
5056

57+
// Kind returns the type of the query executor as a string.
58+
// It is useful for identifying the specific implementation of a query executor in a collection.
5159
func (l *DirectQueryExecutor) Kind() string {
5260
return l.KindName
5361
}
5462

63+
// IsComparable checks if the given QueryExecutor is of the same type and has comparable configurations.
64+
// It returns an error if the types do not match or if the configurations are not comparable.
5565
func (g *DirectQueryExecutor) IsComparable(otherQueryExecutor QueryExecutor) error {
5666
otherType := reflect.TypeOf(otherQueryExecutor)
5767

@@ -83,6 +93,9 @@ func (l *DirectQueryExecutor) compareQueries(other map[string]DirectQueryFn) err
8393
return nil
8494
}
8595

96+
// Validate checks if the query executor is properly configured.
97+
// It ensures that a generator is set and at least one query is provided.
98+
// Returns an error if validation fails, helping to prevent execution issues.
8699
func (g *DirectQueryExecutor) Validate() error {
87100
if g.Generator == nil {
88101
return errors.New("generator is not set")
@@ -95,6 +108,9 @@ func (g *DirectQueryExecutor) Validate() error {
95108
return nil
96109
}
97110

111+
// Execute runs the defined queries using the data from the generator.
112+
// It validates the generator's data and aggregates responses before executing each query.
113+
// This function is essential for processing and retrieving results from multiple queries concurrently.
98114
func (g *DirectQueryExecutor) Execute(_ context.Context) error {
99115
if g.Generator == nil {
100116
return errors.New("generator is not set")
@@ -130,6 +146,8 @@ func (g *DirectQueryExecutor) Execute(_ context.Context) error {
130146
return nil
131147
}
132148

149+
// TimeRange ensures that the query executor operates within the specified time range.
150+
// It is a no-op for executors that already have responses stored in the correct time range.
133151
func (g *DirectQueryExecutor) TimeRange(_, _ time.Time) {
134152
// nothing to do here, since all responses stored in the generator are already in the right time range
135153
}
@@ -198,6 +216,9 @@ func (g *DirectQueryExecutor) standardQuery(standardMetric StandardLoadMetric) (
198216
}
199217
}
200218

219+
// MarshalJSON customizes the JSON representation of the DirectQueryExecutor.
220+
// It serializes only the relevant fields, including query names and results,
221+
// making it suitable for efficient data transmission and storage.
201222
func (g *DirectQueryExecutor) MarshalJSON() ([]byte, error) {
202223
// we need custom marshalling to only include query names, since the functions are not serializable
203224
type QueryExecutor struct {
@@ -226,6 +247,9 @@ func (g *DirectQueryExecutor) MarshalJSON() ([]byte, error) {
226247
})
227248
}
228249

250+
// UnmarshalJSON decodes JSON data into a DirectQueryExecutor instance.
251+
// It populates the executor's fields, including queries and results,
252+
// enabling seamless integration of JSON configurations into the executor's structure.
229253
func (g *DirectQueryExecutor) UnmarshalJSON(data []byte) error {
230254
// helper struct with QueryExecutors as json.RawMessage and QueryResults as map[string]interface{}
231255
// 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,9 @@ import (
1515
"golang.org/x/sync/errgroup"
1616
)
1717

18+
// NewLokiQueryExecutor creates a new LokiQueryExecutor instance.
19+
// It initializes the executor with provided queries and Loki configuration,
20+
// enabling efficient querying of logs from Loki in a structured manner.
1821
func NewLokiQueryExecutor(queries map[string]string, lokiConfig *wasp.LokiConfig) *LokiQueryExecutor {
1922
return &LokiQueryExecutor{
2023
KindName: string(StandardQueryExecutor_Loki),
@@ -40,14 +43,21 @@ type LokiQueryExecutor struct {
4043
Config *wasp.LokiConfig `json:"-"`
4144
}
4245

46+
// Results returns the query results as a map of string to interface{}.
47+
// It allows users to access the outcomes of executed queries, facilitating further processing or type assertions.
4348
func (l *LokiQueryExecutor) Results() map[string]interface{} {
4449
return l.QueryResults
4550
}
4651

52+
// Kind returns the type of the query executor as a string.
53+
// It is used to identify the specific kind of query executor in various operations.
4754
func (l *LokiQueryExecutor) Kind() string {
4855
return l.KindName
4956
}
5057

58+
// IsComparable checks if the given QueryExecutor is of the same type as the current instance.
59+
// It compares the queries of both executors to ensure they are equivalent in structure and content.
60+
// This function is useful for validating compatibility between different query executors.
5161
func (l *LokiQueryExecutor) IsComparable(otherQueryExecutor QueryExecutor) error {
5262
otherType := reflect.TypeOf(otherQueryExecutor)
5363

@@ -58,6 +68,9 @@ func (l *LokiQueryExecutor) IsComparable(otherQueryExecutor QueryExecutor) error
5868
return l.compareQueries(otherQueryExecutor.(*LokiQueryExecutor).Queries)
5969
}
6070

71+
// Validate checks if the LokiQueryExecutor has valid queries and configuration.
72+
// It returns an error if no queries are set or if the configuration is missing,
73+
// ensuring that the executor is ready for execution.
6174
func (l *LokiQueryExecutor) Validate() error {
6275
if len(l.Queries) == 0 {
6376
return errors.New("there are no Loki queries, there's nothing to fetch. Please set them and try again")
@@ -69,6 +82,9 @@ func (l *LokiQueryExecutor) Validate() error {
6982
return nil
7083
}
7184

85+
// Execute runs the configured Loki queries concurrently and collects the results.
86+
// It requires a valid configuration and handles basic authentication if provided.
87+
// The function returns an error if any query execution fails or if the configuration is missing.
7288
func (l *LokiQueryExecutor) Execute(ctx context.Context) error {
7389
var basicAuth client.LokiBasicAuth
7490

@@ -159,11 +175,15 @@ func (l *LokiQueryExecutor) compareQueries(other map[string]string) error {
159175
return nil
160176
}
161177

178+
// TimeRange sets the start and end time for the Loki query execution.
179+
// This function is essential for defining the time window of the data to be fetched.
162180
func (l *LokiQueryExecutor) TimeRange(start, end time.Time) {
163181
l.StartTime = start
164182
l.EndTime = end
165183
}
166184

185+
// UnmarshalJSON parses the JSON-encoded data and populates the LokiQueryExecutor fields.
186+
// It converts the query results from a generic map to a specific type map, enabling type-safe access to the results.
167187
func (l *LokiQueryExecutor) UnmarshalJSON(data []byte) error {
168188
// helper struct with QueryResults map[string]interface{}
169189
type Alias LokiQueryExecutor
@@ -188,6 +208,8 @@ func (l *LokiQueryExecutor) UnmarshalJSON(data []byte) error {
188208
return nil
189209
}
190210

211+
// NewStandardMetricsLokiExecutor creates a LokiQueryExecutor configured with standard metrics queries.
212+
// It generates queries based on provided test parameters and time range, returning the executor or an error if query generation fails.
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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ 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 understand data distributions
11+
// by retrieving values at specific percentiles, such as median or 95th percentile.
912
func CalculatePercentile(numbers []float64, percentile float64) float64 {
1013
// Sort the slice
1114
sort.Float64s(numbers)
@@ -31,6 +34,8 @@ func CalculatePercentile(numbers []float64, percentile float64) float64 {
3134
return numbers[lowerIndex]*(1-weight) + numbers[upperIndex]*weight
3235
}
3336

37+
// StringSliceToFloat64Slice converts a slice of strings to a slice of float64 values.
38+
// It returns an error if any string cannot be parsed as a float64, making it useful for data conversion tasks.
3439
func StringSliceToFloat64Slice(s []string) ([]float64, error) {
3540
numbers := make([]float64, len(s))
3641
for i, str := range s {

wasp/benchspy/prometheus.go

Lines changed: 28 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 used to configure query execution for Prometheus data sources.
2527
func NewPrometheusConfig(nameRegexPatterns ...string) *PrometheusConfig {
2628
return &PrometheusConfig{
2729
Url: os.Getenv(PrometheusUrlEnvVar),
@@ -56,6 +58,9 @@ func NewPrometheusQueryExecutor(queries map[string]string, config *PrometheusCon
5658
}, nil
5759
}
5860

61+
// NewStandardPrometheusQueryExecutor creates a PrometheusQueryExecutor with standard queries
62+
// based on the provided time range and configuration. It simplifies the process of generating
63+
// queries for Prometheus, making it easier to integrate Prometheus data into reports.
5964
func NewStandardPrometheusQueryExecutor(startTime, endTime time.Time, config *PrometheusConfig) (*PrometheusQueryExecutor, error) {
6065
p := &PrometheusQueryExecutor{}
6166

@@ -74,6 +79,8 @@ func NewStandardPrometheusQueryExecutor(startTime, endTime time.Time, config *Pr
7479
return NewPrometheusQueryExecutor(standardQueries, config)
7580
}
7681

82+
// Execute runs the defined Prometheus queries concurrently, collecting results and warnings.
83+
// It returns an error if any query fails, allowing for efficient data retrieval in reporting tasks.
7784
func (r *PrometheusQueryExecutor) Execute(ctx context.Context) error {
7885
for name, query := range r.Queries {
7986
result, warnings, queryErr := r.client.Query(ctx, query, r.EndTime)
@@ -91,14 +98,20 @@ func (r *PrometheusQueryExecutor) Execute(ctx context.Context) error {
9198
return nil
9299
}
93100

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

107+
// Kind returns the type of the query executor as a string.
108+
// It is used to identify the specific kind of executor in a collection of query executors.
98109
func (l *PrometheusQueryExecutor) Kind() string {
99110
return l.KindName
100111
}
101112

113+
// Validate checks the PrometheusQueryExecutor for a valid client and ensures that at least one query is provided.
114+
// It returns an error if the client is nil or no queries are specified, helping to ensure proper configuration before execution.
102115
func (r *PrometheusQueryExecutor) Validate() error {
103116
if r.client == nil {
104117
return errors.New("prometheus client is nil")
@@ -111,6 +124,8 @@ func (r *PrometheusQueryExecutor) Validate() error {
111124
return nil
112125
}
113126

127+
// IsComparable checks if the provided QueryExecutor is of the same type as the receiver.
128+
// It returns an error if the types do not match, ensuring type safety for query comparisons.
114129
func (r *PrometheusQueryExecutor) IsComparable(other QueryExecutor) error {
115130
otherType := reflect.TypeOf(other)
116131
if otherType != reflect.TypeOf(r) {
@@ -141,10 +156,15 @@ func (r *PrometheusQueryExecutor) compareQueries(other map[string]string) error
141156
return nil
142157
}
143158

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

166+
// MustResultsAsValue retrieves the query results as a map of metric names to their corresponding values.
167+
// It ensures that the results are in a consistent format, making it easier to work with metrics in subsequent operations.
148168
func (r *PrometheusQueryExecutor) MustResultsAsValue() map[string]model.Value {
149169
results := make(map[string]model.Value)
150170
for name, result := range r.QueryResults {
@@ -180,6 +200,8 @@ func (r *PrometheusQueryExecutor) MustResultsAsValue() map[string]model.Value {
180200
return results
181201
}
182202

203+
// TimeRange sets the start and end time for the Prometheus query execution.
204+
// This function is essential for defining the time window for data retrieval, ensuring accurate and relevant results.
183205
func (r *PrometheusQueryExecutor) TimeRange(startTime, endTime time.Time) {
184206
r.StartTime = startTime
185207
r.EndTime = endTime
@@ -222,6 +244,9 @@ type TypedMetric struct {
222244
MetricType string `json:"metric_type"`
223245
}
224246

247+
// MarshalJSON customizes the JSON representation of PrometheusQueryExecutor.
248+
// It includes only essential fields: Kind, Queries, and simplified QueryResults.
249+
// This function is useful for serializing the executor's state in a concise format.
225250
func (g *PrometheusQueryExecutor) MarshalJSON() ([]byte, error) {
226251
// we need custom marshalling to only include some parts of the metrics
227252
type QueryExecutor struct {
@@ -248,6 +273,9 @@ func (g *PrometheusQueryExecutor) MarshalJSON() ([]byte, error) {
248273
return json.Marshal(q)
249274
}
250275

276+
// UnmarshalJSON decodes JSON data into a PrometheusQueryExecutor instance.
277+
// It populates the QueryResults field with appropriately typed metrics,
278+
// enabling easy access to the results of Prometheus queries.
251279
func (r *PrometheusQueryExecutor) UnmarshalJSON(data []byte) error {
252280
// helper struct with QueryResults map[string]interface{}
253281
type Alias PrometheusQueryExecutor

0 commit comments

Comments
 (0)