Hello,
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
*/
public static function getCPUScore
() { $k = 0;
$result = [];
for($i = 0; $i < 25; $i++) {
$start = System::getMiroTime(); for($j = 0; $j < 1000000; $j++) {
}
$loop1 = System::getMiroTime() - $start; $start = System::getMiroTime(); for($j = 0; $j < 1000000; $j++) {
$k++;
$k--;
$k++;
$k--;
}
$loop2 = System::getMiroTime() - $start; 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
*/
public static function getDatabaseScore
($database) { 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++) {
$start = System::getMiroTime(); for($j = 0; $j < 10; $j++) {
}
$loop1 = System::getMiroTime() - $start; $start = System::getMiroTime(); for($j = 0; $j < 10; $j++) {
try {
$database->setQuery('INSERT INTO #__performance(ID, REF_ID, NAME) VALUES (?, ?, ?)');
$database->execute([NULL, $j, str_repeat('xyz', 66
)]); $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');
}
}
$loop2 = System::getMiroTime() - $start; 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
*/
public static function getIOScore
($tempdir = '.') { $result = [];
$testfile = $tempdir . '/io_benchmark_#N#.dat';
$content = '<?php /*' . str_repeat('I/O Benchmark. ', 1024) . '*/ ?>'; for($i = 0; $i < 25; $i++) {
$start = System::getMiroTime(); for($j = 0; $j < 1000; $j++) {
}
$loop1 = System::getMiroTime() - $start; $start = System::getMiroTime(); for($j = 0; $j < 1000; $j++) {
file_put_contents($file, $content, LOCK_EX);
require($file);
}
$loop2 = System::getMiroTime() - $start; 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) {
die('Something went wrong!'); }
var_dump(\Platform\Benchmark
::getCPUScore()); var_dump(\Platform\Benchmark
::getDatabaseScore($database)); var_dump(\Platform\Benchmark
::getIOScore('.'));
Wyniki:
Kod
int(187)
int(153)
int(1224)
Ten post edytował q.michal 16.06.2017, 15:04:39