# Jmh Bench

> 该skill没有执行文件，为操作指引：运行JMH基准测试的标准流程

- **Type:** Skill
- **Install:** `agentstack add skill-hellyguo-self-ai-spec-jmh-bench`
- **Verified:** Yes — security-reviewed for prompt injection and unsafe behavior
- **Seller:** [hellyguo](https://agentstack.voostack.com/s/hellyguo)
- **Installs:** 0
- **Category:** [Agent Skills](https://agentstack.voostack.com/c/agent-skills)
- **Latest version:** 0.1.0
- **License:** Apache-2.0
- **Upstream author:** [hellyguo](https://github.com/hellyguo)
- **Source:** https://github.com/hellyguo/self-ai-spec/tree/main/skills/jmh-bench

## Install

```sh
agentstack add skill-hellyguo-self-ai-spec-jmh-bench
```

Requires the [AgentStack CLI](https://agentstack.voostack.com/docs/cli). Works with Claude Code, Cursor, and any MCP-compatible agent.

## About

# JMH 基准测试执行指南

## 核心要求

1. 测试默认参数：

- 预热10次
- 测试5次
- 每次预热执行100ms
- 每次测试执行20ms
- 4线程执行
- fork 3次

2. 测试首推用mvnd

## 前置准备

### 1. 获取环境信息

执行 skill `/java-env` 获取 Java 开发环境信息：

```
skill java-env
```

确认：
- mvnd/maven 目录位置
- JDK 版本匹配项目需求

### 2. 配置 pom.xml

在项目中添加 JMH 依赖：

```xml

    
    
        org.openjdk.jmh
        jmh-core
        1.37
    
    
    
    
        org.openjdk.jmh
        jmh-generator-annprocess
        1.37
        provided
    

    
        
        
            org.apache.maven.plugins
            maven-compiler-plugin
            3.11.0
            
                
                    
                        org.openjdk.jmh
                        jmh-generator-annprocess
                        1.37
                    
                
            
        
    

```

## 执行方式

### 方式一：Maven Exec 插件（推荐）

```bash
# 编译项目
mvn clean compile -q

# 运行全部基准测试
mvn exec:exec -Dexec.executable="java" \
    -Dexec.args="-cp %classpath org.openjdk.jmh.Main" -q

# 运行指定基准测试类
mvn exec:exec -Dexec.executable="java" \
    -Dexec.args="-cp %classpath org.openjdk.jmh.Main BenchmarkClassName" -q

# 运行指定基准测试（带参数）
mvn exec:exec -Dexec.executable="java" \
    -Dexec.args="-cp %classpath org.openjdk.jmh.Main BenchmarkClassName -wi 3 -i 3 -t 1 -f 1" -q
```

**参数说明：**
- `-wi N`: Warmup 迭代次数
- `-i N`: Measurement 迭代次数  
- `-t N`: 线程数
- `-f N`: Fork 次数

### 方式二：run_bench.sh 脚本

创建脚本 `run_bench.sh`：

```bash
#!/bin/bash

# Run JMH benchmark
# Usage: ./run_bench.sh [BenchmarkClass] [jmh_args...]

PROJECT_DIR=$(dirname "$0")
cd "$PROJECT_DIR"

BENCHMARK_CLASS="${1:-}"
JMH_ARGS="${2:-}"

# Build classpath
CLASSPATH="target/classes:target/test-classes:$(mvn dependency:build-classpath -Dmdep.outputFile=/dev/stdout -q)"

# Run JMH
if [ -n "$BENCHMARK_CLASS" ]; then
    java -cp "$CLASSPATH" org.openjdk.jmh.Main "$BENCHMARK_CLASS" $JMH_ARGS
else
    java -cp "$CLASSPATH" org.openjdk.jmh.Main
fi
```

使用方式：
```bash
chmod +x run_bench.sh

# 运行全部
./run_bench.sh

# 运行指定类
./run_bench.sh MyBenchmark

# 运行指定类带参数
./run_bench.sh MyBenchmark "-wi 5 -i 5"
```

## 常用命令模板

| 场景 | 命令 |
|------|------|
| 快速验证 | `-wi 3 -i 3 -t 1 -f 1` |
| 标准测试 | `-wi 5 -i 5 -t 4 -f 2` |
| 精确测试 | `-wi 10 -i 10 -t 1 -f 3` |
| 列出所有Benchmark | `-l` |

## 输出解读

典型输出示例：
```
Benchmark                         Mode  Cnt    Score    Error  Units
MyBenchmark.testMethod           thrpt    5  123456.789 ± 5000  ops/s
```

**指标说明：**
- `thrpt`: Throughput 吞吐量（ops/s）
- `avgt`: Average Time 平均耗时（us/op）
- `sample`: Sampling 时间采样
- `Score`: 平均值
- `Error`: 置信区间误差（99.9%）

## 最佳实践

1. **Warmup 充足** - 至少 5 次预热，让 JIT 编译稳定
2. **避免 GC 影响** - 测试前可手动触发 GC：`System.gc()`
3. **Blackhole 消费** - 防止死代码消除：`bh.consume(result)`
4. **State 隔离** - 使用 `@State(Scope.Thread)` 避免线程竞争
5. **单线程对比** - 多框架对比时先用 `-t 1` 消除并发因素

## Source & license

This open-source skill is cataloged on AgentStack and links to its original source — we do not rehost the code.

- **Author:** [hellyguo](https://github.com/hellyguo)
- **Source:** [hellyguo/self-ai-spec](https://github.com/hellyguo/self-ai-spec)
- **License:** Apache-2.0

Install and usage instructions live in the source repository linked above.

## Pricing

- **Free** — Free

## Security capabilities

Automated source analysis of v0.1.0 — what this tool can access:

- **Network access:** no
- **Filesystem access:** no
- **Shell / process execution:** no
- **Environment & secrets:** no
- **Dynamic code execution:** no

*"Yes" means the capability is present in the source — more access means more to trust, not that it is unsafe.*


## Versions

- **0.1.0** — security scan: passed — Imported from the upstream source.

## Links

- Listing page: https://agentstack.voostack.com/l/skill-hellyguo-self-ai-spec-jmh-bench
- Seller: https://agentstack.voostack.com/s/hellyguo
- Browse the marketplace: https://agentstack.voostack.com/browse

---
Listed on AgentStack — the marketplace for AI agent skills and MCP servers. Every listing is security-reviewed. Creators keep 70%.
