โปรไฟล์พื้นฐาน & การดำเนินการ GitHub
คำแนะนำที่ง่ายที่สุดในการสร้างกฎโดยอัตโนมัติ
บทความนี้มีวัตถุประสงค์เพื่อพูดคุยเกี่ยวกับ Macrobenchmark, Baseline Profiles และ GitHub Actions จำเป็นต้องมีความรู้พื้นฐานของวิชาเหล่านี้ล่วงหน้า โปรดทราบว่าบทเรียนที่ได้เรียนรู้ที่นี่เกี่ยวกับ GitHub Actions สามารถย้ายไปยังโครงการประเภทอื่นได้
                    ในช่วงหลายปีที่ผ่านมา Google ได้ดำเนินการอัปเดตมากมายเกี่ยวกับการปรับปรุงประสิทธิภาพ Baseline Profiles เป็นหนึ่งในเครื่องมือล่าสุดที่ออกโดยบริษัท และพวกเขาอ้างว่าสามารถปรับปรุงเวลาเริ่มต้นของแอพ Android ได้ถึง 40% หากคุณเคยลองใช้ Baseline Profiles คุณอาจทราบดีว่าการสร้างกฎ แม้จะใช้ไลบรารี Macrobenchmark ก็อาจเจ็บปวดมาก บทความนี้แสดงวิธีง่ายๆ ในการสร้างกฎเหล่านี้โดยอัตโนมัติ ไม่ว่าโปรเจ็กต์ของคุณจะมีขนาดเท่าใด ตราบใดที่ยังเป็นเวอร์ชันบน GitHub
ไม่ว่าจะเลือกเริ่มโครงการใหม่หรือไม่ก็ตาม คุณควรตั้งค่าcompileSdk 33หรือสูงกว่านั้นและรองรับ Android Gradle Plugin 7.3.0 หรือสูงกว่า
หมายเหตุ: สามารถใช้ Gradle Kotlin DSL ของสคริปต์ต่อไปนี้ได้ที่นี่
/**
*
*  :app Gradle file
*
**/
android {
  compileSdk 33
}
/**
*
*  Top-level Gradle file
*
**/
plugins {
  id 'com.android.application' version '7.3.0' apply false
  id 'com.android.library' version '7.3.0' apply false
  id 'com.android.test' version '7.3.0' apply false
}
     
    #2 — เพิ่มไลบรารี Macrobenchmark ในโครงการของคุณ
                    #3 — สร้างbenchmark-rules.proไฟล์ภายใต้appไดเร็กทอรี เพื่อให้แน่ใจว่า Proguard จะไม่สร้างความสับสนให้กับรุ่นมาตรฐานและตั้งค่าเป็นไฟล์ Proguard ในbenchmarkประเภทรุ่น
# Benchmark-rules.pro
# Disables obfuscation for benchmark builds.
-dontobfuscate
/**
*
* :app Gradle file
*
**/
android {
  buildTypes {
        benchmark {
            signingConfig signingConfigs.debug
            proguardFiles("benchmark-rules.pro")
            matchingFallbacks = ['release']
            debuggable false
        }
    }
}
     
    
    
import com.android.build.api.dsl.ManagedVirtualDevice
/**
*
*  :benchmark Gradle file
*
**/
android {
  testOptions {
    managedDevices {
      devices {
        pixel2Api31(ManagedVirtualDevice) {
          device = "Pixel 2"
          apiLevel = 31
          systemImageSource = "aosp"
        }
      }
    }
  }
}
     
    
    /**
*
*  :benchmark module
*
**/
package com.example.benchmark
import androidx.benchmark.macro.ExperimentalBaselineProfilesApi
import androidx.benchmark.macro.junit4.BaselineProfileRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
/**
*
*  ExperimentalBaselineProfilesApi is required so that
*  macrobenchmark tests can be skipped when running 
*  the :benchmark:pixel2Api31BenchmarkAndroidTest task with
*  androidx.benchmark.enabledRules=BaselineProfile
*  as an argument. Check out actions.yml file.
*
**/
@OptIn(ExperimentalBaselineProfilesApi::class)
class BaselineProfileGenerator {
    @get:Rule
    val baselineProfileRule = BaselineProfileRule()
    /**
    *
    *  @packageName Same as your applicationId (:app Gradle file)
    *
    **/
    @Test
    fun startup() =
        baselineProfileRule.collectBaselineProfile(
            packageName = "com.example.myapplication",
            profileBlock = {
                startActivityAndWait()
            }
        )
}
     
    
    # Workflow name
name: baseline-profiles
# Workflow title
run-name: ${{ github.actor }} requested a workflow
# Event trigger so this actions gets executed every time a push is made on the main branch
# Change this event to what suits your project best.
# Read more at https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows
on:
  push:
    branches:
      - main
