-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysqlreplay.php
executable file
·231 lines (181 loc) · 4.21 KB
/
mysqlreplay.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#!/usr/bin/php
<?
function main()
{
$opts = getopt('f::s::u::p::');
if ( ! $opts)
{
print('Usage: ./mysqlreplay.php -fmysql.log -s127.0.0.1 -uusername -ppass');
die();
}
$file = $opts['f'];
$server = $opts['s'];
$user = $opts['u'];
$pass = $opts['p'];
$adapter = new Mysqlpdo();
$replay = new Mysqlreplay($user, $pass, $server);
$replay->set_status_update(function($message) { echo $message; });
$replay->start($file, $adapter);
}
class Mysqlreplay {
private $file = null;
private $server = null;
private $user = null;
private $pass = null;
private $file_handle = null;
private $connections = array();
private $adapter = null;
private $status_update_func = null;
public function __construct($user, $pass, $server = '127.0.0.1')
{
$this->user = $user;
$this->pass = $pass;
$this->server = $server;
}
public function set_status_update($func)
{
$this->status_update_func = $func;
}
private function status_update($message)
{
if ( ! is_callable($this->status_update_func))
return;
$func = $this->status_update_func;
$func($message);
}
public function start($file, Mysqladapter $adapter)
{
set_time_limit(0);
gc_enable();
$this->adapter = $adapter;
$this->file_handle = fopen($file, 'r');
if ($this->file_handle === false)
throw new Exception('Unable to open file ' . $file);
// skip until we get to some data
while ( ! feof($this->file_handle))
{
$line = stream_get_line($this->file_handle, 4096, "\n");
if (substr($line, 0, 4) == 'Time')
{
break;
}
}
$this->loop();
fclose($this->file_handle);
}
private function loop()
{
while ( ! feof($this->file_handle))
{
$line = stream_get_line($this->file_handle, 4096, "\n");
$info = $this->parse_line($line);
if ( ! $info)
{
$this->status_update('E');
continue;
}
try
{
$this->execute($info);
}
catch (Exception $e)
{
//$this->status_update("\nError: " . $e->getMessage() . "\n");
}
}
}
private function parse_line($line)
{
$return = array();
$line = trim($line);
$query = explode("\t", $line, 3);
if (count($query) == 3)
{
//remove timestamp
array_shift($query);
}
$parts = explode(" ", $query[0], 2);
$info['command'] = array_pop($parts);
$info['connection'] = array_pop($parts);
if ( ! array_key_exists(1, $query))
return $info;
$info['query'] = $query[1];
return $info;
}
private function execute($info)
{
$conn = $this->get_connection($info['connection']);
switch ($info['command'])
{
case 'Quit':
$this->status_update('x');
$conn->quit();
$this->connections[$info['connection']] = null;
gc_collect_cycles();
break;
case 'Query':
$this->status_update('q');
$conn->query($info['query']);
break;
case 'Init DB':
$this->status_update('i');
$conn->init_db($info['query']);
break;
case 'Connect':
$this->status_update('c');
// nothing, get_connection() already connects
break;
}
}
private function get_connection($id)
{
// return existing connection
if (array_key_exists($id, $this->connections))
return $this->adapter->handle($this->connections[$id]);
// make new connection
$this->connections[$id] = $this->adapter->connect($this->user, $this->pass, $this->server);
return $this->adapter;
}
}
interface Mysqladapter {
public function handle($handle);
public function connect($user, $pass, $server);
public function init_db($db);
public function query($query);
public function quit();
}
class Mysqlpdo implements Mysqladapter {
private $handle = null;
public function handle($handle)
{
$this->handle = $handle;
return $this;
}
public function connect($user, $pass, $server)
{
$pdo = new PDO('mysql:host=' . $server, $user, $pass, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
$pdo->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);
$this->handle = $pdo;
return $pdo;
}
public function init_db($db)
{
$statement = $this->handle->prepare('use ' . $db);
$statement->execute();
}
public function query($query)
{
$statement = $this->handle->prepare($query);
$statement->execute();
$i = 0;
while ($statement->fetch())
{
// wait
}
}
public function quit()
{
$this->handle = null;
}
}
main();