From 580ac7cad2cfb599c63ea9ee06dd06e512f025e3 Mon Sep 17 00:00:00 2001 From: David Llop Date: Sat, 4 Jan 2020 21:54:10 +0100 Subject: [PATCH] Reorganize code, database ones perform inserts at the end --- src/Runners/Console.php | 47 ------------ src/Runners/SQLite.php | 95 ----------------------- src/Runners/SlowestTests/Channel.php | 52 +++++++++++++ src/Runners/SlowestTests/Console.php | 17 ++++ src/Runners/{ => SlowestTests}/Csv.php | 7 +- src/Runners/{ => SlowestTests}/MySQL.php | 98 ++++++++++-------------- src/Runners/SlowestTests/SQLite.php | 52 +++++++++++++ src/Time.php | 18 ----- 8 files changed, 163 insertions(+), 223 deletions(-) delete mode 100644 src/Runners/Console.php delete mode 100644 src/Runners/SQLite.php create mode 100644 src/Runners/SlowestTests/Channel.php create mode 100644 src/Runners/SlowestTests/Console.php rename src/Runners/{ => SlowestTests}/Csv.php (84%) rename src/Runners/{ => SlowestTests}/MySQL.php (58%) create mode 100644 src/Runners/SlowestTests/SQLite.php delete mode 100644 src/Time.php diff --git a/src/Runners/Console.php b/src/Runners/Console.php deleted file mode 100644 index 2facac3..0000000 --- a/src/Runners/Console.php +++ /dev/null @@ -1,47 +0,0 @@ -rows = $rows; - } - - public function executeAfterTest(string $test, float $time): void - { - $this->tests[$test] = new Time($time); - } - - public function executeAfterLastTest(): void - { - $this->sortTestsBySpeed(); - - $this->printResults(); - } - - protected function sortTestsBySpeed(): void - { - arsort($this->tests); - } - - protected function printResults(): void - { - echo PHP_EOL . "Showing the top {$this->rows} slowest tests:" . PHP_EOL; - - foreach (array_slice($this->tests, 0, $this->rows, true) as $test => $time) { - echo str_pad($time, 5, ' ', STR_PAD_LEFT) . " ms: {$test}" . PHP_EOL; - } - - echo PHP_EOL; - } -} \ No newline at end of file diff --git a/src/Runners/SQLite.php b/src/Runners/SQLite.php deleted file mode 100644 index fd59a4f..0000000 --- a/src/Runners/SQLite.php +++ /dev/null @@ -1,95 +0,0 @@ - 'phpunit_results.db', - 'table' => 'default', - ]; - - public function __construct(array $credentials = []) - { - try { - $this->credentials = array_merge($this->credentials, $credentials); - - $this->connect(); - - if (! $this->tableExists()) { - $this->createTable(); - } - } catch (Exception $e) { - echo "SQLite Runner skipped: {$e->getMessage()}" . PHP_EOL; - } - } - - public function executeAfterTest(string $test, float $time): void - { - if ($this->connection === null) { - return; - } - - try { - $this->insert($test, $time); - } catch (Exception $e) { - echo "SQLite Runner skipped: {$e->getMessage()}" . PHP_EOL; - - $this->connection = null; - } - } - - protected function insert(string $test, float $time): void - { - [$class, $method] = explode('::', $test); - - $this->connection - ->prepare( - "INSERT INTO `{$this->credentials['table']}` (time, method, class, name) - VALUES(:time, :method, :class, :name) - ON CONFLICT(name) DO UPDATE SET time = :time;" - ) - ->execute([ - 'time' => new Time($time), - 'method' => $method, - 'class' => $class, - 'name' => $test, - ]); - } - - protected function connect(): void - { - $this->connection = new PDO("sqlite:{$this->credentials['database']}"); - - $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); - } - - protected function tableExists(): bool - { - $query = $this->connection->prepare("SELECT name FROM sqlite_master WHERE type='table' AND name=:table;"); - - $query->execute(['table' => $this->credentials['table']]); - - return $query->fetch() !== false; - } - - protected function createTable(): void - { - $this->connection->prepare( - "CREATE TABLE {$this->credentials['table']} ( - `time` float DEFAULT NULL, - `name` varchar(255) NOT NULL, - `method` varchar(255) DEFAULT NULL, - `class` varchar(255) DEFAULT NULL, - PRIMARY KEY (`name`) - );" - )->execute(); - } -} diff --git a/src/Runners/SlowestTests/Channel.php b/src/Runners/SlowestTests/Channel.php new file mode 100644 index 0000000..6304c62 --- /dev/null +++ b/src/Runners/SlowestTests/Channel.php @@ -0,0 +1,52 @@ +rows = $rows; + } + + public function executeAfterTest(string $test, float $time): void + { + $this->tests[$test] = $this->timeToMiliseconds($time); + } + + protected function timeToMiliseconds(float $time) + { + return (int)($time * 1000); + } + + public function executeAfterLastTest(): void + { + $this->sortTestsBySpeed(); + + $this->printResults(); + } + + protected function sortTestsBySpeed(): void + { + arsort($this->tests); + } + + abstract protected function printResults(): void; + + protected function testsToPrint(): array + { + return array_slice($this->tests, 0, $this->rows, true); + } + + protected function getClassName(): string + { + return get_class($this); + } +} \ No newline at end of file diff --git a/src/Runners/SlowestTests/Console.php b/src/Runners/SlowestTests/Console.php new file mode 100644 index 0000000..abe4688 --- /dev/null +++ b/src/Runners/SlowestTests/Console.php @@ -0,0 +1,17 @@ +rows} slowest tests:" . PHP_EOL; + + foreach ($this->testsToPrint() as $test => $time) { + echo str_pad($time, 5, ' ', STR_PAD_LEFT) . " ms: {$test}" . PHP_EOL; + } + + echo PHP_EOL; + } +} \ No newline at end of file diff --git a/src/Runners/Csv.php b/src/Runners/SlowestTests/Csv.php similarity index 84% rename from src/Runners/Csv.php rename to src/Runners/SlowestTests/Csv.php index ad9db50..e4e8808 100644 --- a/src/Runners/Csv.php +++ b/src/Runners/SlowestTests/Csv.php @@ -1,12 +1,9 @@ '127.0.0.1' ]; - public function __construct(array $credentials = []) + public function __construct(int $rows = 5, array $credentials = []) { + parent::__construct($rows); + try { $this->credentials = array_merge($this->credentials, $credentials); $this->connect(); - if (! $this->tableExists()) { - $this->createTable(); - } + $this->createTableIfNotExists(); } catch (Exception $e) { - echo "MySQL Runner skipped: {$e->getMessage()}" . PHP_EOL; + echo "{$this->getClassName()} failed: {$e->getMessage()}" . PHP_EOL; } } - public function executeAfterTest(string $test, float $time): void + protected function connect(): void + { + $this->connection = new PDO( + "mysql:dbname={$this->credentials['database']};host={$this->credentials['host']}", + $this->credentials['username'], + $this->credentials['password'], + [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION] + ); + } + + protected function createTableIfNotExists(): void + { + $this->connection->prepare( + "CREATE TABLE IF NOT EXISTS {$this->credentials['table']} ( + `time` float DEFAULT NULL, + `name` varchar(255) COLLATE utf8_unicode_ci NOT NULL, + `method` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, + `class` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, + PRIMARY KEY (`name`) + ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;" + )->execute(); + } + + protected function printResults(): void { if ($this->connection === null) { return; } - try { - $this->insert($test, $time); - } catch (Exception $e) { - echo "MySQL Runner skipped: {$e->getMessage()}" . PHP_EOL; + foreach ($this->testsToPrint() as $test => $time) { + try { + $this->insert($test, $time); + } catch (Exception $e) { + echo "{$this->getClassName()} failed: {$e->getMessage()}" . PHP_EOL; - $this->connection = null; + break; + } } } - protected function insert(string $test, float $time): void + protected function insert(string $test, string $time): void { [$class, $method] = explode('::', $test); @@ -60,51 +83,10 @@ protected function insert(string $test, float $time): void ON DUPLICATE KEY UPDATE time = :time;" ) ->execute([ - 'time' => new Time($time), + 'time' => $time, 'method' => $method, 'class' => $class, 'name' => $test, ]); } - - protected function connect(): void - { - $this->connection = new PDO( - "mysql:dbname={$this->credentials['database']};host={$this->credentials['host']}", - $this->credentials['username'], - $this->credentials['password'], - [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION] - ); - } - - protected function tableExists(): bool - { - $query = $this->connection->prepare( - "SELECT TABLE_NAME - FROM information_schema.tables - WHERE table_schema = :database - AND table_name = :table - LIMIT 1;" - ); - - $query->execute([ - 'database' => $this->credentials['database'], - 'table' => $this->credentials['table'] - ]); - - return $query->fetch() !== false; - } - - protected function createTable(): void - { - $this->connection->prepare( - "CREATE TABLE {$this->credentials['table']} ( - `time` float DEFAULT NULL, - `name` varchar(255) COLLATE utf8_unicode_ci NOT NULL, - `method` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, - `class` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, - PRIMARY KEY (`name`) - ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;" - )->execute(); - } } diff --git a/src/Runners/SlowestTests/SQLite.php b/src/Runners/SlowestTests/SQLite.php new file mode 100644 index 0000000..e49835d --- /dev/null +++ b/src/Runners/SlowestTests/SQLite.php @@ -0,0 +1,52 @@ + 'phpunit_results.db', + 'table' => 'default', + ]; + + protected function connect(): void + { + $this->connection = new PDO("sqlite:{$this->credentials['database']}"); + + $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + } + + protected function createTableIfNotExists(): void + { + $this->connection->prepare( + "CREATE TABLE IF NOT EXISTS `{$this->credentials['table']}` ( + `time` float DEFAULT NULL, + `name` varchar(255) NOT NULL, + `method` varchar(255) DEFAULT NULL, + `class` varchar(255) DEFAULT NULL, + PRIMARY KEY (`name`) + );" + )->execute(); + } + + protected function insert(string $test, string $time): void + { + [$class, $method] = explode('::', $test); + + $this->connection + ->prepare( + "INSERT INTO `{$this->credentials['table']}` (time, method, class, name) + VALUES(:time, :method, :class, :name) + ON CONFLICT(name) DO UPDATE SET time = :time;" + ) + ->execute([ + 'time' => $time, + 'method' => $method, + 'class' => $class, + 'name' => $test, + ]); + } +} diff --git a/src/Time.php b/src/Time.php deleted file mode 100644 index b6ddc2e..0000000 --- a/src/Time.php +++ /dev/null @@ -1,18 +0,0 @@ -time = $time; - } - - public function __toString(): string - { - return (int)($this->time * 1000); - } -} \ No newline at end of file