feat: add bench and update tracing version

This commit is contained in:
gongzhengyang 2022-12-24 10:39:39 +08:00
parent e1c222655a
commit 6d38469d77
4 changed files with 43 additions and 19 deletions

View File

@ -2,27 +2,23 @@
# 使用方式 # 使用方式
使用`cargo`新建一个项目,如`cargo new ip-test`
配置`Cargo.toml`如下 配置`Cargo.toml`如下
```toml ```toml
[dependencies] [dependencies]
search = { git = "https://github.com/lionsoul2014/ip2region.git", branch = "master" } search = { git = "https://github.com/lionsoul2014/ip2region.git", branch = "master" }
# 如果要在异步环境下使用,需要加上如下依赖
tokio = { version = "1", features = ["full"]}
``` ```
程序启动的时候是没加载文件,这个程序占用内存`1M`左右 `ip2region.xdb`文件会直接加载到内存,程序占用内存`13M`左右
一旦开始执行查询,`ip2region.xdb`文件会直接加载到内存,程序占用内存`12M`左右 预先加载整个` ip2region.xdb` 到内存,完全基于内存查询,只会加载一次数据,多线程安全,可以自由使用`tokio`异步运行时或者标准库的多线程`std::thread`
预先加载整个` ip2region.xdb` 到内存,完全基于内存查询,该方式线程安全,采用`once_cell::sync::OnceCell`,只会加载一次数据,多线程安全,可以自由使用`tokio`异步运行时或者标准库的多线程`std::thread`
### 缓存整个 `xdb` 数据 ### 缓存整个 `xdb` 数据
编写`main.rs` 编写`main.rs`
**需要使用`XDB_FILEPATH`指定`ip2region.xdb`文件的路径**,该参数可以使用相对路径或者绝对路径,如果使用相对路径报错,请修改为绝对路径
```rust ```rust
use std::env; use std::env;
@ -65,17 +61,39 @@ Ok("0|0|0|内网IP|内网IP")
Ok("0|0|0|内网IP|内网IP") Ok("0|0|0|内网IP|内网IP")
``` ```
# `binding/rust`路径下面的结构说明
`ip2region2`
- 包含了`ip`到`region`的函数调用库
- 里面包含了单元测试和`benchmark`测试
`example`
- 包含了命令行可执行文件生成的源码程序
- 作为一个用于`rust`的开发集成例子
开始编译之后会生成如下
`target`
- 文件夹存放编译之后的文件以及编译产生的临时文件与缓存
`Cargo.lock`
- 固定`rust`第三方库的版本
这个些编译生成的文件全部在`.gitignore`中有标识,不会被提交
# 编译程序 # 编译程序
通过如下方式编译得到 `ip2region` 可执行程序 切换到 `binding/rust` 路径,执行如下命令
切换到 `rust binding` 根目录,执行如下命令
```bash ```bash
➜ cargo build -r ➜ cargo build -r
``` ```
生成的二进制文件会在`./target/release/ip2region`位置 生成的二进制文件会在`./target/release/rust-example`位置
# 查询测试 # 查询测试

View File

@ -1,6 +1,6 @@
[package] [package]
name = "example" name = "rust-example"
default-run = "example" default-run = "rust-example"
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2021"
rust-version = "1.66.0" rust-version = "1.66.0"
@ -13,4 +13,4 @@ license = "Apache-2.0"
ip2region2 = { path = "../ip2region2" } ip2region2 = { path = "../ip2region2" }
clap = { version = "4.0" } clap = { version = "4.0" }
tracing = "0.1" tracing = "0.1"
tracing-subscriber = "0.2.0" tracing-subscriber = "0.3.14"

View File

@ -1,6 +1,5 @@
extern crate core; extern crate core;
use clap::ArgMatches;
use std::fs::File; use std::fs::File;
use std::io::Read; use std::io::Read;
use std::io::Write; use std::io::Write;
@ -8,6 +7,8 @@ use std::net::Ipv4Addr;
use std::str::FromStr; use std::str::FromStr;
use std::time::Instant; use std::time::Instant;
use clap::ArgMatches;
use ip2region2::{search_by_ip, searcher_init}; use ip2region2::{search_by_ip, searcher_init};
mod cmd; mod cmd;
@ -28,11 +29,15 @@ fn bench_test(src_filepath: &str) {
let mut file = File::open(src_filepath).unwrap(); let mut file = File::open(src_filepath).unwrap();
let mut contents = String::new(); let mut contents = String::new();
file.read_to_string(&mut contents).unwrap(); file.read_to_string(&mut contents).unwrap();
for line in contents.split('\n') { for line in contents.split('\n') {
if !line.contains('|') { if !line.contains('|') {
continue; continue;
} }
let ip_test_line = line.splitn(3, '|').collect::<Vec<&str>>(); let ip_test_line = line.splitn(3, '|').collect::<Vec<&str>>();
if ip_test_line.len() != 3 {
panic!("this line {line} don`t have enough `|` for spilt");
}
let start_ip = Ipv4Addr::from_str(ip_test_line[0]).unwrap(); let start_ip = Ipv4Addr::from_str(ip_test_line[0]).unwrap();
let end_ip = Ipv4Addr::from_str(ip_test_line[1]).unwrap(); let end_ip = Ipv4Addr::from_str(ip_test_line[1]).unwrap();
if end_ip < start_ip { if end_ip < start_ip {
@ -48,7 +53,8 @@ fn bench_test(src_filepath: &str) {
((mid_ip as u64 + end_ip as u64) >> 1) as u32, ((mid_ip as u64 + end_ip as u64) >> 1) as u32,
end_ip, end_ip,
] { ] {
search_by_ip(ip).unwrap(); let result = search_by_ip(ip).unwrap();
assert_eq!(result.as_str(), ip_test_line[2]);
count += 1; count += 1;
} }
} }
@ -75,7 +81,7 @@ fn query_test() {
let now = Instant::now(); let now = Instant::now();
let result = search_by_ip(line); let result = search_by_ip(line);
let cost = now.elapsed(); let cost = now.elapsed();
println!("region: {result:?}, took: {cost:?}", ); println!("region: {result:?}, took: {cost:?}",);
} }
} }

View File

@ -11,7 +11,7 @@ license = "Apache-2.0"
[dependencies] [dependencies]
once_cell = "1.16" once_cell = "1.16"
tracing = "0.1" tracing = "0.1"
tracing-subscriber = "0.2.0" tracing-subscriber = "0.3.14"
[dev-dependencies] [dev-dependencies]
criterion = "0.4" criterion = "0.4"