Skip to content

Commit db4ed6d

Browse files
authored
Merge pull request #8 from krowinski/event-dispacher
Event dispacher
2 parents 643a43c + aaf7525 commit db4ed6d

Some content is hidden

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

66 files changed

+546
-296
lines changed

.gitignore

100755100644
File mode changed.

.travis.yml

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
language: php
22

33
php:
4-
- 5.4
54
- 5.5
65
- 5.6
76
- 7.0

LICENSE

100755100644
File mode changed.

README.md

100755100644
File mode changed.

composer.json

100755100644
+4-2
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99
],
1010
"type": "library",
1111
"require": {
12-
"php": ">=5.4",
12+
"php": ">=5.5.9",
1313
"doctrine/dbal": "^2.5",
1414
"doctrine/collections": "^1.3",
15-
"ext-sockets": "*"
15+
"ext-sockets": "*",
16+
"symfony/event-dispatcher": "^3.1",
17+
"symfony/dependency-injection": "^3.1"
1618
},
1719
"require-dev": {
1820
"phpunit/phpunit": "*"

example/benchmark.php

100755100644
+67-30
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,57 @@
11
<?php
2+
3+
namespace example;
4+
25
error_reporting(E_ALL);
36
ini_set('display_errors', 1);
47
date_default_timezone_set('UTC');
58
include __DIR__ . '/../vendor/autoload.php';
69

10+
use Doctrine\DBAL\Connection;
11+
use Doctrine\DBAL\DBALException;
712
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;
817
use MySQLReplication\MySQLReplicationFactory;
918
use MySQLReplication\Config\ConfigService;
1019
use MySQLReplication\Definitions\ConstEventType;
1120
use MySQLReplication\Event\DTO\UpdateRowsDTO;
1221

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+
1352
/**
1453
* Class Benchmark
54+
* @package example
1555
*/
1656
class Benchmark
1757
{
@@ -22,33 +62,41 @@ class Benchmark
2262

2363
/**
2464
* Benchmark constructor.
65+
* @throws DBALException
66+
* @throws ConfigException
67+
* @throws BinLogException
68+
* @throws MySQLReplicationException
2569
*/
2670
public function __construct()
2771
{
2872
$conn = $this->getConnection();
29-
$conn->exec("DROP DATABASE IF EXISTS " . $this->database);
30-
$conn->exec("CREATE DATABASE " . $this->database);
31-
$conn->exec("USE " . $this->database);
32-
$conn->exec("CREATE TABLE test (i INT) ENGINE = MEMORY");
33-
$conn->exec("INSERT INTO test VALUES(1)");
34-
$conn->exec("CREATE TABLE test2 (i INT) ENGINE = MEMORY");
35-
$conn->exec("INSERT INTO test2 VALUES(1)");
36-
$conn->exec("RESET MASTER");
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');
3781

3882
$this->binLogStream = new MySQLReplicationFactory(
3983
(new ConfigService())->makeConfigFromArray([
4084
'user' => 'root',
4185
'ip' => '127.0.0.1',
4286
'password' => 'root',
87+
// we only interest in update row event
4388
'eventsOnly' => [ConstEventType::UPDATE_ROWS_EVENT_V1, ConstEventType::UPDATE_ROWS_EVENT_V2],
4489
'slaveId' => 9999
4590
])
4691
);
92+
93+
// register benchmark subscriber handler
94+
$this->binLogStream->registerSubscriber(new BenchmarkEventSubscribers());
4795
}
4896

4997
/**
50-
* @return \Doctrine\DBAL\Connection
51-
* @throws \Doctrine\DBAL\DBALException
98+
* @return Connection
99+
* @throws DBALException
52100
*/
53101
private function getConnection()
54102
{
@@ -63,12 +111,13 @@ private function getConnection()
63111
}
64112

65113
/**
66-
*
114+
* @throws MySQLReplicationException
115+
* @throws DBALException
67116
*/
68117
public function run()
69118
{
70119
$pid = pcntl_fork();
71-
if ($pid == -1)
120+
if ($pid === -1)
72121
{
73122
die('could not fork');
74123
}
@@ -84,29 +133,17 @@ public function run()
84133
}
85134

86135
/**
87-
*
136+
* @throws MySQLReplicationException
88137
*/
89138
private function consume()
90139
{
91-
$start = microtime(true);
92-
$i = 0;
93-
94-
while (1)
95-
{
96-
$result = $this->binLogStream->getBinLogEvent();
97-
if ($result instanceof UpdateRowsDTO)
98-
{
99-
++$i;
100-
if (0 === ($i % 1000))
101-
{
102-
echo ((int)($i / (microtime(true) - $start)) . ' event by seconds (' . $i . ' total)') . PHP_EOL;
103-
}
104-
}
140+
while(1) {
141+
$this->binLogStream->binLogEvent();
105142
}
106143
}
107144

108145
/**
109-
* @throws \Doctrine\DBAL\DBALException
146+
* @throws DBALException
110147
*/
111148
private function produce()
112149
{
@@ -115,8 +152,8 @@ private function produce()
115152
echo 'Start insert data' . PHP_EOL;
116153
while (1)
117154
{
118-
$conn->exec("UPDATE test SET i = i + 1;");
119-
$conn->exec("UPDATE test2 SET i = i + 1;");
155+
$conn->exec('UPDATE test SET i = i + 1;');
156+
$conn->exec('UPDATE test2 SET i = i + 1;');
120157
}
121158

122159
$conn->close();

example/dump_events.php

100755100644
+51-31
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,52 @@
1-
<?php
2-
error_reporting(E_ALL);
3-
ini_set('display_errors', 1);
4-
date_default_timezone_set('UTC');
5-
include __DIR__ . '/../vendor/autoload.php';
6-
7-
use MySQLReplication\MySQLReplicationFactory;
8-
use MySQLReplication\Config\ConfigService;
9-
10-
$binLogStream = new MySQLReplicationFactory(
11-
(new ConfigService())->makeConfigFromArray([
12-
'user' => 'root',
13-
'ip' => '127.0.0.1',
14-
'password' => 'root',
15-
//'mariaDbGtid' => '1-1-3,0-1-88',
16-
//'gtid' => '9b1c8d18-2a76-11e5-a26b-000c2976f3f3:1-177592',
17-
])
18-
);
19-
while (1)
20-
{
21-
$result = $binLogStream->getBinLogEvent();
22-
if (!is_null($result))
23-
{
24-
// all events got __toString() implementation
25-
echo $result;
26-
27-
// all events got JsonSerializable implementation
28-
//echo json_encode($result, JSON_PRETTY_PRINT);
29-
30-
echo 'Memory usage ' . round(memory_get_usage() / 1048576, 2) . ' MB' . PHP_EOL;
31-
}
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 MySQLReplication\Config\ConfigService;
11+
use MySQLReplication\Event\DTO\EventDTO;
12+
use MySQLReplication\Event\EventSubscribers;
13+
use MySQLReplication\MySQLReplicationFactory;
14+
15+
$binLogStream = new MySQLReplicationFactory(
16+
(new ConfigService())->makeConfigFromArray([
17+
'user' => 'root',
18+
'ip' => '127.0.0.1',
19+
'password' => 'root',
20+
//'mariaDbGtid' => '1-1-3,0-1-88',
21+
//'gtid' => '9b1c8d18-2a76-11e5-a26b-000c2976f3f3:1-177592',
22+
])
23+
);
24+
25+
/**
26+
* Class BenchmarkEventSubscribers
27+
* @package example
28+
*/
29+
class MyEventSubscribers extends EventSubscribers
30+
{
31+
/**
32+
* @param EventDTO $event (your own handler more in EventSubscribers class )
33+
*/
34+
public function allEvents(EventDTO $event)
35+
{
36+
// all events got __toString() implementation
37+
echo $event;
38+
39+
// all events got JsonSerializable implementation
40+
//echo json_encode($result, JSON_PRETTY_PRINT);
41+
42+
echo 'Memory usage ' . round(memory_get_usage() / 1048576, 2) . ' MB' . PHP_EOL;
43+
}
44+
}
45+
46+
// register your events handler here
47+
$binLogStream->registerSubscriber(new MyEventSubscribers());
48+
49+
// start consuming events
50+
while (1) {
51+
$binLogStream->binLogEvent();
3252
}

example/dump_events_callback.php

-31
This file was deleted.

phpunit.xml

100755100644
File mode changed.

src/MySQLReplication/BinLog/BinLogAuth.php

100755100644
File mode changed.

0 commit comments

Comments
 (0)