/**
  * 本测试展示了如何用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);
 }
Exemplo n.º 2
0
 /**
  * @dataProvider configBuilderDataProvider
  */
 public function testConfigBuilder($singleHost, $expected)
 {
     $dcb = new LtDbConfigBuilder();
     $dcb->addSingleHost($singleHost);
     $this->assertEquals($expected, $dcb->getServers());
 }
Exemplo n.º 3
0
<?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();