onmcp/scripts/test_cpp_result_cache.sh

55 lines
2.3 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
"$ROOT/scripts/build_cpp.sh" >/dev/null
mkdir -p "$ROOT/reports"
MOCK_PORT=19281
MCPD_PORT=19280
"$ROOT/build/cpp/mock_mcp_server" --port "$MOCK_PORT" >"$ROOT/reports/result_cache_mock.log" 2>&1 &
MOCK_PID=$!
"$ROOT/build/cpp/mcpd" --port "$MCPD_PORT" --upstream "http://127.0.0.1:$MOCK_PORT/rpc" >"$ROOT/reports/result_cache_mcpd.log" 2>&1 &
MCPD_PID=$!
cleanup() {
kill "$MCPD_PID" "$MOCK_PID" >/dev/null 2>&1 || true
}
trap cleanup EXIT
sleep 0.3
CLIENT=("$ROOT/build/cpp/oh_mcp_client" --endpoint "http://127.0.0.1:$MCPD_PORT/rpc" --token demo-token --raw)
FIRST="$("${CLIENT[@]}" call-tool amap.maps_weather city=苏州)"
SECOND="$("${CLIENT[@]}" call-tool amap.maps_weather city=苏州)"
THIRD="$("${CLIENT[@]}" call-tool amap.maps_weather city=杭州)"
STATS="$(curl -s "http://127.0.0.1:$MCPD_PORT/stats")"
printf '%s' "$FIRST" > "$ROOT/reports/result_cache_first.json"
printf '%s' "$SECOND" > "$ROOT/reports/result_cache_second.json"
printf '%s' "$THIRD" > "$ROOT/reports/result_cache_third.json"
printf '%s' "$STATS" > "$ROOT/reports/result_cache_stats.json"
FIRST_CACHED=$(printf '%s' "$FIRST" | grep -o '"cached":false' | wc -l | tr -d ' ')
SECOND_CACHED=$(printf '%s' "$SECOND" | grep -o '"cached":true' | wc -l | tr -d ' ')
STATS_HITS=$(printf '%s' "$STATS" | grep -o '"result_cache_hits":[0-9]*' | head -1 | cut -d: -f2)
STATS_MISSES=$(printf '%s' "$STATS" | grep -o '"result_cache_misses":[0-9]*' | head -1 | cut -d: -f2)
cat > "$ROOT/reports/result_cache_summary.json" <<JSON
{
"test": "tools/call result cache",
"first_same_call_cached_false_count": $FIRST_CACHED,
"second_same_call_cached_true_count": $SECOND_CACHED,
"stats_result_cache_hits": ${STATS_HITS:-0},
"stats_result_cache_misses": ${STATS_MISSES:-0}
}
JSON
printf '[结果缓存测试]\n'
printf 'first call cached=false count: %s\n' "$FIRST_CACHED"
printf 'second call cached=true count: %s\n' "$SECOND_CACHED"
printf 'stats result_cache_hits: %s\n' "${STATS_HITS:-0}"
printf 'stats result_cache_misses: %s\n' "${STATS_MISSES:-0}"
printf 'report: %s\n' "$ROOT/reports/result_cache_summary.json"
if [[ "$FIRST_CACHED" -lt 1 || "$SECOND_CACHED" -lt 1 || "${STATS_HITS:-0}" -lt 1 ]]; then
echo "result cache test failed" >&2
exit 1
fi