Dzis przedstawiam na szybko napisane 3 metody do testowania wydajnosci CPU, operacji I/O oraz bazy danych.
Kazda metoda zwraca przyznane punkty w formie INTa. Oczywiscie im wiecej tym lepiej.
Uwagi odnosnie tej jakze nikomu niepotrzebnej klasie jak zwykle mile widziane
Klasa:
<?php final class Benchmark { /** * Benchmarks CPU and returns performance score * * @return integer CPU performance score */ $k = 0; $result = []; for($i = 0; $i < 25; $i++) { for($j = 0; $j < 1000000; $j++) { } for($j = 0; $j < 1000000; $j++) { $k++; $k--; $k++; $k--; } if ($loop2 > $loop1) $result[] = 1 / ($loop2 - $loop1); } } else { return 0; } } /** * Benchmarks database and returns performance score * * @param object database object * @return integer database operations score * @throws exception if database connection or SQL query fails */ if(!is_a($database, 'Platform\\Database') || !$database->isConnected()) { throw new \Exception('No connection to database'); } try { $database->setQuery('DELETE FROM #__performance'); $database->execute(); } catch(\PDOException $e) { throw new \Exception('Unable to truncate #__performance table'); } $result = []; for($i = 0; $i < 25; $i++) { for($j = 0; $j < 10; $j++) { } for($j = 0; $j < 10; $j++) { try { $database->setQuery('INSERT INTO #__performance(ID, REF_ID, NAME) VALUES (?, ?, ?)'); $database->setQuery('SELECT * FROM #__performance WHERE REF_ID = ?'); $database->execute([$j]); $database->fetchRow(); } catch(\PDOException $e) { throw new \Exception('Query execution failed during database benchmark'); } } if($loop2 > $loop1) { $result[] = 100 / ($loop2 - $loop1); } } } else { return 0; } } /** * Benchmarks I/O operations and returns performance score * * @param string path to directory where temporary files necessary for test will be created * @return integer I/O operations performance score */ $result = []; $testfile = $tempdir . '/io_benchmark_#N#.dat'; for($i = 0; $i < 25; $i++) { for($j = 0; $j < 1000; $j++) { } for($j = 0; $j < 1000; $j++) { file_put_contents($file, $content, LOCK_EX); require($file); } if($loop2 > $loop1) $result[] = 1000 / ($loop2 - $loop1); } } else { return 0; } } } /* class */
Przyklad uzycia:
<?php try { $database = \Platform\Database::getInstance(); $database->connect('sqlite:tests/data/database.qdb', NULL, NULL, 'test_'); $database->setQuery('CREATE TABLE IF NOT EXISTS #__performance(`ID` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, `REF_ID` INTEGER DEFAULT NULL, `NAME` varchar(200) DEFAULT NULL)'); $database->execute(); } catch(\PDOException $e) { }
Wyniki:
Kod
int(187)
int(153)
int(1224)
int(153)
int(1224)