-
Notifications
You must be signed in to change notification settings - Fork 4.8k
TRT-2423: feat(tests): add BigQuery-based test duration tracking #30493
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
Draft
stbenjam
wants to merge
1
commit into
openshift:main
Choose a base branch
from
stbenjam:test-duration
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
pkg/cmd/openshift-tests/generate/durations/durations_command.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| package durations | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "os" | ||
| "strings" | ||
|
|
||
| "cloud.google.com/go/bigquery" | ||
| "github.com/spf13/cobra" | ||
| "google.golang.org/api/iterator" | ||
| "k8s.io/cli-runtime/pkg/genericclioptions" | ||
| "k8s.io/kubectl/pkg/util/templates" | ||
| ) | ||
|
|
||
| func NewDurationsCommand(streams genericclioptions.IOStreams) *cobra.Command { | ||
| o := NewDurationsOptions(streams) | ||
|
|
||
| cmd := &cobra.Command{ | ||
| Use: "durations", | ||
| Short: "Generate test duration data from BigQuery", | ||
| Long: templates.LongDesc(` | ||
| Generate test duration data from BigQuery | ||
|
|
||
| This command queries the BigQuery table containing junit test results and produces | ||
| a JSON file with average test durations for tests in the openshift-tests suite | ||
| from the last 7 days (configurable). Durations are calculated as the average | ||
| and rounded to the nearest second. | ||
|
|
||
| By default, the output is written to pkg/test/ginkgo/testDurations.json. | ||
|
|
||
| The output format is: | ||
| { | ||
| "test.name": { | ||
| "average_duration": 123 | ||
| } | ||
| } | ||
| `), | ||
| SilenceUsage: true, | ||
| SilenceErrors: true, | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| if err := o.Complete(args); err != nil { | ||
| return err | ||
| } | ||
| if err := o.Validate(); err != nil { | ||
| return err | ||
| } | ||
| if err := o.Run(context.Background()); err != nil { | ||
| return err | ||
| } | ||
| return nil | ||
| }, | ||
| } | ||
|
|
||
| o.AddFlags(cmd.Flags()) | ||
| return cmd | ||
| } | ||
|
|
||
| // DurationResult represents a test duration result from BigQuery | ||
| type DurationResult struct { | ||
| TestName string `bigquery:"test_name"` | ||
| AverageDuration float64 `bigquery:"average_duration"` | ||
| } | ||
|
|
||
| // TestDurationData represents the output format for a single test | ||
| type TestDurationData struct { | ||
| AverageDuration int `json:"average_duration"` | ||
| } | ||
|
|
||
| // Run executes the durations command | ||
| func (o *DurationsOptions) Run(ctx context.Context) error { | ||
| // Create BigQuery client | ||
| client, err := bigquery.NewClient(ctx, o.ProjectID) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to create BigQuery client: %w", err) | ||
| } | ||
| defer client.Close() | ||
|
|
||
| // Build the SQL query | ||
| query := o.buildQuery() | ||
|
|
||
| // Execute the query | ||
| q := client.Query(query) | ||
| it, err := q.Read(ctx) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to execute query: %w", err) | ||
| } | ||
|
|
||
| // Collect results | ||
| results := make(map[string]TestDurationData) | ||
| for { | ||
| var row DurationResult | ||
| err := it.Next(&row) | ||
| if err == iterator.Done { | ||
| break | ||
| } | ||
| if err != nil { | ||
| return fmt.Errorf("failed to read query result: %w", err) | ||
| } | ||
|
|
||
| // Round to nearest second and store | ||
| results[row.TestName] = TestDurationData{ | ||
| AverageDuration: int(row.AverageDuration), | ||
| } | ||
| } | ||
|
|
||
| // Convert to JSON | ||
| jsonData, err := json.MarshalIndent(results, "", " ") | ||
| if err != nil { | ||
| return fmt.Errorf("failed to marshal JSON: %w", err) | ||
| } | ||
|
|
||
| // Ensure the directory exists | ||
| dir := fmt.Sprintf("%s", o.OutputFile[:strings.LastIndex(o.OutputFile, "/")]) | ||
| if err := os.MkdirAll(dir, 0755); err != nil { | ||
| return fmt.Errorf("failed to create output directory: %w", err) | ||
| } | ||
|
|
||
| // Write output to file | ||
| err = os.WriteFile(o.OutputFile, jsonData, 0644) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to write output file: %w", err) | ||
| } | ||
| fmt.Fprintf(o.Out, "Test duration data written to %s\n", o.OutputFile) | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // buildQuery constructs the BigQuery SQL query | ||
| func (o *DurationsOptions) buildQuery() string { | ||
| return fmt.Sprintf(` | ||
| SELECT | ||
| test_name, | ||
| ROUND(AVG(duration_ms / 1000.0)) as average_duration | ||
| FROM `+"`%s.%s.%s`"+` | ||
| WHERE | ||
| modified_time >= DATETIME_SUB(CURRENT_DATETIME(), INTERVAL %d DAY) | ||
| AND testsuite LIKE '%%openshift-tests%%' | ||
| GROUP BY test_name | ||
| ORDER BY test_name | ||
| `, o.ProjectID, o.DatasetID, o.TableID, o.Days) | ||
| } | ||
14 changes: 14 additions & 0 deletions
14
pkg/cmd/openshift-tests/generate/durations/durations_flags.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package durations | ||
|
|
||
| import ( | ||
| "github.com/spf13/pflag" | ||
| ) | ||
|
|
||
| // AddFlags adds command-line flags to the provided flagset | ||
| func (o *DurationsOptions) AddFlags(flags *pflag.FlagSet) { | ||
| flags.StringVar(&o.ProjectID, "project", o.ProjectID, "BigQuery project ID") | ||
| flags.StringVar(&o.DatasetID, "dataset", o.DatasetID, "BigQuery dataset ID") | ||
| flags.StringVar(&o.TableID, "table", o.TableID, "BigQuery table ID") | ||
| flags.IntVar(&o.Days, "days", o.Days, "Number of days to look back for test data") | ||
| flags.StringVar(&o.OutputFile, "output", o.OutputFile, "Output file path") | ||
| } |
65 changes: 65 additions & 0 deletions
65
pkg/cmd/openshift-tests/generate/durations/durations_options.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| package durations | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "k8s.io/cli-runtime/pkg/genericclioptions" | ||
| ) | ||
|
|
||
| // DurationsOptions contains the options for the durations command | ||
| type DurationsOptions struct { | ||
| // BigQuery configuration | ||
| ProjectID string | ||
| DatasetID string | ||
| TableID string | ||
|
|
||
| // Query configuration | ||
| Days int | ||
|
|
||
| // Output configuration | ||
| OutputFile string | ||
|
|
||
| // IO streams | ||
| genericclioptions.IOStreams | ||
| } | ||
|
|
||
| // NewDurationsOptions creates a new DurationsOptions with default values | ||
| func NewDurationsOptions(streams genericclioptions.IOStreams) *DurationsOptions { | ||
| return &DurationsOptions{ | ||
| ProjectID: "openshift-gce-devel", | ||
| DatasetID: "ci_analysis_us", | ||
| TableID: "junit", | ||
| Days: 7, | ||
| OutputFile: "pkg/test/ginkgo/testDurations.json", | ||
| IOStreams: streams, | ||
| } | ||
| } | ||
|
|
||
| // Complete completes the options based on command arguments | ||
| func (o *DurationsOptions) Complete(args []string) error { | ||
| // No arguments expected for this command | ||
| if len(args) > 0 { | ||
| return fmt.Errorf("no arguments are expected") | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // Validate validates the options | ||
| func (o *DurationsOptions) Validate() error { | ||
| if o.ProjectID == "" { | ||
| return fmt.Errorf("project ID cannot be empty") | ||
| } | ||
| if o.DatasetID == "" { | ||
| return fmt.Errorf("dataset ID cannot be empty") | ||
| } | ||
| if o.TableID == "" { | ||
| return fmt.Errorf("table ID cannot be empty") | ||
| } | ||
| if o.Days <= 0 { | ||
| return fmt.Errorf("days must be a positive integer") | ||
| } | ||
| if o.Days > 365 { | ||
| return fmt.Errorf("days cannot exceed 365") | ||
| } | ||
| return nil | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Have you tested it with
openshift-ci-data-analysis.ci_data.JobTestSummaryByDatenoted in TRT-2389? Thought being that using the daily summary would be more cost effective over time. Also considered this being part of jobrunhistoricaldataanalyzerUh oh!
There was an error while loading. Please reload this page.
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.
JobTestSummaryByDate appears to be job-level data? I only care about average test duration
Uh oh!
There was an error while loading. Please reload this page.
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.
Oh, maybe
TestsSummaryByDate? It is still grouped by job. I'm not sure I care about job-level data, I just need a rough estimate for test duration, and my JUnit query is only 13GB here and I'd guess we only care to run it at most once a day or week.I'm a little concerned about building things in our repos on bespoke tables without schema management, especially if we'll plan to eventually try to change our data sources. Relying on JUnit makes various things easier to move en masse if we need to because its widely used
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.
Yeah,
openshift-ci-data-analysis.ci_data.TestsSummaryByDatemy bad.We have a whole process setup that does this already for disruption and alert data. We have a job that runs weekly to update the data. It would make sense to me to have this logic running in the same process and if ci-tool already works with biq query then keeping the logic over there makes more sense to me as well.