services

package
v0.0.0-...-b8497f2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 7, 2025 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Overview

Package services contains the interfaces for the application's core services.

Package services contains the interfaces for the application's core services.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FindTimeRange

func FindTimeRange(traces []models.SQLTrace) (minTime, maxTime time.Time)

func MySQLToClickHouseTypeMapper

func MySQLToClickHouseTypeMapper(sourceType string) (string, error)

MySQLToClickHouseTypeMapper is a simple type mapper for converting MySQL types to ClickHouse types.

Types

type BenchmarkConfig

type BenchmarkConfig struct {
	TraceCount  int
	Concurrency int
	Timeout     time.Duration
}

type BenchmarkResult

type BenchmarkResult struct {
	ModelName       string
	Throughput      float64 // traces/sec
	AvgLatency      float64 // ms
	P95Latency      float64
	P99Latency      float64
	MemoryUsageMB   float64
	CPUUsagePercent float64
	ValidationScore float64 // 0-1
	ErrorRate       float64 // 0-1
}

type BenchmarkRunner

type BenchmarkRunner struct {
	Models   []TraceGeneratorModel
	Analyzer *PerformanceAnalyzer
}

func NewBenchmarkRunner

func NewBenchmarkRunner(models []TraceGeneratorModel) *BenchmarkRunner

func (*BenchmarkRunner) Run

type BoundUniformSampler

type BoundUniformSampler struct {
	// contains filtered or unexported fields
}

BoundUniformSampler adapts a uniform sampling strategy.

func (*BoundUniformSampler) Sample

func (s *BoundUniformSampler) Sample() (interface{}, error)

type BoundWeightedSampler

type BoundWeightedSampler struct {
	// contains filtered or unexported fields
}

BoundWeightedSampler adapts the generic WeightedRandomSampler to the ModelSampler interface.

func (*BoundWeightedSampler) Sample

func (s *BoundWeightedSampler) Sample() (interface{}, error)

type BoundZipfSampler

type BoundZipfSampler struct {
	// contains filtered or unexported fields
}

BoundZipfSampler adapts the generic ZipfSampler to the ModelSampler interface by binding it to a specific ParameterModel.

func (*BoundZipfSampler) Sample

func (s *BoundZipfSampler) Sample() (interface{}, error)

type ExecutionService

type ExecutionService struct {
	// contains filtered or unexported fields
}

ExecutionService is responsible for running a benchmark workload and collecting performance metrics.

func NewExecutionService

func NewExecutionService(rc RateController, slowThreshold time.Duration) *ExecutionService

NewExecutionService creates a new ExecutionService.

func (*ExecutionService) Reset

func (s *ExecutionService) Reset()

Reset resets the metrics recorder.

func (*ExecutionService) RunBench

RunBench executes a benchmark workload and returns the final performance metrics.

type GenerationConfig

type GenerationConfig struct {
	TotalQueries int
	ScaleFactor  float64
}

GenerationConfig holds configuration for workload generation.

type HotspotDetector

type HotspotDetector struct {
	Threshold float64 // e.g. 0.05 for Top 5%
}

func NewHotspotDetector

func NewHotspotDetector() *HotspotDetector

func (*HotspotDetector) DetectDistribution

func (d *HotspotDetector) DetectDistribution(stats *ParameterStats) *models.ParameterModel

DetectDistribution analyzes the frequency map and determines the distribution type and parameters.

type MetricsRecorder

type MetricsRecorder struct {
	// contains filtered or unexported fields
}

MetricsRecorder is responsible for recording performance metrics during a benchmark run. It is designed to be thread-safe.

func NewMetricsRecorder

func NewMetricsRecorder(slowThreshold time.Duration) *MetricsRecorder

NewMetricsRecorder creates a new metrics recorder. - slowThreshold: The duration after which a query is considered "slow".

func (*MetricsRecorder) Finalize

func (r *MetricsRecorder) Finalize(totalDuration time.Duration) *models.PerformanceMetrics

Finalize calculates the summary statistics after the benchmark run is complete. It should be called once at the end of the benchmark.

