refactor: unify device analysis and performance testing logic for improved module consistency
This commit is contained in:
@@ -1,40 +1,59 @@
|
||||
import 'dart:io';
|
||||
import 'dart:isolate';
|
||||
|
||||
class PerformanceTester {
|
||||
/// ✅ فحص سرعة الكتابة إلى التخزين (Storage Write Speed) بوحدة MB/s
|
||||
static Future<double> testStorageWriteSpeed() async {
|
||||
try {
|
||||
final tempDir = Directory.systemTemp;
|
||||
final testFile = File('${tempDir.path}/speed_test.txt');
|
||||
final data = List<int>.filled(1024 * 1024 * 5, 0); // 5MB
|
||||
/// يكرر القياس عدة مرات ويأخذ المتوسط الوسيط لتقليل تأثير الحمل العرضي على الجهاز.
|
||||
static Future<double> testStorageWriteSpeed({int samples = 3}) async {
|
||||
final List<double> speeds = [];
|
||||
for (int i = 0; i < samples; i++) {
|
||||
try {
|
||||
final tempDir = Directory.systemTemp;
|
||||
final testFile = File('${tempDir.path}/speed_test_$i.txt');
|
||||
final data = List<int>.filled(1024 * 1024 * 5, 0); // 5MB
|
||||
|
||||
final stopwatch = Stopwatch()..start();
|
||||
await testFile.writeAsBytes(data, flush: true);
|
||||
stopwatch.stop();
|
||||
final stopwatch = Stopwatch()..start();
|
||||
await testFile.writeAsBytes(data, flush: true);
|
||||
stopwatch.stop();
|
||||
|
||||
await testFile.delete();
|
||||
await testFile.delete();
|
||||
|
||||
double seconds = stopwatch.elapsedMilliseconds / 1000;
|
||||
if (seconds == 0) seconds = 0.001;
|
||||
double seconds = stopwatch.elapsedMilliseconds / 1000;
|
||||
if (seconds == 0) seconds = 0.001;
|
||||
|
||||
final speed = 5 / seconds;
|
||||
return double.parse(speed.toStringAsFixed(2));
|
||||
} catch (e) {
|
||||
print("❌ Storage write error: $e");
|
||||
return 0.0;
|
||||
speeds.add(5 / seconds);
|
||||
} catch (e) {
|
||||
print("❌ Storage write error: $e");
|
||||
}
|
||||
}
|
||||
if (speeds.isEmpty) return 0.0;
|
||||
speeds.sort();
|
||||
final median = speeds[speeds.length ~/ 2];
|
||||
return double.parse(median.toStringAsFixed(2));
|
||||
}
|
||||
|
||||
/// عملية حسابية مكثفة تُنفَّذ داخل isolate منفصل حتى لا تجمّد واجهة المستخدم.
|
||||
static double _cpuBurn(int iterations) {
|
||||
double x = 0;
|
||||
for (int i = 0; i < iterations; i++) {
|
||||
x += i * 0.000001;
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
/// ✅ فحص سرعة المعالج (CPU Compute Speed) بوحدة الثواني
|
||||
static Future<double> testCPUSpeed() async {
|
||||
/// يعمل في isolate منفصل (لا يجمّد الواجهة) ويكرر القياس ويأخذ الوسيط.
|
||||
static Future<double> testCPUSpeed({int samples = 3}) async {
|
||||
try {
|
||||
final stopwatch = Stopwatch()..start();
|
||||
double x = 0;
|
||||
for (int i = 0; i < 100000000; i++) {
|
||||
x += i * 0.000001;
|
||||
final List<double> times = [];
|
||||
for (int i = 0; i < samples; i++) {
|
||||
final stopwatch = Stopwatch()..start();
|
||||
await Isolate.run(() => _cpuBurn(100000000));
|
||||
stopwatch.stop();
|
||||
times.add(stopwatch.elapsedMilliseconds / 1000.0);
|
||||
}
|
||||
stopwatch.stop();
|
||||
return stopwatch.elapsedMilliseconds / 1000.0;
|
||||
times.sort();
|
||||
return times[times.length ~/ 2];
|
||||
} catch (e) {
|
||||
print("❌ CPU compute error: $e");
|
||||
return 999.0;
|
||||
|
||||
Reference in New Issue
Block a user