# Environment variables (Optional)
# Small projects might have signingConfigs locally. This could lead to failures on GitHub Actions.
# If that's the case, upload your properties defined locally to GitHub Secrets.
# On your signingConfigs, you can recover GitHub Secrets using: variable = System.getenv("VARIABLE")
# Then uncomment this block properly defining your uploaded variables
# env:
#  VARIABLE: ${{ secrets.VARIABLE }}
# Read more at https://docs.github.com/en/actions/security-guides/encrypted-secrets
# Jobs to executed on GitHub machines
jobs:
    # Job name
    generate-baseline-profiles:
      # Operating system where the job gets to be executed
      runs-on: macos-latest
      # Job steps
      steps:
        # Checks your code out on the machine
        - uses: actions/checkout@v3
        # Sets java up
        - uses: actions/setup-java@v3
          with:
            distribution: temurin
            java-version: 11
        # Sets gradle up
        - name: Setup Gradle
          uses: gradle/gradle-build-action@v2
        # Grants execute permission to gradle (safety step)
        - name: Grant Permissions to gradlew
          run: chmod +x gradlew
        # Cleans managed device if previously settle and space currently is not available
        - name: Clean Managed Devices
          run: ./gradlew cleanManagedDevices --unused-only
        # Generates Baseline Profile
        - name: Generate Baseline Profile
          ## swiftshader_indirect: 
          ## Required to use software acceleration on machines that can't use hardware acceleration
          ## enabledRules=BaselineProfile:
          ## Required to skip macrobenchmark tests. 
          ## Read more at https://developer.android.com/topic/performance/benchmarking/benchmarking-in-ci#real-devices
          ## Read more at https://developer.android.com/topic/performance/benchmarking/macrobenchmark-overview#configuration-errors
          
          ## gradle.workers.max: 
          ## Required to avoid running multiple managed device tests concurrently. Read more at https://issuetracker.google.com/issues/193118030
          
          run: ./gradlew :benchmark:pixel2Api31BenchmarkAndroidTest -Pandroid.testoptions.manageddevices.emulator.gpu="swiftshader_indirect" -Pandroid.testInstrumentationRunnerArguments.androidx.benchmark.enabledRules=BaselineProfile -Dorg.gradle.workers.max=4
        # Sets the Baseline Profile on its proper place so it gets correctly bundled into Play Store
        - name: Move & Rename Baseline Profiles
          run:  |
            mv -f benchmark/build/outputs/managed_device_android_test_additional_output/pixel2Api31/BaselineProfileGenerator_startup-baseline-prof.txt app/src/main/baseline-prof.txt
        # Commits the generated Baseline Profile to your origin/remote
        - name: Commit Baseline Profiles
          run: |
            git config --global user.name 'Baseline Profiles - GitHub Actions'
            git config --global user.email 'github@actions'
            git add app/src/main/baseline-prof.txt
            git commit -m "Generate baseline profiles"
            git push
     
    EOF — ในเวลาประมาณสิบนาที งานที่สำเร็จควรปรากฏบนแท็บ Actions และสุดท้าย การกระทำล่าสุดในพื้นที่เก็บข้อมูลของคุณจะแสดงการเปลี่ยนแปลงของbaseline-prof.txtไฟล์ที่เพิ่งเพิ่มเข้ามา
                    GitHub Actions เป็นเครื่องมือที่ทรงพลังมากในการสร้างกฎโปรไฟล์พื้นฐานด้วยวิธีที่ปรับขนาดได้ โดยการเรียกใช้คำสั่งอื่นๆactions/*และการแทนที่runคำสั่งใน*.ymlไฟล์อย่างถูกต้อง สามารถสร้างเวิร์กโฟลว์อื่นๆ ได้อีกมากมาย GitHub Actions เป็นหนึ่งในวิธีที่ดีที่สุดในการทำให้เวิร์กโฟลว์เป็นอัตโนมัติซึ่งเหมาะกับโครงการของคุณที่สุด
บอกฉันที คุณคิดว่า Baseline Profiles หรือ GitHub Actions มีประโยชน์อย่างไร บอกฉันเพิ่มเติมเกี่ยวกับมุมมองของคุณในความคิดเห็น ขอบคุณสำหรับการปรบมือและแบ่งปันหากคุณชอบมัน และอย่าลืมติดตามฉันเพื่อติดตามบทความต่อไป! =}
อ้างอิง
- มาโครเบนช์มาร์ก
 - โปรไฟล์พื้นฐาน
 - การกระทำของ GitHub
 - ภาพรวมการปรับปรุงประสิทธิภาพ
 - บทนำสู่การแสดง — ทักษะ MAD
 - การปรับปรุงประสิทธิภาพของแอพด้วยโปรไฟล์พื้นฐาน
 - สร้างโปรไฟล์พื้นฐานด้วยตนเอง
 - ตั้งค่า Android 12 SDK
 - ปลั๊กอิน Android Gradle 7.3.0
 - การเขียน Macrobenchmark
 - การค้นหารหัส Android — ตัวอย่างโปรไฟล์
 - Macrobenchmark — ข้อผิดพลาดในการกำหนดค่า
 - โปรไฟล์พื้นฐาน — ปัญหาที่ทราบ
 





































![รายการที่เชื่อมโยงคืออะไร? [ส่วนที่ 1]](https://post.nghiatu.com/assets/images/m/max/724/1*Xokk6XOjWyIGCBujkJsCzQ.jpeg)