定制 vtsmedia/paratest 二次开发

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

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

vtsmedia/paratest

Composer 安装命令:

composer require --dev vtsmedia/paratest

包简介

Parallel testing for PHP

README 文档

README

Build Status HHVM Status Scrutinizer Code Quality Packagist

The objective of ParaTest is to support parallel testing in PHPUnit. Provided you have well-written PHPUnit tests, you can drop paratest in your project and start using it with no additional bootstrap or configurations!

Benefits

Why use paratest over the alternative parallel test runners out there?

  • Code Coverage report combining. Run your tests in N parallel processes and all the code coverage output will be combined into one report.
  • Zero configuration. After composer install, run with vendor/bin/paratest -p4 path/to/tests. That's it!
  • Flexible. Isolate test files in separate processes or take advantage of WrapperRunner for even faster runs.

Installation

Composer

To install with composer add the following to your composer.json file:

"require": {
    "brianium/paratest": "dev-master"
}

Then run php composer.phar install

Usage

After installation, the binary can be found at vendor/bin/paratest. Usage is as follows:

Usage:
 paratest [-p|--processes="..."] [-f|--functional] [--no-test-tokens] [-h|--help] [--coverage-clover="..."] [--coverage-html="..."] [--coverage-php="..."] [-m|--max-batch-size="..."] [--filter="..."] [--phpunit="..."] [--runner="..."] [--bootstrap="..."] [-c|--configuration="..."] [-g|--group="..."] [--exclude-group="..."] [--stop-on-failure] [--log-junit="..."] [--colors] [--testsuite[="..."]] [--path="..."] [path]

Arguments:
 path                  The path to a directory or file containing tests. (default: current directory)

Options:
 --processes (-p)      The number of test processes to run. (default: 5)
 --functional (-f)     Run methods instead of suites in separate processes.
 --no-test-tokens      Disable TEST_TOKEN environment variables. (default: variable is set)
 --help (-h)           Display this help message.
 --coverage-clover     Generate code coverage report in Clover XML format.
 --coverage-html       Generate code coverage report in HTML format.
 --coverage-php        Serialize PHP_CodeCoverage object to file.
 --max-batch-size (-m) Max batch size (only for functional mode). (default: 0)
 --filter              Filter (only for functional mode).
 --phpunit             The PHPUnit binary to execute. (default: vendor/bin/phpunit)
 --runner              Runner or WrapperRunner. (default: Runner)
 --bootstrap           The bootstrap file to be used by PHPUnit.
 --configuration (-c)  The PHPUnit configuration file to use.
 --group (-g)          Only runs tests from the specified group(s).
 --exclude-group       Don't run tests from the specified group(s).
 --stop-on-failure     Don't start any more processes after a failure.
 --log-junit           Log test execution in JUnit XML format to file.
 --colors              Displays a colored bar as a test result.
 --testsuite           Filter which testsuite to run
 --path                An alias for the path argument.

Optimizing Speed

To get the most out of paratest, you have to adjust the parameters carefully.

  1. Adjust the number of processes with -p

    To allow full usage of your cpu cores, you should have at least one process per core. More processes allow better resource usage but keep in mind that each process has it's own costs for spawning.

  2. Choose between per-testcase- and per-testmethod-parallelization with -f

    Given you have few testcases (classes) with many long running methods, you should use the -f option to enable the functional mode and allow different methods of the same class to be executed in parallel. Keep in mind that the default is per-testcase-parallelization to address inter-testmethod dependencies. Note that in most projects, using -f is slower since each test method will need to be bootstrapped separately.

  3. Use the WrapperRunner if possible

    The default Runner for PHPUnit spawns a new process for each testcase (or method in functional mode). This provides the highest compatibility but comes with the cost of many spawned processes and a bootstrapping for each process. Especially when you have a slow bootstrapping in your tests (like a database setup) you should try the WrapperRunner with --runner WrapperRunner. It spawns one "worker"-process for each parallel process (-p), executes the bootstrapping once and reuses these processes for each test executed. That way the overhead of process spawning and bootstrapping is reduced to the minimum.

  4. Tune batch max size --max-batch-size

    Batch size will affect on max amount of atomic tests which will be used for single test method. One atomic test will be either one test method from test class if no data provider available for method or will be only one item from dataset for method. Increase this value to reduce per-process overhead and in most cases it will also reduce parallel efficiency. Decrease this value to increase per-process overhead and in most cases it will also increase parallel efficiency. If amount of all tests less then max batch size then everything will be processed in one process thread so paratest is completely useless in that case. The best way to find the most effective batch size is to test with different batch size values and select best. Max batch size = 0 means that grouping in batches will not be used and one batch will equal to all method tests (one or all from data provider). Max batch size = 1 means that each batch will contain only one test from data provider or one method if data provider is not used. Bigger max batch size can significantly increase phpunit command line length so process can failed. Decrease max batch size to reduce command line length. Windows has limit around 32k, Linux - 2048k, Mac OS X - 256k.

Examples

Examples assume your tests are located under ./test/unit.

# Run all unit tests in 8 parallel processes
vendor/bin/paratest -p8 test/unit
# Run all unit tests in 4 parallel processes with WrapperRunner and output html code coverage report to /tmp/coverage
# (Code coverage requires Xdebug to be installed)
vendor/bin/paratest -p8 --runner=WrapperRunner --coverage-html=/tmp/coverage test/unit

Windows

Windows users be sure to use the appropriate batch files.

An example being:

vendor\bin\paratest.bat --phpunit vendor\bin\phpunit.bat ...

ParaTest assumes PSR-0 for loading tests.

For convenience paratest windows version use 79 columns mode to prevent blank lines in standard 80x25 windows console.

PHPUnit Xml Config Support

When running PHPUnit tests, ParaTest will automatically pass the phpunit.xml or phpunit.xml.dist to the phpunit runner via the --configuration switch. ParaTest also allows the configuration path to be specified manually.

ParaTest will rely on the testsuites node of phpunit's xml configuration to handle loading of suites.

The following phpunit config file is used for ParaTest's test cases.

<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
         backupStaticAttributes="false"
         bootstrap="../bootstrap.php"
         colors="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         processIsolation="false"
         stopOnFailure="false"
         syntaxCheck="false"
        >
    <testsuites>
        <testsuite name="ParaTest Fixtures">
            <directory>./tests/</directory>
        </testsuite>
    </testsuites>
</phpunit>

Test token

The TEST_TOKEN environment variable is guaranteed to have a value that is different from every other currently running test. This is useful to e.g. use a different database for each test:

if (getenv('TEST_TOKEN') !== false) {  // Using partest
    $dbname = 'testdb_' . getenv('TEST_TOKEN');
} else {
    $dbname = 'testdb';
}

For Contributors: Testing paratest itself

ParaTest's test suite depends on PHPUnit being installed via composer. Make sure you run composer install after cloning.

Note that The display_errors php.ini directive must be set to stderr to run the test suite.

To run unit tests: vendor/bin/phpunit test/unit

To run functional tests: vendor/bin/phpunit test/functional

You can run all tests at once by running phpunit from the project directory. vendor/bin/phpunit

ParaTest can run its own test suite by running it from the bin directory. bin/paratest

For an example of ParaTest out in the wild check out the example.

vtsmedia/paratest 适用场景与选型建议

vtsmedia/paratest 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.85k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2017 年 05 月 18 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 2.85k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 14
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 3
  • Forks: 252
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-05-18