diff --git a/binding/erlang/Makefile b/binding/erlang/Makefile new file mode 100644 index 0000000..d71c6d4 --- /dev/null +++ b/binding/erlang/Makefile @@ -0,0 +1,18 @@ +.PHONY: all compile test clean bench-v4 bench-v6 + +all: compile + +compile: + rebar3 compile + +test: + rebar3 eunit + +clean: + rebar3 clean + +bench-v4: + sh benchmarks/xdb-benchmark.sh ipv4 + +bench-v6: + sh benchmarks/xdb-benchmark.sh ipv6 diff --git a/binding/erlang/README.md b/binding/erlang/README.md index 99d99c3..5a51647 100644 --- a/binding/erlang/README.md +++ b/binding/erlang/README.md @@ -131,44 +131,78 @@ Finished in 0.150 seconds ### Benchmark +Both IPv4 and IPv6 benchmarks share the same script. Run it with the desired IP version: + ``` $ cd benchmarks/ -$ 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] +$ sh xdb-benchmark.sh ipv4 +``` -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 +For IPv6: -===> Booted ip2region -===> Evaluating: "xdb_benchmark:main(\"../../data/ip.merge.txt\"), init:stop()." +``` +$ sh xdb-benchmark.sh ipv6 +``` + +Or use the Makefile targets from the `binding/erlang` directory: + +``` +$ make bench-v4 +$ make bench-v6 +``` + +#### IPv4 benchmark example + +``` CPU info: -model name : AMD EPYC 7K62 48-Core Processor -cache size : 512 KB -cpu MHz : 2595.124 -bogomips : 5190.24 -cores/threads : 2 +model name : Apple M4 +cores/threads : 10/10 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 4.835593s +system_version:Erlang/OTP 29 [erts-17.0.2] [source] [64-bit] [smp:10:10] [ds:10:10:10] [async-threads:1] [jit] [dtrace] +load test data use 1.34496s start run benchmark tests search from file: -ip count:683844, -total time: 28.201699s, -search 24248.326315375536 times per second, -use 41.23995969841075 micro second per search +ip count:487169, +total time: 11.564375s, +search 42126.70377776577 times per second, +use 23.737912305585947 micro second per search search from cache: -ip count:683844, -total time: 0.671801s, -search 1017926.4395259906 times per second, -use 0.9823892583688677 micro second per search +ip count:487169, +total time: 0.213552s, +search 2281266.389450813 times per second, +use 0.43835301507279817 micro second per search + +benchmark test finish +``` + +#### IPv6 benchmark example + +``` +CPU info: +model name : Apple M4 +cores/threads : 10/10 + +Erlang info: +system_version:Erlang/OTP 29 [erts-17.0.2] [source] [64-bit] [smp:10:10] [ds:10:10:10] [async-threads:1] [jit] [dtrace] +load test data use 2.472659s + +start run benchmark tests + +search from file: +ip count:638953, +total time: 18.463896s, +search 34605.535039842085 times per second, +use 28.897111368128797 micro second per search + +search from cache: +ip count:638953, +total time: 0.541899s, +search 1179099.7953493178 times per second, +use 0.8481046336741513 micro second per search benchmark test finish ``` diff --git a/binding/erlang/README_zh.md b/binding/erlang/README_zh.md index 6643b07..4355a9a 100644 --- a/binding/erlang/README_zh.md +++ b/binding/erlang/README_zh.md @@ -107,44 +107,79 @@ Finished in 0.074 seconds ``` ### 基准测试 + +IPv4 与 IPv6 共用一个脚本,通过参数指定版本: + ``` $ cd benchmarks/ -$ 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] +$ sh xdb-benchmark.sh ipv4 +``` -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 +IPv6: -===> Booted ip2region -===> Evaluating: "xdb_benchmark:main(\"../../data/ip.merge.txt\"), init:stop()." +``` +$ sh xdb-benchmark.sh ipv6 +``` + +也可以在 `binding/erlang` 目录直接用 Makefile: + +``` +$ make bench-v4 +$ make bench-v6 +``` + +#### IPv4 基准测试示例 + +``` CPU info: -model name : AMD EPYC 7K62 48-Core Processor -cache size : 512 KB -cpu MHz : 2595.124 -bogomips : 5190.24 -cores/threads : 2 +model name : Apple M4 +cores/threads : 10/10 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 4.835593s +system_version:Erlang/OTP 29 [erts-17.0.2] [source] [64-bit] [smp:10:10] [ds:10:10:10] [async-threads:1] [jit] [dtrace] +load test data use 1.34496s start run benchmark tests search from file: -ip count:683844, -total time: 28.201699s, -search 24248.326315375536 times per second, -use 41.23995969841075 micro second per search +ip count:487169, +total time: 11.564375s, +search 42126.70377776577 times per second, +use 23.737912305585947 micro second per search search from cache: -ip count:683844, -total time: 0.671801s, -search 1017926.4395259906 times per second, -use 0.9823892583688677 micro second per search +ip count:487169, +total time: 0.213552s, +search 2281266.389450813 times per second, +use 0.43835301507279817 micro second per search + +benchmark test finish +``` + +#### IPv6 基准测试示例 + +``` +CPU info: +model name : Apple M4 +cores/threads : 10/10 + +Erlang info: +system_version:Erlang/OTP 29 [erts-17.0.2] [source] [64-bit] [smp:10:10] [ds:10:10:10] [async-threads:1] [jit] [dtrace] +load test data use 2.472659s + +start run benchmark tests + +search from file: +ip count:638953, +total time: 18.463896s, +search 34605.535039842085 times per second, +use 28.897111368128797 micro second per search + +search from cache: +ip count:638953, +total time: 0.541899s, +search 1179099.7953493178 times per second, +use 0.8481046336741513 micro second per search benchmark test finish ``` diff --git a/binding/erlang/benchmarks/xdb-benchmark.sh b/binding/erlang/benchmarks/xdb-benchmark.sh index 40d13f3..218cdf4 100755 --- a/binding/erlang/benchmarks/xdb-benchmark.sh +++ b/binding/erlang/benchmarks/xdb-benchmark.sh @@ -1,5 +1,36 @@ #!/bin/bash +set -e -cd .. +# Run from binding/erlang so paths and rebar3 artifacts are correct. +cd "$(dirname "$0")/.." -rebar3 shell --eval="xdb_benchmark:main(\"../../data/ip.merge.txt\"), init:stop()." +VERSION="${1:-ipv4}" +PROJECT_ROOT="$(cd ../.. && pwd)" + +if [ "$VERSION" = "ipv6" ]; then + DB_FILE="${PROJECT_ROOT}/data/ip2region_v6.xdb" + DATA_FILE="${PROJECT_ROOT}/data/ipv6_source.txt" + DB_CONFIG="[{ipv4, \"ip2region.xdb\"}, {ipv6, \"${DB_FILE}\"}]" +elif [ "$VERSION" = "ipv4" ]; then + DATA_FILE="${PROJECT_ROOT}/data/ipv4_source.txt" + DB_CONFIG="[{ipv4, \"ip2region.xdb\"}]" +else + echo "Usage: $0 [ipv4|ipv6]" + exit 1 +fi + +rebar3 compile + +# shellcheck disable=SC2089 +EVAL="application:load(ip2region), \ +application:set_env(ip2region, db, ${DB_CONFIG}), \ +application:set_env(ip2region, poolargs, [{size,4},{max_overflow,8}]), \ +{ok,_}=application:ensure_all_started(ip2region), \ +xdb_benchmark:main(\"${DATA_FILE}\"), \ +init:stop()." + +# shellcheck disable=SC2090 +erl -pa _build/default/lib/poolboy/ebin \ + -pa _build/default/lib/ip2region/ebin \ + -noshell \ + -eval "${EVAL}" diff --git a/binding/erlang/src/xdb_benchmark.erl b/binding/erlang/src/xdb_benchmark.erl index e52801c..0fa6e7f 100644 --- a/binding/erlang/src/xdb_benchmark.erl +++ b/binding/erlang/src/xdb_benchmark.erl @@ -18,11 +18,20 @@ main(DataFile) -> show_hw_sw_info() -> io:format("CPU info:~n", []), - io:format("~s", [os:cmd("egrep '^model name' /proc/cpuinfo | head -1")]), - io:format("~s", [os:cmd("egrep '^cache' /proc/cpuinfo | head -1")]), - io:format("~s", [os:cmd("egrep '^cpu MHz' /proc/cpuinfo | head -1")]), - io:format("~s", [os:cmd("egrep '^bogomips' /proc/cpuinfo | head -1")]), - io:format("cores/threads : ~s~n", [os:cmd("egrep -c '^processor' /proc/cpuinfo")]), + case os:type() of + {unix, darwin} -> + io:format("model name : ~s~n", [string:trim(os:cmd("sysctl -n machdep.cpu.brand_string 2>/dev/null"))]), + io:format("cores/threads : ~s/~s~n", [string:trim(os:cmd("sysctl -n hw.physicalcpu 2>/dev/null")), + string:trim(os:cmd("sysctl -n hw.logicalcpu 2>/dev/null"))]); + {unix, linux} -> + io:format("~s", [os:cmd("egrep '^model name' /proc/cpuinfo | head -1")]), + io:format("~s", [os:cmd("egrep '^cache' /proc/cpuinfo | head -1")]), + io:format("~s", [os:cmd("egrep '^cpu MHz' /proc/cpuinfo | head -1")]), + io:format("~s", [os:cmd("egrep '^bogomips' /proc/cpuinfo | head -1")]), + io:format("cores/threads : ~s~n", [os:cmd("egrep -c '^processor' /proc/cpuinfo")]); + _ -> + io:format("unsupported os~n", []) + end, io:format("Erlang info:~n", []), io:format("system_version:~s", [erlang:system_info(system_version)]), ok.