Skip to content

Commit

Permalink
MM-56605: Create results file and print to file (#729)
Browse files Browse the repository at this point in the history
* MM-56605: Create results file and print to file

* Separate writter into its own function for later

* Mofify writeToFile. Passing the file object.

* implement 	multiWriter so I can double the writes and then send them to cosole as well as file.

* Replace file object with interface.

* - Remove separate function to create file
- Move file creatuin and writting to the file to Comparison function

* Draft unti test for PrintResults

* Make test runnable by using LoadTests and Status properly.

* - Solve missing use of interface in the initail code chnage
- Solve whitespace mismatch

* Change expected to allign with the output. This solves failing unit test.

* Address most several of the code review feedback point.

* Use existing file path that is already being provided to the function.

* Address linter complaint about use of `Sprintf` for strings that need no formating.

* Address review comments

* Remove line printing an error as it was redundant.

* Update another if block that redundantly prints and returns the same error.

* Implement missed correction from Alejandro. If block must return the correct var when returning the error.
Ensure this is not occuring on other lines I've added.
  • Loading branch information
DHaussermann authored Sep 10, 2024
1 parent f869c7e commit 80e6355
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 22 deletions.
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) {
// 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())
}

0 comments on commit 80e6355

Please sign in to comment.