Skip to content

Commit 1596bb4

Browse files
authored
Create benchmark.php
1 parent 89f01f8 commit 1596bb4

File tree

1 file changed

+126
-0
lines changed

1 file changed

+126
-0
lines changed

benchmark.php

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
<?php
2+
3+
Class DBPerformance
4+
{
5+
6+
private $performanceList = array();
7+
8+
private $startTime;
9+
10+
private $inStartTime;
11+
12+
private $counter;
13+
14+
private $db;
15+
16+
public function __construct($dbname, $host, $username, $password = '')
17+
{
18+
$this->db = new PDO('mysql:dbname=' . $dbname . ';host=' . $host, $username, $password);
19+
}
20+
21+
public function insertBenchmark()
22+
{
23+
$this->setStartTime(5000);
24+
$this->setCounter(1);
25+
26+
while ($this->startTime - (microtime(1) * 1000) > 0)
27+
{
28+
$this->setInStartTime();
29+
30+
$prepare = $this->db->prepare('INSERT INTO members (name, number, gb) VALUES(?, ?, ?)');
31+
$prepare->execute(array('name' . $this->getCounter(), $this->getCounter(), $this->getCounter() * 2));
32+
33+
$this->addPerformanceList('INSERT');
34+
}
35+
}
36+
37+
public function selectBenchmark()
38+
{
39+
$this->setStartTime(5000);
40+
$this->setCounter(1);
41+
42+
while ($this->startTime - (microtime(1) * 1000) > 0)
43+
{
44+
$this->setInStartTime();
45+
46+
$prepare = $this->db->prepare('SELECT * FROM members WHERE id = ?');
47+
$prepare->execute(array($this->getCounter()));
48+
$prepare->fetchAll();
49+
50+
$this->addPerformanceList('SELECT');
51+
}
52+
}
53+
54+
public function updateBenchmark()
55+
{
56+
$this->setStartTime(5000);
57+
$this->setCounter(1);
58+
59+
while ($this->startTime - (microtime(1) * 1000) > 0)
60+
{
61+
$this->setInStartTime();
62+
63+
$prepare = $this->db->prepare('UPDATE members SET name = ?, number = ? WHERE id = ?');
64+
$prepare->execute(array('name' . $this->getCounter(), $this->getCounter(), $this->getCounter()));
65+
66+
$this->addPerformanceList('UPDATE');
67+
}
68+
}
69+
70+
public function drawBenchmark()
71+
{
72+
foreach ($this->performanceList as $benchmarkName => $benchmarkData)
73+
{
74+
echo '<h4>' . $benchmarkName . '</h4>';
75+
foreach ($benchmarkData as $time => $times)
76+
{
77+
$totalTime = count($times);
78+
$sumTime = array_sum($times);
79+
echo date('d.m.Y H:i:s', $time) . ', &nbsp;&nbsp; Süre: ' . number_format($sumTime , 25 , "." , "") . ', &nbsp;&nbsp; R: ' . $this->getRTime($sumTime, $totalTime) . ', &nbsp;&nbsp; TPS: '. $totalTime;
80+
echo '<br>';
81+
}
82+
}
83+
}
84+
85+
private function setCounter($counter)
86+
{
87+
$this->counter = $counter;
88+
}
89+
90+
private function getCounter()
91+
{
92+
return $this->counter;
93+
}
94+
95+
private function addCounter($add)
96+
{
97+
$this->counter += $add;
98+
}
99+
100+
private function setStartTime($time)
101+
{
102+
$this->startTime = (microtime(1) * 1000) + $time;
103+
}
104+
105+
private function setInStartTime()
106+
{
107+
$this->inStartTime = microtime(1);
108+
}
109+
110+
private function addPerformanceList($key)
111+
{
112+
$this->performanceList[$key][time()][] = microtime(1) - $this->inStartTime;
113+
$this->addCounter(1);
114+
}
115+
116+
private function getRTime($sumTime, $totalTime)
117+
{
118+
$newTime = $sumTime / $totalTime;
119+
if ($newTime >= 0.001) {
120+
return round($newTime * 1000) . ' MiliSecond';
121+
}
122+
123+
return round($newTime * 1000 * 1000) . ' MicroSecond';
124+
}
125+
126+
}

0 commit comments

Comments
 (0)