func (*MetricsRecorder) Record

func (r *MetricsRecorder) Record(latency time.Duration, err error)

Record records the result of a single query execution. It captures the latency and whether an error occurred.

type ModelSampler

type ModelSampler interface {
	Sample() (interface{}, error)
}

ModelSampler defines the interface for a sampler bound to a specific parameter model.

type ParamType

type ParamType int
const (
	ParamTypeUnknown ParamType = iota
	ParamTypeInt
	ParamTypeString
	ParamTypeDatetime
)

type ParameterAnalyzer

type ParameterAnalyzer struct {
	MaxCardinality int // Max unique values to track per parameter
	// contains filtered or unexported fields
}

func NewParameterAnalyzer

func NewParameterAnalyzer() *ParameterAnalyzer

func (*ParameterAnalyzer) Analyze

func (a *ParameterAnalyzer) Analyze(traces []models.SQLTrace) map[string]*models.ParameterModel

Analyze processes traces and returns statistical models for each parameter.

type ParameterExtractor

type ParameterExtractor interface {
	Extract(sql string, template *models.SQLTemplate) (map[string]interface{}, error)
}

ParameterExtractor defines the interface for extracting parameters from SQL queries.

type ParameterService

type ParameterService struct {
	// contains filtered or unexported fields
}

ParameterService is responsible for building a statistical model of parameters from SQL traces.

func NewParameterService

func NewParameterService() *ParameterService

NewParameterService creates a new ParameterService.

func (*ParameterService) BuildModel

BuildModel analyzes a collection of SQL traces and builds a WorkloadParameterModel.

type ParameterStats

type ParameterStats struct {
	ParamName   string
	Type        ParamType
	ValueCounts map[interface{}]int
	TotalCount  int
}

type Parser

type Parser interface {
	// ListTables extracts the table names from a given SQL query.
	ListTables(sql string) ([]string, error)
}

Parser is the interface for a SQL parser. It defines the contract for any parser implementation, whether it's regex-based or a full-fledged AST parser.

type PerformanceAnalyzer

type PerformanceAnalyzer struct{}

func (*PerformanceAnalyzer) CalculateAverage

func (a *PerformanceAnalyzer) CalculateAverage(values []float64) float64

func (*PerformanceAnalyzer) CalculatePercentile

func (a *PerformanceAnalyzer) CalculatePercentile(values []float64, percentile float64) float64

func (*PerformanceAnalyzer) CalculateValidationScore

func (a *PerformanceAnalyzer) CalculateValidationScore(
	original, generated []models.SQLTrace,
	validator *StatisticalValidator,
) float64

func (*PerformanceAnalyzer) GetResourceUsage

func (a *PerformanceAnalyzer) GetResourceUsage() (memoryMB float64, cpuPercent float64)

type RateController

type RateController interface {
	Start(ctx context.Context)
	Acquire(ctx context.Context) error
	Stop()
	MaxConcurrency() int
}

RateController defines the interface for controlling the rate and concurrency of execution.

type RegexParameterExtractor

type RegexParameterExtractor struct {
	// contains filtered or unexported fields
}

RegexParameterExtractor extracts parameter values using regex matching against the template.

func (*RegexParameterExtractor) Extract

func (r *RegexParameterExtractor) Extract(sql string, template *models.SQLTemplate) (map[string]interface{}, error)

type ReportGenerator

type ReportGenerator struct{}

ReportGenerator generates validation reports from test results.

func NewReportGenerator

func NewReportGenerator() *ReportGenerator

NewReportGenerator creates a new ReportGenerator.

func (*ReportGenerator) Generate

func (g *ReportGenerator) Generate(
	distResults, temporalResults, queryResults []ValidationResult,
) *ValidationReport

Generate aggregates validation results into a structured report.

type Sampler

type Sampler interface {
	// Sample selects a value from the distribution based on the sampling strategy.
	Sample(dist *models.ParameterModel) (interface{}, error)
}

Sampler is the interface for parameter value samplers.

