README.md 3.0 KB
Newer Older
L
LeiHua 已提交
1
# ip2region xdb erlang 查询客户端
L
LeiHua 已提交
2

3 4 5 6 7 8 9 10 11 12 13 14 15
### 简介
该bingding以erlang语言实现xdb查询客户端,基于Erlang OTP Application,查询逻辑由ip2region_worker工作进程实现,支持配多个工作进程来进行负载均衡。

### 应用配置
该应用可配置的参数在ip2region.app.src中,如下:
``` erlang
  {env,[
    {poolargs, [
        {size, 1},  %% 工作进程默认数量
        {max_overflow, 5}  %% 工作进程最大数量
    ]}
  ]}
```
L
LeiHua 已提交
16

L
LeiHua 已提交
17
### 编译
L
LeiHua 已提交
18

L
LeiHua 已提交
19 20 21 22
```
$ rebar3 compile
```
### 运行
L
LeiHua 已提交
23
将xdb文件放到priv目录下,然后启动erlang节点:
L
LeiHua 已提交
24 25 26 27
```
$ rebar3 shell

```
28
在erlang shell中调用xdb:search/1接口查询Ip地址信息, 该接口支持以list格式字符串、bianry格式字符串、tuple和整数表示的IP地址,如下:
L
LeiHua 已提交
29
```
30 31 32
1> xdb:search("1.0.8.0").
[20013,22269,124,48,124,24191,19996,30465,124,24191,24030,
 24066,124,30005,20449]
L
LeiHua 已提交
33
2>
34 35 36 37 38 39 40 41
3> io:format("~ts~n", [xdb:search("1.0.8.0")]).
中国|0|广东省|广州市|电信
io:format("~ts~n", [xdb:search(<<"1.0.8.0">>)]).
中国|0|广东省|广州市|电信
4> io:format("~ts~n", [xdb:search({1,0,8,0})]).
中国|0|广东省|广州市|电信
6> io:format("~ts~n", [xdb:search(16779264)]).
中国|0|广东省|广州市|电信
L
LeiHua 已提交
42
```
L
LeiHua 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

### 使用方法
* 在rebar.config中引入依赖
```
{deps, [
  ip2region
]}.

```
* 启动ip2region Application
```
......

application:ensure_started(ip2region),

......
```

61
* 调用xdb:search/1接口查询IP信息
L
LeiHua 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
```
......

ip2region:search("1.0.8.0"),

......
```

### 单元测试

```
$ rebar3 eunit
===> Verifying dependencies...
===> Analyzing applications...
===> Compiling ip2region
===> Performing EUnit tests...
78
=INFO REPORT==== 17-Jan-2023::11:52:59.920155 ===
L
LeiHua 已提交
79
XdbFile:/home/admin/erl-workspace/ip2region/binding/erlang/_build/test/lib/ip2region/priv/ip2region.xdb
80 81 82 83

....
Finished in 0.074 seconds
4 tests, 0 failures
L
LeiHua 已提交
84 85 86 87 88 89

```

### 基准测试
```
$ cd benchmarks/
90 91 92 93 94 95 96 97 98 99 100 101
$ sh xdb-benchmark.sh
===> Verifying dependencies...
===> Analyzing applications...
===> Compiling ip2region
Erlang/OTP 24 [erts-12.3.2.2] [source] [64-bit] [smp:2:2] [ds:2:2:10] [async-threads:1] [jit]

Eshell V12.3.2.2  (abort with ^G)
1> =INFO REPORT==== 17-Jan-2023::11:37:35.631095 ===
XdbFile:/home/admin/erl-workspace/ip2region/binding/erlang/_build/default/lib/ip2region/priv/ip2region.xdb

===> Booted ip2region
===> Evaluating: "xdb_benchmark:main(\"../../data/ip.merge.txt\"), init:stop()."
L
LeiHua 已提交
102 103 104 105 106 107 108 109 110
CPU info:
model name      : AMD EPYC 7K62 48-Core Processor
cache size      : 512 KB
cpu MHz         : 2595.124
bogomips        : 5190.24
cores/threads   : 2

Erlang info:
system_version:Erlang/OTP 24 [erts-12.3.2.2] [source] [64-bit] [smp:2:2] [ds:2:2:10] [async-threads:1] [jit]
111
load test data use 4.835593s
L
LeiHua 已提交
112 113 114 115 116

start run benchmark tests

search from file:
ip count:683844,
117 118 119
total time: 28.201699s,
search 24248.326315375536 times per second,
use 41.23995969841075 micro second per search
L
LeiHua 已提交
120 121 122

search from cache:
ip count:683844,
123 124 125
total time: 0.671801s,
search 1017926.4395259906 times per second,
use 0.9823892583688677 micro second per search
L
LeiHua 已提交
126 127 128 129

benchmark test finish

```