lwc / kumite
一个用于网络应用中多元测试的简单库
Requires
- php: >=5.3.2
Requires (Dev)
- mockery/mockery: >=0.7.2
- phpunit/phpunit: ~3.7
Suggests
- lwc/kumite-moa: Easily save kumite data in mongodb using moa
- lwc/kumite-pheasant: Easily save kumite data in mysql using pheasant
README
目录
什么是kumite?
Kumite是一个用于管理A/B测试的框架。
它的目的是管理和跟踪参与A/B测试的参与者,以及跟踪事件/结果。它不对参与者如何分配到变体做出任何假设。
测试可以定义多个变体和一个分配策略,该策略可以将一个变体分配给一个请求。可以通过可选的元数据对变体进行事件跟踪。它不对您的应用程序如何与cookies交互或您希望如何存储测试数据做出任何假设。这个名字来源于1988年让·克劳德·范·达梅电影《血战》中虚构的格斗锦标赛。
安装
最简单的安装方法是使用composer: composer require lwc/kumite
运行测试
开始测试
为了运行一个测试,您必须首先选择测试的起始点。通常这将在您的应用程序中的控制器操作中发生。在开始测试时,您必须提供已定义测试的测试密钥,以及可选的元数据数组,该数组将覆盖测试定义中声明的方法。分配器必须是实现 Allocator 的实例,在PHP术语中是可调用的,或者是一个表示变体的常量值。
使用常量值
<?php // some controller action function productPage() { // place users into variants based on a query string value. Useful for integrating with external tools. Kumite::start('pricing-test', null, $_GET['v']); }
使用分配器接口
<?php // defined elsewhere class MyRandomAllocator implements Kumite\Allocator { // selects a variant at random. Everyone gets tagged with a variant. public funciton allocate($variantKeys) { return array_rand($variantKeys); } } // some controller action function productPage() { Kumite::start('pricing-test', null, new MyRandomAllocator()); }
使用回调
<?php // some controller action function productPage() { $user = getActiveUser(); $metadata = array( 'userId' => isset($user) ? $user->id : null ); // place users into variants based on user id (why, who knows?!). Logged out users are disqualified from participating Kumite::start('pricing-test', $metadata, function($variantKeys) use($user) { if ($user) { return $variantKeys[$user->id() % count($variantKeys)]; } return null; // user is logged out }); }
更改内容
有两种主要方法可以更改测试中的参与者内容,通过变体键和通过变体属性
<?php if (Kumite::variant('pricing-test') == 'lowerprice'): ?> <div> <p>Look at our new low price of <?php echo Kumite::variant('pricing-test')->property('price') ?></p> </div> <?php endif; ?>
未参与测试的请求将分配给定义的默认变体以更改内容,但不会对它们进行事件跟踪。
跟踪事件
在kumite中跟踪事件是通过调用 Kumite::event($testKey, $eventKey, $metadata=array())
实现的
function invoicePage() { // ... $sale->save(); // user made a purchase, track the event in kumite Kumite::event('pricing-test', 'sale', array('amount' => $sale->amount())); }
定义测试
测试以下格式定义
<?php $config = array( 'pricing-test' => array( 'allocator' => new Allocators\UCB1Allocator('purchase') // there are several included allocators, or define your own 'active' => true, // controls allocation to tests, events will still be tracked for participants 'default' => 'control', // defines the default variant, served to request not participating in the test. Typically the control. 'variants' => array( 'control', // this variant defines no properties 'lowerprice' => array('price' => '$300') // this variant defines properties ), 'events' => array('purchase', 'refund') ) );
与应用程序集成
配置
在您的应用程序初始化过程中,您需要配置kumite。
<?php Kumite::setup(array( 'storageAdapter' => new MyStorageAdapter(), 'cookieAdapter' => new MyCookieAdapter(), // Optional, defaults to using PHP's $_COOKIE if not provided 'tests' => function() { require_once('path/to/configuration/file.php'); return $config; } ));
StorageAdapter
为了kumite能够保存参与者和事件数据,您需要定义一个 StorageAdapter。
幸运的是,这相当简单,例如
<?php class MyStorageAdapter implements Kumite\Adapters\StorageAdapter { /** * @return participantId */ public function createParticipant($testKey, $variantKey) { $participant = new KumiteParticipant(array( 'test' => $testKey, 'variant' => $variantKey )); $participant->save(); return $participant->id(); // Now we have a participant id, courtesy of the database } public function createEvent($testKey, $variantKey, $eventKey, $participantId, $metadata=null) { $event = new KumiteEvent(array( 'test' => $testKey, 'variant' => $variantKey, 'event' => $eventKey, 'participantId' => $participantId, 'metadata' => $metadata )); $event->save(); } // used for results and for intelligent allocators, such as UCB1 public function countParticipants($testKey, $variantKey) { return Participant::getTotalForVariant($testKey, $variantKey); } public function countEvents($testKey, $variantKey, $eventKey) { return Event::getTotalForEvent($testKey, $variantKey, $eventKey); } }
CookieAdapter
如果您的应用程序有特定的与cookies交互的机制,您将需要定义一个 CookieAdapter,这样kumite就可以用变体令牌标记请求。
例如,以下是包含的默认 PhpCookieAdapter
的源代码
<?php class PhpCookieAdapter implements Kumite\Adapters\CookieAdapter { public function getCookies() { return $_COOKIE; } public function getCookie($name) { return isset($_COOKIE[$name]) ? $_COOKIE[$name] : null; } public function setCookie($name, $data) { setcookie($name, $data); } }
贡献
如果您发现了一个错误或想做出贡献,请在此GitHub上创建一个问题,或者更好的是,fork项目并提交一个pull请求!