abtest

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package abtest provides A/B testing capabilities for TokMan

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CI

type CI struct {
	Lower float64
	Upper float64
}

CI represents a confidence interval

type Experiment

type Experiment struct {
	ID                string
	Name              string
	Description       string
	Hypothesis        string
	Status            Status
	Type              ExperimentType
	Variants          []Variant
	TrafficAllocation float64
	PrimaryMetric     string
	SecondaryMetrics  []string
	SampleSize        int
	Duration          time.Duration
	StartTime         time.Time
	EndTime           time.Time
	Segments          []Segment
	Randomization     RandomizationMethod
	Results           *Results
	// contains filtered or unexported fields
}

Experiment represents an A/B test experiment

type ExperimentConfig

type ExperimentConfig struct {
	Name              string
	Description       string
	Hypothesis        string
	Type              ExperimentType
	Variants          []VariantConfig
	TrafficAllocation float64
	PrimaryMetric     string
	SecondaryMetrics  []string
	SampleSize        int
	Duration          time.Duration
	Segments          []Segment
	Randomization     RandomizationMethod
}

ExperimentConfig holds experiment configuration

type ExperimentType

type ExperimentType string

ExperimentType defines the type of experiment

const (
	TypeAB           ExperimentType = "ab"
	TypeMultivariate ExperimentType = "multivariate"
	TypeBandit       ExperimentType = "bandit"
	TypeSwitchback   ExperimentType = "switchback"
)

type InMemoryStorage

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

InMemoryStorage provides in-memory storage for experiments

func NewInMemoryStorage

func NewInMemoryStorage() *InMemoryStorage

NewInMemoryStorage creates in-memory storage

func (*InMemoryStorage) List

func (s *InMemoryStorage) List() ([]*Experiment, error)

List lists all experiments

func (*InMemoryStorage) Load

func (s *InMemoryStorage) Load(id string) (*Experiment, error)

Load loads an experiment

func (*InMemoryStorage) Save

func (s *InMemoryStorage) Save(exp *Experiment) error

Save saves an experiment

type Manager

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

Manager manages A/B test experiments

func NewManager

func NewManager(storage Storage) *Manager

NewManager creates an A/B test manager

func (*Manager) Analyze

func (m *Manager) Analyze(ctx context.Context, id string) (*Results, error)

Analyze performs statistical analysis on experiment results

func (*Manager) AssignVariant

func (m *Manager) AssignVariant(ctx context.Context, experimentID, userID string) (*Variant, error)

AssignVariant assigns a user to a variant

func (*Manager) CreateExperiment

func (m *Manager) CreateExperiment(config ExperimentConfig) (*Experiment, error)

CreateExperiment creates a new experiment

func (*Manager) GetExperiment

func (m *Manager) GetExperiment(id string) (*Experiment, error)

GetExperiment returns experiment details

func (*Manager) ListExperiments

func (m *Manager) ListExperiments() []*Experiment

ListExperiments returns all experiments

func (*Manager) RecordEvent

func (m *Manager) RecordEvent(ctx context.Context, experimentID, userID, eventType string, value float64) error

RecordEvent records an event for a user in an experiment

func (*Manager) Start

func (m *Manager) Start(ctx context.Context, id string) error

Start begins an experiment

func (*Manager) Stop

func (m *Manager) Stop(ctx context.Context, id string) error

Stop ends an experiment

type RandomizationMethod

type RandomizationMethod string

RandomizationMethod defines how users are assigned to variants

const (
	RandomizationRandom   RandomizationMethod = "random"
	RandomizationUserID   RandomizationMethod = "user_id"
	RandomizationSession  RandomizationMethod = "session"
	RandomizationDevice   RandomizationMethod = "device"
	RandomizationWeighted RandomizationMethod = "weighted"
)

type Results

type Results struct {
	StartTime           time.Time
	EndTime             time.Time
	TotalParticipants   int64
	Winner              *Variant
	ConfidenceLevel     float64
	StatisticalPower    float64
	PValues             map[string]float64
	EffectSizes         map[string]float64
	ConfidenceIntervals map[string]CI
	Recommendation      string
}

Results holds experiment results

type Segment

type Segment struct {
	Name              string
	Condition         SegmentCondition
	TrafficAllocation float64
}

Segment defines user segments

type SegmentCondition

type SegmentCondition struct {
	Attribute string
	Operator  string
	Value     interface{}
}

SegmentCondition defines a segment condition

type Status

type Status string

Status represents experiment status

const (
	StatusDraft     Status = "draft"
	StatusScheduled Status = "scheduled"
	StatusRunning   Status = "running"
	StatusPaused    Status = "paused"
	StatusCompleted Status = "completed"
	StatusStopped   Status = "stopped"
)

type Storage

type Storage interface {
	Save(experiment *Experiment) error
	Load(id string) (*Experiment, error)
	List() ([]*Experiment, error)
}

Storage interface for persisting experiments

type Variant

type Variant struct {
	ID             string
	Name           string
	Description    string
	TrafficPercent float64
	Config         map[string]interface{}
	Metrics        VariantMetrics
	IsControl      bool
	IsWinner       bool
}

Variant represents an experiment variant

type VariantConfig

type VariantConfig struct {
	Name           string
	Description    string
	TrafficPercent float64
	Config         map[string]interface{}
	IsControl      bool
}

VariantConfig holds variant configuration

type VariantMetrics

type VariantMetrics struct {
	Impressions    int64
	Conversions    int64
	ConversionRate float64
	Revenue        float64
	Engagement     float64
	CustomMetrics  map[string]float64
}

VariantMetrics holds metrics for a variant

Jump to

Keyboard shortcuts

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