Skip to content

Instantly share code, notes, and snippets.

@ghostwriter
Created May 19, 2023 11:36
Show Gist options
  • Select an option

  • Save ghostwriter/e994b6b440b8219f79f6ca2ef8994c9a to your computer and use it in GitHub Desktop.

Select an option

Save ghostwriter/e994b6b440b8219f79f6ca2ef8994c9a to your computer and use it in GitHub Desktop.
[wip]Trying to learn how to use blackfire for Performance testing by using already existing PHPUnit tests.
<?php
use PHPUnit\Framework\TestCase;
class MarkdownToHtmlTest extends TestCase
{
use WithBlackfire;
/**
* @dataProvider markdownProvider
* @before startInstrumentation
* @after stopInstrumentation
*/
public function testMarkdownToHtmlConversion($markdown)
{
// Markdown to HTML conversion logic here
$html = new MarkdownToHtml($markdown);
$this->assertNotEmpty($html);
}
public static function markdownProvider()
{
return [
['**bold text**'],
['# heading'],
['* list item'],
['[link](https://example.com)'],
];
}
}
<?php
use PHPUnit\Framework\TestCase;
use BlackfireProbe;
use Blackfire\Client;
use Blackfire\Profile\Configuration;
trait WithBlackfire
{
protected Configuration $configuration;
protected Client $blackfire;
protected BlackfireProbe $probe;
protected function setUp(): void
{
// $numberOfIterationsPerProfile = 10;
// todo: i need a way to generate the samples manually (running each test 10 times)
// $this->blackfire = new LoopClient(new Client(), $numberOfIterationsPerProfile);
$this->blackfire = new Client();
$config = new BlackfireConfiguration();
$config->setTitle('Markdown to HTML Performance Test');
$config->setMetadata('skip_timeline', 'true');
$this->configuration = $config;
$autoEnable = false;
$this->probe = $this->blackfire->createProbe($config, $autoEnable);
}
protected function tearDown(): void
{
// end the profiling session
$profile = $this->blackfire->endProbe($this->probe);
self::assertTrue(
$profile->isSuccessful(),
'Blackfire assertions failed. '. $profile->getUrl()
);
print $profile->getUrl().PHP_EOL;
// an error occurred when running the tests (different from assertion failures)
// an error can be a syntax error in an assertion for instance
// // get test results (returns an array of Blackfire\Profile\Test instances)
// $tests = $profile->getTests();
// // display all failures
// if (!$profile->isErrored()) {
// foreach ($tests as $test) {
// if ($test->isSuccessful()) {
// continue;
// }
// printf(" %s: %s\n", $test->getState(), $test->getName());
// foreach ($test->getFailures() as $assertion) {
// printf(" - %s\n", $assertion);
// }
// }
// }
}
public function startInstrumentation(): void
{
$this->probe->enable();
}
public function stopInstrumentation(): void
{
// Calling close() instead of disable() stops the profiling and forces the collected data to be sent to Blackfire:
//
// If we do not call $probe->close() the same number of times as the number of configured samples,
// we will get a 404 (not found) result when calling $blackfire->endProbe().
$this->probe->disable();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment