Skip to content

Commit

Permalink
Merge pull request #12 from krowinski/mariadb-compatibility
Browse files Browse the repository at this point in the history
Mariadb compatibility and little code cleanup
  • Loading branch information
krowinski authored Nov 5, 2016
2 parents 403a04d + ed88378 commit f08f6c9
Show file tree
Hide file tree
Showing 44 changed files with 2,637 additions and 2,560 deletions.
15 changes: 6 additions & 9 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
.idea
out.log
log
/vendor
composer.phar
composer.lock
.php_cs.cache
.DS_Store
Thumbs.db
out.log
log
/vendor
composer.phar
composer.lock
.php_cs.cache
/example/profiler.php
42 changes: 21 additions & 21 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
The MIT License (MIT)
Copyright (c) 2016 krowinski
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
The MIT License (MIT)

Copyright (c) 2016 krowinski

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
328 changes: 164 additions & 164 deletions example/benchmark.php
Original file line number Diff line number Diff line change
@@ -1,164 +1,164 @@
<?php

namespace example;

error_reporting(E_ALL);
ini_set('display_errors', 1);
date_default_timezone_set('UTC');
include __DIR__ . '/../vendor/autoload.php';

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\DriverManager;
use MySQLReplication\BinLog\Exception\BinLogException;
use MySQLReplication\Config\Exception\ConfigException;
use MySQLReplication\Event\EventSubscribers;
use MySQLReplication\Exception\MySQLReplicationException;
use MySQLReplication\MySQLReplicationFactory;
use MySQLReplication\Config\ConfigService;
use MySQLReplication\Definitions\ConstEventType;
use MySQLReplication\Event\DTO\UpdateRowsDTO;

/**
* Class BenchmarkEventSubscribers
* @package example
*/
class BenchmarkEventSubscribers extends EventSubscribers
{
/**
* @var int
*/
private $start = 0;
/**
* @var int
*/
private $counter = 0;

public function __construct()
{
$this->start = microtime(true);
}

public function onUpdate(UpdateRowsDTO $event)
{
++$this->counter;
if (0 === ($this->counter % 1000))
{
echo ((int)($this->counter / (microtime(true) - $this->start)) . ' event by seconds (' . $this->counter . ' total)') . PHP_EOL;
}
}
}

/**
* Class Benchmark
* @package example
*/
class Benchmark
{
/**
* @var string
*/
private $database = 'mysqlreplication_test';

/**
* Benchmark constructor.
* @throws DBALException
* @throws ConfigException
* @throws BinLogException
* @throws MySQLReplicationException
*/
public function __construct()
{
$conn = $this->getConnection();
$conn->exec('DROP DATABASE IF EXISTS ' . $this->database);
$conn->exec('CREATE DATABASE ' . $this->database);
$conn->exec('USE ' . $this->database);
$conn->exec('CREATE TABLE test (i INT) ENGINE = MEMORY');
$conn->exec('INSERT INTO test VALUES(1)');
$conn->exec('CREATE TABLE test2 (i INT) ENGINE = MEMORY');
$conn->exec('INSERT INTO test2 VALUES(1)');
$conn->exec('RESET MASTER');

$this->binLogStream = new MySQLReplicationFactory(
(new ConfigService())->makeConfigFromArray([
'user' => 'root',
'ip' => '127.0.0.1',
'password' => 'root',
// we only interest in update row event
'eventsOnly' => [ConstEventType::UPDATE_ROWS_EVENT_V1, ConstEventType::UPDATE_ROWS_EVENT_V2],
'slaveId' => 9999
])
);

// register benchmark subscriber handler
$this->binLogStream->registerSubscriber(new BenchmarkEventSubscribers());
}

/**
* @return Connection
* @throws DBALException
*/
private function getConnection()
{
return DriverManager::getConnection([
'user' => 'root',
'password' => 'root',
'host' => '127.0.0.1',
'port' => 3306,
'driver' => 'pdo_mysql',
'dbname' => $this->database
]);
}

/**
* @throws MySQLReplicationException
* @throws DBALException
*/
public function run()
{
$pid = pcntl_fork();
if ($pid === -1)
{
die('could not fork');
}
else if ($pid)
{
$this->consume();
pcntl_wait($status);
}
else
{
$this->produce();
}
}

/**
* @throws MySQLReplicationException
*/
private function consume()
{
while(1) {
$this->binLogStream->binLogEvent();
}
}

/**
* @throws DBALException
*/
private function produce()
{
$conn = $this->getConnection();

echo 'Start insert data' . PHP_EOL;
while (1)
{
$conn->exec('UPDATE test SET i = i + 1;');
$conn->exec('UPDATE test2 SET i = i + 1;');
}

$conn->close();
die;
}
}

