mirror of
https://github.com/chinayin/ip2region-sdk-php.git
synced 2025-04-06 04:15:48 +08:00
feat: 更新测试类/文档
This commit is contained in:
parent
50d5b6fdbf
commit
0f680dbd41
19
README.md
19
README.md
@ -1,3 +1,20 @@
|
|||||||
### ip2region
|
### ip2region SDK for PHP
|
||||||
|
|
||||||
|
### Installation
|
||||||
> composer require chinayin/ip2region
|
> composer require chinayin/ip2region
|
||||||
|
|
||||||
|
### Quick Examples
|
||||||
|
```php
|
||||||
|
use lionsoul2014\Ip2Region;
|
||||||
|
|
||||||
|
$ip2region = new Ip2Region;
|
||||||
|
|
||||||
|
$ip = '220.181.38.150';
|
||||||
|
$data = $ip2region->memorySearch($ip);
|
||||||
|
$data = $ip2region->binarySearch($ip);
|
||||||
|
$data = $ip2region->btreeSearch($ip);
|
||||||
|
|
||||||
|
// binary算法/b-tree算法/Memory算法:
|
||||||
|
// 0.x毫秒/0.1x毫秒/0.1x毫秒
|
||||||
|
// 任何客户端b-tree都比binary算法快,当然Memory算法固然是最快的!
|
||||||
|
```
|
||||||
|
@ -8,7 +8,7 @@
|
|||||||
processIsolation="false"
|
processIsolation="false"
|
||||||
stopOnFailure="false"
|
stopOnFailure="false"
|
||||||
syntaxCheck="false"
|
syntaxCheck="false"
|
||||||
bootstrap="tests/Bootstrap.php"
|
bootstrap="tests/bootstrap.php"
|
||||||
>
|
>
|
||||||
<testsuites>
|
<testsuites>
|
||||||
<testsuite name="All">
|
<testsuite name="All">
|
||||||
|
@ -7,26 +7,24 @@ use lionsoul2014\Ip2Region;
|
|||||||
class TestCase extends \PHPUnit\Framework\TestCase
|
class TestCase extends \PHPUnit\Framework\TestCase
|
||||||
{
|
{
|
||||||
|
|
||||||
public function testBinarySearch()
|
public function testFeature()
|
||||||
{
|
{
|
||||||
|
$funs = [
|
||||||
|
'binarySearch', 'memorySearch', 'btreeSearch'
|
||||||
|
];
|
||||||
$ip = '220.181.38.150';
|
$ip = '220.181.38.150';
|
||||||
|
$expected = [
|
||||||
|
'city_id' => 215,
|
||||||
|
'region' => '中国|0|北京|北京市|电信',
|
||||||
|
];
|
||||||
$ip2region = new Ip2Region;
|
$ip2region = new Ip2Region;
|
||||||
$s_time = $this->getTime();
|
foreach ($funs as $fn) {
|
||||||
$data = $ip2region->binarySearch($ip);
|
$start = $this->getTime();
|
||||||
$c_time = $this->getTime() - $s_time;
|
$data = $ip2region->$fn($ip);
|
||||||
printf("%s ==> %s|%s in %.5f millseconds\n", $ip, $data['city_id'], $data['region'], $c_time);
|
$end = $this->getTime() - $start;
|
||||||
$this->assertNotEmpty($data);
|
printf("%s (%s) ==> %s|%s in %.5f millseconds\n", $ip, $fn, $data['city_id'], $data['region'], $end);
|
||||||
}
|
$this->assertEquals($expected, $data);
|
||||||
|
}
|
||||||
public function testMemorySearch()
|
|
||||||
{
|
|
||||||
$ip = '220.181.38.150';
|
|
||||||
$ip2region = new Ip2Region;
|
|
||||||
$s_time = $this->getTime();
|
|
||||||
$data = $ip2region->memorySearch($ip);
|
|
||||||
$c_time = $this->getTime() - $s_time;
|
|
||||||
printf("%s ==> %s|%s in %.5f millseconds\n", $ip, $data['city_id'], $data['region'], $c_time);
|
|
||||||
$this->assertNotEmpty($data);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function getTime()
|
function getTime()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user