A pure MoonBit streaming analytics library with frequency, heavy-hitter, cardinality, quantile, sliding-window, telemetry, and serialization components.
| 模块 | 用途 | 主要保证 |
|---|---|---|
| Count-Min Sketch | 频率估计 | 只高估,误差由 ε/δ 控制 |
| Count Sketch | 有符号频率估计 | 基于中位数的无偏近似 |
| Space-Saving / Misra-Gries | Top-K 重频项 | 亚线性空间、误差界 |
| Bloom / Counting Bloom | 成员判断与删除 | 无假阴性;Counting 支持删除 |
| HyperLogLog | 独立用户/API/设备数 | 固定寄存器空间、可合并 |
| T-Digest | 延迟/大小 P50/P95/P99 | 增量摘要、CDF、可合并 |
| Sliding Window / Decay | 时间窗口与衰减 | 桶轮换或指数衰减 |
| Binary Codec | CMS 快照与合并 | Magic 校验、差异比对 |
| Telemetry Monitor | API 监控组合门面 | 请求量、错误率、Top-K、基数、分位数 |
# 编译与测试
moon check --target all
moon test --target all
# 运行完整 CLI 示例
moon run cli
# 运行确定性基准
moon run benchmoon add Hhsqoo/moon-frequency-sketchlet users = @cardinality.HyperLogLog::new(12).unwrap()
users.add_str("user:alice")
users.add_str("user:bob")
println(users.estimate().to_string())
let latency = @quantile.TDigest::new(100).unwrap()
latency.add(12.5).unwrap()
latency.add(80.0).unwrap()
match latency.quantile(0.95) {
Some(p95) => println("p95=" + p95.to_string())
None => println("empty digest")
}let monitor = @telemetry.Monitor::new(20, 10, 5, 1000, 12, 100).unwrap()
monitor.observe(
@telemetry.Event::new("/api/users", "user:alice", 200, 18.0, 512L),
).unwrap()
let report = monitor.report()
println("events=" + report.total_events.to_string())
println("unique=" + report.unique_entities.to_string())moon-frequency-sketch/
├── hash/ # MurmurHash3, FNV-1a, xxHash32
├── sketch/
│ ├── cms/ # Count-Min Sketch 与合并
│ ├── countsketch/ # Count Sketch 与统计估计器
│ ├── topk/ # Space-Saving, Misra-Gries
│ ├── bloomfilter/ # Bloom 与 Counting Bloom
│ ├── cardinality/ # HyperLogLog
│ └── quantile/ # T-Digest
├── window/ # RingBuffer, SlidingWindow, Decay
├── telemetry/ # API 监控应用门面
├── codec/ # 二进制快照、合并与差异
├── bench/ # 确定性 workload 与真实计时
└── cli/ # 可运行 API 监控示例events=10000 distinct_entities=1000
measured_ms=56 throughput_events_per_sec=178571.42857142858
unique_estimate=987 relative_error=0.013
latency_p95_estimate=104.98969072164948 exact=104.0
latency_p99_estimate=108.98969072164948 exact=108.0
error_events=104 error_rate=0.0104moon version --all
moon update
moon fmt
moon info
moon check --target all
moon test --target all
moon build --target all
moon run cli
moon run benchA pure MoonBit streaming analytics library with frequency, heavy-hitter, cardinality, quantile, sliding-window, telemetry, and serialization components.