Skip to content

Commit f08f6c9

Browse files
authored
Merge pull request #12 from krowinski/mariadb-compatibility
Mariadb compatibility and little code cleanup
2 parents 403a04d + ed88378 commit f08f6c9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+2637
-2560
lines changed

.gitignore

+6-9
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
.idea
2-
out.log
3-
log
4-
/vendor
5-
composer.phar
6-
composer.lock
7-
.php_cs.cache
8-
.DS_Store
9-
Thumbs.db
1+
out.log
2+
log
3+
/vendor
4+
composer.phar
5+
composer.lock
6+
.php_cs.cache
107
/example/profiler.php

LICENSE

+21-21
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
The MIT License (MIT)
2-
3-
Copyright (c) 2016 krowinski
4-
5-
Permission is hereby granted, free of charge, to any person obtaining a copy
6-
of this software and associated documentation files (the "Software"), to deal
7-
in the Software without restriction, including without limitation the rights
8-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9-
copies of the Software, and to permit persons to whom the Software is
10-
furnished to do so, subject to the following conditions:
11-
12-
The above copyright notice and this permission notice shall be included in all
13-
copies or substantial portions of the Software.
14-
15-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21-
SOFTWARE.
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2016 krowinski
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

example/benchmark.php

+164-164
Original file line numberDiff line numberDiff line change
@@ -1,164 +1,164 @@
1-
<?php
2-
3-
namespace example;
4-
5-
error_reporting(E_ALL);
6-
ini_set('display_errors', 1);
7-
date_default_timezone_set('UTC');
8-
include __DIR__ . '/../vendor/autoload.php';
9-
10-
use Doctrine\DBAL\Connection;
11-
use Doctrine\DBAL\DBALException;
12-
use Doctrine\DBAL\DriverManager;
13-
use MySQLReplication\BinLog\Exception\BinLogException;
14-
use MySQLReplication\Config\Exception\ConfigException;
15-
use MySQLReplication\Event\EventSubscribers;
16-
use MySQLReplication\Exception\MySQLReplicationException;
17-
use MySQLReplication\MySQLReplicationFactory;
18-
use MySQLReplication\Config\ConfigService;
19-
use MySQLReplication\Definitions\ConstEventType;
20-
use MySQLReplication\Event\DTO\UpdateRowsDTO;
21-
22-
/**
23-
* Class BenchmarkEventSubscribers
24-
* @package example
25-
*/
26-
class BenchmarkEventSubscribers extends EventSubscribers
27-
{
28-
/**
29-
* @var int
30-
*/
31-
private $start = 0;
32-
/**
33-
* @var int
34-
*/
35-
private $counter = 0;
36-
37-
public function __construct()
38-
{
39-
$this->start = microtime(true);
40-
}
41-
42-
public function onUpdate(UpdateRowsDTO $event)
43-
{
44-
++$this->counter;
45-
if (0 === ($this->counter % 1000))
46-
{
47-
echo ((int)($this->counter / (microtime(true) - $this->start)) . ' event by seconds (' . $this->counter . ' total)') . PHP_EOL;
48-
}
49-
}
50-
}
51-
52-
/**
53-
* Class Benchmark
54-
* @package example
55-
*/
56-
class Benchmark
57-
{
58-
/**
59-
* @var string
60-
*/
61-
private $database = 'mysqlreplication_test';
62-
63-
/**
64-
* Benchmark constructor.
65-
* @throws DBALException
66-
* @throws ConfigException
67-
* @throws BinLogException
68-
* @throws MySQLReplicationException
69-
*/
70-
public function __construct()
71-
{
72-
$conn = $this->getConnection();
73-
$conn->exec('DROP DATABASE IF EXISTS ' . $this->database);
74-
$conn->exec('CREATE DATABASE ' . $this->database);
75-
$conn->exec('USE ' . $this->database);
76-
$conn->exec('CREATE TABLE test (i INT) ENGINE = MEMORY');
77-
$conn->exec('INSERT INTO test VALUES(1)');
78-
$conn->exec('CREATE TABLE test2 (i INT) ENGINE = MEMORY');
79-
$conn->exec('INSERT INTO test2 VALUES(1)');
80-
$conn->exec('RESET MASTER');
81-
82-
$this->binLogStream = new MySQLReplicationFactory(
83-
(new ConfigService())->makeConfigFromArray([
84-
'user' => 'root',
85-
'ip' => '127.0.0.1',
86-
'password' => 'root',
87-
// we only interest in update row event
88-
'eventsOnly' => [ConstEventType::UPDATE_ROWS_EVENT_V1, ConstEventType::UPDATE_ROWS_EVENT_V2],
89-
'slaveId' => 9999
90-
])
91-
);
92-
93-
// register benchmark subscriber handler
94-
$this->binLogStream->registerSubscriber(new BenchmarkEventSubscribers());
95-
}
96-
97-
/**
98-
* @return Connection
99-
* @throws DBALException
100-
*/
101-
private function getConnection()
102-
{
103-
return DriverManager::getConnection([
104-
'user' => 'root',
105-
'password' => 'root',
106-
'host' => '127.0.0.1',
107-
'port' => 3306,
108-
'driver' => 'pdo_mysql',
109-
'dbname' => $this->database
110-
]);
111-
}
112-
113-
/**
114-
* @throws MySQLReplicationException
115-
* @throws DBALException
116-
*/
117-
public function run()
118-
{
119-
$pid = pcntl_fork();
120-
if ($pid === -1)
121-
{
122-
die('could not fork');
123-
}
124-
else if ($pid)
125-
{
126-
$this->consume();
127-
pcntl_wait($status);
128-
}
129-
else
130-
{
131-
$this->produce();
132-
}
133-
}
134-
135-
/**
136-
* @throws MySQLReplicationException
137-
*/
138-
private function consume()
139-
{
140-
while(1) {
141-
$this->binLogStream->binLogEvent();
142-
}
143-
}
144-
145-
/**
146-
* @throws DBALException
147-
*/
148-
private function produce()
149-
{
150-
$conn = $this->getConnection();
151-
152-
echo 'Start insert data' . PHP_EOL;
153-
while (1)
154-
{
155-
$conn->exec('UPDATE test SET i = i + 1;');
156-
$conn->exec('UPDATE test2 SET i = i + 1;');
157-
}
158-
159-
$conn->close();
160-
die;
161-
}
162-
}
163-
164-
(new Benchmark())->run();
1+
<?php
2+
3+
namespace example;
4+
5+
error_reporting(E_ALL);
6+
date_default_timezone_set('UTC');
7+
include __DIR__ . '/../vendor/autoload.php';
8+
9+
use Doctrine\DBAL\Connection;
10+
use Doctrine\DBAL\DBALException;
11+
use Doctrine\DBAL\DriverManager;
12+
use MySQLReplication\BinLog\Exception\BinLogException;
13+
use MySQLReplication\Config\Exception\ConfigException;
14+
use MySQLReplication\Event\EventSubscribers;
15+
use MySQLReplication\Exception\MySQLReplicationException;
16+
use MySQLReplication\MySQLReplicationFactory;
17+
use MySQLReplication\Config\ConfigService;
18+
use MySQLReplication\Definitions\ConstEventType;
19+
use MySQLReplication\Event\DTO\UpdateRowsDTO;
20+
use SebastianBergmann\RecursionContext\InvalidArgumentException;
21+
22+
/**
23+
* Class BenchmarkEventSubscribers
24+
* @package example
25+
*/
26+
class BenchmarkEventSubscribers extends EventSubscribers
27+
{
28+
/**
29+
* @var int
30+
*/
31+
private $start = 0;
32+
/**
33+
* @var int
34+
*/
35+
private $counter = 0;
36+
37+
public function __construct()
38+
{
39+
$this->start = microtime(true);
40+
}
41+
42+
public function onUpdate(UpdateRowsDTO $event)
43+
{
44+
++$this->counter;
45+
if (0 === ($this->counter % 1000))
46+
{
47+
echo ((int)($this->counter / (microtime(true) - $this->start)) . ' event by seconds (' . $this->counter . ' total)') . PHP_EOL;
48+
}
49+
}
50+
}
51+
52+
/**
53+
* Class Benchmark
54+
* @package example
55+
*/
56+
class Benchmark
57+
{
58+
/**
59+
* @var string
60+
*/
61+
private $database = 'mysqlreplication_test';
62+
63+
/**
64+
* Benchmark constructor.
65+
* @throws DBALException
66+
* @throws ConfigException
67+
* @throws BinLogException
68+
* @throws MySQLReplicationException
69+
*/
70+
public function __construct()
71+
{
72+
$conn = $this->getConnection();
73+
$conn->exec('DROP DATABASE IF EXISTS ' . $this->database);
74+
$conn->exec('CREATE DATABASE ' . $this->database);
75+
$conn->exec('USE ' . $this->database);
76+
$conn->exec('CREATE TABLE test (i INT) ENGINE = MEMORY');
77+
$conn->exec('INSERT INTO test VALUES(1)');
78+
$conn->exec('CREATE TABLE test2 (i INT) ENGINE = MEMORY');
79+
$conn->exec('INSERT INTO test2 VALUES(1)');
80+
$conn->exec('RESET MASTER');
81+
82+
$this->binLogStream = new MySQLReplicationFactory(
83+
(new ConfigService())->makeConfigFromArray([
84+
'user' => 'root',
85+
'ip' => '127.0.0.1',
86+
'password' => 'root',
87+
// we only interest in update row event
88+
'eventsOnly' => [ConstEventType::UPDATE_ROWS_EVENT_V1, ConstEventType::UPDATE_ROWS_EVENT_V2],
89+
'slaveId' => 9999
90+
])
91+
);
92+
93+
// register benchmark subscriber handler
94+
$this->binLogStream->registerSubscriber(new BenchmarkEventSubscribers());
95+
}
96+
97+
/**
98+
* @return Connection
99+
* @throws DBALException
100+
*/
101+
private function getConnection()
102+
{
103+
return DriverManager::getConnection([
104+
'user' => 'root',
105+
'password' => 'root',
106+
'host' => '127.0.0.1',
107+
'port' => 3306,
108+
'driver' => 'pdo_mysql',
109+
'dbname' => $this->database
110+
]);
111+
}
112+
113+
/**
114+
* @throws MySQLReplicationException
115+
* @throws DBALException
116+
* @throws \InvalidArgumentException
117+
*/
118+
public function run()
119+
{
120+
$pid = pcntl_fork();
121+
if ($pid === -1)
122+
{
123+
throw new \InvalidArgumentException('Could not fork');
124+
}
125+
else if ($pid)
126+
{
127+
$this->consume();
128+
pcntl_wait($status);
129+
}
130+
else
131+
{
132+
$this->produce();
133+
}
134+
}
135+
136+
/**
137+
* @throws MySQLReplicationException
138+
*/
139+
private function consume()
140+
{
141+
while(1) {
142+
$this->binLogStream->binLogEvent();
143+
}
144+
}
145+
146+
/**
147+
* @throws DBALException
148+
*/
149+
private function produce()
150+
{
151+
$conn = $this->getConnection();
152+
153+
echo 'Start insert data' . PHP_EOL;
154+
while (1)
155+
{
156+
$conn->exec('UPDATE test SET i = i + 1;');
157+
$conn->exec('UPDATE test2 SET i = i + 1;');
158+
}
159+
160+
$conn->close();
161+
}
162+
}
163+
164+
(new Benchmark())->run();

0 commit comments

Comments
 (0)