public function testPerformance() { /** * 构造缓存配置 */ $ccb = new LtCacheConfigBuilder(); // $ccb->addSingleHost(array("adapter" => "apc")); // $ccb->addSingleHost(array("adapter" => "eAccelerator")); // $ccb->addSingleHost(array("adapter" => "file")); // $ccb->addSingleHost(array("adapter" => "memcache", "host" => "localhost", "port" => 11211)); // $ccb->addSingleHost(array("adapter" => "memcached", "host" => "localhost", "port" => 11211)); $ccb->addSingleHost(array("adapter" => "file")); // $ccb->addSingleHost(array("adapter" => "Xcache", "key_prefix" => "test_xcache_")); /** * 实例化组件入口类 */ $cache = new LtCache(); $cache->configHandle->addConfig(array("cache.servers" => $ccb->getServers())); $cache->init(); // 初始化完毕,测试其效果 $ch = $cache->getTDG('test-performance'); $this->assertTrue($ch->add("test_key", "test_value")); $this->assertEquals("test_value", $ch->get("test_key")); $this->assertTrue($ch->update("test_key", "new_value")); $this->assertEquals("new_value", $ch->get("test_key")); $this->assertTrue($ch->del("test_key")); $this->assertFalse($ch->get("test_key")); /** * 运行500次,要求在1秒内运行完 */ $base_memory_usage = memory_get_usage(); $times = 500; $startTime = microtime(true); // ----------------------------测试读取 $ch->add("test_key", "test_value"); for ($i = 0; $i < $times; $i++) { $ch->get("test_key"); } $ch->update("test_key", "new_value"); $ch->del("test_key"); // ----------------------------测试完成 $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--------------LtCache get -----------------\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); }
/** * 使用尚未支持的adapter * * @expectedException PHPUnit_Framework_Error */ public function testInvalidAdapter() { /** * 构造缓存配置 */ $ccb = new LtCacheConfigBuilder(); $ccb->addSingleHost(array("adapter" => "not_exists")); /** * 实例化组件入口类 */ $cache = new LtCache(); $cache->configHandle->addConfig(array("cache.servers" => $ccb->getServers())); $cache->init(); $ch = $cache->getTDG('test'); $ch->add("test_key", "test_value"); }
public function __construct() { $this->db = new LtDb(); $this->db->group = "Backend"; $this->db->node = "Search"; $this->db->init(); // 启用Cache $Cache = new LtCache(); $Cache->configHandle->get("cache.servers"); $Cache->group = 'Backend'; $Cache->node = 'Search'; $Cache->init(); $this->cache = $Cache->getTDG("Basedao"); $this->cacheTime = $this->cacheTime ? $this->cacheTime : 3600; $this->dbh = $this->db->getDbHandle(); $this->smc = $this->db->getSqlMapClient(); }
/** * 在不同group下存储相同的table, key */ public function testMostUsedWayWithMultiGroup() { /** * 构造缓存配置 */ $ccb = new LtCacheConfigBuilder(); $ccb->addHost("group_file", "node_0", "master", array("adapter" => "file")); if (extension_loaded('memcache')) { $ccb->addHost("group_memo", "node_0", "master", array("adapter" => "memcache", "host" => "localhost", "port" => 11211)); } else { $ccb->addHost("group_memo", "node_0", "master", array("adapter" => "file")); } /** * 操作第一个Group: group_file */ $cache1 = new LtCache(); /** * LtCache 创建实例后初始化$configHandle */ $cache1->configHandle->addConfig(array("cache.servers" => $ccb->getServers())); $cache1->group = "group_file"; $cache1->init(); $ch = $cache1->getTDG("prod_info"); $this->assertTrue($ch->add("key_1", "prod_1")); $this->assertEquals("prod_1", $ch->get("key_1")); $this->assertTrue($ch->update("key_1", "new_value")); $this->assertEquals("new_value", $ch->get("key_1")); $this->assertTrue($ch->del("key_1")); $this->assertFalse($ch->get("key_1")); /** * 操作第二个Group: group_memo * 如下代码也用了prod_info这个table和key_1这个 键,但他并不会跟上一个group的key_1冲突,因为他们的是不同的group */ $cache2 = new LtCache(); $cache2->configHandle->addConfig(array("cache.servers" => $ccb->getServers())); $cache2->group = "group_memo"; $cache2->init(); $ch = $cache2->getTDG("prod_info"); $this->assertTrue($ch->add("key_1", "prod_1")); $this->assertEquals("prod_1", $ch->get("key_1")); $this->assertTrue($ch->update("key_1", "new_value")); $this->assertEquals("new_value", $ch->get("key_1")); $this->assertTrue($ch->del("key_1")); $this->assertFalse($ch->get("key_1")); }