#
π PixelNebula Benchmarks
[δΈζ](README.md) | 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 Avatar Generation
|

Styles & Themes
|

Animation Effects
|

Cache System
|

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
```bash
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:
```go
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