type SchemaExtractor

type SchemaExtractor interface {
	// ExtractSchema connects to a database and extracts its schema.
	ExtractSchema(ctx context.Context) (*models.DatabaseSchema, error)
}

SchemaExtractor defines the interface for extracting a database schema.

type SchemaLoader

type SchemaLoader interface {
	// LoadSchema connects to a database and applies the given schema.
	LoadSchema(ctx context.Context, schema *models.DatabaseSchema) error
}

SchemaLoader defines the interface for loading a database schema.

type SchemaService

type SchemaService struct {
	// contains filtered or unexported fields
}

SchemaService is responsible for schema-related operations, such as conversion.

func NewSchemaService

func NewSchemaService() *SchemaService

NewSchemaService creates a new SchemaService.

func (*SchemaService) ConvertTo

func (s *SchemaService) ConvertTo(sourceSchema *models.DatabaseSchema, targetDialect string) (*models.DatabaseSchema, error)

ConvertTo converts a DatabaseSchema to a target dialect. It iterates through the tables and columns, applying the appropriate type mapping.

func (*SchemaService) RegisterTypeMapper

func (s *SchemaService) RegisterTypeMapper(dialect string, mapper TypeMapper)

RegisterTypeMapper registers a type mapper for a specific target dialect.

type StatisticalValidator

type StatisticalValidator struct {
	KSThreshold        float64
	ChiSquareThreshold float64
}

StatisticalValidator performs statistical tests on datasets.

func NewStatisticalValidator

func NewStatisticalValidator(ksThreshold, chiSquareThreshold float64) *StatisticalValidator

NewStatisticalValidator creates a new StatisticalValidator.

func (*StatisticalValidator) ChiSquareTest

func (v *StatisticalValidator) ChiSquareTest(observedFreq, expectedFreq []int) *ValidationResult

ChiSquareTest performs the Chi-Square Goodness of Fit test.

func (*StatisticalValidator) JensenShannonDivergence

func (v *StatisticalValidator) JensenShannonDivergence(p, q []float64) float64

JensenShannonDivergence calculates the Jensen-Shannon Divergence between two probability distributions.

func (*StatisticalValidator) KolmogorovSmirnovTest

func (v *StatisticalValidator) KolmogorovSmirnovTest(observed, expected []float64) *ValidationResult

KolmogorovSmirnovTest performs the Two-Sample Kolmogorov-Smirnov test.

type Synthesizer

type Synthesizer struct {
	// contains filtered or unexported fields
}

Synthesizer is responsible for filling SQL templates with synthetic data based on statistical parameter models.

func NewSynthesizer

func NewSynthesizer(workloadModel *models.WorkloadParameterModel) *Synthesizer

NewSynthesizer creates a new Synthesizer initialized with the provided workload parameter models.

func (*Synthesizer) FillParameters

func (s *Synthesizer) FillParameters(tmpl *models.SQLTemplate) ([]interface{}, error)

FillParameters generates values for the template's parameters and returns the list of arguments.

type TemplateService

type TemplateService struct{}

TemplateService is responsible for processing raw SQL traces and extracting SQL templates. It normalizes SQL queries to group them into templates and calculates the frequency of each template.

func NewTemplateService

func NewTemplateService() *TemplateService

NewTemplateService creates a new TemplateService.

func (*TemplateService) ExtractTemplates

func (s *TemplateService) ExtractTemplates(tc models.TraceCollection) []models.SQLTemplate

ExtractTemplates takes a collection of SQL traces and returns a slice of SQL templates. It groups traces by a normalized version of their SQL query, calculates the weight (frequency) of each template, and sorts the templates by weight in descending order.

type TemporalPattern

type TemporalPattern struct {
	Window    time.Duration
	BinCounts map[int]int // key=time bin index, value=query count
}

type TemporalPatternExtractor

type TemporalPatternExtractor struct {
	Window time.Duration
}

func (*TemporalPatternExtractor) Extract

type TokenBucketRateController

type TokenBucketRateController struct {
	// contains filtered or unexported fields
}

