提交 72b9ffd2 编写于 作者: G gongzhengyang

feat: add loop test ip

上级 49756bfb
# 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
# 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)。
......@@ -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()
}
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());
}
}
......@@ -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
// 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);
......@@ -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.
先完成此消息的编辑!
想要评论请 注册