Files
go-pixelnebula/benchmark/README_EN.md
2025-03-19 21:10:19 +08:00

5.8 KiB

🚀 PixelNebula Benchmarks

中文 | English

📊 Introduction

This directory contains comprehensive benchmark tests for the PixelNebula library, designed to measure and analyze performance across various operational scenarios. These tests help identify potential performance bottlenecks and guide subsequent optimization efforts.


🧪 Test Content

Basic Generation
Basic Avatar Generation
Styles & Themes
Styles & Themes
Animations
Animation Effects
Cache System
Cache System
Concurrency
Concurrency & Memory

The benchmarks cover the following core functionalities of PixelNebula:

1. 🖼️ Basic Avatar Generation (basic_benchmark_test.go)

  • Regular avatar generation
  • No-environment avatar generation
  • Avatar generation with different sizes
  • Multiple generations with the same ID

2. 🎨 Styles and Themes (style_theme_benchmark_test.go)

  • Performance comparison between different styles
  • Performance comparison between different themes
  • Custom themes
  • Style and theme combinations

3. Animation Effects (animation_benchmark_test.go)

  • Rotation animation
  • Gradient animation
  • Fade-in/out animation
  • Transform animation
  • Color animation
  • Multiple animation combinations

4. 💾 Cache System (cache_benchmark_test.go)

  • No cache vs. default cache
  • Different cache sizes
  • Cache compression effects
  • Different expiry time configurations

5. Concurrency and Memory Usage (concurrency_memory_benchmark_test.go)

  • Performance at different concurrency levels
  • Concurrent performance with shared instances
  • Memory usage analysis for various operations

🚀 Running Benchmarks

Run All Tests

cd benchmark
go test -bench=. -benchmem

Run Specific Test Groups

🏃 Basic Tests

go test -bench=BenchmarkBasic -benchmem

💾 Cache Tests

go test -bench=BenchmarkCache -benchmem

Animation Tests

go test -bench=BenchmarkAnimation -benchmem

Advanced Configuration

⚙️ Set CPU Count

go test -bench=. -benchmem -cpu=1,2,4,8

⏱️ Set Iteration Count and Duration

go test -bench=. -benchmem -count=5 -benchtime=5s

📈 Test Result Analysis

After running the tests, results will be displayed in the following format:

BenchmarkBasicAvatarGeneration-8     5000     234567 ns/op    12345 B/op    123 allocs/op
Component Description
BenchmarkBasicAvatarGeneration-8 Test name, 8 indicates using 8 CPUs
5000 Number of iterations the test ran
234567 ns/op Average time per operation (nanoseconds)
12345 B/op Average memory allocation per operation (bytes)
123 allocs/op Average number of memory allocations per operation

📝 Adding Benchmark Tests

When adding new features, it's recommended to add corresponding benchmark tests:

  1. Create a Test Function: Create a new test function for the specific feature, named BenchmarkXXX
  2. Reset Timer: Use b.ResetTimer() to reset the timer after setup work is complete
  3. Create Sub-tests: Use b.Run() to create sub-tests for comparing different variables
  4. Use Iteration Count: Use b.N as the iteration count to ensure statistical accuracy

Example:

func BenchmarkMyFeature(b *testing.B) {
    // Setup code
    pn := pixelnebula.NewPixelNebula()
    
    // Reset timer before the actual benchmark
    b.ResetTimer()
    
    // Run the benchmark
    for i := 0; i < b.N; i++ {
        // Code to benchmark
        pn.MyFeature()
    }
}

For more information, check out the PixelNebula documentation and examples.

© 2024 landaiqing