221 lines
6.1 KiB
Dart
221 lines
6.1 KiB
Dart
///
|
|
/// Result structure for crypto operations
|
|
/// Handles success/error states and performance metrics
|
|
///
|
|
|
|
/// Result of a cryptographic operation
|
|
class CryptoResult {
|
|
final String operationId;
|
|
final List<int> data;
|
|
final bool success;
|
|
final String? error;
|
|
final String? stackTrace;
|
|
final Duration processingTime;
|
|
final String workerId;
|
|
final DateTime completedAt;
|
|
|
|
/// Create successful result
|
|
CryptoResult.success({
|
|
required this.operationId,
|
|
required this.data,
|
|
required this.processingTime,
|
|
required this.workerId,
|
|
}) : success = true,
|
|
error = null,
|
|
stackTrace = null,
|
|
completedAt = DateTime.now();
|
|
|
|
/// Create error result
|
|
CryptoResult.error({
|
|
required this.operationId,
|
|
required this.error,
|
|
this.stackTrace,
|
|
required this.processingTime,
|
|
required this.workerId,
|
|
}) : success = false,
|
|
data = const [],
|
|
completedAt = DateTime.now();
|
|
|
|
/// Serialize for isolate communication
|
|
Map<String, dynamic> toMap() {
|
|
return {
|
|
'operationId': operationId,
|
|
'data': data,
|
|
'success': success,
|
|
'error': error,
|
|
'stackTrace': stackTrace,
|
|
'processingTimeMs': processingTime.inMilliseconds,
|
|
'workerId': workerId,
|
|
'completedAtMs': completedAt.millisecondsSinceEpoch,
|
|
};
|
|
}
|
|
|
|
/// Deserialize from isolate communication
|
|
static CryptoResult fromMap(Map<String, dynamic> map) {
|
|
final success = map['success'] as bool;
|
|
final processingTime = Duration(milliseconds: map['processingTimeMs'] as int);
|
|
final workerId = map['workerId'] as String;
|
|
final operationId = map['operationId'] as String;
|
|
|
|
if (success) {
|
|
return CryptoResult.success(
|
|
operationId: operationId,
|
|
data: List<int>.from(map['data'] as List),
|
|
processingTime: processingTime,
|
|
workerId: workerId,
|
|
);
|
|
} else {
|
|
return CryptoResult.error(
|
|
operationId: operationId,
|
|
error: map['error'] as String?,
|
|
stackTrace: map['stackTrace'] as String?,
|
|
processingTime: processingTime,
|
|
workerId: workerId,
|
|
);
|
|
}
|
|
}
|
|
|
|
/// Get result data size in bytes
|
|
int get dataSize => data.length;
|
|
|
|
/// Get processing speed in bytes per second
|
|
double get bytesPerSecond {
|
|
final seconds = processingTime.inMilliseconds / 1000.0;
|
|
if (seconds <= 0) return 0.0;
|
|
return dataSize / seconds;
|
|
}
|
|
|
|
/// Get human-readable processing speed
|
|
String get formattedSpeed {
|
|
final bps = bytesPerSecond;
|
|
if (bps >= 1024 * 1024) {
|
|
return '${(bps / (1024 * 1024)).toStringAsFixed(2)} MB/s';
|
|
} else if (bps >= 1024) {
|
|
return '${(bps / 1024).toStringAsFixed(2)} KB/s';
|
|
} else {
|
|
return '${bps.toStringAsFixed(2)} B/s';
|
|
}
|
|
}
|
|
|
|
/// Check if operation was slow
|
|
bool get isSlow => processingTime.inMilliseconds > 1000; // > 1 second
|
|
|
|
/// Check if operation was fast
|
|
bool get isFast => processingTime.inMilliseconds < 100; // < 100ms
|
|
|
|
@override
|
|
String toString() {
|
|
if (success) {
|
|
return 'CryptoResult.success('
|
|
'id: $operationId, '
|
|
'dataSize: ${dataSize}B, '
|
|
'time: ${processingTime.inMilliseconds}ms, '
|
|
'speed: $formattedSpeed, '
|
|
'worker: $workerId'
|
|
')';
|
|
} else {
|
|
return 'CryptoResult.error('
|
|
'id: $operationId, '
|
|
'error: $error, '
|
|
'time: ${processingTime.inMilliseconds}ms, '
|
|
'worker: $workerId'
|
|
')';
|
|
}
|
|
}
|
|
|
|
@override
|
|
bool operator ==(Object other) =>
|
|
identical(this, other) ||
|
|
other is CryptoResult &&
|
|
runtimeType == other.runtimeType &&
|
|
operationId == other.operationId &&
|
|
success == other.success;
|
|
|
|
@override
|
|
int get hashCode => operationId.hashCode ^ success.hashCode;
|
|
}
|
|
|
|
/// Performance metrics for crypto operations
|
|
class CryptoMetrics {
|
|
int encryptionCount = 0;
|
|
int decryptionCount = 0;
|
|
Duration totalEncryptionTime = Duration.zero;
|
|
Duration totalDecryptionTime = Duration.zero;
|
|
int errorCount = 0;
|
|
int priorityOperations = 0;
|
|
DateTime? lastOperationTime;
|
|
|
|
/// Record successful encryption
|
|
void recordEncryption(Duration time, bool wasPriority) {
|
|
encryptionCount++;
|
|
totalEncryptionTime += time;
|
|
if (wasPriority) priorityOperations++;
|
|
lastOperationTime = DateTime.now();
|
|
}
|
|
|
|
/// Record successful decryption
|
|
void recordDecryption(Duration time) {
|
|
decryptionCount++;
|
|
totalDecryptionTime += time;
|
|
lastOperationTime = DateTime.now();
|
|
}
|
|
|
|
/// Record error
|
|
void recordError() {
|
|
errorCount++;
|
|
lastOperationTime = DateTime.now();
|
|
}
|
|
|
|
/// Get total operations
|
|
int get totalOperations => encryptionCount + decryptionCount;
|
|
|
|
/// Get average encryption time
|
|
Duration get averageEncryptionTime {
|
|
if (encryptionCount == 0) return Duration.zero;
|
|
return Duration(milliseconds: totalEncryptionTime.inMilliseconds ~/ encryptionCount);
|
|
}
|
|
|
|
/// Get average decryption time
|
|
Duration get averageDecryptionTime {
|
|
if (decryptionCount == 0) return Duration.zero;
|
|
return Duration(milliseconds: totalDecryptionTime.inMilliseconds ~/ decryptionCount);
|
|
}
|
|
|
|
/// Get error rate (0.0 to 1.0)
|
|
double get errorRate {
|
|
final total = totalOperations + errorCount;
|
|
if (total == 0) return 0.0;
|
|
return errorCount / total;
|
|
}
|
|
|
|
/// Get priority operation percentage
|
|
double get priorityPercentage {
|
|
if (totalOperations == 0) return 0.0;
|
|
return priorityOperations / totalOperations;
|
|
}
|
|
|
|
/// Reset all metrics
|
|
void reset() {
|
|
encryptionCount = 0;
|
|
decryptionCount = 0;
|
|
totalEncryptionTime = Duration.zero;
|
|
totalDecryptionTime = Duration.zero;
|
|
errorCount = 0;
|
|
priorityOperations = 0;
|
|
lastOperationTime = null;
|
|
}
|
|
|
|
@override
|
|
String toString() {
|
|
return 'CryptoMetrics('
|
|
'operations: $totalOperations, '
|
|
'encryptions: $encryptionCount, '
|
|
'decryptions: $decryptionCount, '
|
|
'errors: $errorCount, '
|
|
'avgEncTime: ${averageEncryptionTime.inMilliseconds}ms, '
|
|
'avgDecTime: ${averageDecryptionTime.inMilliseconds}ms, '
|
|
'errorRate: ${(errorRate * 100).toStringAsFixed(1)}%'
|
|
')';
|
|
}
|
|
}
|