Files
OxideJS-New/benchmark/build.sh
T
pjh123 0ae3d2cb9d feat: 多引擎对比 — QuickJS/BOA/JerryScript/V8(node)/Hermes
- 新增 Engine 类抽象: 统一 run_script/run_file/run_test262 接口
- bench_timing/bench_stress: 循环对比所有可用引擎, 缺失自动 N/A
- bench_test262: OxideJS 实测 + 各引擎已知通过率
- V8(node)/Hermes 主流平台可选, 嵌入式自动跳过
- build.sh: 新增 build_boa() / build_jerryscript() 编译

(cherry picked from commit 708f0f32dc8dbf5ebc129b9dcd863e342412dc3c)
2026-06-30 12:42:57 +08:00

195 lines
8.2 KiB
Bash

#!/bin/bash
# OxideJS Benchmark Suite — 一键构建
# 兼容: Debian/Ubuntu, RHEL/Fedora, Arch, Alpine, openSUSE
set -e
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
REPO_ROOT="$(dirname "$SCRIPT_DIR")"
RED='\033[0;31m'; GREEN='\033[0;32m'; YELLOW='\033[1;33m'; CYAN='\033[0;36m'; BOLD='\033[1m'; NC='\033[0m'
info() { echo -e "${GREEN}[ ✓ ]${NC} $*"; }
warn() { echo -e "${YELLOW}[ ⚠ ]${NC} $*"; }
err() { echo -e "${RED}[ ✗ ]${NC} $*"; }
step() { echo -e "\n${BOLD}${CYAN}═══ $* ═══${NC}"; }
# ── 包管理器检测 ─────────────────────────────────────────────────────
detect_pm() {
if command -v apt-get &>/dev/null; then echo "apt" "apt-get install -y" "apt-get update -qq"
elif command -v dnf &>/dev/null; then echo "dnf" "dnf install -y" "dnf check-update 2>/dev/null || true"
elif command -v yum &>/dev/null; then echo "yum" "yum install -y" "yum check-update 2>/dev/null || true"
elif command -v pacman &>/dev/null; then echo "pacman" "pacman -S --noconfirm" "pacman -Sy --noconfirm"
elif command -v apk &>/dev/null; then echo "apk" "apk add --no-cache" "apk update"
elif command -v zypper &>/dev/null; then echo "zypper" "zypper install -y" "zypper refresh"
else echo "unknown" "" ""
fi
}
map_pkgs() {
case "$1" in
apt) echo "gcc make git python3 python3-pip curl";;
dnf|yum) echo "gcc make git python3 python3-pip curl";;
pacman) echo "gcc make git python python-pip curl";;
apk) echo "gcc make git python3 py3-pip curl";;
zypper) echo "gcc make git python3 python3-pip curl";;
*) echo "gcc make git python3 curl";;
esac
}
install_deps() {
local pm=$1 icmd=$2 ucmd=$3; shift 3
local missing=()
for pkg in "$@"; do
command -v "$pkg" &>/dev/null && continue
[ "$pm" = "apt" ] && dpkg -s "$pkg" &>/dev/null 2>&1 && continue
missing+=("$pkg")
done
[ ${#missing[@]} -eq 0 ] && return 0
info "安装: ${missing[*]}"
[ "$pm" = "pacman" ] || [ "$pm" = "apt" ] && $ucmd 2>/dev/null
$icmd "${missing[@]}" 2>&1 | tail -3
}
ensure_rust() {
step "Rust 工具链"
command -v rustc &>/dev/null && info "rustc $(rustc --version)" && return 0
info "正在安装 Rust..."
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain stable
source "$HOME/.cargo/env" 2>/dev/null || . "$HOME/.cargo/env" 2>/dev/null
}
ensure_python() {
step "Python 环境"
info "python3 $(python3 --version 2>&1)"
python3 -c "import psutil" 2>/dev/null || pip3 install --break-system-packages psutil 2>/dev/null || apt-get install -y python3-psutil 2>/dev/null || true
}
# ── QuickJS ───────────────────────────────────────────────────────────
build_quickjs() {
step "QuickJS (baseline)"
local qjs_dir="$REPO_ROOT/baseline-quickjs"
if [ ! -d "$qjs_dir" ]; then
warn "baseline-quickjs/ 不存在,跳过 (请放 QuickJS 源码到此目录)"
return 0
fi
cd "$qjs_dir"
if [ -f build/qjs ] && [ -f build/run-test262 ]; then
info "QuickJS 已构建"
return 0
fi
info "编译 QuickJS..."
make clean 2>&1 | tail -1 # 清理跨平台残留 .obj,避免 PE/ELF 混用
if make -j"$(nproc 2>/dev/null || echo 4)" all 2>&1; then
info "QuickJS 编译成功"
else
err "QuickJS 编译失败 (rc=$?)"
cd "$SCRIPT_DIR"
return 1
fi
# QuickJS Makefile 将二进制输出到源根目录,拷贝到 build/ 备用
cp -f qjs run-test262 build/ 2>/dev/null || true
cd "$SCRIPT_DIR"
info "QuickJS 完成"
}
# ── BOA ──────────────────────────────────────────────────────────────────
build_boa() {
step "BOA (baseline v0.21)"
local boa_dir="$REPO_ROOT/baseline-boa"
if [ ! -d "$boa_dir" ]; then
warn "baseline-boa/ 不存在,跳过"
return 0
fi
if [ -f "$boa_dir/target/release/boa" ]; then
info "BOA 已构建"
return 0
fi
info "编译 BOA (Rust --release, 约 5-10 分钟)..."
cd "$boa_dir"
if cargo build --release -p boa_cli --bin boa 2>&1 | tail -5; then
info "BOA 编译成功"
else
warn "BOA 编译失败,跳过 (不影响整体)"
fi
cd "$SCRIPT_DIR"
}
# ── JerryScript ──────────────────────────────────────────────────────────
build_jerryscript() {
step "JerryScript"
local jerry_dir="$REPO_ROOT/baseline-jerryscript"
if [ ! -d "$jerry_dir" ]; then
warn "baseline-jerryscript/ 不存在,跳过"
return 0
fi
if [ -f "$jerry_dir/build/bin/MinSizeRel/jerry" ] || [ -f "$jerry_dir/build/bin/MinSizeRel/jerry.exe" ]; then
info "JerryScript 已构建"
return 0
fi
info "编译 JerryScript (CMake, 约 1 分钟)..."
mkdir -p "$jerry_dir/build"
cd "$jerry_dir/build"
if command -v cmake &>/dev/null; then
cmake .. -DCMAKE_BUILD_TYPE=MinSizeRel -DJERRY_ERROR_MESSAGES=ON -DJERRY_LOGGING=OFF 2>&1 | tail -3
if make -j"$(nproc 2>/dev/null || echo 4)" 2>&1 | tail -5; then
info "JerryScript 编译成功"
else
warn "JerryScript 编译失败,跳过 (不影响整体)"
fi
else
warn "cmake 未安装,跳过 JerryScript"
fi
cd "$SCRIPT_DIR"
}
# ── OxideJS ────────────────────────────────────────────────────────────
build_oxide() {
step "OxideJS"
cd "$REPO_ROOT"
if [ -f target/release/oxide ] && [ -f target/release/test262-runner ]; then
info "OxideJS 已构建"
return 0
fi
info "编译 OxideJS (release, 约 3-6 分钟)..."
cargo build --release 2>&1 | grep -E "Compiling|Finished|error" || true
info "OxideJS 完成"
}
# ── test262 ────────────────────────────────────────────────────────────
ensure_test262() {
step "test262"
local t262="$REPO_ROOT/tests/test262/test"
if [ -d "$t262" ]; then
info "test262 已存在: $(find "$t262" -name '*.js' | wc -l) 测试"
return 0
fi
warn "test262 未找到"
if [ -f "$SCRIPT_DIR/fetch_test262.sh" ]; then
cd "$REPO_ROOT/tests" && bash "$SCRIPT_DIR/fetch_test262.sh"
fi
}
# ── Main ────────────────────────────────────────────────────────────────
echo ""
echo -e "${BOLD}${CYAN}╔══════════════════════════════════════════════════╗${NC}"
echo -e "${BOLD}${CYAN}║ OxideJS Benchmark Suite — 构建 ║${NC}"
echo -e "${BOLD}${CYAN}╚══════════════════════════════════════════════════╝${NC}"
step "检测环境"
echo " OS: $(uname -a 2>/dev/null | cut -d' ' -f1-3)"
echo " CPU: $(nproc 2>/dev/null || echo '?') cores"
read pm icmd ucmd <<< $(detect_pm)
info "包管理器: $pm"
install_deps "$pm" "$icmd" "$ucmd" $(map_pkgs "$pm")
ensure_rust
ensure_python
build_quickjs
build_boa
build_jerryscript
build_oxide
ensure_test262
echo ""
echo -e "${BOLD}${GREEN}╔══════════════════════════════════════════════════╗${NC}"
echo -e "${BOLD}${GREEN}║ 构建完成! 运行: python3 benchmark/run_benchmark.py ║${NC}"
echo -e "${BOLD}${GREEN}╚══════════════════════════════════════════════════╝${NC}"