Skip to content
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

MM-56605: Create results file and print to file #729

Merged
merged 17 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
c3dce66
MM-56605: Create results file and print to file
DHaussermann Apr 16, 2024
bbaabaf
Separate writter into its own function for later
DHaussermann Apr 18, 2024
d560e9c
Mofify writeToFile. Passing the file object.
DHaussermann Apr 18, 2024
ef54815
implement multiWriter so I can double the writes and then send them …
DHaussermann Apr 18, 2024
00134be
Replace file object with interface.
DHaussermann May 7, 2024
4a2e621
- Remove separate function to create file
DHaussermann May 21, 2024
65aee47
Draft unti test for PrintResults
DHaussermann Jun 7, 2024
d65c7ec
Make test runnable by using LoadTests and Status properly.
DHaussermann Jun 18, 2024
a38abb5
- Solve missing use of interface in the initail code chnage
DHaussermann Jul 9, 2024
18ed511
Change expected to allign with the output. This solves failing unit t…
DHaussermann Jul 24, 2024
c6113ee
Address most several of the code review feedback point.
DHaussermann Sep 5, 2024
93e4052
Use existing file path that is already being provided to the function.
DHaussermann Sep 6, 2024
5d8cf1b
Address linter complaint about use of `Sprintf` for strings that need…
DHaussermann Sep 6, 2024
90a549f
Address review comments
DHaussermann Sep 9, 2024
21151df
Remove line printing an error as it was redundant.
DHaussermann Sep 10, 2024
128ff74
Update another if block that redundantly prints and returns the same …
DHaussermann Sep 10, 2024
455ccab
Implement missed correction from Alejandro. If block must return the …
DHaussermann Sep 10, 2024
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
59 changes: 37 additions & 22 deletions cmd/ltctl/comparison.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,30 +63,30 @@ func getReportFilename(id int, res comparison.Result) string {
return fmt.Sprintf("report_%s.md", name)
}

func printResults(results []comparison.Result) {
func writeResults(results []comparison.Result, writer io.Writer) error {
var content string

for i, res := range results {
fmt.Println("==================================================")
fmt.Println("Comparison result:")
if res.Report != "" {
fmt.Printf("Report: %s\n", getReportFilename(i, res))
}
if res.DashboardURL != "" {
fmt.Printf("Grafana Dashboard: %s\n", res.DashboardURL)
}
content += "=================================================="
content += "Comparison result:"
content += fmt.Sprintf("Report: %s\n", getReportFilename(i, res))
content += fmt.Sprintf("Grafana Dashboard: %s\n", res.DashboardURL)
for _, ltRes := range res.LoadTests {
fmt.Printf("%s:\n", ltRes.Label)
fmt.Printf(" Type: %s\n", ltRes.Config.Type)
fmt.Printf(" DB Engine: %s\n", ltRes.Config.DBEngine)
content += fmt.Sprintf("%s:\n", ltRes.Label)
content += fmt.Sprintf(" Type: %s\n", ltRes.Config.Type)
content += fmt.Sprintf(" DB Engine: %s\n", ltRes.Config.DBEngine)
if ltRes.Config.Type == comparison.LoadTestTypeBounded {
fmt.Printf(" Duration: %s\n", ltRes.Config.Duration)
fmt.Printf(" Users: %d\n", ltRes.Config.NumUsers)
content += fmt.Sprintf(" Duration: %s\n", ltRes.Config.Duration)
content += fmt.Sprintf(" Users: %d\n", ltRes.Config.NumUsers)
} else if ltRes.Config.Type == comparison.LoadTestTypeUnbounded {
fmt.Printf(" Supported Users: %d\n", ltRes.Status.SupportedUsers)
content += fmt.Sprintf(" Supported Users: %d\n", ltRes.Status.SupportedUsers)
}
fmt.Printf(" Errors: %d\n", ltRes.Status.NumErrors)
content += fmt.Sprintf(" Errors: %d\n", ltRes.Status.NumErrors)
}
fmt.Printf("==================================================\n\n")
content += "==================================================\n\n"
}
_, err := fmt.Fprintf(writer, content)
return err
}

func writeReports(results []comparison.Result, outPath string) error {
Expand Down Expand Up @@ -161,16 +161,31 @@ func RunComparisonCmdF(cmd *cobra.Command, args []string) error {
if err := writeReports(output.Results, outputPath); err != nil {
return fmt.Errorf("failed to write reports: %w", err)
}
printResults(output.Results)
}

if archive {
if err := createArchive(outputPath, archivePath); err != nil {
return fmt.Errorf("failed to create archive: %w", err)
//create the file
resultsFile, errResult := os.Create(filepath.Join(outputPath, "results.txt"))
if errResult != nil {
return fmt.Errorf("failed to create file: %w", errResult)
}
defer resultsFile.Close()

multiWriter := io.MultiWriter(resultsFile, os.Stdout)

// Call writeResults and handle any errors it returns
err := writeResults(output.Results, multiWriter)
if err != nil {
return fmt.Errorf("failed to write results: %w", err)
}

if archive {
if err := createArchive(outputPath, archivePath); err != nil {
return fmt.Errorf("failed to create archive: %w", err)
}
}
}

return nil

}

func CollectComparisonCmdF(cmd *cobra.Command, args []string) error {
Expand Down
73 changes: 73 additions & 0 deletions cmd/ltctl/comparison_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package main

import (
"bytes"
"testing"

"github.com/mattermost/mattermost-load-test-ng/comparison"
"github.com/mattermost/mattermost-load-test-ng/coordinator"
"github.com/stretchr/testify/require"
)

func TestPrintResults(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great test!

// Create a buffer to capture the output
buf := &bytes.Buffer{}

// Prepare test data
results := []comparison.Result{
{
Report: "Sample Report",
DashboardURL: "http://example.com/dashboard",
LoadTests: [2]comparison.LoadTestResult{
{
Label: "Test1",
Config: comparison.LoadTestConfig{
Type: "bounded",
DBEngine: "postgres",
NumUsers: 100,
Duration: "10m",
},
Status: coordinator.Status{
SupportedUsers: 80,
NumErrors: 2,
},
},
{
Label: "Test2",
Config: comparison.LoadTestConfig{
Type: "bounded",
DBEngine: "postgres",
NumUsers: 100,
Duration: "10m",
},
Status: coordinator.Status{
SupportedUsers: 80,
NumErrors: 2,
},
},
},
},
}
// Call the function with the test data and the buffer
writeResults(results, buf)

// Verify the output
expectedOutput := `==================================================Comparison result:Report: report_0_postgres_bounded_100.md
Grafana Dashboard: http://example.com/dashboard
Test1:
Type: bounded
DB Engine: postgres
Duration: 10m
Users: 100
Errors: 2
Test2:
Type: bounded
DB Engine: postgres
Duration: 10m
Users: 100
Errors: 2
==================================================

`
require.Equal(t, expectedOutput, buf.String())
}
Loading