(new Benchmark())->run();
<?php

namespace example;

error_reporting(E_ALL);
date_default_timezone_set('UTC');
include __DIR__ . '/../vendor/autoload.php';

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\DriverManager;
use MySQLReplication\BinLog\Exception\BinLogException;
use MySQLReplication\Config\Exception\ConfigException;
use MySQLReplication\Event\EventSubscribers;
use MySQLReplication\Exception\MySQLReplicationException;
use MySQLReplication\MySQLReplicationFactory;
use MySQLReplication\Config\ConfigService;
use MySQLReplication\Definitions\ConstEventType;
use MySQLReplication\Event\DTO\UpdateRowsDTO;
use SebastianBergmann\RecursionContext\InvalidArgumentException;

/**
* Class BenchmarkEventSubscribers
* @package example
*/
class BenchmarkEventSubscribers extends EventSubscribers
{
/**
* @var int
*/
private $start = 0;
/**
* @var int
*/
private $counter = 0;

public function __construct()
{
$this->start = microtime(true);
}

public function onUpdate(UpdateRowsDTO $event)
{
++$this->counter;
if (0 === ($this->counter % 1000))
{
echo ((int)($this->counter / (microtime(true) - $this->start)) . ' event by seconds (' . $this->counter . ' total)') . PHP_EOL;
}
}
}

/**
* Class Benchmark
* @package example
*/
class Benchmark
{
/**
* @var string
*/
private $database = 'mysqlreplication_test';

/**
* Benchmark constructor.
* @throws DBALException
* @throws ConfigException
* @throws BinLogException
* @throws MySQLReplicationException
*/
public function __construct()
{
$conn = $this->getConnection();
$conn->exec('DROP DATABASE IF EXISTS ' . $this->database);
$conn->exec('CREATE DATABASE ' . $this->database);
$conn->exec('USE ' . $this->database);
$conn->exec('CREATE TABLE test (i INT) ENGINE = MEMORY');
$conn->exec('INSERT INTO test VALUES(1)');
$conn->exec('CREATE TABLE test2 (i INT) ENGINE = MEMORY');
$conn->exec('INSERT INTO test2 VALUES(1)');
$conn->exec('RESET MASTER');

$this->binLogStream = new MySQLReplicationFactory(
(new ConfigService())->makeConfigFromArray([
'user' => 'root',
'ip' => '127.0.0.1',
'password' => 'root',
// we only interest in update row event
'eventsOnly' => [ConstEventType::UPDATE_ROWS_EVENT_V1, ConstEventType::UPDATE_ROWS_EVENT_V2],
'slaveId' => 9999
])
);

// register benchmark subscriber handler
$this->binLogStream->registerSubscriber(new BenchmarkEventSubscribers());
}

/**
* @return Connection
* @throws DBALException
*/
private function getConnection()
{
return DriverManager::getConnection([
'user' => 'root',
'password' => 'root',
'host' => '127.0.0.1',
'port' => 3306,
'driver' => 'pdo_mysql',
'dbname' => $this->database
]);
}

/**
* @throws MySQLReplicationException
* @throws DBALException
* @throws \InvalidArgumentException
*/
public function run()
{
$pid = pcntl_fork();
if ($pid === -1)
{
throw new \InvalidArgumentException('Could not fork');
}
else if ($pid)
{
$this->consume();
pcntl_wait($status);
}
else
{
$this->produce();
}
}

/**
* @throws MySQLReplicationException
*/
private function consume()
{
while(1) {
$this->binLogStream->binLogEvent();
}
}

/**
* @throws DBALException
*/
private function produce()
{
$conn = $this->getConnection();

echo 'Start insert data' . PHP_EOL;
while (1)
{
$conn->exec('UPDATE test SET i = i + 1;');
$conn->exec('UPDATE test2 SET i = i + 1;');
}

$conn->close();
}
}

(new Benchmark())->run();
Loading

0 comments on commit f08f6c9

Please sign in to comment.