Download the PHP package deminy/counit without Composer

On this page you can find all versions of the php package deminy/counit. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package counit

counit: to run time/IO related unit tests faster using Swoole

Library Status Latest Stable Version Latest Unstable Version License

This package helps to run time/IO related unit tests (e.g., sleep function calls, database queries, API calls, etc) faster using Swoole.

Table of Contents

How Does It Work

Package counit allows running multiple time/IO related tests concurrently within a single PHP process using Swoole. Counit is compatible with PHPUnit, which means:

  1. Test cases can be written in the same way as those for PHPUnit.
  2. Test cases can run directly under PHPUnit.

A typical test case of counit looks like this:

Comparing to PHPUnit, counit could make your test cases faster. Here is a comparison when running the same test suite using PHPUnit and counit for a real project. In the test suite, many tests make calls to method \Deminy\Counit\Counit::sleep() to wait something to happen (e.g., wait data to expire).

  # of Tests # of Assertions Time to Finish
counit (without Swoole), or PHPUnit 44 1148 9 minutes and 18 seconds
counit (with Swoole enabled) 19 seconds

Installation

The package can be installed using Composer:

Or, in your composer.json file, make sure to have package deminy/counit included:

Use "counit" in Your Project

Examples

Folder ./tests/unit/global and ./tests/unit/case-by-case contain some sample tests, where we have following time-related tests included:

Setup Test Environment

To run the sample tests, please start the Docker containers and install Composer packages first:

There are five containers started: a PHP container, a Swoole container, a Redis container, a MySQL container, and a web server. The PHP container doesn't have the Swoole extension installed, while the Swoole container has it installed and enabled.

As said previously, test cases can be written in the same way as those for PHPUnit. However, to run time/IO related tests faster with counit, we need to make some adjustments when writing those test cases; these adjustments can be made in two different styles.

The "global" Style (recommended)

In this style, each test case runs in a separate coroutine automatically.

For test cases written in this style, the only change to make on your existing test cases is to use class Deminy\Counit\TestCase instead of PHPUnit\Framework\TestCase as the base class.

A typical test case of the global style looks like this:

When customized method setUpBeforeClass() and tearDownAfterClass() are defined in the test cases, please make sure to call their parent methods accordingly in these customized methods.

This style assumes there is no immediate assertions in test cases, nor assertions before a sleep() function call or a coroutine-friendly IO operation. Test cases like following still work, but they will trigger some warning messages when tested:

We can rewrite this test class using the "case by case" style (discussed in the next section) to eliminate the warning messages.

To find more tests written in this style, please check tests under folder ./tests/unit/global (test suite "global").

The "case by case" Style

In this style, you make changes directly on a test case to make it work asynchronously.

For test cases written in this style, we need to use class Deminy\Counit\Counit accordingly in the test cases where we need to wait for PHP execution or to perform IO operations. Typically, following method calls will be used:

A typical test case of the case-by-case style looks like this:

In case you need to suppress warning message "This test did not perform any assertions" or to make the number of assertions match, you can include a 2nd parameter when creating the new coroutine:

To find more tests written in this style, please check tests under folder ./tests/unit/case-by-case (test suite "case-by-case").

Comparisons

Here we will run the tests under different environments, with or without Swoole.

#1 Run the test suites using PHPUnit:

#2 Run the test suites using counit (without Swoole):

#3 Run the test suites using counit (with extension Swoole enabled):

The first two sets of commands take about same amount of time to finish. The last set of commands uses counit and runs in the Swoole container (where the Swoole extension is enabled); thus it's faster than the others:

  Style # of Tests # of Assertions Time to Finish
counit (without Swoole), or PHPUnit global 16 24 48 seconds
case by case 48 seconds
counit (with Swoole enabled) global 7 seconds
case by case 7 seconds

Additional Notes

Since this package allows running multiple tests simultaneously, we should not use same resources in different tests; otherwise, racing conditions could happen. For example, if multiple tests use the same Redis key, some of them could fail occasionally. In this case, we should use different Redis keys in different test cases. Method \Deminy\Counit\Helper::getNewKey() and \Deminy\Counit\Helper::getNewKeys() can be used to generate random and unique test keys.

The package works best for tests that have function call sleep() in use; It can also help to run some IO related tests faster, with limitations apply. Here is a list of limitations of this package:

Local Development

There are pre-built images deminy/counit for running the sample tests. Here are the commands to build the images:

Alternatives

This package allows to use Swoole to run multiple time/IO related tests without multiprocessing, which means all tests can run within a single PHP process. To understand how exactly it works, I'd recommend checking this free online talk: CSP Programming in PHP (and here are the slides).

In the PHP ecosystem, there are other options to run unit tests in parallel, most end up using multiprocessing:

TODOs

License

MIT license.


All versions of counit with dependencies

PHP Build Version
Package Version
Requires php Version >=7.2
phpunit/phpunit Version ~8.0 || ~9.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package deminy/counit contains the following files

Loading the files please wait ....