|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:flutter_screenutil/flutter_screenutil.dart' as su; |
| 3 | +import 'package:flutter_screenutil_plus/flutter_screenutil_plus.dart' as sup; |
| 4 | +import 'package:flutter_test/flutter_test.dart'; |
| 5 | + |
| 6 | +const Size designSize = Size(390, 844); |
| 7 | +const Size physicalSize = Size(428, 926); |
| 8 | + |
| 9 | +void main() { |
| 10 | + testWidgets('Run Scaling Benchmarks explicitly', (WidgetTester tester) async { |
| 11 | + // 1. Setup a real widget tree to get a valid BuildContext |
| 12 | + late BuildContext realContext; |
| 13 | + await tester.pumpWidget( |
| 14 | + MaterialApp( |
| 15 | + home: Builder( |
| 16 | + builder: (context) { |
| 17 | + realContext = context; |
| 18 | + return const SizedBox(); |
| 19 | + }, |
| 20 | + ), |
| 21 | + ), |
| 22 | + ); |
| 23 | + |
| 24 | + // 2. Initialize Standard ScreenUtil |
| 25 | + su.ScreenUtil.init(realContext, designSize: designSize, minTextAdapt: true); |
| 26 | + |
| 27 | + // 3. Initialize ScreenUtilPlus |
| 28 | + sup.ScreenUtilPlus.configure( |
| 29 | + data: MediaQuery.of(realContext), |
| 30 | + designSize: designSize, |
| 31 | + minTextAdapt: true, |
| 32 | + splitScreenMode: false, |
| 33 | + ); |
| 34 | + |
| 35 | + const iterations = 100000; |
| 36 | + |
| 37 | + // --- Benchmark Standard ScreenUtil --- |
| 38 | + final suStopwatch = Stopwatch()..start(); |
| 39 | + double suTotal = 0; |
| 40 | + for (var i = 0; i < iterations; i++) { |
| 41 | + final double w = su.SizeExtension(i).w; |
| 42 | + final double h = su.SizeExtension(i).h; |
| 43 | + final double sp = su.SizeExtension(i).sp; |
| 44 | + final double r = su.SizeExtension(i).r; |
| 45 | + suTotal += w + h + sp + r; |
| 46 | + } |
| 47 | + suStopwatch.stop(); |
| 48 | + |
| 49 | + // --- Benchmark ScreenUtilPlus --- |
| 50 | + final supStopwatch = Stopwatch()..start(); |
| 51 | + double supTotal = 0; |
| 52 | + for (var i = 0; i < iterations; i++) { |
| 53 | + final double w = sup.SizeExtension(i).w; |
| 54 | + final double h = sup.SizeExtension(i).h; |
| 55 | + final double sp = sup.SizeExtension(i).sp; |
| 56 | + final double r = sup.SizeExtension(i).r; |
| 57 | + supTotal += w + h + sp + r; |
| 58 | + } |
| 59 | + supStopwatch.stop(); |
| 60 | + |
| 61 | + // To prevent dead-code elimination |
| 62 | + expect(suTotal > 0, isTrue); |
| 63 | + expect(supTotal > 0, isTrue); |
| 64 | + |
| 65 | + print( |
| 66 | + 'ScreenUtil Standard Scaling (RunTime for $iterations iterations): ${suStopwatch.elapsedMicroseconds} us', |
| 67 | + ); |
| 68 | + print( |
| 69 | + 'ScreenUtilPlus Scaling (RunTime for $iterations iterations): ${supStopwatch.elapsedMicroseconds} us', |
| 70 | + ); |
| 71 | + }); |
| 72 | +} |
0 commit comments