Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Ip2region
提交
3255355e
I
Ip2region
项目概览
int
/
Ip2region
上一次同步 11 个月
通知
19
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
I
Ip2region
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
3255355e
编写于
1月 17, 2023
作者:
L
LeiHua
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
1.查询接口模块名为xdb;2.查询接口返回字符串;3.增加 apache 授权信息;4.调整基准测试数据来源
上级
32372fa8
变更
11
展开全部
隐藏空白更改
内联
并排
Showing
11 changed file
with
151 addition
and
683897 deletion
+151
-683897
binding/erlang/README.md
binding/erlang/README.md
+50
-20
binding/erlang/benchmarks/ip2region-benchmark.sh
binding/erlang/benchmarks/ip2region-benchmark.sh
+0
-7
binding/erlang/benchmarks/test_data.txt
binding/erlang/benchmarks/test_data.txt
+0
-683844
binding/erlang/benchmarks/xdb-benchmark.sh
binding/erlang/benchmarks/xdb-benchmark.sh
+5
-0
binding/erlang/src/ip2region_app.erl
binding/erlang/src/ip2region_app.erl
+5
-1
binding/erlang/src/ip2region_sup.erl
binding/erlang/src/ip2region_sup.erl
+4
-0
binding/erlang/src/ip2region_util.erl
binding/erlang/src/ip2region_util.erl
+9
-0
binding/erlang/src/ip2region_worker.erl
binding/erlang/src/ip2region_worker.erl
+11
-15
binding/erlang/src/xdb.erl
binding/erlang/src/xdb.erl
+32
-0
binding/erlang/src/xdb_benchmark.erl
binding/erlang/src/xdb_benchmark.erl
+17
-10
binding/erlang/test/xdb_test.erl
binding/erlang/test/xdb_test.erl
+18
-0
未找到文件。
binding/erlang/README.md
浏览文件 @
3255355e
# ip2region xdb erlang 查询客户端
### 简介
该bingding以erlang语言实现xdb查询客户端,基于Erlang OTP Application,查询逻辑由ip2region_worker工作进程实现,支持配多个工作进程来进行负载均衡。
### 应用配置
该应用可配置的参数在ip2region.app.src中,如下:
```
erlang
{
env
,[
{
poolargs
,
[
{
size
,
1
},
%% 工作进程默认数量
{
max_overflow
,
5
}
%% 工作进程最大数量
]}
]}
```
### 编译
...
...
@@ -12,15 +25,20 @@ $ rebar3 compile
$ rebar3 shell
```
在erlang
节点中执行以下指令查询Ip地址信息
:
在erlang
shell中调用xdb:search/1接口查询Ip地址信息, 该接口支持以list格式字符串、bianry格式字符串、tuple和整数表示的IP地址,如下
:
```
1> ip2region:search("1.0.8.0").
#{city => <<229,185,191,229,183,158,229,184,130>>,
country => <<228,184,173,229,155,189>>,
isp => <<231,148,181,228,191,161>>,
province => <<229,185,191,228,184,156,231,156,129>>,
region => <<>>}
1> xdb:search("1.0.8.0").
[20013,22269,124,48,124,24191,19996,30465,124,24191,24030,
24066,124,30005,20449]
2>
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|广东省|广州市|电信
```
### 使用方法
...
...
@@ -40,7 +58,7 @@ application:ensure_started(ip2region),
......
```
*
调用
ip2region
:search/1接口查询IP信息
*
调用
xdb
:search/1接口查询IP信息
```
......
...
...
@@ -57,18 +75,30 @@ $ rebar3 eunit
===> Analyzing applications...
===> Compiling ip2region
===> Performing EUnit tests...
=INFO REPORT==== 1
3-Jan-2023::21:26:15.137021
===
=INFO REPORT==== 1
7-Jan-2023::11:52:59.920155
===
XdbFile:/home/admin/erl-workspace/ip2region/binding/erlang/_build/test/lib/ip2region/priv/ip2region.xdb
...
Finished in 0.085 seconds
3 tests, 0 failures
....
Finished in 0.074 seconds
4 tests, 0 failures
```
### 基准测试
```
$
cd
benchmarks
/
$
sh
ip2region
-
benchmark
.
sh
$
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()."
CPU
info
:
model
name
:
AMD
EPYC
7
K62
48
-
Core
Processor
cache
size
:
512
KB
...
...
@@ -78,21 +108,21 @@ 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
]
load
test
data
use
2.724344
s
load
test
data
use
4.835593
s
start
run
benchmark
tests
search
from
file
:
ip
count
:
683844
,
total
time
:
2
5.56304
s
,
search
2
6751.27840820184
times
per
second
,
use
37.381391077497206
micro
second
per
search
total
time
:
2
8.201699
s
,
search
2
4248.326315375536
times
per
second
,
use
41.23995969841075
micro
second
per
search
search
from
cache
:
ip
count
:
683844
,
total
time
:
0.67
0307
s
,
search
10
20195.2239794602
times
per
second
,
use
0.98
02045495756342
micro
second
per
search
total
time
:
0.67
1801
s
,
search
10
17926.4395259906
times
per
second
,
use
0.98
23892583688677
micro
second
per
search
benchmark
test
finish
...
...
binding/erlang/benchmarks/ip2region-benchmark.sh
已删除
100755 → 0
浏览文件 @
32372fa8
#!/bin/bash
awk
-v
FS
=
'|'
'{print $1}'
../../../data/ip.merge.txt
>
test_data.txt
cd
..
rebar3 shell
--eval
=
"ip2region_benchmark:main(
\"
./benchmarks/test_data.txt
\"
), init:stop()."
binding/erlang/benchmarks/test_data.txt
已删除
100644 → 0
浏览文件 @
32372fa8
此差异已折叠。
点击以展开。
binding/erlang/benchmarks/xdb-benchmark.sh
0 → 100755
浏览文件 @
3255355e
#!/bin/bash
cd
..
rebar3 shell
--eval
=
"xdb_benchmark:main(
\"
../../data/ip.merge.txt
\"
), init:stop()."
binding/erlang/src/ip2region_app.erl
浏览文件 @
3255355e
%%%-------------------------------------------------------------------
%% @doc ip2region public API
%% Copyright 2022 The Ip2Region Authors. All rights reserved.
%% Use of this source code is governed by a Apache2.0-style
%% license that can be found in the LICENSE file.
%%
%% @doc
%% @end
%%%-------------------------------------------------------------------
...
...
binding/erlang/src/ip2region_sup.erl
浏览文件 @
3255355e
%%%-------------------------------------------------------------------
%% Copyright 2022 The Ip2Region Authors. All rights reserved.
%% Use of this source code is governed by a Apache2.0-style
%% license that can be found in the LICENSE file.
%%
%% @doc ip2region top level supervisor.
%% @end
%%%-------------------------------------------------------------------
...
...
binding/erlang/src/ip2region_util.erl
浏览文件 @
3255355e
%%%-------------------------------------------------------------------
%% Copyright 2022 The Ip2Region Authors. All rights reserved.
%% Use of this source code is governed by a Apache2.0-style
%% license that can be found in the LICENSE file.
%%
%% @doc
%% ip2region utils
%% @end
%%%-------------------------------------------------------------------
-
module
(
ip2region_util
).
-
export
([
ipv4_to_n
/
1
]).
...
...
binding/erlang/src/ip2region_worker.erl
浏览文件 @
3255355e
%%%===============================================================
%%% @author leihua <leihua918@sina.com>
%%% @doc
%%% ip2region工作进程
%%% Created: 2023-1-13 16:53
%%% @end
%%%===============================================================
%%%-------------------------------------------------------------------
%% Copyright 2022 The Ip2Region Authors. All rights reserved.
%% Use of this source code is governed by a Apache2.0-style
%% license that can be found in the LICENSE file.
%%
%% @doc
%% ip2region xdb client worker
%% @end
%%%-------------------------------------------------------------------
-
module
(
ip2region_worker
).
-
behaviour
(
gen_server
).
-
include
(
"ip2region.hrl"
).
...
...
@@ -37,6 +39,7 @@ search(Pid, Ip) ->
%% gen_server callbacks
%% =========================================
init
(_
Args
)
->
process_flag
(
trap_exit
,
true
),
AppName
=
case
application
:
get_application
()
of
{
ok
,
AName
}
->
AName
;
...
...
@@ -159,14 +162,7 @@ search_ip(IoDevice, IntIp, SPtr, EPtr, Low, High) when Low =< High ->
search_ip
(
IoDevice
,
IntIp
,
SPtr
,
EPtr
,
Middle
+
1
,
High
);
true
->
{
ok
,
DataBin
}
=
read_file
(
IoDevice
,
DataPtr
,
DataLen
),
[
Country
,
Region
,
Province
,
City
,
ISP
]
=
string
:
tokens
(
binary_to_list
(
DataBin
),
"|"
),
#
{
country
=>
?
IF
(
Country
==
"0"
,
<<>>
,
list_to_binary
(
Country
)),
region
=>
?
IF
(
Region
==
"0"
,
<<>>
,
list_to_binary
(
Region
)),
province
=>
?
IF
(
Province
==
"0"
,
<<>>
,
list_to_binary
(
Province
)),
city
=>
?
IF
(
City
==
"0"
,
<<>>
,
list_to_binary
(
City
)),
isp
=>
?
IF
(
ISP
==
"0"
,
<<>>
,
list_to_binary
(
ISP
))
}
unicode
:
characters_to_nfc_list
(
DataBin
)
end
;
search_ip
(_
IoDevice
,
_
IntIp
,
_
SPtr
,
_
EPtr
,
_
Low
,
_
High
)
->
{
error
,
unknown
}.
...
...
binding/erlang/src/
ip2region
.erl
→
binding/erlang/src/
xdb
.erl
浏览文件 @
3255355e
%%%===============================================================
%%% @author leihua <leihua918@sina.com>
%%% @doc
%%% ip2region xdb 查询客户端
%%% Created: 2023-1-13 16:53
%%% @end
%%%===============================================================
-
module
(
ip2region
).
%%%-------------------------------------------------------------------
%% Copyright 2022 The Ip2Region Authors. All rights reserved.
%% Use of this source code is governed by a Apache2.0-style
%% license that can be found in the LICENSE file.
%%
%% @doc
%% ip2region xdb client search api
%% @end
%%%-------------------------------------------------------------------
-
module
(
xdb
).
-
include
(
"ip2region.hrl"
).
-
export
([
search
/
1
]).
-
spec
search
(
Ip
::
tuple
()
|
list
()
|
binary
())
->
Result
::
{
error
,
Reason
::
atom
()}
|
map
()
.
search
(
Ip
)
->
-
spec
search
(
Ip
::
tuple
()
|
list
()
|
binary
())
->
Result
::
binary
|
{
error
,
Reason
::
atom
()}
.
search
(
Ip
)
when
is_integer
(
Ip
);
is_list
(
Ip
);
is_tuple
(
Ip
);
is_binary
(
Ip
)
->
case
ip2region_util
:
ipv4_to_n
(
Ip
)
of
IntIp
when
is_integer
(
IntIp
)
->
case
ets
:
lookup
(
?
IP2REGION_CACHE
,
IntIp
)
of
...
...
binding/erlang/src/
ip2region
_benchmark.erl
→
binding/erlang/src/
xdb
_benchmark.erl
浏览文件 @
3255355e
%%%===============================================================
%%% @author leihua <leihua918@sina.com>
%%% @doc
%%% ip2region性能基准测试
%%% Created: 2023-1-13 17:46
%%% @end
%%%===============================================================
-
module
(
ip2region_benchmark
).
%%%-------------------------------------------------------------------
%% Copyright 2022 The Ip2Region Authors. All rights reserved.
%% Use of this source code is governed by a Apache2.0-style
%% license that can be found in the LICENSE file.
%%
%% @doc
%% ip2region xdb client benchmark test
%% @end
%%%-------------------------------------------------------------------
-
module
(
xdb_benchmark
).
-
export
([
main
/
1
]).
main
(
DataFile
)
->
...
...
@@ -37,7 +39,12 @@ load_test_data(DataFile) ->
load_test_data
(
Fd
,
IpList
)
->
case
file
:
read_line
(
Fd
)
of
{
ok
,
Ip
}
->
load_test_data
(
Fd
,
[
string
:
trim
(
Ip
)|
IpList
]);
case
string
:
tokens
(
unicode
:
characters_to_list
(
Ip
),
"|"
)
of
[
SIp
|
_
Tail
]
->
load_test_data
(
Fd
,
[
string
:
trim
(
SIp
)|
IpList
]);
_
->
load_test_data
(
Fd
,
IpList
)
end
;
_
->
file
:
close
(
Fd
),
IpList
...
...
@@ -63,6 +70,6 @@ run_test(IpList) ->
run_test_aux
([])
->
ok
;
run_test_aux
([
Ip
|
Tail
])
->
#
{}
=
ip2region
:
search
(
Ip
),
xdb
:
search
(
Ip
),
run_test_aux
(
Tail
).
binding/erlang/test/
ip2region
_test.erl
→
binding/erlang/test/
xdb
_test.erl
浏览文件 @
3255355e
-
module
(
ip2region
_test
).
-
module
(
xdb
_test
).
-
include_lib
(
"eunit/include/eunit.hrl"
).
search_test_
()
->
application
:
ensure_started
(
ip2region
),
A
=
#
{
city
=>
<<
"广州市"
/
utf8
>>
,
country
=>
<<
"中国"
/
utf8
>>
,
isp
=>
<<
"电信"
/
utf8
>>
,
province
=>
<<
"广东省"
/
utf8
>>
,
region
=>
<<>>
},
Region1
=
ip2region
:
search
(
"1.0.8.0"
),
Region2
=
ip2region
:
search
({
1
,
0
,
8
,
0
}),
Region3
=
ip2region
:
search
(
"xxx.0.8.0"
),
A
=
"中国|0|广东省|广州市|电信"
,
Region0
=
xdb
:
search
(
"1.0.8.0"
),
Region1
=
xdb
:
search
(
<<
"1.0.8.0"
>>
),
Region2
=
xdb
:
search
({
1
,
0
,
8
,
0
}),
Region3
=
xdb
:
search
(
"xxx.0.8.0"
),
[
?
_
assert
(
A
=:=
Region0
),
?
_
assert
(
A
=:=
Region1
),
?
_
assert
(
A
=:=
Region2
),
?
_
assert
({
error
,
bad_ip_format
}
=:=
Region3
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录