|
1
|
|
|
<?php |
|
2
|
|
|
|
|
|
|
|
|
|
3
|
|
|
namespace Db3v4l\Core\SqlExecutor\Forked; |
|
4
|
|
|
|
|
5
|
|
|
use Db3v4l\API\Interfaces\SqlExecutor\Forked\CommandExecutor; |
|
6
|
|
|
use Db3v4l\API\Interfaces\SqlExecutor\Forked\FileExecutor; |
|
7
|
|
|
use Db3v4l\API\Interfaces\TimedExecutor as TimedExecutorInterface; |
|
8
|
|
|
|
|
9
|
|
|
class TimedExecutor implements CommandExecutor, FileExecutor, TimedExecutorInterface |
|
|
|
|
|
|
10
|
|
|
{ |
|
11
|
|
|
/** @var CommandExecutor|FileExecutor */ |
|
|
|
|
|
|
12
|
|
|
protected $wrappedExecutor; |
|
13
|
|
|
protected $timingFile; |
|
14
|
|
|
|
|
15
|
|
|
protected $timeCmd = '/usr/bin/time'; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
|
|
|
|
|
18
|
|
|
* @param CommandExecutor|FileExecutor $wrappedExecutor |
|
|
|
|
|
|
19
|
|
|
*/ |
|
20
|
|
|
public function __construct($wrappedExecutor) |
|
21
|
|
|
{ |
|
22
|
|
|
$this->wrappedExecutor = $wrappedExecutor; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function getExecuteStatementProcess($sql) |
|
|
|
|
|
|
26
|
|
|
{ |
|
27
|
|
|
$process = $this->wrappedExecutor->getExecuteStatementProcess($sql); |
|
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
// wrap in a `time` call |
|
30
|
|
|
$this->timingFile = tempnam(sys_get_temp_dir(), 'db3v4l_'); |
|
31
|
|
|
$process->setCommandLine( |
|
32
|
|
|
$this->timeCmd . ' ' . escapeshellarg('--output=' . $this->timingFile) . ' ' . escapeshellarg('--format=%M %e') . ' ' |
|
33
|
|
|
. $process->getCommandLine()); |
|
|
|
|
|
|
34
|
|
|
return $process; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function getExecuteFileProcess($sql) |
|
|
|
|
|
|
38
|
|
|
{ |
|
39
|
|
|
$process = $this->wrappedExecutor->getExecuteFileProcess($sql); |
|
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
// wrap in a `time` call |
|
42
|
|
|
$this->timingFile = tempnam(sys_get_temp_dir(), 'db3v4l_'); |
|
43
|
|
|
$process->setCommandLine( |
|
44
|
|
|
$this->timeCmd . ' ' . escapeshellarg('--output=' . $this->timingFile) . ' ' . escapeshellarg('--format=%M %e') . ' ' |
|
45
|
|
|
. $process->getCommandLine()); |
|
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
return $process; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function resultSetToArray($data) |
|
|
|
|
|
|
51
|
|
|
{ |
|
52
|
|
|
return $this->wrappedExecutor->resultSetToArray($data); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function getTimingData($onceIsEnough = true) |
|
|
|
|
|
|
56
|
|
|
{ |
|
57
|
|
|
if (!is_file($this->timingFile)) { |
|
58
|
|
|
throw new \Exception("File with timing data gone missing: '{$this->timingFile}'"); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$timingData = file_get_contents($this->timingFile); |
|
62
|
|
|
if ($timingData != '') { |
|
63
|
|
|
$timingData = preg_replace('/Command exited with non-zero status [0-9]+/', '', $timingData); |
|
64
|
|
|
$timingData = explode(' ', trim($timingData), 2); |
|
65
|
|
|
$results['time'] = $timingData[1]; |
|
|
|
|
|
|
66
|
|
|
$results['memory'] = $timingData[0]; |
|
67
|
|
|
} else { |
|
68
|
|
|
// happens eg. if `time` command is not available |
|
69
|
|
|
throw new \Exception("File with timing data empty: '{$this->timingFile}'"); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
if ($onceIsEnough) { |
|
73
|
|
|
unlink ($this->timingFile); |
|
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return $results; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|