From f5051e3ebc89d11d4b651e3f66146890e0f89638 Mon Sep 17 00:00:00 2001 From: Lion Date: Wed, 22 Jun 2022 18:06:50 +0800 Subject: [PATCH] util test script to test all the utils functions --- binding/php/XdbSearcher.class.php | 19 +++++++- binding/php/util_test.php | 79 +++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 binding/php/util_test.php diff --git a/binding/php/XdbSearcher.class.php b/binding/php/XdbSearcher.class.php index b75fa7e..b2a2e81 100644 --- a/binding/php/XdbSearcher.class.php +++ b/binding/php/XdbSearcher.class.php @@ -64,7 +64,10 @@ class XdbSearcher // check and autoload the vector index if ($vectorIndex != null) { // load the vector index - $this->vectorIndex = null; + $this->vectorIndex = self::loadVectorIndexFromBuff($cBuff); + if ($this->vectorIndex == null) { + throw new Exception("failed to load vector index from buffer"); + } } $this->contentBuff = $cBuff; @@ -301,6 +304,16 @@ class XdbSearcher return self::loadVectorIndex($handle); } + // load vector index from the specified buffer + public static function loadVectorIndexFromBuff($cBuff) { + $len = self::VectorIndexRows * self::VectorIndexCols * self::SegmentIndexSize; + if (strlen($cBuff) < (self::HeaderInfoLength + $len)) { + return null; + } + + return substr($cBuff, self::HeaderInfoLength, $len); + } + // load the xdb content from a file handle public static function loadContent($handle) { if (fseek($handle, 0, SEEK_END) == -1) { @@ -340,4 +353,8 @@ class XdbSearcher } } + public static function now() { + return (microtime(true) * 1000); + } + } \ No newline at end of file diff --git a/binding/php/util_test.php b/binding/php/util_test.php new file mode 100644 index 0000000..a90271e --- /dev/null +++ b/binding/php/util_test.php @@ -0,0 +1,79 @@ + +// @Date 2022/06/22 + +require dirname(__FILE__) . '/XdbSearcher.class.php'; + +function testLoadHeader() { + $header = XdbSearcher::loadHeaderFromFile('../../data/ip2region.xdb'); + if ($header == null) { + printf("failed to load header from file\n"); + return; + } + + printf("header loaded: "); + print_r($header); +} + +function testLoadVectorIndex() { + $vIndex = XdbSearcher::loadVectorIndexFromFile('../../data/ip2region.xdb'); + if ($vIndex == null) { + printf("failed to load vector index from file\n"); + return; + } + + printf("vector index loaded: length=%d\n", strlen($vIndex)); +} + +function testLoadVectorIndexFromBuff() { + $cBuff = XdbSearcher::loadContentFromFile('../../data/ip2region.xdb'); + if ($cBuff == null) { + printf("failed to load content from file\n"); + return; + } + + $vIndex = XdbSearcher::loadVectorIndexFromBuff($cBuff); + if ($vIndex == null) { + printf("failed to load content from file\n"); + return; + } + + printf("vector index loaded, length=%d\n", strlen($vIndex)); +} + +function testLoadContent() { + $cBuff = XdbSearcher::loadContentFromFile('../../data/ip2region.xdb'); + if ($cBuff == null) { + printf("failed to load content from file\n"); + return; + } + + printf("content loaded, length=%d\n", strlen($cBuff)); +} + +printf("testing loadHeader ... \n"); +$now = XdbSearcher::now(); +testLoadHeader(); +printf("done, cost: %0.5f ms\n\n", XdbSearcher::now() - $now); + + +printf("testing loadVectorIndex ... \n"); +$now = XdbSearcher::now(); +testLoadVectorIndex(); +printf("done, cost: %0.5f ms\n\n", XdbSearcher::now() - $now); + + +printf("testing loadVectorIndexFromBuff ... \n"); +$now = XdbSearcher::now(); +testLoadVectorIndexFromBuff(); +printf("done, cost: %0.5f ms\n\n", XdbSearcher::now() - $now); + + +printf("testing loadContent ... \n"); +$now = XdbSearcher::now(); +testLoadContent(); +printf("done, cost: %0.5f ms\n\n", XdbSearcher::now() - $now);