Giter Club home page Giter Club logo

tachycardia's Introduction

Nexus Tachycardia

PHP version build Coverage Status PHPStan Latest Stable Version License Total Downloads

Tachycardia is a PHPUnit extension that detects and reports slow running tests and prints them right in your console. It can also optionally inline annotate the specific tests in the files during pull requests.

NOTE: Tachycardia will only detect the slow tests in your test suites but will offer no explanation as to why these identified are slow. You should use a dedicated profiler for these instead.

$ vendor/bin/phpunit
PHPUnit 10.5.5 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.3.1 with Xdebug 3.3.1
Configuration: /home/runner/work/tachycardia/tachycardia/phpunit.xml.dist
Random Seed:   1698146158

................................................................. 65 / 96 ( 67%)
...............................                                   96 / 96 (100%)

Nexus\PHPUnit\Tachycardia\TachycardiaExtension identified this sole slow test:
⚠  Took 1.3374s from 1.0000s limit to run Nexus\\PHPUnit\\Tachycardia\\Tests\\Renderer\\GithubRendererTest::testRendererWorksProperly

Slow tests: Time: 00:00:01.710 (2.54%)

Time: 00:58.737, Memory: 16.00 MB

OK (96 tests, 265 assertions)

Generating code coverage report in Clover XML format ... done [00:00.391]

Generating code coverage report in HTML format ... done [00:01.930]

Installation

Tachycardia should only be installed as a development-time dependency to aid in running your project's test suite. You can install using Composer:

composer require --dev nexusphp/tachycardia

Configuration

Tachycardia supports these parameters:

  • time-limit - Time limit in seconds to be enforced for all tests. All tests exceeding this amount will be considered as slow. Default: 1.00
  • report-count - Number of slow tests to be displayed in the console report. This is ignored on Github Actions report. Default: 10
  • precision - Degree of precision of the decimals of the test's consumed time and allotted time limit. Default: 4
  • format - The format of the renderer for the console.
  • ci-format - The format of the renderer for the CI.

Renderer formats for both the console and CI could be any of:

Format For Console? For CI? Remarks
list ✅︎ Default for console
table ✅︎
github ✅︎ ✅︎ Default for CI
gitlab ✅︎ ✅︎
teamcity ✅︎ ✅︎

To use the extension with its default configuration options, you can simply add the following into your phpunit.xml.dist or phpunit.xml file.

<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
         bootstrap="vendor/autoload.php"
         cacheResultFile="build/.phpunit.cache/test-results"
         colors="true"
         beStrictAboutOutputDuringTests="true"
         beStrictAboutTodoAnnotatedTests="true"
         failOnRisky="true"
         failOnWarning="true">

    <!-- Your other phpunit configurations here -->

    <extensions>
        <bootstrap class="Nexus\PHPUnit\Tachycardia\TachycardiaExtension" />
    </extensions>
</phpunit>

Now, run vendor/bin/phpunit. If there are test cases where the time consumed exceeds the configured time limits, these will be displayed in the console after all tests have completed.

If you wish to customize one or more of the available options, you can just change the entry in your phpunit.xml.dist or phpunit.xml file.

<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
         bootstrap="vendor/autoload.php"
         cacheResultFile="build/.phpunit.cache/test-results"
         colors="true"
         beStrictAboutOutputDuringTests="true"
         beStrictAboutTodoAnnotatedTests="true"
         failOnRisky="true"
         failOnWarning="true">

    <!-- Your other phpunit configurations here -->

    <extensions>
        <bootstrap class="Nexus\PHPUnit\Tachycardia\TachycardiaExtension">
            <parameter name="time-limit" value="2.00" />
            <parameter name="report-count" value="30" />
            <parameter name="precision" value="2" />
            <parameter name="format" value="table" />
            <parameter name="ci-format" value="github" />
        </bootstrap>
    </extensions>
</phpunit>

Documentation

Upgrading

Upgrading from v1.x to v2.x? See the UPGRADING Guide.

Contributing

Contributions are very much welcome. If you see an improvement or bug fix, open a PR now!

Read more on the Contributing to Nexus Tachycardia.

Inspiration

Tachycardia was inspired from johnkary/phpunit-speedtrap, but injected with anabolic steroids.

Tachycardia is actually a medical term referring to a heart rate that exceeds the normal resting rate in general of over 100 beats per minute.

License

This library is licensed under the MIT License.

tachycardia's People

Contributors

dependabot[bot] avatar paulbalandan avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

mgatner

tachycardia's Issues

setUp included in time limit

Environment:

  • PHP version: 7.4
  • Tachycardia version: 1.1.1

Description:
I'm not entirely sure if this is a bug but I did not notice this issue with SpeedTrap. It appears that the time to run setUp() is included in the test time limit - this is particularly confusing for once-run database configurations with data providers where a single data set will show as a lot slower than the others. I have tested without data providers as well to confirm it is not a fringe scenario.

Steps to Reproduce:
See e.g. https://github.com/duke-bluesmith/bluesmith/blob/develop/tests/controllers/JobsControllerTest.php, and the various test results at #7

Expected Output:
I think the ideal scenario would be to split out the "staging" from the actual tests, and be able to report on slow staging (e.g. setUp and tearDown) with their own annotated @timeLimit options. Barring the difficulty of that implementation, I would like to see time limits calculated solely on my actual test methods.

[Feature] Update to PHPUnit 10's Event System

Is your feature request related to a problem? Please describe.

Update extension to PHPUnit 10.

Describe the solution you'd like

Update internals to use PHPUnit 10's new Event System.

[BUG] Annotation with data providers

Environment:

  • PHP version: 7.4
  • Tachycardia version: v1.1.1

Description:
When using the @timeLimit annotation on a test with a data provider the new limit is not respected.

Steps to Reproduce:
Here's my actual test case (using ControllerTester):

	/**
	 * @dataProvider indexDataProvider
	 * @timeLimit 0.75
	 */
	public function testIndexHasCorrectJobs(string $method, array $expected)
	{
		$result = $this->execute($method);
		$result->assertOK();
		$result->assertStatus(200);

		// Check that each expected Job is present and others are not
		foreach (['staff', 'client', 'completed', 'deleted'] as $type)
		{
			if (in_array($type, $expected))
			{
				// Check for the Job name linked by its ID
				$result->see(anchor('jobs/show/' . $this->$type->id, $this->$type->name));
			}
			else
			{
				$result->dontSee($this->$type->name);
			}
		}
	}

	public function indexDataProvider(): array
	{
		return [
			['staff', ['staff']],
			['active', ['staff', 'client']],
			['archive', ['completed']],
			['all', ['staff', 'client', 'completed']],
			['trash', ['deleted']],
		];
	}

No matter what I set for @timeLimit I always get a warning:

Nexus\PHPUnit\Extension\Tachycardia identified this sole slow test:
+--------------------------------------------------------------+---------------+-------------+
| Test Case                                                    | Time Consumed | Time Limit  |
+--------------------------------------------------------------+---------------+-------------+
| JobsControllerTest::testIndexHasCorrectJobs with data set #2 | 00:00:00.68   | 00:00:00.50 |
+--------------------------------------------------------------+---------------+-------------+

Notice that "Time Limit" is 0.5.

Expected Output:
Each iteration of a data-provided test should use the limit override.

[BUG] Line numbers not correctly displayed in Github Actions

Environment:

  • PHP version: 7.4
  • Tachycardia version: v1.0.0

Description:
When enabled, Tachycardia doesn't render the correct line numbers of the slow tests when annotated in Github Actions.

Expected Output:
Correct line numbers will be displayed.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.