Skip to content

基准测试 (Profiler)

源:Gradle Profiler | Gradle 官方文档 - Performance

性能测试工具帮助你量化构建性能,识别瓶颈并验证优化效果。

Gradle Profile 报告

生成 Profile 报告

命令

bash
./gradlew build --profile

报告位置

build/reports/profile/profile-<timestamp>.html

报告内容

Configuration Summary

  • 配置阶段总时间
  • 各项目配置时间
  • 脚本编译时间

Task Execution

  • 任务执行总时间
  • 各任务耗时排序
  • 任务状态(执行/跳过/缓存)

Dependency Resolution

  • 依赖解析时间
  • 依赖下载时间
  • 仓库访问统计

Gradle Profiler

什么是 Gradle Profiler

Gradle Profiler 是官方提供的基准测试工具,用于精确测量构建性能。

核心功能

  • 预热构建(消除 JVM 预热影响)
  • 多次迭代测试
  • 统计分析(平均值、中位数、标准差)
  • 场景对比

安装

使用 SDKMAN

bash
sdk install gradleprofiler

使用 Homebrew

bash
brew install gradle-profiler

手动下载

bash
# 下载最新版本
wget https://repo.gradle.org/gradle/libs-releases-local/org/gradle/profiler/gradle-profiler/0.21.0/gradle-profiler-0.21.0.zip
unzip gradle-profiler-0.21.0.zip
export PATH=$PATH:gradle-profiler-0.21.0/bin

基本用法

简单基准测试

bash
gradle-profiler --benchmark assembleDebug

输出示例

Benchmark: assembleDebug
  Warm-ups: 6
  Builds: 10

Results:
  Mean: 8.234s
  Median: 8.156s
  Min: 7.982s
  Max: 8.678s
  Std Dev: 0.234s

场景文件

创建 performance.scenario

groovy
// 场景1:清理构建
assemble {
    tasks = ["assembleDebug"]
    cleanup = true
}

// 场景2:增量构建
incrementalAssemble {
    tasks = ["assembleDebug"]
    apply-non-abi-change-to = "src/main/java/MainActivity.kt"
}

// 场景3:配置缓存
configCacheAssemble {
    tasks = ["assembleDebug"]
    gradle-args = ["--configuration-cache"]
}

运行场景

bash
gradle-profiler --benchmark --scenario-file performance.scenario

高级功能

预热构建

作用:消除 JVM 预热和文件系统缓存的影响。

配置

groovy
assemble {
    tasks = ["assembleDebug"]
    warm-ups = 10  // 10次预热
    iterations = 20  // 20次测试
}

场景对比

对比配置缓存效果

groovy
// 不使用配置缓存
withoutConfigCache {
    tasks = ["assembleDebug"]
}

// 使用配置缓存
withConfigCache {
    tasks = ["assembleDebug"]
    gradle-args = ["--configuration-cache"]
}

运行

bash
gradle-profiler --benchmark --scenario-file compare.scenario

输出

Scenario: withoutConfigCache
  Mean: 15.234s

Scenario: withConfigCache
  Mean: 8.456s
  
Improvement: 44.5%

修改文件测试

测试增量构建

groovy
incrementalBuild {
    tasks = ["assembleDebug"]
    
    // 在每次迭代后修改文件
    apply-abi-change-to = "src/main/java/domain/User.kt"
}

nonAbiChange {
    tasks = ["assembleDebug"]
    
    // 非 ABI 变更(不影响接口)
    apply-non-abi-change-to = "src/main/java/MainActivity.kt"
}

Git 操作

测试分支切换

groovy
branchSwitch {
    tasks = ["assembleDebug"]
    
    // 每次迭代切换分支
    git-checkout = {
        cleanup = "main"
        build = "feature"
    }
}

性能分析

CPU 火焰图

生成火焰图

bash
gradle-profiler --profile jfr assembleDebug

输出

  • JFR 文件(Java Flight Recorder)
  • 可用 JDK Mission Control 打开
  • 查看 CPU 热点

堆分析

bash
gradle-profiler --profile heap assembleDebug

输出

  • 堆转储文件
  • 可用 VisualVM 或 MAT 分析
  • 识别内存泄漏

