-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
789 lines (736 loc) · 21.7 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
package main
import (
"fmt"
"github.com/spf13/cobra"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
"io/fs"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes/scheme"
"knative.dev/pkg/apis"
"os"
"path/filepath"
"sort"
"strings"
"time"
)
func main() {
tapa := &cobra.Command{
Use: "tapa",
Long: "Tekton Artifact Performance Analysis (tapa) is a tool that inspects lists of Tekton objects or their underlying Pods\n" +
" and determines time spent on particular units of work, or the amount of time between the execution of pieces of work.",
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
},
}
tapa.PersistentFlags().StringVarP(&outputType, "output-type", "t", OutputTypeText, "output type, one of: text, csv")
tapa.ParseFlags(os.Args)
tapa.AddCommand(ParsePipelineRunList())
tapa.AddCommand(ParseTaskRunList())
tapa.AddCommand(ParsePodList())
tapa.AddCommand(ParseAllThreeLists())
if outputType != OutputTypeText && outputType != OutputTypeCsv {
tapa.Help()
fmt.Fprintf(os.Stderr, "Error: Invalid value for output-type: %s\n", outputType)
os.Exit(1)
}
if err := tapa.Execute(); err != nil {
fmt.Fprintf(os.Stderr, "tapa encountered the following error: %s\n", err.Error())
os.Exit(1)
}
}
var prStartTimes = map[string]time.Time{}
var prEndTimes = map[string]time.Time{}
var trStartTimes = map[string]time.Time{}
var trEndTimes = map[string]time.Time{}
var podStartTimes = map[string]time.Time{}
var podEndTimes = map[string]time.Time{}
var containerStartTimes = map[string]time.Time{}
var containerEndTimmes = map[string]time.Time{}
var prToDuration = map[string]float64{}
var prDurations = []float64{}
var prDurationsMap = map[float64]struct{}{}
var podToDuration = map[string]float64{}
var podDurations = []float64{}
var podDurationsMap = map[float64]struct{}{}
var trToDuration = map[string]float64{}
var trDurations = []float64{}
var trDurationsMap = map[float64]struct{}{}
var containerToDuration = map[string]float64{}
var containerDurations = []float64{}
var containerDurationsMap = map[float64]struct{}{}
const (
OutputTypeText string = "text"
OutputTypeCsv string = "csv"
)
var (
outputType = OutputTypeText
containerOnly = false
whoFailed = false
)
func processPRFiles(fileName string) (*v1beta1.PipelineRunList, error) {
var err error
prList := &v1beta1.PipelineRunList{}
prList.Items = []v1beta1.PipelineRun{}
v1beta1.AddToScheme(scheme.Scheme)
decoder := scheme.Codecs.UniversalDecoder()
err = filepath.Walk(fileName, func(path string, info fs.FileInfo, err error) error {
if err != nil {
fmt.Fprintf(os.Stderr, "filepath walk error: %s\n", err.Error())
return nil
}
if !info.IsDir() {
buf, e := os.ReadFile(path)
if e != nil {
fmt.Fprintf(os.Stderr, "problem reading %s: %s\n", path, e.Error())
return nil
}
prl := &v1beta1.PipelineRunList{}
_, _, e1 := decoder.Decode(buf, nil, prl)
pipelineRun := &v1beta1.PipelineRun{}
_, _, e2 := decoder.Decode(buf, nil, pipelineRun)
if e1 != nil && e2 != nil {
return nil
}
if len(prl.Items) > 0 {
// unmarshall is not a perfect type filter
for _, pr := range prl.Items {
if pr.Kind != "PipelineRun" {
continue
}
prList.Items = append(prList.Items, pr)
}
} else {
prList.Items = append(prList.Items, *pipelineRun)
}
}
return nil
})
return prList, err
}
func processTRFiles(fileName string) (*v1beta1.TaskRunList, error) {
var err error
trList := &v1beta1.TaskRunList{}
trList.Items = []v1beta1.TaskRun{}
v1beta1.AddToScheme(scheme.Scheme)
decoder := scheme.Codecs.UniversalDecoder()
err = filepath.Walk(fileName, func(path string, info fs.FileInfo, err error) error {
if err != nil {
fmt.Fprintf(os.Stderr, "filepath walk error: %s\n", err.Error())
return nil
}
if !info.IsDir() {
buf, e := os.ReadFile(path)
if e != nil {
fmt.Fprintf(os.Stderr, "problem reading %s: %s\n", path, e.Error())
return nil
}
trl := &v1beta1.TaskRunList{}
_, _, e1 := decoder.Decode(buf, nil, trl)
taskRun := &v1beta1.TaskRun{}
_, _, e2 := decoder.Decode(buf, nil, taskRun)
if e1 != nil && e2 != nil {
return nil
}
if len(trl.Items) > 0 {
// unmarshall is not a perfect type filter
for _, tr := range trl.Items {
if tr.Kind != "TaskRun" {
continue
}
trList.Items = append(trList.Items, tr)
}
} else {
trList.Items = append(trList.Items, *taskRun)
}
}
return nil
})
return trList, err
}
func processPodFiles(fileName string) (*corev1.PodList, error) {
var err error
podList := &corev1.PodList{}
podList.Items = []corev1.Pod{}
corev1.AddToScheme(scheme.Scheme)
decoder := scheme.Codecs.UniversalDecoder()
err = filepath.Walk(fileName, func(path string, info fs.FileInfo, err error) error {
if err != nil {
fmt.Fprintf(os.Stderr, "filepath walk error: %s\n", err.Error())
return nil
}
if !info.IsDir() {
buf, e := os.ReadFile(path)
if e != nil {
fmt.Fprintf(os.Stderr, "problem reading %s: %s\n", path, e.Error())
return nil
}
pl := &corev1.PodList{}
_, _, e1 := decoder.Decode(buf, nil, pl)
p := &corev1.Pod{}
_, _, e2 := decoder.Decode(buf, nil, p)
if e1 != nil && e2 != nil {
return nil
}
if len(pl.Items) > 0 {
for _, pod := range pl.Items {
if pod.Kind != "Pod" {
continue
}
podList.Items = append(podList.Items, pod)
}
} else {
podList.Items = append(podList.Items, *p)
}
}
return nil
})
return podList, err
}
func ignorePipelineRun(pr *v1beta1.PipelineRun, prFilter string) bool {
prKey := fmt.Sprintf("%s:%s", pr.Namespace, pr.Name)
if len(prFilter) > 0 && prKey != prFilter {
return true
}
if !pr.HasStarted() {
return true
}
if !pr.IsDone() {
return true
}
return false
}
func ignoreTaskRun(tr *v1beta1.TaskRun, prFilter string) bool {
if !tr.HasStarted() {
return true
}
if !tr.IsDone() {
return true
}
trKey := fmt.Sprintf("%s:%s", tr.Namespace, tr.Name)
if len(prFilter) > 0 && !strings.HasPrefix(trKey, prFilter) {
return true
}
return false
}
func ignorePod(pod *corev1.Pod, prFilter string) bool {
if pod.Status.StartTime == nil {
return true
}
if pod.Status.Phase != corev1.PodSucceeded && pod.Status.Phase != corev1.PodFailed {
return true
}
_, ok := pod.Labels["tekton.dev/pipelineRun"]
if !ok {
return true
}
podKey := fmt.Sprintf("%s:%s", pod.Namespace, pod.Name)
if len(prFilter) > 0 && !strings.HasPrefix(podKey, prFilter) {
return true
}
return false
}
func processPipelineRun(pr *v1beta1.PipelineRun) time.Duration {
duration := pr.Status.CompletionTime.Sub(pr.Status.StartTime.Time)
prKey := fmt.Sprintf("%s:%s", pr.Namespace, pr.Name)
prToDuration[prKey] = duration.Seconds()
_, ok := prDurationsMap[duration.Seconds()]
if !ok {
prDurations = append(prDurations, duration.Seconds())
prDurationsMap[duration.Seconds()] = struct{}{}
}
prStartTimes[prKey] = pr.Status.StartTime.Time
prEndTimes[prKey] = pr.Status.CompletionTime.Time
return duration
}
func processTaskRun(tr *v1beta1.TaskRun) time.Duration {
duration := tr.Status.CompletionTime.Sub(tr.Status.StartTime.Time)
trKey := fmt.Sprintf("%s:%s", tr.Namespace, tr.Name)
trToDuration[trKey] = duration.Seconds()
_, ok := trDurationsMap[duration.Seconds()]
if !ok {
trDurations = append(trDurations, duration.Seconds())
trDurationsMap[duration.Seconds()] = struct{}{}
}
trStartTimes[trKey] = tr.Status.StartTime.Time
trEndTimes[trKey] = tr.Status.CompletionTime.Time
return duration
}
func processPod(pod *corev1.Pod) time.Duration {
var terimnatedTime time.Time
for _, status := range pod.Status.ContainerStatuses {
terminated := status.State.Terminated
if terminated != nil {
if terminated.FinishedAt.Time.After(terimnatedTime) {
terimnatedTime = terminated.FinishedAt.Time
}
}
}
duration := terimnatedTime.Sub(pod.Status.StartTime.Time)
podKey := fmt.Sprintf("%s:%s", pod.Namespace, pod.Name)
podToDuration[podKey] = duration.Seconds()
podStartTimes[podKey] = pod.Status.StartTime.Time
podEndTimes[podKey] = terimnatedTime
_, ok := podDurationsMap[duration.Seconds()]
if !ok {
podDurations = append(podDurations, duration.Seconds())
podDurationsMap[duration.Seconds()] = struct{}{}
}
return duration
}
func processContainers(pod *corev1.Pod) []time.Duration {
durations := []time.Duration{}
specNameToIndex := map[string]int{}
statusNameToIndex := map[string]int{}
for index, container := range pod.Spec.Containers {
specNameToIndex[container.Name] = index
}
for index, cstatus := range pod.Status.ContainerStatuses {
statusNameToIndex[cstatus.Name] = index
}
for _, cstatus := range pod.Status.ContainerStatuses {
terminated := cstatus.State.Terminated
if terminated == nil {
continue
}
// containers are created started concurrently, but k8s/linux "pauses" then "resumes" per spec order
// so we take that finish time of the prior container if not the first container
started := terminated.StartedAt.Time
specIndex, _ := specNameToIndex[cstatus.Name]
if specIndex != 0 {
// not first container, get prior container finish time
priorContainerName := pod.Spec.Containers[specIndex-1].Name
priorContainerStatusIndex, _ := statusNameToIndex[priorContainerName]
priorContainerStatus := pod.Status.ContainerStatuses[priorContainerStatusIndex]
if priorContainerStatus.State.Terminated != nil {
started = priorContainerStatus.State.Terminated.FinishedAt.Time
}
}
finished := terminated.FinishedAt.Time
duration := finished.Sub(started)
ckey := fmt.Sprintf("%s:%s-%s", pod.Namespace, pod.Name, cstatus.Name)
containerToDuration[ckey] = duration.Seconds()
containerStartTimes[ckey] = started
containerEndTimmes[ckey] = finished
_, ok := containerDurationsMap[duration.Seconds()]
if !ok {
containerDurations = append(containerDurations, duration.Seconds())
containerDurationsMap[duration.Seconds()] = struct{}{}
}
}
return durations
}
func determinePRConcurrency(prKey string) int {
return innerConcurrency(prKey, prStartTimes, prEndTimes)
}
func determineTRConcurrency(trKey string) int {
return innerConcurrency(trKey, trStartTimes, trEndTimes)
}
func determinePodConcurrency(prKey string) int {
return innerConcurrency(prKey, podStartTimes, podEndTimes)
}
func determineContainerConcurrency(ckey string) int {
return innerConcurrency(ckey, containerStartTimes, containerEndTimmes)
}
func innerConcurrency(key string, starts map[string]time.Time, ends map[string]time.Time) int {
st, _ := starts[key]
en, _ := ends[key]
total := 1
for k, start := range starts {
if k == key {
continue
}
end, _ := ends[k]
if start.Equal(st) && end.Equal(en) {
total++
continue
}
if start.Before(en) && end.After(st) {
total++
}
}
return total
}
func findFailedPipelineRns(fileName, prFilter string) ([]string, []string) {
nslist := []string{}
namelist := []string{}
prList, err := processPRFiles(fileName)
if err != nil {
return []string{fmt.Sprintf("ERROR: problem reading file %s: %s\n", fileName, err.Error())}, nil
}
for _, pr := range prList.Items {
if ignorePipelineRun(&pr, prFilter) {
continue
}
if !pr.IsDone() {
fmt.Fprintf(os.Stderr, "PipelineRun %s:%s is not done\n", pr.Namespace, pr.Name)
continue
}
succeedCondition := pr.Status.GetCondition(apis.ConditionSucceeded)
// IsDone guarantees that the success condition is not nil
if succeedCondition.IsFalse() {
fmt.Fprintf(os.Stdout, "PipelineRun %s:%s failed\n", pr.Namespace, pr.Name)
nslist = append(nslist, pr.Namespace)
namelist = append(namelist, pr.Name)
}
}
return nslist, namelist
}
func findPodLogsForPipelineRun(ns, name, fileName string) error {
err := filepath.Walk(fileName, func(path string, info fs.FileInfo, err error) error {
if err != nil {
fmt.Fprintf(os.Stderr, "filepath walk error: %s\n", err.Error())
return nil
}
if !info.IsDir() {
if !strings.HasSuffix(path, ".log") {
return nil
}
if !strings.Contains(path, ns) {
return nil
}
if !strings.Contains(path, name) {
return nil
}
buf, e := os.ReadFile(path)
if e != nil {
fmt.Fprintf(os.Stderr, "problem reading %s: %s\n", path, e.Error())
return nil
}
fmt.Fprintf(os.Stdout, "PipelineRun %s:%s pod file %s has contents:\n %s\n", ns, name, info.Name(), string(buf))
}
return nil
})
if err != nil {
fmt.Fprintf(os.Stderr, "error finding pod logs: %s\n", err.Error())
}
return err
}
func parsePipelineRunList(fileName, prFilter string) ([]string, []float64, []int, bool) {
prList, err := processPRFiles(fileName)
if err != nil {
return []string{fmt.Sprintf("ERROR: problem reading file %s: %s\n", fileName, err.Error())}, nil, nil, false
}
for _, pr := range prList.Items {
if ignorePipelineRun(&pr, prFilter) {
continue
}
processPipelineRun(&pr)
}
sort.Float64s(prDurations)
retS := []string{}
retF := []float64{}
retI := []int{}
for _, duration := range prDurations {
for key, value := range prToDuration {
if value == duration {
retS = append(retS, key)
retF = append(retF, value)
retI = append(retI, determinePRConcurrency(key))
}
}
}
return retS, retF, retI, true
}
func ParsePipelineRunList() *cobra.Command {
parsePRList := &cobra.Command{
Use: "prlist <file location or directory tree with files> [<options>]",
Short: "Parse a list of Tekton PipelineRuns for various statistics",
Long: "Parse a list of Tekton PipelineRuns for various statistics",
Example: `
# Print the runtime stats
$ tapa prlist <pipelinerun list json/yaml files or directory with files>
# Print the pipelineruns that failed
$ tapa prlist <pipelinerun list json/yaml files or directory with files> --who-failed
`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
fmt.Fprintf(os.Stderr, "ERROR: not enough arguments: %s\n", cmd.Use)
return
}
fileName := args[0]
if whoFailed {
prns, prname := findFailedPipelineRns(fileName, "")
for i, ns := range prns {
name := prname[i]
findPodLogsForPipelineRun(ns, name, fileName)
}
return
}
retS, retF, retI, ok := parsePipelineRunList(fileName, "")
w := os.Stdout
if !ok {
w = os.Stderr
for _, str := range retS {
fmt.Fprintf(w, str)
}
}
printList("PipelineRun", retS, retF, retI)
},
}
parsePRList.Flags().BoolVar(&whoFailed, "who-failed", whoFailed,
"Only list pipelineruns that failed")
return parsePRList
}
func parsePodList(fileName, prFilter string) ([]string, []float64, []int, bool) {
podList, err := processPodFiles(fileName)
if err != nil {
return []string{fmt.Sprintf("ERROR: file %s not marshalling into a Pod list: %s\n", fileName, err.Error())}, nil, nil, false
}
for _, pod := range podList.Items {
if ignorePod(&pod, prFilter) {
continue
}
if !containerOnly {
processPod(&pod)
} else {
processContainers(&pod)
}
}
retS := []string{}
retF := []float64{}
retI := []int{}
if !containerOnly {
sort.Float64s(podDurations)
for _, duration := range podDurations {
for key, value := range podToDuration {
if value == duration {
retS = append(retS, key)
retF = append(retF, value)
retI = append(retI, determinePodConcurrency(key))
}
}
}
}
if containerOnly {
sort.Float64s(containerDurations)
for _, duration := range containerDurations {
for key, value := range containerToDuration {
if value == duration {
retS = append(retS, key)
retF = append(retF, value)
retI = append(retI, determineContainerConcurrency(key))
}
}
}
}
return retS, retF, retI, true
}
func ParsePodList() *cobra.Command {
parsePodListCmd := &cobra.Command{
Use: "podlist <file location or directory tree with files> [<options>]",
Short: "Parse a list of Pods for various statistics",
Long: "Parse a list of Pods for various statistics",
Example: `
# Print just the pods
$ tapa podlist <pod list json/yaml file or directory with files>
# Print just the containers
$ tapa podlist <pod list json/yaml file or directory with files> --containers-only
`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
fmt.Fprintf(os.Stderr, "ERROR: not enough arguments: %s\n", cmd.Use)
return
}
fileName := args[0]
retS, retF, retI, ok := parsePodList(fileName, "")
w := os.Stdout
if !ok {
w = os.Stderr
for _, str := range retS {
fmt.Fprintf(w, str)
}
return
}
printList("Pod", retS, retF, retI)
},
}
parsePodListCmd.Flags().BoolVar(&containerOnly, "containers-only", containerOnly,
"Only list containers and not pods")
return parsePodListCmd
}
func parseTaskRunList(fileName, prFilter string) ([]string, []float64, []int, bool) {
trList, err := processTRFiles(fileName)
if err != nil {
return []string{fmt.Sprintf("ERROR: file %s not marshalling into a TaskRun list: %s\n", fileName, err.Error())}, nil, nil, false
}
for _, tr := range trList.Items {
if ignoreTaskRun(&tr, prFilter) {
continue
}
processTaskRun(&tr)
}
sort.Float64s(trDurations)
retS := []string{}
retF := []float64{}
retI := []int{}
for _, duration := range trDurations {
for key, value := range trToDuration {
if value == duration {
retS = append(retS, key)
retF = append(retF, value)
retI = append(retI, determineTRConcurrency(key))
}
}
}
return retS, retF, retI, true
}
func ParseTaskRunList() *cobra.Command {
parseTRList := &cobra.Command{
Use: "trlist <file location or directory tree with files> [<options>]",
Short: "Parse a list of TaskRun for various statistics",
Long: "Parse a list of TaskRun for various statistics",
Example: `
`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
fmt.Fprintf(os.Stderr, "ERROR: not enough arguments: %s\n", cmd.Use)
return
}
fileName := args[0]
retS, retF, retI, ok := parseTaskRunList(fileName, "")
w := os.Stdout
if !ok {
w = os.Stderr
for _, str := range retS {
fmt.Fprintf(w, str)
}
return
}
printList("TaskRun", retS, retF, retI)
},
}
return parseTRList
}
func ParseAllThreeLists() *cobra.Command {
allList := &cobra.Command{
Use: "all <pr file location> <tr file location> <pod file location> [<options>]",
Short: "Parse a list of PipelineRuns, their TaskRuns, and their Pods, for various statistics",
Long: "Parse a list of PipelineRuns, their TaskRuns, and their Pods, for various statistics",
Example: `
`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) < 1 {
fmt.Fprintf(os.Stderr, "ERROR: not enough arguments: %s\n", cmd.Use)
return
}
fileStat, err := os.Stat(args[0])
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: could not analyze file %s: %s\n", args[0], err.Error())
return
}
if len(args) < 3 && !fileStat.IsDir() {
fmt.Fprintf(os.Stderr, "ERROR: not enough arguments: %s\n", cmd.Use)
return
}
var prFileName, trFileName, podFileName string
if !fileStat.IsDir() {
prFileName = args[0]
trFileName = args[1]
podFileName = args[2]
} else {
prFileName, trFileName, podFileName = args[0], args[0], args[0]
}
retS1, retF1, retI1, ok1 := parsePipelineRunList(prFileName, "")
if !ok1 {
for _, s := range retS1 {
fmt.Fprintf(os.Stderr, s)
}
return
}
retS2, retF2, retI2, ok2 := parseTaskRunList(trFileName, "")
if !ok2 {
for _, s := range retS2 {
fmt.Fprintf(os.Stderr, s)
}
return
}
retS3, retF3, retI3, ok3 := parsePodList(podFileName, "")
if !ok3 {
for _, s := range retS3 {
fmt.Fprintf(os.Stderr, s)
}
return
}
printHeader("PipelineRun", "Duration", "Concurrency", "TaskRunsDuration", "TaskRunsDelta", "TaskRunsPercentage", "TaskRunsMaxConcurrency", "PodsDuration", "PodsDelta", "PodsPercentage", "PodsMaxConcurrency")
for i, prkey := range retS1 {
prDuration := retF1[i]
prConcurency := retI1[i]
totalTRDuration := float64(0)
maxTRConcurrency := 0
for ii, trKey := range retS2 {
if !strings.HasPrefix(trKey, prkey) {
continue
}
totalTRDuration = totalTRDuration + retF2[ii]
if retI2[ii] > maxTRConcurrency {
maxTRConcurrency = retI2[ii]
}
}
totalPodDuration := float64(0)
maxPodConcurrency := 0
for iii, podKey := range retS3 {
if !strings.HasPrefix(podKey, prkey) {
continue
}
totalPodDuration = totalPodDuration + retF3[iii]
maxPodConcurrency = maxPodConcurrency + retI3[iii]
if retI3[iii] > maxPodConcurrency {
maxPodConcurrency = retI3[iii]
}
}
printLine("PipelineRun %s\t\t took %v seconds with pr concurrency %d with taskruns %v seconds delta %v percent %f taskrun max concurrency %d pods %v seconds delta %v percent %f pod max concurrency %d\n",
prkey,
prDuration,
prConcurency,
totalTRDuration,
prDuration-totalTRDuration,
totalTRDuration/prDuration,
maxTRConcurrency,
totalPodDuration,
prDuration-totalPodDuration,
totalPodDuration/prDuration,
maxPodConcurrency)
}
},
}
return allList
}
func printHeader(headers ...string) {
out := ""
switch outputType {
case OutputTypeCsv:
for _, h := range headers {
if len(out) > 0 {
out += fmt.Sprintf(";%s", h)
} else {
out = h
}
}
fmt.Fprintln(os.Stdout, out)
default:
// text output does not have a header, do not print anything
}
}
func printLine(format string, values ...any) {
w := os.Stdout
out := ""
switch outputType {
case OutputTypeCsv:
for _, v := range values {
if len(out) > 0 {
out += fmt.Sprintf(";%v", v)
} else {
out = fmt.Sprintf("%v", v)
}
}
fmt.Fprintln(w, out)
default:
fmt.Fprintf(w, format, values...)
}
}
func printList(resource string, keys []string, durations []float64, concurencies []int) {
printHeader(resource, "Duration", "Concurrency")
for i, key := range keys {
printLine(fmt.Sprintf("%s %%s\t\ttook %%v seconds concurrency %%d\n", resource), key, durations[i], concurencies[i])
}
}