Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Ip2region
提交
72b9ffd2
I
Ip2region
项目概览
int
/
Ip2region
上一次同步 12 个月
通知
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 搜索 >>
提交
72b9ffd2
编写于
12月 20, 2022
作者:
G
gongzhengyang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat: add loop test ip
上级
49756bfb
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
147 addition
and
26 deletion
+147
-26
binding/rust/Makefile
binding/rust/Makefile
+12
-0
binding/rust/ReadMe.md
binding/rust/ReadMe.md
+73
-1
binding/rust/bin/src/cmd.rs
binding/rust/bin/src/cmd.rs
+1
-1
binding/rust/bin/src/main.rs
binding/rust/bin/src/main.rs
+25
-5
binding/rust/search/Cargo.toml
binding/rust/search/Cargo.toml
+4
-4
binding/rust/search/benches/search.rs
binding/rust/search/benches/search.rs
+14
-13
binding/rust/search/src/lib.rs
binding/rust/search/src/lib.rs
+18
-2
未找到文件。
binding/rust/Makefile
0 → 100644
浏览文件 @
72b9ffd2
# ip2region rust binding makefile
all
:
build
.PHONY
:
all
build
:
cargo build
-r
test
:
cargo
test
bench
:
cargo bench
clean
:
find ./
-name
xdb_searcher | xargs
rm
-f
\ No newline at end of file
binding/rust/ReadMe.md
浏览文件 @
72b9ffd2
# ip2region rust 查询客户端实现
# ip2region
xdb
rust 查询客户端实现
# 使用方式
### 缓存整个 `xdb` 数据
可以预先加载整个 ip2region.xdb 到内存,完全基于内存查询,类似于之前的 memory search 查询。
```
golang
// 1、从 dbPath 加载整个 xdb 到内存
cBuff
,
err
:=
LoadContentFromFile
(
dbPath
)
if
err
!=
nil
{
fmt
.
Printf
(
"failed to load content from `%s`: %s
\n
"
,
dbPath
,
err
)
return
}
// 2、用全局的 cBuff 创建完全基于内存的查询对象。
searcher
,
err
:=
xdb
.
NewWithBuffer
(
cBuff
)
if
err
!=
nil
{
fmt
.
Printf
(
"failed to create searcher with content: %s
\n
"
,
err
)
return
}
// 备注:并发使用,用整个 xdb 缓存创建的 searcher 对象可以安全用于并发。
```
# 编译测试程序
通过如下方式编译得到 xdb_searcher 可执行程序:
```
bash
# 切换到 golang binding 根目录
make
```
# 查询测试
通过
`xdb_searcher search`
命令来测试 ip2region.xdb 的查询:
```
➜ golang git:(v2.0_xdb) ./xdb_searcher search
./xdb_searcher search [command options]
options:
--db string ip2region binary xdb file path
--cache-policy string cache policy: file/vectorIndex/content
```
例如:使用默认的 data/ip2region.xdb 进行查询测试
```
bash
➜ golang git:
(
v2.0_xdb
)
✗ ./xdb_searcher search
--db
=
../../data/ip2region.xdb
ip2region xdb searcher
test
program,
type
`
quit
`
to
exit
ip2region>> 1.2.3.4
{
region:美国|0|华盛顿|0|谷歌, took:101.57µs
}
```
输入 ip 地址进行查询即可,输入 quit 退出测试程序。可以设置
`cache-policy`
为 file/vectorIndex/content 来测试不同的查询缓存机制。
# bench 测试
通过
`xdb_searcher bench`
命令来进行自动 bench 测试,一方面确保程序和
`xdb`
文件都没有错误,另一方面通过大量的查询得到平均查询性能:
```
bash
➜ golang git:
(
v2.0_xdb
)
./xdb_searcher bench
./xdb_searcher bench
[
command
options]
options:
--db
string ip2region binary xdb file path
--src
string
source
ip text file path
--cache-policy
string cache policy: file/vectorIndex/content
```
例如:通过 data/ip2region.xdb 和 data/ip.merge.txt 进行 bench 测试:
```
bash
➜ golang git:
(
v2.0_xdb
)
✗ ./xdb_searcher bench
--db
=
../../data/ip2region.xdb
--src
=
../../data/ip.merge.txt
Bench finished,
{
total: 3417955, took: 28.211578339s, cost: 8253 ns/op
}
```
可以设置
`cache-policy`
参数来分别测试 file/vectorIndex/content 不同缓存实现机制的效率。
*请注意 bench 使用的 src 文件需要是生成对应的 xdb 文件的相同的源文件*
。
bench 程序会逐行读取
`src`
指定的源IP文件,然后每个 IP 段选取 5 个固定位置的 IP 进行测试,以确保查询的 region 信息和原始的 region 信息是相同。测试途中没有调试信息的输出,有错误会打印错误信息并且终止运行,所以看到
`Bench finished`
就表示 bench 成功了,cost 是表示每次查询操作的平均时间(ns)。
binding/rust/bin/src/cmd.rs
浏览文件 @
72b9ffd2
...
...
@@ -4,6 +4,6 @@ pub fn get_matches() -> ArgMatches {
Command
::
new
(
"ip2region"
)
.version
(
"0.1"
)
.about
(
"ip2region bin program"
)
.arg
(
arg!
(
--
xdb
<
xdb
>
"the xdb filepath, you can set this field like ../data/ip2region.xdb"
)
.required
(
true
)
)
.arg
(
arg!
(
--
xdb
<
xdb
>
"the xdb filepath, you can set this field like ../data/ip2region.xdb"
))
.get_matches
()
}
binding/rust/bin/src/main.rs
浏览文件 @
72b9ffd2
use
std
::
env
;
use
std
::
io
::
Write
;
use
std
::
time
::
Instant
;
mod
cmd
;
fn
main
()
{
let
matches
=
cmd
::
get_matches
();
let
xdb_filepath
=
matches
.get_one
::
<
String
>
(
"xdb"
)
.unwrap
();
println!
(
"{xdb_filepath}----"
);
// let result = search::search_by_ip("1.2.165.128");
// println!("{:?}", result);
env
::
var
(
"XDB_FILEPATH"
)
.unwrap_or_else
(|
_
|
{
let
matches
=
cmd
::
get_matches
();
let
xdb_filepath
=
matches
.get_one
::
<
String
>
(
"xdb"
)
.unwrap
();
env
::
set_var
(
"XDB_FILEPATH"
,
xdb_filepath
);
xdb_filepath
.to_owned
()
});
search
::
global_searcher
();
println!
(
"ip2region xdb searcher test program, type `quit` to exit"
);
loop
{
print!
(
"ip2region>> "
);
std
::
io
::
stdout
()
.flush
()
.unwrap
();
let
mut
line
=
String
::
new
();
std
::
io
::
stdin
()
.read_line
(
&
mut
line
)
.unwrap
();
if
line
.contains
(
"quit"
)
{
break
}
let
now
=
Instant
::
now
();
let
result
=
search
::
search_by_ip
(
line
.trim
());
println!
(
"region: {:?}, took: {:?}"
,
result
,
now
.elapsed
());
}
}
binding/rust/search/Cargo.toml
浏览文件 @
72b9ffd2
...
...
@@ -12,9 +12,9 @@ license = "Apache-2.0"
once_cell
=
"1.16"
[dev-dependencies]
criterion
=
"0.
3
"
criterion
=
"0.
4
"
rand
=
"0.8"
#
[[bench]]
#
name = "search"
#
harness = false
[[bench]]
name
=
"search"
harness
=
false
binding/rust/search/benches/search.rs
浏览文件 @
72b9ffd2
// use criterion::{black_box, criterion_group, criterion_main, Criterion, BenchmarkId};
// use rand;
//
// fn ip_search_benchmark(c: &mut Criterion) {
// let searcher = search::Searcher::new("../../../data/ip2region.xdb").unwrap();
// c.bench_with_input(BenchmarkId::new("searcher", searcher),
// &searcher, |b, &seacher| {
// b.iter(|| seacher.search_by_ip(black_box(rand::random::<u32>())));
// });
// }
//
// criterion_group!(benches, ip_search_benchmark);
// criterion_main!(benches);
use
criterion
::{
Criterion
,
criterion_group
,
criterion_main
};
use
rand
;
use
search
::
search_by_ip
;
fn
ip_search_benchmark
(
c
:
&
mut
Criterion
)
{
c
.bench_function
(
"ip_search_bench"
,
|
b
|
b
.iter
(||
{
let
ip
=
rand
::
random
::
<
u32
>
();
search_by_ip
(
ip
)
.unwrap
();
}));
}
criterion_group!
(
benches
,
ip_search_benchmark
);
criterion_main!
(
benches
);
binding/rust/search/src/lib.rs
浏览文件 @
72b9ffd2
...
...
@@ -4,6 +4,7 @@ use std::error::Error;
use
std
::
fs
::
File
;
use
std
::
io
::
Read
;
use
std
::
fmt
;
use
std
::
env
;
use
std
::
fmt
::
Formatter
;
use
once_cell
::
sync
::
OnceCell
;
...
...
@@ -22,10 +23,13 @@ pub struct Searcher {
pub
buffer
:
Vec
<
u8
>
,
}
fn
global_searcher
()
->
&
'static
Searcher
{
pub
fn
global_searcher
()
->
&
'static
Searcher
{
static
SEARCHER
:
OnceCell
<
Searcher
>
=
OnceCell
::
new
();
SEARCHER
.get_or_init
(||
{
Searcher
::
new
(
DEFAULT_XDB_FILEPATH
)
.unwrap
()
let
xdp_filepath
=
env
::
var
(
"XDB_FILEPATH"
)
.unwrap_or_else
(|
_
|
DEFAULT_XDB_FILEPATH
.to_owned
());
println!
(
"init xdb searcher at {xdp_filepath}"
);
Searcher
::
new
(
xdp_filepath
.as_str
())
.unwrap
()
})
}
...
...
@@ -103,6 +107,7 @@ mod tests {
use
super
::
*
;
use
std
::
net
::
Ipv4Addr
;
use
std
::
str
::
FromStr
;
use
std
::
thread
;
const
TEST_IP_FILEPATH
:
&
str
=
"../../../data/ip.test.txt"
;
...
...
@@ -135,4 +140,15 @@ mod tests {
}
}
}
#[test]
fn
test_multi_thread
()
{
let
handle
=
thread
::
spawn
(||
{
let
result
=
search_by_ip
(
"2.2.2.2"
)
.unwrap
();
println!
(
"ip search in spawn: {result}"
);
});
let
r
=
search_by_ip
(
"1.1.1.1"
)
.unwrap
();
println!
(
"ip search in main thread: {r}"
);
handle
.join
()
.unwrap
();
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录