/** * 本测试展示了如何用LtCache给LtDb提高性能 */ public function testPerformance() { /** * 配置数据库连接信息 */ $dcb = new LtDbConfigBuilder(); $dcb->addSingleHost(array("adapter" => "mysql", "username" => "test", "password" => "", "dbname" => "test")); /** * 实例化组件入口类 */ $db = new LtDb(); $db->configHandle->addConfig(array("db.servers" => $dcb->getServers())); $db->init(); /** * 用法 1: 直接操作数据库 * 优点:学习成本低,快速入门 * 适用场景: 1. 临时写个脚本操作数据库,不想花时间学习LtDb的查询引擎 2. 只写少量脚本,不是一个完整持续的项目,不需要SqlMap来管理SQL语句 */ $dbh = $db->getDbHandle(); $dbh->query("DROP TABLE IF EXISTS test_user"); $dbh->query("\n\t\t\tCREATE TABLE test_user (\n\t\t\tid INT NOT NULL AUTO_INCREMENT,\n\t\t\tname VARCHAR( 20 ) NOT NULL ,\n\t\t\tage INT NOT NULL ,\n\t\t\tPRIMARY KEY ( id ) \n\t\t)"); /** * 用法 2: 使用Table Gateway查询引擎 * * 优点:自动生成SQL语句 * * 适用场景: 1. 对数据表进行增简单的删查改操作,尤其是单条数据的操作 * 2. 简单的SELECT,动态合成WHERE子句 */ $tg = $db->getTDG("test_user"); /** * 运行100次,要求在1秒内运行完 */ $base_memory_usage = memory_get_usage(); $times = 100; $startTime = microtime(true); for ($i = 0; $i < $times; $i++) { $tg->insert(array("id" => $i, "name" => "lotusphp", "age" => 1)); } $dbh->query("DROP TABLE IF EXISTS test_user"); $endTime = microtime(true); $totalTime = round($endTime - $startTime, 6); $averageTime = round($totalTime / $times, 6); $memory_usage = memory_get_usage() - $base_memory_usage; $averageMemory = formatSize($memory_usage / $times); $memory_usage = formatSize($memory_usage); if (LOTUS_UNITTEST_DEBUG) { echo "\n----------------db getTDG insert----------------\n"; echo "times \t{$times}\n"; echo "totalTime \t{$totalTime}s\taverageTime \t{$averageTime}s\n"; echo "memoryUsage \t{$memory_usage}\taverageMemory \t{$averageMemory}"; echo "\n---------------------------------------------------------\n"; } $this->assertTrue(1 > $totalTime); }
/** * 测试分布式数据库的配置方法 */ public function testConfigBuilderDistDb() { $dcb = new LtDbConfigBuilder(); /** * 配置系统数据组 * 一个节点, 一主两从,分布在三台不同的机器上 */ $dcb->addHost("sys_group", "sys_node_1", "master", array("host" => "10.0.0.1", "password" => "123456", "dbname" => "sys_data", "schema" => "public", "adapter" => "pgsql")); $dcb->addHost("sys_group", "sys_node_1", "slave", array("host" => "10.0.0.2", "adapter" => "pdo_pgsql")); $dcb->addHost("sys_group", "sys_node_1", "slave", array("host" => "10.0.0.3")); /** * 配置用户数据组 * 两个节点 * 每个节点一主一从 * 都在同一台机器上,不同节点数据库名不同,主从服务器的端口不同 */ $dcb->addHost("user_group", "user_node_1", "master", array("host" => "10.0.1.1", "password" => "123456", "adapter" => "mysqli", "dbname" => "member_1")); $dcb->addHost("user_group", "user_node_1", "slave", array("port" => 3307)); $dcb->addHost("user_group", "user_node_2", "master", array("dbname" => "member_2")); $dcb->addHost("user_group", "user_node_2", "slave", array("port" => 3307)); /** * 配置交易数据组 * 三个节点 * 每个节点两台机器互为主从 */ $dcb->addHost("trade_group", "trade_node_1", "master", array("host" => "10.0.2.1", "password" => "123456", "adapter" => "oci", "dbname" => "finance", "schema" => "trade")); $dcb->addHost("trade_group", "trade_node_1", "master", array("host" => "10.0.2.2")); $dcb->addHost("trade_group", "trade_node_2", "master", array("host" => "10.0.2.3")); $dcb->addHost("trade_group", "trade_node_2", "master", array("host" => "10.0.2.4")); $dcb->addHost("trade_group", "trade_node_3", "master", array("host" => "10.0.2.5")); $dcb->addHost("trade_group", "trade_node_3", "master", array("host" => "10.0.2.6")); $this->assertEquals(array("sys_group" => array("sys_node_1" => array("master" => array(array("host" => "10.0.0.1", "port" => 5432, "username" => "root", "password" => "123456", "adapter" => "pgsql", "charset" => "UTF-8", "pconnect" => true, "connection_ttl" => 3600, "dbname" => "sys_data", "schema" => "public", "connection_adapter" => "pgsql", "sql_adapter" => "pgsql")), "slave" => array(array("host" => "10.0.0.2", "port" => 5432, "username" => "root", "password" => "123456", "adapter" => "pdo_pgsql", "charset" => "UTF-8", "pconnect" => true, "connection_ttl" => 3600, "dbname" => "sys_data", "schema" => "public", "connection_adapter" => "pdo", "sql_adapter" => "pgsql"), array("host" => "10.0.0.3", "port" => 5432, "username" => "root", "password" => "123456", "adapter" => "pdo_pgsql", "charset" => "UTF-8", "pconnect" => true, "connection_ttl" => 3600, "dbname" => "sys_data", "schema" => "public", "connection_adapter" => "pdo", "sql_adapter" => "pgsql")))), "user_group" => array("user_node_1" => array("master" => array(array("host" => "10.0.1.1", "port" => 3306, "username" => "root", "password" => "123456", "adapter" => "mysqli", "charset" => "UTF-8", "pconnect" => false, "connection_ttl" => 30, "dbname" => null, "schema" => "member_1", "connection_adapter" => "mysqli", "sql_adapter" => "mysql")), "slave" => array(array("host" => "10.0.1.1", "port" => 3307, "username" => "root", "password" => "123456", "adapter" => "mysqli", "charset" => "UTF-8", "pconnect" => false, "connection_ttl" => 30, "dbname" => null, "schema" => "member_1", "connection_adapter" => "mysqli", "sql_adapter" => "mysql"))), "user_node_2" => array("master" => array(array("host" => "10.0.1.1", "port" => 3306, "username" => "root", "password" => "123456", "adapter" => "mysqli", "charset" => "UTF-8", "pconnect" => false, "connection_ttl" => 30, "dbname" => null, "schema" => "member_2", "connection_adapter" => "mysqli", "sql_adapter" => "mysql")), "slave" => array(array("host" => "10.0.1.1", "port" => 3307, "username" => "root", "password" => "123456", "adapter" => "mysqli", "charset" => "UTF-8", "pconnect" => false, "connection_ttl" => 30, "dbname" => null, "schema" => "member_2", "connection_adapter" => "mysqli", "sql_adapter" => "mysql")))), "trade_group" => array("trade_node_1" => array("master" => array(array("host" => "10.0.2.1", "port" => 1521, "username" => "root", "password" => "123456", "adapter" => "oci", "charset" => "UTF-8", "pconnect" => true, "connection_ttl" => 3600, "dbname" => "finance", "schema" => "trade", "connection_adapter" => "oci", "sql_adapter" => "oracle"), array("host" => "10.0.2.2", "port" => 1521, "username" => "root", "password" => "123456", "adapter" => "oci", "charset" => "UTF-8", "pconnect" => true, "connection_ttl" => 3600, "dbname" => "finance", "schema" => "trade", "connection_adapter" => "oci", "sql_adapter" => "oracle"))), "trade_node_2" => array("master" => array(array("host" => "10.0.2.3", "port" => 1521, "username" => "root", "password" => "123456", "adapter" => "oci", "charset" => "UTF-8", "pconnect" => true, "connection_ttl" => 3600, "dbname" => "finance", "schema" => "trade", "connection_adapter" => "oci", "sql_adapter" => "oracle"), array("host" => "10.0.2.4", "port" => 1521, "username" => "root", "password" => "123456", "adapter" => "oci", "charset" => "UTF-8", "pconnect" => true, "connection_ttl" => 3600, "dbname" => "finance", "schema" => "trade", "connection_adapter" => "oci", "sql_adapter" => "oracle"))), "trade_node_3" => array("master" => array(array("host" => "10.0.2.5", "port" => 1521, "username" => "root", "password" => "123456", "adapter" => "oci", "charset" => "UTF-8", "pconnect" => true, "connection_ttl" => 3600, "dbname" => "finance", "schema" => "trade", "connection_adapter" => "oci", "sql_adapter" => "oracle"), array("host" => "10.0.2.6", "port" => 1521, "username" => "root", "password" => "123456", "adapter" => "oci", "charset" => "UTF-8", "pconnect" => true, "connection_ttl" => 3600, "dbname" => "finance", "schema" => "trade", "connection_adapter" => "oci", "sql_adapter" => "oracle"))))), $dcb->getServers()); //end $this->assertEquals }
<?php $dcb = new LtDbConfigBuilder(); $dcb->addHost("user_group", "user_node_1", "master", array("host" => "10.0.1.1", "password" => "123456", "adapter" => "mysqli", "dbname" => "member_1")); $dcb->addHost("group_1", "node_0", "master", array("adapter" => "sqlite", "host" => '/tmp/Lotus/DBSqlite/', "port" => '', "password" => "", "dbname" => 'sqlite_test0.db', 'pconnect' => '')); $dcb->addHost("group_1", "node_1", "master", array("adapter" => "pdo_sqlite", "host" => '/tmp/Lotus/DBSqlite/', "port" => '', "password" => "", "dbname" => 'sqlite_test1.db', 'pconnect' => '', 'sqlite_version' => 'sqlite2')); $dcb->addHost("group_8", "node_8", "master", array("adapter" => "mysql", "host" => 'localhost', "port" => '', 'username' => 'root', "password" => "123456", "dbname" => 'test')); $config["db.servers"] = $dcb->getServers();
<?php $host = substr(dirname(__FILE__), 0, strpos(__FILE__, "Admin")) . 'data/'; $dbname = 'publish.db'; $singlehost = array("adapter" => "sqlite", "host" => $host, "port" => '', "password" => "", "dbname" => $dbname, 'pconnect' => false); $dcb = new LtDbConfigBuilder(); $dcb->addSingleHost($singlehost); $config["db.servers"] = $dcb->getServers();
<?php // Build DB Config array $dbConfigBuild = new LtDbConfigBuilder(); // 部署分布式数据库 $dbConfigBuild->addHost("Publish", "Admin", "master", array("adapter" => "pdo_mysql", "host" => 'localhost', "port" => '', "password" => "123456", "dbname" => 'projectdb')); $dbConfigBuild->addHost("Publish", "Admin", "slave", array("adapter" => "pdo_mysql", "host" => 'localhost', "port" => '', "password" => "123456", "dbname" => 'projectdb')); // 指定数据库配置数组 $config["db.servers"] = $dbConfigBuild->getServers();
<?php // Build DB Config array $dbConfigBuild = new LtDbConfigBuilder(); // 部署分布式数据库 $dbConfigBuild->addHost("WebIM", "Backend", "master", array("adapter" => "pdo_mysql", "host" => '10.0.247.179', "port" => '3306', "password" => "123456", "username" => 'odd_user', "dbname" => 'odd_db')); $dbConfigBuild->addHost("WebIM", "Backend", "slave", array("adapter" => "pdo_mysql", "host" => '10.0.247.179', "port" => '3306', "password" => "123456", "username" => 'odd_user', "dbname" => 'odd_db')); // 指定数据库配置数组 $config["db.servers"] = $dbConfigBuild->getServers();