AgentSkillsCN

test-performance

性能测试与基准测试——使用BenchmarkDotNet进行微基准测试、K6进行负载/压力/峰值测试、性能回归追踪以及报告生成。这些工具可用于性能的测量与优化。关键词:性能测试、基准测试、BenchmarkDotNet、K6、负载测试、压力测试、性能优化、回归测试、DTT、技能、概览、DRN、测试

SKILL.md
--- frontmatter
name: test-performance
description: Performance testing and benchmarking - BenchmarkDotNet for micro-benchmarks, K6 for load/stress/spike testing, performance regression tracking, and report generation. Use for measuring and optimizing performance. Keywords: performance-testing, benchmarking, benchmarkdotnet, k6, load-testing, stress-testing, performance-optimization, regression-testing, dtt, skills, overview, drn, testing

DRN.Test.Performance

Performance testing with BenchmarkDotNet and K6 load testing.

When to Apply

  • Benchmarking code performance
  • Load testing APIs
  • Measuring performance regressions
  • Generating performance reports

Project Structure

code
DRN.Test.Performance/
├── Benchmark/          # BenchmarkDotNet benchmarks
│   ├── Framework/      # Framework benchmarks
│   └── Sample/         # Application benchmarks
├── K6/                 # K6 load test scripts
│   ├── scripts/        # Test scripts
│   └── config/         # K6 configuration
├── Reports/            # Generated reports
│   ├── BenchmarkDotNet.Artifacts/
│   └── K6Results/
├── Sketch.cs           # Experimental benchmarks
├── Todo.cs             # Planned benchmarks
└── xunit.runner.json

BenchmarkDotNet

Basic Benchmark

csharp
[MemoryDiagnoser]
public class MyBenchmark
{
    private readonly List<int> _data = Enumerable.Range(0, 1000).ToList();
    
    [Benchmark(Baseline = true)]
    public int ForLoop()
    {
        var sum = 0;
        for (var i = 0; i < _data.Count; i++)
            sum += _data[i];
        return sum;
    }
    
    [Benchmark]
    public int LinqSum() => _data.Sum();
    
    [Benchmark]
    public int Foreach()
    {
        var sum = 0;
        foreach (var item in _data)
            sum += item;
        return sum;
    }
}

Running Benchmarks

bash
# Run all benchmarks
dotnet run -c Release --project DRN.Test.Performance

# Run specific benchmark
dotnet run -c Release --project DRN.Test.Performance -- --filter "*MyBenchmark*"

Benchmark Attributes

AttributePurpose
[Benchmark]Mark method as benchmark
[Benchmark(Baseline = true)]Set baseline for comparison
[MemoryDiagnoser]Include memory allocation metrics
[GlobalSetup]One-time setup before all iterations
[IterationSetup]Setup before each iteration
[Params(1, 10, 100)]Parameterized benchmarks

K6 Load Testing

Basic K6 Script

javascript
// k6/scripts/api-load-test.js
import http from 'k6/http';
import { check, sleep } from 'k6';

export const options = {
    vus: 10,           // Virtual users
    duration: '30s',   // Test duration
    thresholds: {
        http_req_failed: ['rate<0.01'],       // <1% failures
        http_req_duration: ['p(95)<500'],     // 95th percentile under 500ms
    },
};

export default function () {
    const res = http.get('http://localhost:5988/api/status');
    
    check(res, {
        'is status 200': (r) => r.status === 200,
        'response time < 200ms': (r) => r.timings.duration < 200,
    });
    
    sleep(1);
}

Running K6

bash
# Run load test
k6 run k6/scripts/api-load-test.js

# Run with custom VUs and duration
k6 run --vus 50 --duration 1m k6/scripts/api-load-test.js

# Output to JSON
k6 run --out json=results.json k6/scripts/api-load-test.js

K6 Test Types

TypePurposeConfiguration
Load TestNormal loadvus: 10, duration: '5m'
Stress TestBeyond capacityRamp up VUs until failure
Spike TestSudden loadQuick ramp to high VUs
Soak TestExtended durationLow VUs, long duration

Reports

BenchmarkDotNet Output

Reports generated in Reports/BenchmarkDotNet.Artifacts/:

  • HTML reports
  • Markdown reports
  • JSON results

K6 Output

bash
# HTML report (with k6-reporter)
k6 run --out json=report.json script.js

# Grafana + InfluxDB (real-time)
k6 run --out influxdb=http://localhost:8086/k6 script.js

xunit.runner.json

json
{
  "parallelizeAssembly": false,
  "parallelizeTestCollections": false
}

Performance tests should not run in parallel.


Best Practices

PracticeReason
Run in Release modeAccurate performance numbers
Warm-up iterationsJIT compilation effects
Multiple iterationsStatistical significance
Isolate testsNo parallel execution
Document baselineTrack regressions

Related Skills