Files
LinkPort/internal/store/audit_benchmark_test.go
曾志威 9da95beb42
continuous-integration/drone Build is failing
feat: refine management UI, connection diagnostics and local startup
Migrate the workspace to React and Ant Design, improve account forms and responsive layouts, support outbound diagnostics and SSH fingerprint confirmation, and persist the management startup port. Preserve database migrations, remove S3 settings storage, and improve gateway audit handling.
2026-10-13 19:43:23 +08:00

82 lines
2.3 KiB
Go

package store
import (
"encoding/base64"
"fmt"
"linkport/internal/model"
"linkport/internal/security"
"path/filepath"
"testing"
"time"
)
func benchmarkAuditStore(b *testing.B) *Store {
b.Helper()
vault, err := security.NewVault(base64.StdEncoding.EncodeToString(make([]byte, 32)))
if err != nil {
b.Fatal(err)
}
database, err := Open(filepath.Join(b.TempDir(), "config.sqlite"), vault)
if err != nil {
b.Fatal(err)
}
b.Cleanup(func() { database.Close() })
return database
}
func BenchmarkCommandAudit(b *testing.B) {
for _, parallel := range []bool{false, true} {
b.Run(fmt.Sprintf("parallel=%v", parallel), func(b *testing.B) {
database := benchmarkAuditStore(b)
proxy := model.Proxy{ID: "fixture", Type: "redis"}
command := model.Command{Kind: "ping", Text: `["PING"]`, Decision: "allowed"}
if err := database.RecordCommand(proxy, "fixture", command); err != nil {
b.Fatal(err)
}
b.ResetTimer()
if parallel {
b.RunParallel(func(worker *testing.PB) {
for worker.Next() {
if err := database.RecordCommand(proxy, "fixture", command); err != nil {
b.Error(err)
return
}
}
})
} else {
for iteration := 0; iteration < b.N; iteration++ {
if err := database.RecordCommand(proxy, "fixture", command); err != nil {
b.Fatal(err)
}
}
}
})
}
}
func BenchmarkCommandOutputAudit(b *testing.B) {
for _, shards := range []int{1, 64} {
b.Run(fmt.Sprintf("shards=%d", shards), func(b *testing.B) {
database := benchmarkAuditStore(b)
when := time.Now().UTC()
for day := 1; day < shards; day++ {
if _, err := database.auditDB(when.AddDate(0, 0, -day).Format("2006-01-02")); err != nil {
b.Fatal(err)
}
}
proxy := model.Proxy{ID: "fixture", Type: "mysql"}
command := model.Command{AuditID: "fixture-query", RecordedAt: when, Kind: "query", Text: "SELECT 1", Decision: "allowed"}
if err := database.RecordCommand(proxy, "fixture", command); err != nil {
b.Fatal(err)
}
output := model.CommandOutput{CommandID: command.AuditID, RecordedAt: when, Stream: "result", Data: []byte("fixture-row\n")}
b.ResetTimer()
for iteration := 0; iteration < b.N; iteration++ {
if err := database.RecordCommandOutput(proxy, "fixture", output); err != nil {
b.Fatal(err)
}
}
})
}
}