TokenBucketRateController implements a rate controller using the token bucket algorithm combined with time.Sleep to ensure smooth distribution.

func NewTokenBucketRateController

func NewTokenBucketRateController(targetQPS, maxConcurrency int) *TokenBucketRateController

NewTokenBucketRateController creates a new token bucket rate controller.

func (*TokenBucketRateController) Acquire

Acquire blocks until a request is allowed to proceed based on the configured QPS. It uses time.Sleep to smooth out the request rate.

func (*TokenBucketRateController) MaxConcurrency

func (c *TokenBucketRateController) MaxConcurrency() int

MaxConcurrency returns the maximum concurrency of the rate controller.

func (*TokenBucketRateController) Start

Start initializes the rate controller. For this implementation, no background goroutine is needed as we calculate sleeps on demand.

func (*TokenBucketRateController) Stop

func (c *TokenBucketRateController) Stop()

Stop cleans up resources.

type TraceGeneratorModel

type TraceGeneratorModel interface {
	Name() string
	// Generate generates a specific number of traces.
	Generate(ctx context.Context, count int) ([]models.SQLTrace, error)
}

TraceGeneratorModel defines the interface for workload generation models in benchmarking.

type TypeMapper

type TypeMapper func(sourceType string) (string, error)

TypeMapper defines the interface for mapping data types between different database dialects.

type ValidationReport

type ValidationReport struct {
	GeneratedAt       time.Time
	OverallScore      float64 // 0-100
	DistributionTests []ValidationResult
	TemporalTests     []ValidationResult
	QueryTypeTests    []ValidationResult
	Summary           string
}

ValidationReport contains the aggregated results of all validation tests.

type ValidationResult

type ValidationResult struct {
	TestName string
	PValue   float64
	Passed   bool
	Details  map[string]interface{}
}

ValidationResult represents the result of a statistical test.

type ValidationService

type ValidationService struct {
	// contains filtered or unexported fields
}

ValidationService is responsible for comparing benchmark runs and generating a structured report.

func NewValidationService

func NewValidationService() *ValidationService

NewValidationService creates a new ValidationService.

func (*ValidationService) ValidateAndReport

func (s *ValidationService) ValidateAndReport(
	baseMetrics, candMetrics *models.PerformanceMetrics,
	metadata *models.ReportMetadata,
	outputPath string,
) (*models.Report, error)

ValidateAndReport compares the performance of a base and candidate run, then generates and saves JSON and HTML reports.

type WeightedRandomSampler

type WeightedRandomSampler struct {
	// contains filtered or unexported fields
}

WeightedRandomSampler selects a value from a distribution based on its observed frequency.

func NewSeededWeightedRandomSampler

func NewSeededWeightedRandomSampler(seed int64) *WeightedRandomSampler

func NewWeightedRandomSampler

func NewWeightedRandomSampler() *WeightedRandomSampler

func (*WeightedRandomSampler) Sample

func (s *WeightedRandomSampler) Sample(dist *models.ParameterModel) (interface{}, error)

type WorkloadService

type WorkloadService struct {
	// contains filtered or unexported fields
}

WorkloadService is responsible for generating a benchmark workload.

func NewWorkloadService

func NewWorkloadService(sampler Sampler) *WorkloadService

NewWorkloadService creates a new WorkloadService.

func (*WorkloadService) GenerateWorkload

GenerateWorkload creates a BenchmarkWorkload from SQL templates and a parameter model. It uses the Synthesizer for "Smart Generation".

type ZipfSampler

type ZipfSampler struct {
	// contains filtered or unexported fields
}

ZipfSampler selects a value from a distribution based on a Zipfian distribution. It prioritizes the stored ZipfS parameter if available.

func NewSeededZipfSampler

func NewSeededZipfSampler(seed int64, s float64) *ZipfSampler

func NewZipfSampler

func NewZipfSampler(s float64) *ZipfSampler

func (*ZipfSampler) Sample

func (z *ZipfSampler) Sample(dist *models.ParameterModel) (interface{}, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL