|
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