Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/VKCOM/ktest

Test and benchmark KPHP code
https://github.com/VKCOM/ktest

benchmarking kphp php testing

Last synced: 3 months ago
JSON representation

Test and benchmark KPHP code

Awesome Lists containing this project

README

        

![](docs/readme_header.png)

## Overview

`ktest` is a tool that makes [kphp](https://github.com/VKCOM/kphp/) programs easier to test.

* `ktest phpunit` can run [PHPUnit](https://github.com/sebastianbergmann/phpunit) tests using KPHP
* `ktest compare` run given script with PHP and KPHP, check that output is identical
* `ktest bench` run benchmarks using KPHP
* `ktest bench-ab` run two selected benchmarks using KPHP and compare their results
* `ktest bench-php` run benchmarks using PHP
* `ktest bench-vs-php` run benchmarks using both KPHP and PHP, compare the results
* `ktest benchstat` compute and compare statistics about benchmark results (see [benchstat](https://godoc.org/golang.org/x/perf/cmd/benchstat))
* `ktest env` print ktest-related env variables

## Installation

```bash
$ composer require --dev vkcom/ktest-script
```

If composer is not an option or you want to install `ktest` binary globally, consider these options:

* Download the `ktest` binary from the [latest release](https://github.com/VKCOM/ktest/releases)
* or build `ktest` from sources

You may need to set `KPHP_ROOT` environment variable to point to your [KPHP repository](https://github.com/VKCOM/kphp/) folder:

```bash
$ git clone https://github.com/VKCOM/kphp.git
$ export KPHP_ROOT=$(pwd)/kphp
```

## Example - phpunit

Imagine that we have an ordinary `PHPUnit` test:

```php
assertSame(Strings::contains('foo', 'bar'), false);
$this->assertTrue(Strings::contains('foo', 'foo'));
}

public function testHasPrefix() {
$this->assertSame(Strings::hasPrefix('Hello World', 'Hello'), true);
$this->assertFalse(Strings::hasPrefix('Hello World', 'ello'));
}
}
```

It comes without a surprise that you can run it with `phpunit` tool:

```bash
$ ./vendor/bin/phpunit tests

...... 6 / 6 (100%)

Time: 70 ms, Memory: 4.00 MB

OK (6 tests, 14 assertions)
```

When you're using `phpunit`, tests are executed as PHP, not KPHP.

`ktest` makes it possible to run your phpunit-compatible tests with KPHP:

```bash
$ ./vendor/bin/ktest phpunit tests

.... 4 / 6 (66%) OK
.. 6 / 6 (100%) OK

Time: 10.74657386s

OK (6 tests, 14 assertions)
```

> Note that running KPHP tests is slower: a separate binary is compiled per every Test class.

All you need is `ktest` utility and installed [kphpunit](https://github.com/VKCOM/kphpunit) package:

```bash
$ composer require --dev vkcom/kphpunit
```

Now let's do something more exciting.

Take a look at this `Integers::getFirst` method:

```php
assertSame(Integers::getFirst([]), null);
$this->assertSame(Integers::getFirst([1]), 1);
}
}
```

All tests are passing:

```
. 1 / 1 (100%)

Time: 36 ms, Memory: 4.00 MB

OK (1 test, 2 assertions)
```

But if you try to run it with `ktest`, you'll see how that code would behave in KPHP:

```
F 1 / 1 (100%) FAIL

Time: 4.59874429s

There was 1 failure:

1) IntegersTest::testGetFirst
Failed asserting that null is identical to 0.

IntegersTest.php:8

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
```

Accessing unset array index can yield a "zero value" instead of null.

Running with `ktest` makes it easier to ensure that your code behaves identically in both PHP and KPHP.

## Example - bench

There are 2 main ways to do benchmarking with `bench` subcommand:

1. Run different benchmarks and see how they relate
2. Run benchmarks by the same name and compare samples with [benchstat](https://pkg.go.dev/golang.org/x/perf/cmd/benchstat?utm_source=godoc)

Let's assume that you have a function that concatenates 3 strings. You can write a benchmark for it:

```php