flutter_performance_testing 0.0.1
flutter_performance_testing: ^0.0.1 copied to clipboard
Performance testing utilities and benchmarks for Flutter applications
import 'package:flutter/material.dart';
import 'package:flutter_performance_testing/flutter_performance_testing.dart';
void main() {
runApp(const PerformanceTestingApp());
}
class PerformanceTestingApp extends StatelessWidget {
const PerformanceTestingApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Performance Testing Example',
theme: ThemeData(
primarySwatch: Colors.blue,
useMaterial3: true,
),
home: const PerformanceTestingHomePage(),
);
}
}
class PerformanceTestingHomePage extends StatefulWidget {
const PerformanceTestingHomePage({super.key});
@override
State<PerformanceTestingHomePage> createState() =>
_PerformanceTestingHomePageState();
}
class _PerformanceTestingHomePageState
extends State<PerformanceTestingHomePage> {
final BenchmarkSuite _benchmarkSuite = BenchmarkSuite();
final MemoryProfiler _memoryProfiler = MemoryProfiler();
final FrameRateMonitor _frameRateMonitor = FrameRateMonitor();
final WidgetPerformanceTester _widgetTester = WidgetPerformanceTester();
String _results = '';
@override
void initState() {
super.initState();
StartupTimer.start();
}
@override
void dispose() {
_memoryProfiler.dispose();
_frameRateMonitor.dispose();
super.dispose();
}
Future<void> _runPerformanceTests() async {
setState(() {
_results = 'Running performance tests...\n';
});
// Add benchmark tests
_benchmarkSuite.addTest('widget_build', () async {
// Simulate widget building
await Future.delayed(const Duration(milliseconds: 1));
}, iterations: 10, description: 'Widget build performance test');
_benchmarkSuite.addTest('memory_operation', () async {
// Simulate memory operations
final list = List.generate(1000, (i) => i);
await Future.delayed(const Duration(milliseconds: 1));
}, iterations: 10, description: 'Memory operation performance test');
// Run benchmarks
final benchmarkResults = await _benchmarkSuite.run();
// Test widget performance
final widgetResult = await _widgetTester.testWidgetBuild(
'example_widget',
() async {
await Future.delayed(const Duration(milliseconds: 1));
},
iterations: 5,
description: 'Example widget performance test',
);
// Generate reports
final benchmarkReport = _benchmarkSuite.exportResults();
final widgetReport = _widgetTester.generateReport();
setState(() {
_results = '''
Performance Tests Completed!
Benchmark Results:
${benchmarkResults.map((r) => '- ${r.testName}: ${r.averageExecutionTime.toStringAsFixed(2)}μs').join('\n')}
Widget Performance:
- ${widgetResult.testName}: ${widgetResult.averageBuildTime.toStringAsFixed(2)}μs
Benchmark Summary:
- Total Tests: ${benchmarkReport['totalTests']}
- Results: ${benchmarkReport['results'].length}
Widget Summary:
- Total Tests: ${widgetReport.totalTests}
- Fastest Test: ${widgetReport.summary.fastestTest}
- Slowest Test: ${widgetReport.summary.slowestTest}
''';
});
}
Future<void> _startMemoryProfiling() async {
_memoryProfiler.startTracking(interval: const Duration(milliseconds: 500));
setState(() {
_results = 'Memory profiling started...\n';
});
// Simulate some memory operations
await Future.delayed(const Duration(seconds: 3));
_memoryProfiler.stopTracking();
final report = _memoryProfiler.generateReport();
setState(() {
_results = '''
Memory Profiling Completed!
Memory Report:
- Duration: ${report.duration.inMilliseconds}ms
- Total Snapshots: ${report.totalSnapshots}
- Average Memory: ${(report.averageMemoryUsage / 1024).toStringAsFixed(2)} KB
- Peak Memory: ${(report.peakMemoryUsage / 1024).toStringAsFixed(2)} KB
- Memory Growth: ${(report.memoryGrowth / 1024).toStringAsFixed(2)} KB
''';
});
}
Future<void> _startFrameRateMonitoring() async {
_frameRateMonitor.startMonitoring();
setState(() {
_results = 'Frame rate monitoring started...\n';
});
// Simulate some frame rendering
await Future.delayed(const Duration(seconds: 3));
_frameRateMonitor.stopMonitoring();
final report = _frameRateMonitor.generateReport();
setState(() {
_results = '''
Frame Rate Monitoring Completed!
Frame Rate Report:
- Duration: ${report.duration.inMilliseconds}ms
- Total Frames: ${report.totalFrames}
- Average FPS: ${report.averageFrameRate.toStringAsFixed(1)}
- Min FPS: ${report.minFrameRate.toStringAsFixed(1)}
- Max FPS: ${report.maxFrameRate.toStringAsFixed(1)}
- Dropped Frames: ${report.droppedFrames}
''';
});
}
void _showPlatformInfo() {
final platformInfo = PlatformSupport.exportPlatformInfo();
final wasmInfo = WasmCompatibility.exportWasmInfo();
setState(() {
_results = '''
Platform Information:
Supported Platforms: ${PlatformSupport.supportedPlatforms.join(', ')}
WASM Support: ${WasmCompatibility.isSupported ? 'Yes' : 'No'}
WASM Versions: ${WasmCompatibility.supportedVersions.join(', ')}
Platform Features:
${PlatformSupport.platformFeatures.entries.map((e) => '- ${e.key}: ${e.value.join(', ')}').join('\n')}
WASM Features:
${WasmCompatibility.supportedFeatures.join(', ')}
''';
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Flutter Performance Testing'),
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
ElevatedButton(
onPressed: _runPerformanceTests,
child: const Text('Run Performance Tests'),
),
const SizedBox(height: 8),
ElevatedButton(
onPressed: _startMemoryProfiling,
child: const Text('Start Memory Profiling'),
),
const SizedBox(height: 8),
ElevatedButton(
onPressed: _startFrameRateMonitoring,
child: const Text('Start Frame Rate Monitoring'),
),
const SizedBox(height: 8),
ElevatedButton(
onPressed: _showPlatformInfo,
child: const Text('Show Platform Info'),
),
const SizedBox(height: 16),
Expanded(
child: Container(
padding: const EdgeInsets.all(8.0),
decoration: BoxDecoration(
border: Border.all(color: Colors.grey),
borderRadius: BorderRadius.circular(4.0),
),
child: SingleChildScrollView(
child: Text(
_results.isEmpty
? 'Click a button to run performance tests...'
: _results,
style: const TextStyle(fontFamily: 'monospace'),
),
),
),
),
],
),
),
);
}
}