abtestx

package module
v0.9.1 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2022 License: MIT Imports: 5 Imported by: 0

README

ABTestX

Go Reference Go Report Card Codacy Badge DeepSource license

A Simple A/B Testing library in Go

abtestx is a tool to help you run A/B tests for your golang applications with minimal effort and multiple strategies.

Requirements

  • go 1.18 or higher

Installation

go get github.com/Planxnx/abtestx

Strategies

  • Round Robin
  • Weighted Random
  • Random (soon)

Example

import (
	"fmt"
	"error"
	"github.com/Planxnx/abtestx"
)

func RoundRobin() {
	abtest := abtestx.NewRoundRobin([]abtestx.RoundRobinTest{
		{
			ID: "A",
			Callback: func() error {
				fmt.Println("execute A!")
				return nil
			},
		},
		{
			ID: "B",
			Callback: func() error {
				fmt.Println("execute B!")
				return nil
			},
		},
		{
			ID: "C",
			Callback: func() error {
				return errors.New("Error C")
			},
		},
	})

	err := abtest.Run() // execute A!
	err := abtest.Run() // execute B!
	err := abtest.Run() // err is not nil, got error "Error C"
	err := abtest.Run() // execute A!
	err := abtest.Run() // execute B!

}

func WeightedRandom() {
	abtest := abtestx.NewWeightedRandom([]abtestx.WeightedRandomTest{
		{
			ID:     "A",
			Weight: 0.8,
			Callback: func() error {
				fmt.Println("execute A!")
				return nil
			},
		},
		{
			ID:     "B",
			Weight: 0.2,
			Callback: func() error {
				fmt.Println("execute B!")
				return nil
			},
		},
	})

	// will execute A 80% of the time and B 20% of the time
	_ = abtest.Run() // execute A!
	_ = abtest.Run() // execute B!
	_ = abtest.Run() // execute A!
	_ = abtest.Run() // execute A!
	_ = abtest.Run() // execute A!
}

LICENSE

abtestx released under MIT license, refer LICENSE file.

Documentation

Index

Constants

View Source
const (
	// default total weight.
	DefaultTotalWeight float64 = 1.0
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client interface {
	Run() error
	Pick() (id string, callback func() error)
}

Client is a a/b testing instance interface.

func NewRoundRobin

func NewRoundRobin(tests []RoundRobinTest) Client

NewRoundRobin creates a new ab-test with round-robin strategy instance.

func NewWeightedRandom

func NewWeightedRandom(tests []WeightedRandomTest) Client

NewWeightedRandom creates a new ab-test with weighted random strategy instance.

type RoundRobin

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

RoundRobin is a ab-test with round-robin strategy.

func (*RoundRobin) Pick

func (c *RoundRobin) Pick() (id string, callback func() error)

Pick using round-robin to choose test returns a test.

func (*RoundRobin) Run

func (c *RoundRobin) Run() error

Run using round-robin to choose test and execute the callback of the test.

type RoundRobinTest

type RoundRobinTest struct {
	ID       string       // Required
	Callback func() error // Optional
}

RoundRobinTest is a test data for round-robin strategy.

type WeightedRandom

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

WeightedRandom is a ab-test with weighted random strategy.

func (*WeightedRandom) Pick

func (c *WeightedRandom) Pick() (id string, callback func() error)

Pick using weighted random to choose test returns a test.

func (*WeightedRandom) Run

func (c *WeightedRandom) Run() error

Run using weighted random to choose test and execute the callback of the test.

type WeightedRandomTest

type WeightedRandomTest struct {
	ID       string       // Required
	Weight   float64      // Optional
	Callback func() error // Optional
	// contains filtered or unexported fields
}

WeightedRandomTest is a test data for weighted random strategy.

Directories

Path Synopsis
pkg
rand
Package rand provides crypto random number functions.
Package rand provides crypto random number functions.

Jump to

Keyboard shortcuts

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