-
Notifications
You must be signed in to change notification settings - Fork 118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Refactor] Refactor common code between all benchmark commands #1611
base: main
Are you sure you want to change the base?
Changes from all commits
505b5e7
3de300c
7bea775
e6c57b6
e5d444a
25d73b2
dab5303
7548ee2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,119 @@ | ||||||||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||||||||
// or more contributor license agreements. Licensed under the Elastic License; | ||||||||
// you may not use this file except in compliance with the Elastic License. | ||||||||
|
||||||||
package common | ||||||||
|
||||||||
import ( | ||||||||
"errors" | ||||||||
"fmt" | ||||||||
"os" | ||||||||
"path/filepath" | ||||||||
"strings" | ||||||||
|
||||||||
"github.com/elastic/go-ucfg/yaml" | ||||||||
) | ||||||||
|
||||||||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||||||||
// or more contributor license agreements. Licensed under the Elastic License; | ||||||||
// you may not use this file except in compliance with the Elastic License. | ||||||||
|
||||||||
const DevPath = "_dev/benchmark/rally" | ||||||||
|
||||||||
type Scenario struct { | ||||||||
Package string `config:"package" json:"package"` | ||||||||
Description string `config:"description" json:"description"` | ||||||||
Version string `config:"version" json:"version"` | ||||||||
DataStream DataStream `config:"data_stream" json:"data_stream"` | ||||||||
Corpora Corpora `config:"corpora" json:"corpora"` | ||||||||
} | ||||||||
|
||||||||
type DataStream struct { | ||||||||
Name string `config:"name" json:"name"` | ||||||||
} | ||||||||
|
||||||||
type Corpora struct { | ||||||||
Generator *Generator `config:"generator" json:"generator"` | ||||||||
} | ||||||||
|
||||||||
type Generator struct { | ||||||||
TotalEvents uint64 `config:"total_events" json:"total_events"` | ||||||||
Template CorporaTemplate `config:"template" json:"template"` | ||||||||
Config CorporaAsset `config:"config" json:"config"` | ||||||||
Fields CorporaAsset `config:"fields" json:"fields"` | ||||||||
} | ||||||||
|
||||||||
type CorporaAsset struct { | ||||||||
Raw map[string]interface{} `config:"raw" json:"raw"` | ||||||||
Path string `config:"path" json:"path"` | ||||||||
} | ||||||||
type CorporaTemplate struct { | ||||||||
Raw string `config:"raw" json:"raw"` | ||||||||
Path string `config:"path" json:"path"` | ||||||||
Type string `config:"type" json:"type"` | ||||||||
} | ||||||||
|
||||||||
func DefaultConfig() *Scenario { | ||||||||
return &Scenario{} | ||||||||
} | ||||||||
|
||||||||
func ReadConfig(path, scenario, packageName, packageVersion string) (*Scenario, error) { | ||||||||
configPath := filepath.Join(path, DevPath, fmt.Sprintf("%s.yml", scenario)) | ||||||||
c := DefaultConfig() | ||||||||
cfg, err := yaml.NewConfigWithFile(configPath) | ||||||||
if err != nil { | ||||||||
return nil, fmt.Errorf("can't load benchmark configuration: %s: %w", configPath, err) | ||||||||
} | ||||||||
|
||||||||
if err == nil { | ||||||||
if err := cfg.Unpack(c); err != nil { | ||||||||
return nil, fmt.Errorf("can't unpack benchmark configuration: %s: %w", configPath, err) | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
c.Package = packageName | ||||||||
c.Version = packageVersion | ||||||||
|
||||||||
if c.DataStream.Name == "" { | ||||||||
return nil, errors.New("can't read data stream name from benchmark configuration: empty") | ||||||||
} | ||||||||
|
||||||||
return c, nil | ||||||||
} | ||||||||
|
||||||||
func ReadScenarios(path, scenarioName, packageName, packageVersion string) (map[string]*Scenario, error) { | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit. Do we have any benefit on returning scenarios as a map? Returning a list would help controlling the order of execution and results.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jsoriano I would prefer to delay code changes to a different PR. The reason is for this PR I only moved code around, not type / logic change to make sure the refactoring does not affect any of the logic. |
||||||||
scenarios := make(map[string]*Scenario) | ||||||||
if len(scenarioName) > 0 { | ||||||||
scenario, err := ReadConfig(path, scenarioName, packageName, packageVersion) | ||||||||
if err != nil { | ||||||||
return nil, fmt.Errorf("error loading scenario: %w", err) | ||||||||
} | ||||||||
scenarios[scenarioName] = scenario | ||||||||
} else { | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit. I think we can remove the else to have one less nesting level.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See my comment above, happy to follow up further code changes but now trying to focus on only moving things around, no logical changes. |
||||||||
err := filepath.Walk(filepath.Join(path, DevPath), func(_ string, info os.FileInfo, err error) error { | ||||||||
if err != nil { | ||||||||
return err | ||||||||
} | ||||||||
|
||||||||
if info.IsDir() { | ||||||||
return nil | ||||||||
} | ||||||||
|
||||||||
if strings.HasSuffix(info.Name(), "-benchmark.yml") { | ||||||||
scenarioName = strings.TrimSuffix(info.Name(), ".yml") | ||||||||
scenario, err := ReadConfig(path, scenarioName, packageName, packageVersion) | ||||||||
if err != nil { | ||||||||
return err | ||||||||
} | ||||||||
scenarios[scenarioName] = scenario | ||||||||
} | ||||||||
|
||||||||
return nil | ||||||||
}) | ||||||||
if err != nil { | ||||||||
return nil, fmt.Errorf("error loading scenario: %w", err) | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
return scenarios, nil | ||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit.
common
seems too generic, could this go directly in theinternal/benchrunner/runners
package? Or to aninternal/benchrunner/scenario
package?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@aspacca Any thoughts?