Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion internal/command/updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,34 @@ import (
"github.com/CustomResourceDefinition/catalog/internal/timing"
)

type teeWriter struct {
Writers []io.Writer
}

func (t teeWriter) Write(p []byte) (n int, err error) {
for _, w := range t.Writers {
if _, err := w.Write(p); err != nil {
return n, err
}
n = len(p)
}
return n, nil
}

func (cmd Updater) createSummaryWriter() (io.Writer, func(), error) {
summaryPath := os.Getenv("GITHUB_STEP_SUMMARY")
if summaryPath == "" {
return cmd.Logger, func() {}, nil
}

f, err := os.OpenFile(summaryPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return nil, nil, err
}

return teeWriter{Writers: []io.Writer{cmd.Logger, f}}, func() { f.Close() }, nil
}

type Updater struct {
Configuration, Schema, Definitions string
Logger io.Writer
Expand Down Expand Up @@ -105,7 +133,12 @@ func (cmd Updater) Run() error {
}
}

totalStats.PrintSummary(cmd.Logger)
writer, closer, err := cmd.createSummaryWriter()
if err != nil {
return err
}
defer closer()
totalStats.PrintSummary(writer)

return merge(tmpDir, cmd.Schema)
}
Expand Down
111 changes: 107 additions & 4 deletions internal/command/updater_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package command

