refactor(main): delegate setup to config, store, and web packages
Replace in-file bootstrap logic with package-level constructors in `run`: - use `config.Load()` instead of local env parsing/AppConfig helpers - use `store.Open()` and `web.New()` for persistence and app wiring - rename local store variable to `benchmarkStore` for clarity This centralizes startup concerns in dedicated modules, reducing `main.go` boilerplate and improving maintainability.refactor(main): delegate setup to config, store, and web packages Replace in-file bootstrap logic with package-level constructors in `run`: - use `config.Load()` instead of local env parsing/AppConfig helpers - use `store.Open()` and `web.New()` for persistence and app wiring - rename local store variable to `benchmarkStore` for clarity This centralizes startup concerns in dedicated modules, reducing `main.go` boilerplate and improving maintainability.
This commit is contained in:
141
lib/model/submission.go
Normal file
141
lib/model/submission.go
Normal file
@@ -0,0 +1,141 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type BenchmarkConfig struct {
|
||||
DurationSecs int `json:"durationSecs"`
|
||||
Intensity int `json:"intensity"`
|
||||
CoreFilter int `json:"coreFilter"`
|
||||
MultiCore bool `json:"multiCore"`
|
||||
}
|
||||
|
||||
type CPUInfo struct {
|
||||
BrandString string `json:"brandString"`
|
||||
VendorID string `json:"vendorID"`
|
||||
PhysicalCores int `json:"physicalCores"`
|
||||
LogicalCores int `json:"logicalCores"`
|
||||
BaseClockMHz int `json:"baseClockMHz"`
|
||||
BoostClockMHz int `json:"boostClockMHz"`
|
||||
L1DataKB int `json:"l1DataKB"`
|
||||
L2KB int `json:"l2KB"`
|
||||
L3MB int `json:"l3MB"`
|
||||
IsHybrid bool `json:"isHybrid,omitempty"`
|
||||
Has3DVCache bool `json:"has3DVCache,omitempty"`
|
||||
PCoreCount int `json:"pCoreCount,omitempty"`
|
||||
ECoreCount int `json:"eCoreCount,omitempty"`
|
||||
Cores []CPUCoreDescriptor `json:"cores,omitempty"`
|
||||
SupportedFeatures []string `json:"supportedFeatures,omitempty"`
|
||||
}
|
||||
|
||||
type CPUCoreDescriptor struct {
|
||||
LogicalID int `json:"LogicalID"`
|
||||
PhysicalID int `json:"PhysicalID"`
|
||||
CoreID int `json:"CoreID"`
|
||||
Type int `json:"Type"`
|
||||
}
|
||||
|
||||
type CoreResult struct {
|
||||
LogicalID int `json:"logicalID"`
|
||||
CoreType string `json:"coreType"`
|
||||
MOpsPerSec float64 `json:"mOpsPerSec"`
|
||||
TotalOps int64 `json:"totalOps"`
|
||||
}
|
||||
|
||||
type BenchmarkResult struct {
|
||||
Config BenchmarkConfig `json:"config"`
|
||||
CPUInfo CPUInfo `json:"cpuInfo"`
|
||||
StartedAt time.Time `json:"startedAt"`
|
||||
Duration int64 `json:"duration"`
|
||||
TotalOps int64 `json:"totalOps"`
|
||||
MOpsPerSec float64 `json:"mOpsPerSec"`
|
||||
Score int64 `json:"score"`
|
||||
CoreResults []CoreResult `json:"coreResults"`
|
||||
}
|
||||
|
||||
type Submission struct {
|
||||
SubmissionID string `json:"submissionID"`
|
||||
Submitter string `json:"submitter"`
|
||||
SubmittedAt time.Time `json:"submittedAt"`
|
||||
BenchmarkResult
|
||||
}
|
||||
|
||||
func (b BenchmarkResult) Validate() error {
|
||||
if strings.TrimSpace(b.CPUInfo.BrandString) == "" {
|
||||
return errors.New("cpuInfo.brandString is required")
|
||||
}
|
||||
|
||||
if b.StartedAt.IsZero() {
|
||||
return errors.New("startedAt is required and must be RFC3339-compatible")
|
||||
}
|
||||
|
||||
if b.Config.DurationSecs <= 0 {
|
||||
return errors.New("config.durationSecs must be greater than zero")
|
||||
}
|
||||
|
||||
if b.Duration <= 0 {
|
||||
return errors.New("duration must be greater than zero")
|
||||
}
|
||||
|
||||
if b.TotalOps < 0 || b.Score < 0 || b.MOpsPerSec < 0 {
|
||||
return errors.New("duration, totalOps, mOpsPerSec, and score must be non-negative")
|
||||
}
|
||||
|
||||
if b.CPUInfo.LogicalCores < 0 || b.CPUInfo.PhysicalCores < 0 {
|
||||
return errors.New("cpu core counts must be non-negative")
|
||||
}
|
||||
|
||||
for _, result := range b.CoreResults {
|
||||
if result.LogicalID < 0 {
|
||||
return fmt.Errorf("coreResults.logicalID must be non-negative")
|
||||
}
|
||||
|
||||
if result.MOpsPerSec < 0 || result.TotalOps < 0 {
|
||||
return fmt.Errorf("coreResults values must be non-negative")
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func NormalizeSubmitter(submitter string) string {
|
||||
submitter = strings.TrimSpace(submitter)
|
||||
if submitter == "" {
|
||||
return "Anonymous"
|
||||
}
|
||||
|
||||
return submitter
|
||||
}
|
||||
|
||||
func ThreadModeLabel(multiCore bool) string {
|
||||
if multiCore {
|
||||
return "Multi-threaded"
|
||||
}
|
||||
|
||||
return "Single-threaded"
|
||||
}
|
||||
|
||||
func CloneSubmission(submission *Submission) *Submission {
|
||||
if submission == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
copySubmission := *submission
|
||||
if len(submission.CoreResults) > 0 {
|
||||
copySubmission.CoreResults = append([]CoreResult(nil), submission.CoreResults...)
|
||||
}
|
||||
|
||||
if len(submission.CPUInfo.Cores) > 0 {
|
||||
copySubmission.CPUInfo.Cores = append([]CPUCoreDescriptor(nil), submission.CPUInfo.Cores...)
|
||||
}
|
||||
|
||||
if len(submission.CPUInfo.SupportedFeatures) > 0 {
|
||||
copySubmission.CPUInfo.SupportedFeatures = append([]string(nil), submission.CPUInfo.SupportedFeatures...)
|
||||
}
|
||||
|
||||
return ©Submission
|
||||
}
|
||||
Reference in New Issue
Block a user