ABTestX

Go Reference Go Report Card Codacy Badge Code Analysis & Tests 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.

GitHub

View Github