bench/xlang/peers.mbt1///|2/// A command to run: `program arguments...` in `cwd`.3priv struct Step {4 program : String5 arguments : Array[String]6 cwd : String7}89///|10/// One harness: an implementation of the benchmark cases in some language.11priv struct Peer {12 /// Names the harness in `--impl` and in the report's column headers.13 name : String14 /// Run in order before any timing: builds, and probes for the runtimes that15 /// `run` needs. A failing step skips the peer.16 setup : Array[Step]17 /// Prints result rows; the inputs directory and the group are appended.18 run : Step19 /// `(label, command)`: a shell command whose first line of output is the20 /// version of what the label names, for the record of a run.21 versions : Array[(String, String)]22}2324///|25/// The groups of cases a harness can be asked for.26let groups : Array[String] = ["norm", "seg", "bidi", "idna", "puny", "case"]2728///|29/// Succeeds when `program` can be started.30fn probe(root : String, program : String) -> Step {31 { program, arguments: ["--version"], cwd: root, }32}3334///|35/// The same MoonBit harness, built for one backend. `moon run` starts the JS36/// build with Node and the Wasm builds with `moonrun`.37fn moonbit_peer(root : String, target : String) -> Peer {38 let package_ = "bench/xlang/moonbit"39 let build = {40 program: "moon",41 arguments: ["build", "--target", target, "--release", package_],42 cwd: root,43 }44 {45 name: "moonbit-\{target}",46 setup: if target == "js" {47 [probe(root, "node"), build]48 } else {49 [build]50 },51 run: {52 program: "moon",53 arguments: ["run", "--target", target, "--release", package_, "--"],54 cwd: root,55 },56 versions: if target == "js" {57 [("moon", "moon version"), ("Node", NodeVersion)]58 } else {59 [("moon", "moon version")]60 },61 }62}6364///|65/// Node's version and the version of the ICU4C it bundles.66const NodeVersion : String = "node -p 'process.version + \" (ICU \" + process.versions.icu + \")\"'"6768///|69/// Where ICU4C is installed, when it is not on the compiler's default paths.70/// Homebrew's `icu4c` is keg-only, so look there after `ICU_ROOT`.71async fn icu_prefix() -> String? {72 if @env.get_env_var("ICU_ROOT") is Some(prefix) {73 return Some(prefix)74 }75 for prefix in ["/opt/homebrew/opt/icu4c", "/usr/local/opt/icu4c"] {76 if @fs.exists("\{prefix}/include/unicode/unistr.h") {77 return Some(prefix)78 }79 }80 None81}8283///|84/// Every harness, in report order. Build products go to `work`, outside the85/// source tree.86async fn all_peers(root : String, work : String) -> Array[Peer] {87 let source = "\{root}/bench/xlang"88 let icu_prefix = icu_prefix()89 let icu_paths = if icu_prefix is Some(prefix) {90 ["-I\{prefix}/include", "-L\{prefix}/lib", "-Wl,-rpath,\{prefix}/lib"]91 } else {92 []93 }94 [95 moonbit_peer(root, "native"),96 moonbit_peer(root, "wasm-gc"),97 moonbit_peer(root, "wasm"),98 moonbit_peer(root, "js"),99 {100 name: "icu4c",101 setup: [102 {103 program: @env.get_env_var("CXX").unwrap_or("c++"),104 arguments: [105 "-O2",106 "-std=c++17",107 "main.cpp",108 ..icu_paths,109 "-licuuc",110 "-licui18n",111 "-o",112 "\{work}/icu4c",113 ],114 cwd: "\{source}/icu4c",115 },116 ],117 run: { program: "\{work}/icu4c", arguments: [], cwd: root, },118 versions: [119 (120 "ICU4C",121 if icu_prefix is Some(prefix) {122 "PKG_CONFIG_PATH=\{prefix}/lib/pkgconfig pkg-config --modversion icu-uc"123 } else {124 "pkg-config --modversion icu-uc"125 },126 ),127 ],128 },129 {130 name: "rust",131 setup: [132 {133 program: "cargo",134 arguments: [135 "build",136 "--release",137 "--locked",138 "--target-dir",139 "\{work}/rust",140 ],141 cwd: "\{source}/rust",142 },143 ],144 run: { program: "\{work}/rust/release/xlang", arguments: [], cwd: root, },145 versions: [146 ("rustc", "rustc --version"),147 (148 "ICU4X",149 "sed -n '/^name = \"icu_normalizer\"$/{n;s/^version = \"\\(.*\\)\"$/\\1/p;}' \{source}/rust/Cargo.lock",150 ),151 ],152 },153 {154 name: "go",155 setup: [156 {157 program: "go",158 arguments: ["build", "-o", "\{work}/go", "."],159 cwd: "\{source}/go",160 },161 ],162 run: { program: "\{work}/go", arguments: [], cwd: root, },163 versions: [("Go", "go env GOVERSION")],164 },165 {166 name: "node",167 setup: [probe(root, "node")],168 run: {169 program: "node",170 // The bundled punycode module is deprecated but still what Node ships.171 arguments: ["--no-deprecation", "\{source}/node/main.cjs"],172 cwd: root,173 },174 versions: [("Node", NodeVersion)],175 },176 {177 name: "python",178 setup: [probe(root, "python3")],179 run: {180 program: "python3",181 arguments: ["\{source}/python/main.py"],182 cwd: root,183 },184 versions: [("Python", "python3 --version")],185 },186 ]187}188
///|
/// A command to run: `program arguments...` in `cwd`.
priv struct Step {
program : String
arguments : Array[String]
cwd : String
}
///|
/// One harness: an implementation of the benchmark cases in some language.
priv struct Peer {
/// Names the harness in `--impl` and in the report's column headers.
name : String
/// Run in order before any timing: builds, and probes for the runtimes that
/// `run` needs. A failing step skips the peer.
setup : Array[Step]
/// Prints result rows; the inputs directory and the group are appended.
run : Step
/// `(label, command)`: a shell command whose first line of output is the
/// version of what the label names, for the record of a run.
versions : Array[(String, String)]
}
///|
/// The groups of cases a harness can be asked for.
let groups : Array[String] = ["norm", "seg", "bidi", "idna", "puny", "case"]
///|
/// Succeeds when `program` can be started.
fn probe(root : String, program : String) -> Step {
{ program, arguments: ["--version"], cwd: root, }
}
///|
/// The same MoonBit harness, built for one backend. `moon run` starts the JS
/// build with Node and the Wasm builds with `moonrun`.
fn moonbit_peer(root : String, target : String) -> Peer {
let package_ = "bench/xlang/moonbit"
let build = {
program: "moon",
arguments: ["build", "--target", target, "--release", package_],
cwd: root,
}
{
name: "moonbit-\{target}",
setup: if target == "js" {
[probe(root, "node"), build]
} else {
[build]
},
run: {
program: "moon",
arguments: ["run", "--target", target, "--release", package_, "--"],
cwd: root,
},
versions: if target == "js" {
[("moon", "moon version"), ("Node", NodeVersion)]
} else {
[("moon", "moon version")]
},
}
}
///|
/// Node's version and the version of the ICU4C it bundles.
const NodeVersion : String = "node -p 'process.version + \" (ICU \" + process.versions.icu + \")\"'"
///|
/// Where ICU4C is installed, when it is not on the compiler's default paths.
/// Homebrew's `icu4c` is keg-only, so look there after `ICU_ROOT`.
async fn icu_prefix() -> String? {
if @env.get_env_var("ICU_ROOT") is Some(prefix) {
return Some(prefix)
}
for prefix in ["/opt/homebrew/opt/icu4c", "/usr/local/opt/icu4c"] {
if @fs.exists("\{prefix}/include/unicode/unistr.h") {
return Some(prefix)
}
}
None
}
///|
/// Every harness, in report order. Build products go to `work`, outside the
/// source tree.
async fn all_peers(root : String, work : String) -> Array[Peer] {
let source = "\{root}/bench/xlang"
let icu_prefix = icu_prefix()
let icu_paths = if icu_prefix is Some(prefix) {
["-I\{prefix}/include", "-L\{prefix}/lib", "-Wl,-rpath,\{prefix}/lib"]
} else {
[]
}
[
moonbit_peer(root, "native"),
moonbit_peer(root, "wasm-gc"),
moonbit_peer(root, "wasm"),
moonbit_peer(root, "js"),
{
name: "icu4c",
setup: [
{
program: @env.get_env_var("CXX").unwrap_or("c++"),
arguments: [
"-O2",
"-std=c++17",
"main.cpp",
..icu_paths,
"-licuuc",
"-licui18n",
"-o",
"\{work}/icu4c",
],
cwd: "\{source}/icu4c",
},
],
run: { program: "\{work}/icu4c", arguments: [], cwd: root, },
versions: [
(
"ICU4C",
if icu_prefix is Some(prefix) {
"PKG_CONFIG_PATH=\{prefix}/lib/pkgconfig pkg-config --modversion icu-uc"
} else {
"pkg-config --modversion icu-uc"
},
),
],
},
{
name: "rust",
setup: [
{
program: "cargo",
arguments: [
"build",
"--release",
"--locked",
"--target-dir",
"\{work}/rust",
],
cwd: "\{source}/rust",
},
],
run: { program: "\{work}/rust/release/xlang", arguments: [], cwd: root, },
versions: [
("rustc", "rustc --version"),
(
"ICU4X",
"sed -n '/^name = \"icu_normalizer\"$/{n;s/^version = \"\\(.*\\)\"$/\\1/p;}' \{source}/rust/Cargo.lock",
),
],
},
{
name: "go",
setup: [
{
program: "go",
arguments: ["build", "-o", "\{work}/go", "."],
cwd: "\{source}/go",
},
],
run: { program: "\{work}/go", arguments: [], cwd: root, },
versions: [("Go", "go env GOVERSION")],
},
{
name: "node",
setup: [probe(root, "node")],
run: {
program: "node",
// The bundled punycode module is deprecated but still what Node ships.
arguments: ["--no-deprecation", "\{source}/node/main.cjs"],
cwd: root,
},
versions: [("Node", NodeVersion)],
},
{
name: "python",
setup: [probe(root, "python3")],
run: {
program: "python3",
arguments: ["\{source}/python/main.py"],
cwd: root,
},
versions: [("Python", "python3 --version")],
},
]
}