llm-api-benchmark-tool/test/report_test.go

87 lines
3.6 KiB
Go

package test
import (
"fmt"
"os"
"strings"
"testing"
"time"
"llm-api-benchmark-tool/pkg/report"
"llm-api-benchmark-tool/pkg/stats"
"github.com/stretchr/testify/assert"
)
// createSampleStats creates a sample FinalStats struct for testing.
func createSampleStats() stats.FinalStats {
return stats.FinalStats{
TotalRequests: 1000,
SuccessfulRequests: 950,
FailedRequests: 50,
TotalDuration: 30 * time.Second,
AvgLatency: 150 * time.Millisecond,
MinLatency: 50 * time.Millisecond,
MaxLatency: 500 * time.Millisecond,
P90Latency: 250 * time.Millisecond,
P95Latency: 300 * time.Millisecond,
P99Latency: 400 * time.Millisecond,
AvgTimeToFirstToken: 80 * time.Millisecond,
MinTimeToFirstToken: 30 * time.Millisecond,
MaxTimeToFirstToken: 200 * time.Millisecond,
P90TimeToFirstToken: 120 * time.Millisecond,
P95TimeToFirstToken: 150 * time.Millisecond,
P99TimeToFirstToken: 180 * time.Millisecond,
AvgQPS: 33.33,
AvgTokensPerSecond: 500.5,
// Add more fields if necessary for report testing
}
}
// TestGenerateHTMLReport tests the basic HTML report generation.
func TestGenerateHTMLReport(t *testing.T) {
sampleStats := createSampleStats()
outputDir := t.TempDir() // Create a temporary directory for the report
outputFilePath := outputDir + "/report.html"
// Define expected chart IDs (must match IDs set in generator.go)
latencyChartID := "latencyHistogram"
ttftChartID := "ttftHistogram"
// Generate the report
err := report.GenerateHTMLReport(sampleStats, outputFilePath)
assert.NoError(t, err, "GenerateHTMLReport should not return an error")
// Check if the file was created
_, err = os.Stat(outputFilePath)
assert.NoError(t, err, "HTML report file should be created")
// Read the generated file content
contentBytes, err := os.ReadFile(outputFilePath)
assert.NoError(t, err, "Should be able to read the generated report file")
content := string(contentBytes)
// Basic checks on the HTML content
assert.True(t, strings.HasPrefix(content, "<!DOCTYPE html>"), "Output should start with DOCTYPE html")
assert.Contains(t, content, "<title>LLM API Benchmark Report</title>", "HTML should contain the correct title")
assert.Contains(t, content, "<h1>LLM API Benchmark Report</h1>", "HTML should contain the main heading")
assert.Contains(t, content, "<h2>Summary</h2>", "HTML should contain Summary section")
assert.Contains(t, content, "<h2>Charts</h2>", "HTML should contain Charts section")
// Check if some key stats are present (as simple text for now)
assert.Contains(t, content, "Total Requests:</td><td>1000</td>", "HTML should contain Total Requests stat")
assert.Contains(t, content, "Avg Latency:</td><td>150ms</td>", "HTML should contain Avg Latency stat")
assert.Contains(t, content, "Avg TTFT:</td><td>80ms</td>", "HTML should contain Avg TTFT stat")
assert.Contains(t, content, "Avg QPS:</td><td>33.33</td>", "HTML should contain Avg QPS stat")
// Check if the ECharts initialization script snippet is present for each chart
// latencyInitScript := fmt.Sprintf("echarts.init(document.getElementById('%s'))", latencyChartID)
// assert.Contains(t, content, latencyInitScript, "HTML should contain JS init for latency chart")
// Check if the chart divs are present (the ones generated by go-echarts render)
assert.Contains(t, content, fmt.Sprintf(`<div id="%s"`, latencyChartID), "HTML should contain div for latency chart")
assert.Contains(t, content, fmt.Sprintf(`<div id="%s"`, ttftChartID), "HTML should contain div for TTFT chart")
// TODO: Add more sophisticated checks? (e.g., parse HTML, check chart script data)
}