定制 lwc/kumite 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

lwc/kumite

Composer 安装命令:

composer require --dev lwc/kumite

包简介

A simple library for multivariate testing in web applications

README 文档

README

Table of contents

What is kumite?

Kumite is a framework for managing split testing.

It's purpose is to manage and track participants in split tests, as well as track events/outcomes. It makes no assumptions about how participants are allocated into variants.

Tests may be defined with a number of variants and an allocation strategy that can assign a variant to a request. Events may be tracked against variants, with optional metadata if desired. It makes no assumptions about how your application interacts with cookies or how you would like to store test data. It is named for the fictional fighting tournament, portrayed in the 1988 Jean-Claude Van Damme movie Bloodsport.

Installing

The easiest way to install is via composer: composer require lwc/kumite

Running tests

Starting tests

In order to run a test, you must first select a starting point for your test. Typically this will happen inside a controller action in your application. When starting a test, you must provide the test key of the defined test and optionally an array of metadata to attach to the test participant and allocation method that will override the method declared in the test definition. Allocators must be an instance that implements Allocator, something callable in php terms, or a constant value representing a variant.

Using a constant value:

<?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']);
}

Using the Allocator interface:

<?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());
}

Using a callback:

<?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
  });
}
Varying content

There are two main ways to vary content to participants in a test, via the variant key and via variant properties:

<?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; ?>

Requests that are not participating in the test will be allocated to the defined default variant for purposes of varying content, but no events will be tracked against them.

Tracking events

Tracking events in kumite is achieved by calling 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()));
}

Defining tests

Tests are defined in the following format:

<?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')
  )
);

Integrating with your application

Configuring

During the initialisation of your application, you need to configure 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

So that kumite can save participant and event data, you need to define a StorageAdapter.

Luckily, this is rather trivial, eg:

<?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

If your application has a specific mechanism for interacting with cookies, you will need to define a CookieAdapter so that kumite may tag requests with variant tokens.

For example, here is the source of the included default 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);
	}
}

Contributing

If you've found a bug or would like to contribute, please create an issue here on GitHub, or better yet fork the project and submit a pull request!

lwc/kumite 适用场景与选型建议

lwc/kumite 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 37.93k 次下载、GitHub Stars 达 22, 最近一次更新时间为 2013 年 01 月 14 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「testing」 「split」 「multivariate」 「a-b」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 lwc/kumite 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 lwc/kumite 我们能提供哪些服务?
定制开发 / 二次开发

基于 lwc/kumite 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 37.93k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 22
  • 点击次数: 16
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

  • Stars: 22
  • Watchers: 1
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2013-01-14