import (
"bytes"
"fmt"
"io"
"net/http"
"net/http/httptest"
"os"
Expand Down Expand Up @@ -328,9 +330,9 @@ func TestRunAggregatesStats(t *testing.T) {

outStr := output.String()
assert.Contains(t, outStr, "Update Statistics")
assert.Contains(t, outStr, "Overall:")
assert.Contains(t, outStr, "**Overall:**")
assert.Contains(t, outStr, "operations")
assert.Contains(t, outStr, "Http:")
assert.Contains(t, outStr, "#### Http")
assert.Contains(t, outStr, "api_fetch")
}

Expand Down Expand Up @@ -417,6 +419,107 @@ func TestRunWithPerformanceLog(t *testing.T) {
perfContent, err := os.ReadFile(logPath)
assert.Nil(t, err)
assert.NotEmpty(t, string(perfContent))
assert.Contains(t, string(perfContent), "http")
assert.Contains(t, string(perfContent), "api_fetch")
}

func TestTeeWriter(t *testing.T) {
var buf1, buf2 bytes.Buffer
tw := teeWriter{Writers: []io.Writer{&buf1, &buf2}}

n, err := tw.Write([]byte("hello world"))
assert.Nil(t, err)
assert.Equal(t, 11, n)

assert.Equal(t, "hello world", buf1.String())
assert.Equal(t, "hello world", buf2.String())
}

func TestTeeWriterPartialError(t *testing.T) {
var buf1 bytes.Buffer
errorWriter := errorWriter{err: io.EOF}
tw := teeWriter{Writers: []io.Writer{&buf1, errorWriter}}

_, err := tw.Write([]byte("hello"))
assert.Error(t, err)
assert.Equal(t, io.EOF, err)
}

type errorWriter struct {
err error
}

func (e errorWriter) Write(p []byte) (n int, err error) {
return 0, e.err
}

func TestCreateSummaryWriterNoEnv(t *testing.T) {
original := os.Getenv("GITHUB_STEP_SUMMARY")
os.Unsetenv("GITHUB_STEP_SUMMARY")
defer func() {
if original != "" {
os.Setenv("GITHUB_STEP_SUMMARY", original)
}
}()

buf := bytes.NewBuffer([]byte{})
updater := Updater{Logger: buf}

writer, closer, err := updater.createSummaryWriter()
assert.Nil(t, err)
assert.Equal(t, buf, writer)
closer()
}

func TestCreateSummaryWriterWithEnv(t *testing.T) {
original := os.Getenv("GITHUB_STEP_SUMMARY")
tmpDir := t.TempDir()
summaryPath := path.Join(tmpDir, "summary.md")
os.Setenv("GITHUB_STEP_SUMMARY", summaryPath)
defer func() {
os.Unsetenv("GITHUB_STEP_SUMMARY")
if original != "" {
os.Setenv("GITHUB_STEP_SUMMARY", original)
}
}()

buf := bytes.NewBuffer([]byte{})
updater := Updater{Logger: buf}

writer, closer, err := updater.createSummaryWriter()
assert.Nil(t, err)
closer()

_, ok := writer.(teeWriter)
assert.True(t, ok)

content, err := os.ReadFile(summaryPath)
assert.Nil(t, err)
assert.Empty(t, string(content))
}

func TestCreateSummaryWriterAppendsToFile(t *testing.T) {
original := os.Getenv("GITHUB_STEP_SUMMARY")
tmpDir := t.TempDir()
summaryPath := path.Join(tmpDir, "summary.md")
os.WriteFile(summaryPath, []byte("Existing content\n"), 0644)
os.Setenv("GITHUB_STEP_SUMMARY", summaryPath)
defer func() {
os.Unsetenv("GITHUB_STEP_SUMMARY")
if original != "" {
os.Setenv("GITHUB_STEP_SUMMARY", original)
}
}()

buf := bytes.NewBuffer([]byte{})
updater := Updater{Logger: buf}

writer, closer, err := updater.createSummaryWriter()
assert.Nil(t, err)

fmt.Fprintf(writer, "New content")
closer()

content, err := os.ReadFile(summaryPath)
assert.Nil(t, err)
assert.Contains(t, string(content), "Existing content")
assert.Contains(t, string(content), "New content")
}
28 changes: 13 additions & 15 deletions internal/timing/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,8 @@ func (s *Stats) PrintSummary(writer io.Writer) {
s.mu.RLock()
defer s.mu.RUnlock()

fmt.Fprintf(writer, "\n=== Update Statistics ===\n\n")
fmt.Fprintf(writer, "Overall: %s (%d operations)\n\n", formatDuration(s.totalTime), s.totalOps)
fmt.Fprintf(writer, "\n### Update Statistics\n\n")
fmt.Fprintf(writer, "**Overall:** %s (%d operations)\n\n", formatDuration(s.totalTime), s.totalOps)

categoryOrder := []Category{CategoryHTTP, CategoryGit, CategoryHelm, CategoryOCI, CategoryGeneration, CategoryMisc}

Expand All @@ -252,7 +252,9 @@ func (s *Stats) PrintSummary(writer io.Writer) {
typeStats[op.Type] = append(typeStats[op.Type], op.Duration)
}

fmt.Fprintf(writer, "%s:\n", strings.ToUpper(string(cat)[:1])+string(cat)[1:])
fmt.Fprintf(writer, "#### %s\n\n", strings.ToUpper(string(cat)[:1])+string(cat)[1:])
fmt.Fprintf(writer, "| Operation | Count | Total | p75 | p90 | p95 |\n")
fmt.Fprintf(writer, "|-----------|-------|-------|-----|-----|-----|\n")

for opType, durations := range typeStats {
if len(durations) == 0 {
Expand All @@ -266,23 +268,19 @@ func (s *Stats) PrintSummary(writer io.Writer) {
durationsSecs[i] = d.Seconds()
}

percs := calculatePercentiles(durationsSecs, []float64{0.75, 0.90, 0.95})
percentiles := calculatePercentiles(durationsSecs, []float64{0.75, 0.90, 0.95})

typeLabel := string(opType)
fmt.Fprintf(writer, " %-10s %4d operations total: %s",
typeLabel+":",
p75 := formatDuration(percentiles[0.75])
p90 := formatDuration(percentiles[0.90])
p95 := formatDuration(percentiles[0.95])

fmt.Fprintf(writer, "| %s | %d | %s | %s | %s | %s |\n",
typeLabel,
len(durations),
formatDuration(total),
p75, p90, p95,
)

if len(percs) > 0 {
fmt.Fprintf(writer, " p75: %s p90: %s p95: %s",
formatDuration(percs[0.75]),
formatDuration(percs[0.90]),
formatDuration(percs[0.95]),
)
}
fmt.Fprintln(writer)
}
fmt.Fprintln(writer)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/timing/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,6 @@ func TestPrintSummaryWriter(t *testing.T) {
s.PrintSummary(&buf)

assert.Contains(t, buf.String(), "Update Statistics")
assert.Contains(t, buf.String(), "Overall:")
assert.Contains(t, buf.String(), "Http:")
assert.Contains(t, buf.String(), "**Overall:**")
assert.Contains(t, buf.String(), "#### Http")
}