Skip to content

doctor: advise $EPOCHREALTIME over external $(date) for timing scripts - #241

Merged
caomengxuan666 merged 1 commit into
masterfrom
perf/epochrealtime
Oct 10, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
perf/epochrealtime

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

背景

$(date +%s%N) 每次调用要创建外部 date.exe 进程(本机冷启动实测首次 ~20ms/次),而 bash 5 内建 $EPOCHREALTIME / $EPOCHSECONDS 微秒级展开。计时脚本应使用内建变量。

实测结论(本 PR 依赖)

niu(rubash 引擎)已实现两个变量,格式与 GNU bash 5 一致,无需引擎改动:

  • $EPOCHREALTIME = 1699999999.123456(整数秒.6 位微秒),动态更新、单调递增
  • $EPOCHSECONDS = 纯整数秒,与外部 date +%s 秒级一致(diff=0)

因此本 PR 只做产品侧收尾:文档提示 + 回归测试钉住契约。

改动

  1. niu doctor 新增 timing 提示行:「计时脚本用 $EPOCHREALTIME 代替 $(date +%s%N)」,说明各自量级;
  2. tests/issue201_epochrealtime.rs(4 个测试,全过):
    • 格式断言(RT 正则 ^\d+\.\d{6}$、EPOCHSECONDS 纯整数、整数部分相等);
    • 与 date +%s 及宿主时钟的秒级一致性;
    • live/单调性(sleep 50ms 后 ${EPOCHREALTIME/.} 差值落在微秒区间);
    • 性能差断言(40 次循环,$(date +%s%N) vs $EPOCHREALTIME ≥50x,CI 容忍抖动);
  3. CHANGELOG Unreleased/Added 条目。

基准(本工作树 debug 构建,300 次循环,${EPOCHREALTIME/.} 计时)

语句 300 次总耗时 单次
x=$(true) 992µs 3.3µs
x=$(echo abc) 2268µs 7.6µs
x=$(date +%s%N) 27522µs(另见冷启动首组 50 次 ≈999ms,≈20ms/次) 稳态 ≈92µs
x=$EPOCHREALTIME 1123µs 3.7µs

稳态约 25x;冷启动( Defender/首次进程创建,即 issue 复现的 31ms/次场景)差距达数百倍。微秒精度验证:sleep 0.05 前后差值 ≈50000µs。

验证

  • cargo test --test issue201_epochrealtime → 4 passed
  • cargo fmt -- --check 通过
  • niu doctor 输出新行正常

closes #201

#201)

$(date +%s%N) spawns date.exe per call (~10-30ms on Windows), while the
bash 5 builtins $EPOCHREALTIME / $EPOCHSECONDS expand in microseconds
with GNU-compatible format (1699999999.123456). rubash already
implements both; add a niu doctor advisory row, regression tests pinning
format / date +%s agreement / live monotonic updates / the speed gap,
and a CHANGELOG entry.

closes #201
@caomengxuan666
caomengxuan666 merged commit ccbcdfc into master Oct 10, 2026
3 of 9 checks passed
@caomengxuan666
caomengxuan666 deleted the perf/epochrealtime branch October 10, 2026 03:29
caomengxuan666 added a commit that referenced this pull request Oct 10, 2026
…notes draft (#254)

- Version 1.3.4 -> 1.4.0 (workspace root + crates/niubash-runtime;
  Cargo.lock synced; internal niubash-runtime dep requirement aligned
  1.2.5 -> 1.4.0). The rubash engine requirement stays 1.3.4: it is
  semver-open (^1.3.4 admits the engine's 1.4.0), and the committed
  [patch] resolves it to the local ../rubash worktree, whose Cargo.toml
  still says 1.3.4 until the engine cuts its own release.
- CHANGELOG: the Unreleased section becomes [1.4.0] - 2026-10-10. Also
  repairs the committed merge damage from #243 (stray conflict marker
  and the #241 doctor-advice entry glued onto the carapace entry) and
  folds in the merged cycle: #235 #239 #246 #247 #250 #251 #252 #253
  plus a compact rubash 1.4.0 engine summary. #240's P2-batch content
  stays under [1.3.4] where the file already records it (no dup).
- docs/release-notes-1.4.0-draft.md: zh+en draft release notes
  (Added / Fixed / Performance), winuxcmd #1151/#1154-#1157 marked as
  in-flight for the coordinator, plus the local verification log:
  release build PASS (niu.exe 10.6 MB), MSIX packed unsigned with a
  placeholder identity (5.1 MB), wpm-source main carries all four
  preinstall packages, and cargo publish --dry-run chain findings
  (rubash -> niubash-runtime -> niubash, blocked on the engine's
  crates.io release).

No tag, no release workflow trigger, no in-flight PRs merged.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[perf] $(date) 每次调用 ≈31ms(外部 date.exe 进程创建),计时脚本应使用 $EPOCHREALTIME(≈61µs)

1 participant