Android 专用场景

Clean Build

groovy
cleanBuild {
    tasks = ["clean", "assembleDebug"]
    cleanup = true
}

Incremental Build

groovy
incrementalBuild {
    tasks = ["assembleDebug"]
    apply-non-abi-change-to = "src/main/java/com/example/MainActivity.kt"
}

配置变更

groovy
versionChange {
    tasks = ["assembleDebug"]
    
    // 修改版本号
    apply-build-script-change-to = "build.gradle.kts" {
        change = {
            version from "1.0.0" to "1.0.1"
        }
    }
}

Build Analyzer

Android Studio Build Analyzer

打开 Build Analyzer

ViewTool WindowsBuild Analyzer

显示内容

  • 总构建时间
  • 任务执行时间
  • 插件耗时
  • 优化建议

分析报告

任务时间线

  • 查看任务执行顺序
  • 识别串行任务
  • 发现并行机会

插件分析

  • 各插件配置时间
  • 插件冲突检测
  • 升级建议

优化建议

  • 启用配置缓存
  • 启用构建缓存
  • 使用 KSP 替代 kapt
  • 增加内存分配

性能优化验证

验证优化效果

场景:验证 KSP 效果

groovy
withKapt {
    tasks = ["assembleDebug"]
    // 使用 kapt
}

withKsp {
    tasks = ["assembleDebug"]
    // 使用 KSP
    gradle-args = ["-Puse.ksp=true"]
}

运行

bash
gradle-profiler --benchmark --scenario-file kapt-vs-ksp.scenario

结果对比

withKapt:  Mean: 25.3s
withKsp:   Mean: 12.8s
Improvement: 49.4%

验证配置缓存

groovy
baseline {
    tasks = ["assembleDebug"]
}

withConfigCache {
    tasks = ["assembleDebug"]
    gradle-args = ["--configuration-cache"]
}

验证并行构建

groovy
serial {
    tasks = ["assembleDebug"]
    gradle-args = ["--no-parallel"]
}

parallel {
    tasks = ["assembleDebug"]
    gradle-args = ["--parallel"]
}

实战案例

案例1:识别最慢任务

步骤

  1. 生成 profile 报告
bash
./gradlew assembleDebug --profile
  1. 打开报告查看 Task Execution

  2. 识别耗时最长的任务

:app:compileDebugKotlin - 8.5s
:app:processDebugResources - 3.2s
:app:mergeDebugResources - 2.1s
  1. 优化最慢任务

案例2:测试 JVM 参数优化

groovy
baseline {
    tasks = ["assembleDebug"]
}

moreMemory {
    tasks = ["assembleDebug"]
    gradle-args = ["-Dorg.gradle.jvmargs=-Xmx6g"]
}

parallelGC {
    tasks = ["assembleDebug"]
    gradle-args = ["-Dorg.gradle.jvmargs=-Xmx4g -XX:+UseParallelGC"]
}

运行

bash
gradle-profiler --benchmark --scenario-file jvm-tuning.scenario

案例3:CI 性能回归检测

GitHub Actions

yaml
name: Performance Test

on: [pull_request]

jobs:
  performance:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      
      - name: Setup Gradle
        uses: gradle/gradle-build-action@v2
      
      - name: Install Profiler
        run: sdk install gradleprofiler
      
      - name: Run Benchmark
        run: gradle-profiler --benchmark --scenario-file ci.scenario
      
      - name: Check Regression
        run: |
          # 对比基线,如果慢于5%则失败
          ./check-performance-regression.sh 5

最佳实践

定期性能测试

  • 每个 Sprint 进行基准测试
  • 记录性能基线
  • 追踪性能趋势

验证优化效果

  • 优化前后对比
  • 使用 Gradle Profiler
  • 统计显著性分析

场景覆盖

  • Clean build
  • Incremental build
  • No-op build
  • 配置变更

环境一致性

  • 使用相同机器
  • 关闭后台应用
  • 预热文件系统缓存

自动化测试

  • CI 集成
  • 性能回归检测
  • 自动报告生成

分析工具组合

  • Profile报告:快速识别
  • Gradle Profiler:精确测量
  • Build Analyzer:优化建议
  • Build Scan:深度分析