@@ -3,6 +3,7 @@ package main
33import (
44 "context"
55 "encoding/json"
6+ "errors"
67 "fmt"
78 "os"
89 "path/filepath"
@@ -11,14 +12,12 @@ import (
1112 "sync"
1213 "time"
1314
14- "github.com/pkg/errors"
15- "go.uber.org/ratelimit"
16- "golang.org/x/sync/errgroup"
17-
1815 "github.com/fatih/color"
1916 "github.com/google/go-github/v50/github"
2017 "github.com/google/uuid"
18+ "go.uber.org/ratelimit"
2119 "golang.org/x/oauth2"
20+ "golang.org/x/sync/errgroup"
2221
2322 "github.com/smartcontractkit/chainlink-testing-framework/framework"
2423)
4039 SlowTestThreshold = 5 * time .Minute
4140 ExtremelySlowTestThreshold = 10 * time .Minute
4241
43- DebugDirRoot = "ctf-ci-debug"
44- DebugSubDirWF = filepath .Join (DebugDirRoot , "workflows" )
45- DebugSubDirJobs = filepath .Join (DebugDirRoot , "jobs" )
42+ DebugDirRoot = "ctf-ci-debug"
43+ DebugSubDirWF = filepath .Join (DebugDirRoot , "workflows" )
44+ DebugSubDirJobs = filepath .Join (DebugDirRoot , "jobs" )
45+ DefaultResultsDir = "."
46+ DefaultResultsFile = "ctf-ci"
4647)
4748
4849type GitHubActionsClient interface {
@@ -51,38 +52,38 @@ type GitHubActionsClient interface {
5152}
5253
5354type AnalysisConfig struct {
54- Debug bool
55- Owner string
56- Repo string
57- WorkflowName string
58- TimeDaysBeforeStart int
59- TimeStart time.Time
60- TimeDaysBeforeEnd int
61- TimeEnd time.Time
62- Typ string
63- ResultsFile string
55+ Debug bool `json:"debug"`
56+ Owner string `json:"owner"`
57+ Repo string `json:"repo"`
58+ WorkflowName string `json:"workflow_name"`
59+ TimeDaysBeforeStart int `json:"time_days_before_start"`
60+ TimeStart time.Time `json:"time_start"`
61+ TimeDaysBeforeEnd int `json:"time_days_before_end"`
62+ TimeEnd time.Time `json:"time_end"`
63+ Typ string `json:"type"`
64+ ResultsFile string `json:"results_file"`
6465}
6566
6667type Stat struct {
67- Name string
68- Successes int
69- Failures int
70- Cancels int
71- ReRuns int
72- P50 time.Duration
73- P95 time.Duration
74- P99 time.Duration
75- TotalDuration time.Duration
76- Durations []time.Duration
68+ Name string `json:"name"`
69+ Successes int `json:"successes"`
70+ Failures int `json:"failures"`
71+ Cancels int `json:"cancels"`
72+ ReRuns int `json:"reRuns"`
73+ P50 time.Duration `json:"p50"`
74+ P95 time.Duration `json:"p95"`
75+ P99 time.Duration `json:"p99"`
76+ TotalDuration time.Duration `json:"totalDuration"`
77+ Durations []time.Duration `json:"-"`
7778}
7879
7980type Stats struct {
80- Mu * sync.Mutex
81- Runs int
82- CancelledRuns int
83- IgnoredRuns int
84- Jobs map [string ]* Stat
85- Steps map [string ]* Stat
81+ Mu * sync.Mutex `json:"-"`
82+ Runs int `json:"runs"`
83+ CancelledRuns int `json:"cancelled_runs"`
84+ IgnoredRuns int `json:"ignored_runs"`
85+ Jobs map [string ]* Stat `json:"jobs"`
86+ Steps map [string ]* Stat `json:"steps"`
8687}
8788
8889func calculatePercentiles (stat * Stat ) * Stat {
@@ -104,7 +105,7 @@ func refreshDebugDirs() {
104105 }
105106}
106107
107- func writeStruct (enabled bool , dir , name string , data interface {}) error {
108+ func dumpResults (enabled bool , dir , name string , data interface {}) error {
108109 if enabled {
109110 d , err := json .MarshalIndent (data , "" , " " )
110111 if err != nil {
@@ -137,6 +138,7 @@ func AnalyzeJobsSteps(ctx context.Context, client GitHubActionsClient, cfg *Anal
137138
138139 eg := & errgroup.Group {}
139140 for _ , wr := range runs .WorkflowRuns {
141+ framework .L .Debug ().Str ("Name" , * wr .Name ).Msg ("Analyzing workflow run" )
140142 if ! strings .Contains (* wr .Name , cfg .WorkflowName ) {
141143 stats .IgnoredRuns ++
142144 continue
@@ -145,7 +147,7 @@ func AnalyzeJobsSteps(ctx context.Context, client GitHubActionsClient, cfg *Anal
145147 // analyze workflow
146148 name := * wr .Name
147149 framework .L .Debug ().Str ("Name" , name ).Msg ("Analyzing workflow run" )
148- _ = writeStruct (cfg .Debug , DebugSubDirWF , name , wr )
150+ _ = dumpResults (cfg .Debug , DebugSubDirWF , name , wr )
149151 eg .Go (func () error {
150152 rlJobs .Take ()
151153 jobs , _ , err := client .ListWorkflowJobs (ctx , cfg .Owner , cfg .Repo , * wr .ID , & github.ListWorkflowJobsOptions {
@@ -155,11 +157,11 @@ func AnalyzeJobsSteps(ctx context.Context, client GitHubActionsClient, cfg *Anal
155157 return err
156158 }
157159 // analyze jobs
160+ stats .Mu .Lock ()
161+ defer stats .Mu .Unlock ()
158162 for _ , j := range jobs .Jobs {
159- stats .Mu .Lock ()
160- defer stats .Mu .Unlock ()
161163 name := * j .Name
162- _ = writeStruct (cfg .Debug , DebugSubDirJobs , name , wr )
164+ _ = dumpResults (cfg .Debug , DebugSubDirJobs , name , wr )
163165 if skippedOrInProgressJob (j ) {
164166 stats .IgnoredRuns ++
165167 continue
@@ -283,6 +285,9 @@ func AnalyzeCIRuns(cfg *AnalysisConfig) (*Stats, error) {
283285 if err != nil {
284286 return nil , fmt .Errorf ("failed to fetch workflow runs: %w" , err )
285287 }
288+ if cfg .ResultsFile != "" {
289+ _ = dumpResults (true , DefaultResultsDir , DefaultResultsFile , stats )
290+ }
286291 return stats , nil
287292}
288293
0 commit comments