Fix/Update #1
| @ -3,10 +3,11 @@ | ||||
| // ----- | ||||
| // File: benchmark_page.dart | ||||
| // Created Date: 28/12/2021 15:12:39 | ||||
| // Last Modified: 25/05/2022 15:26:42 | ||||
| // Last Modified: 25/05/2022 17:16:12 | ||||
| // ----- | ||||
| // Copyright (c) 2021 | ||||
| 
 | ||||
| import 'dart:math'; | ||||
| import 'dart:typed_data'; | ||||
| 
 | ||||
| import 'package:flutter/material.dart'; | ||||
| @ -24,10 +25,12 @@ class BenchmarkPage extends ConsumerWidget { | ||||
|   final Output keyContent = Output(); | ||||
|   final Output benchmarkStatus = Output(large: true); | ||||
| 
 | ||||
|   Future<void> _benchmarkEncryptionOnly( | ||||
|   Future<void> _benchmark( | ||||
|     WidgetRef ref, | ||||
|     Cipher cipher, | ||||
|   ) async { | ||||
|     Cipher cipher, { | ||||
|     bool usePc = false, | ||||
|     bool encryptionOnly = false, | ||||
|   }) async { | ||||
|     Session state = ref.read(sessionProvider.state).state; | ||||
|     AesGcm pc = AesGcm(); | ||||
| 
 | ||||
| @ -37,79 +40,47 @@ class BenchmarkPage extends ConsumerWidget { | ||||
|       return; | ||||
|     } | ||||
| 
 | ||||
|     benchmarkStatus.print("Benchmark 2/4/8/16/32/64/128/256MB\n"); | ||||
|     List<int> testedSizes = [2, 4, 8, 16, 32, 64, 128, 256]; | ||||
|     String csv = "size;encryption time\n"; | ||||
|     List<int> testedSizes = [2, 6, 10, 14, 18, 22, 26, 30, 34, 38, 42, 46, 50]; | ||||
|     int multiplier = pow(2, 20).toInt(); // MiB | ||||
| 
 | ||||
|     benchmarkStatus.print("[Benchmark] Sizes: ${testedSizes.join('/')}MiB\n"); | ||||
|     benchmarkStatus.appendln( | ||||
|         "[Benchmark] Engine: " + (usePc ? " PointyCastle" : " NativeCrypto")); | ||||
|     benchmarkStatus.appendln("[Benchmark] Test: " + | ||||
|         (encryptionOnly ? " Encryption Only" : " Encryption & Decryption")); | ||||
|     benchmarkStatus.appendln( | ||||
|         '[Benchmark] bytesCountPerChunk: ${Cipher.bytesCountPerChunk} bytes/chunk'); | ||||
| 
 | ||||
|     String csv = encryptionOnly | ||||
|         ? "Run;Size (B);Encryption Time (ms)\n" | ||||
|         : "Run;Size (B);Encryption Time (ms);Decryption Time (ms)\n"; | ||||
| 
 | ||||
|     int run = 0; | ||||
|     var beforeBench = DateTime.now(); | ||||
|     Cipher.bytesCountPerChunk = Cipher.bytesCountPerChunk; | ||||
|     benchmarkStatus | ||||
|         .append('[Benchmark] ${Cipher.bytesCountPerChunk} bytes/chunk \n'); | ||||
| 
 | ||||
|     for (int size in testedSizes) { | ||||
|       var b = Uint8List(size * 1000000); | ||||
|       csv += "${size * 1000000};"; | ||||
|       run++; | ||||
|       final StringBuffer csvLine = StringBuffer(); | ||||
|       final dummyBytes = Uint8List(size * multiplier); | ||||
|       csvLine.write('$run;${size * multiplier};'); | ||||
| 
 | ||||
|       // Encryption | ||||
|       var before = DateTime.now(); | ||||
|       var encryptedBigFile = await cipher.encrypt(b); | ||||
|       var after = DateTime.now(); | ||||
| 
 | ||||
|       var benchmark = | ||||
|           after.millisecondsSinceEpoch - before.millisecondsSinceEpoch; | ||||
|       benchmarkStatus.append('[$size MB] Encryption took $benchmark ms\n'); | ||||
| 
 | ||||
|       csv += "$benchmark\n"; | ||||
|     } | ||||
|     var afterBench = DateTime.now(); | ||||
|     var benchmark = | ||||
|         afterBench.millisecondsSinceEpoch - beforeBench.millisecondsSinceEpoch; | ||||
|     var sum = testedSizes.reduce((a, b) => a + b); | ||||
|     benchmarkStatus.append( | ||||
|         'Benchmark finished.\nGenerated, and encrypted $sum MB in $benchmark ms'); | ||||
|     debugPrint("[Benchmark cvs]\n$csv"); | ||||
|   } | ||||
| 
 | ||||
|   Future<void> _benchmark(WidgetRef ref, Cipher cipher, | ||||
|       {bool usePc = false}) async { | ||||
|     Session state = ref.read(sessionProvider.state).state; | ||||
|     AesGcm pc = AesGcm(); | ||||
| 
 | ||||
|     if (state.secretKey.bytes.isEmpty) { | ||||
|       benchmarkStatus | ||||
|           .print('No SecretKey!\nGo in Key tab and generate or derive one.'); | ||||
|       return; | ||||
|     } | ||||
| 
 | ||||
|     benchmarkStatus.print("Benchmark 2/4/8/16/32/64/128/256MB\n"); | ||||
|     List<int> testedSizes = [2, 4, 8, 16, 32, 64, 128, 256]; | ||||
|     String csv = "size;encryption time;decryption time;crypto time\n"; | ||||
| 
 | ||||
|     var beforeBench = DateTime.now(); | ||||
|     Cipher.bytesCountPerChunk = Cipher.bytesCountPerChunk; | ||||
|     benchmarkStatus | ||||
|         .append('[Benchmark] ${Cipher.bytesCountPerChunk} bytes/chunk \n'); | ||||
|     for (int size in testedSizes) { | ||||
|       var b = Uint8List(size * 1000000); | ||||
|       csv += "${size * 1000000};"; | ||||
|       var cryptoTime = 0; | ||||
| 
 | ||||
|       // Encryption | ||||
|       var before = DateTime.now(); | ||||
|       Object encryptedBigFile; | ||||
|       var before = DateTime.now(); | ||||
|       if (usePc) { | ||||
|         encryptedBigFile = pc.encrypt(b, state.secretKey.bytes); | ||||
|         encryptedBigFile = pc.encrypt(dummyBytes, state.secretKey.bytes); | ||||
|       } else { | ||||
|         encryptedBigFile = await cipher.encrypt(b); | ||||
|         encryptedBigFile = await cipher.encrypt(dummyBytes); | ||||
|       } | ||||
|       var after = DateTime.now(); | ||||
| 
 | ||||
|       var benchmark = | ||||
|           after.millisecondsSinceEpoch - before.millisecondsSinceEpoch; | ||||
|       benchmarkStatus.append('[$size MB] Encryption took $benchmark ms\n'); | ||||
| 
 | ||||
|       csv += "$benchmark;"; | ||||
|       cryptoTime += benchmark; | ||||
|       benchmarkStatus | ||||
|           .appendln('[Benchmark] ${size}MiB => Encryption took $benchmark ms'); | ||||
|       csvLine.write('$benchmark'); | ||||
| 
 | ||||
|       if (!encryptionOnly) { | ||||
|         // Decryption | ||||
|         before = DateTime.now(); | ||||
|         if (usePc) { | ||||
| @ -118,21 +89,23 @@ class BenchmarkPage extends ConsumerWidget { | ||||
|           await cipher.decrypt(encryptedBigFile as CipherText); | ||||
|         } | ||||
|         after = DateTime.now(); | ||||
| 
 | ||||
|       benchmark = after.millisecondsSinceEpoch - before.millisecondsSinceEpoch; | ||||
|       benchmarkStatus.append('[$size MB] Decryption took $benchmark ms\n'); | ||||
| 
 | ||||
|       csv += "$benchmark;"; | ||||
|       cryptoTime += benchmark; | ||||
|       csv += "$cryptoTime\n"; | ||||
|         benchmark = | ||||
|             after.millisecondsSinceEpoch - before.millisecondsSinceEpoch; | ||||
|         benchmarkStatus | ||||
|             .appendln('[Benchmark] ${size}MiB => Decryption took $benchmark ms'); | ||||
|         csvLine.write(';$benchmark'); | ||||
|       } | ||||
|       csv += csvLine.toString() + '\n'; | ||||
|     } | ||||
|     var afterBench = DateTime.now(); | ||||
|     var benchmark = | ||||
|         afterBench.millisecondsSinceEpoch - beforeBench.millisecondsSinceEpoch; | ||||
|     var sum = testedSizes.reduce((a, b) => a + b); | ||||
|     benchmarkStatus.append( | ||||
|         'Benchmark finished.\nGenerated, encrypted and decrypted $sum MB in $benchmark ms'); | ||||
|     debugPrint("[Benchmark cvs]\n$csv"); | ||||
|     benchmarkStatus | ||||
|         .appendln('[Benchmark] Finished: ${sum}MiB in $benchmark ms'); | ||||
|     benchmarkStatus.appendln('[Benchmark] Check the console for csv data'); | ||||
|     benchmarkStatus.appendln(csv); | ||||
|     print(csv); | ||||
|   } | ||||
| 
 | ||||
|   void _clear() { | ||||
| @ -174,37 +147,39 @@ class BenchmarkPage extends ConsumerWidget { | ||||
|               alignment: Alignment.centerLeft, | ||||
|             ), | ||||
|             keyContent, | ||||
|             Column( | ||||
|               crossAxisAlignment: CrossAxisAlignment.start, | ||||
|               children: [ | ||||
|                 Row( | ||||
|                   mainAxisAlignment: MainAxisAlignment.spaceEvenly, | ||||
|             Wrap( | ||||
|               children: [ | ||||
|                 Button( | ||||
|                   () => _benchmark(ref, cipher), | ||||
|                   "NativeCrypto", | ||||
|                 ), | ||||
|                 const SizedBox(width: 8), | ||||
|                 Button( | ||||
|                   () => _benchmark(ref, cipher, usePc: true), | ||||
|                   "PointyCastle", | ||||
|                 ), | ||||
|                   ], | ||||
|                 ), | ||||
|                 Row( | ||||
|                   mainAxisAlignment: MainAxisAlignment.spaceEvenly, | ||||
|                   children: [ | ||||
|                 const SizedBox(width: 8), | ||||
|                 Button( | ||||
|                       () => _benchmarkEncryptionOnly(ref, cipher), | ||||
|                       "NC Persistence", | ||||
|                   () => _benchmark(ref, cipher, encryptionOnly: true), | ||||
|                   "NC Encryption Only", | ||||
|                 ), | ||||
|                 const SizedBox(width: 8), | ||||
|                 Button( | ||||
|                   () => _benchmark( | ||||
|                     ref, | ||||
|                     cipher, | ||||
|                     usePc: true, | ||||
|                     encryptionOnly: true, | ||||
|                   ), | ||||
|                   "PC Encryption Only", | ||||
|                 ), | ||||
|                 const SizedBox(width: 8), | ||||
|                 Button( | ||||
|                   _clear, | ||||
|                   "Clear", | ||||
|                 ), | ||||
|               ], | ||||
|             ), | ||||
|               ], | ||||
|             ), | ||||
|             benchmarkStatus, | ||||
|           ], | ||||
|         ), | ||||
|  | ||||
| @ -3,7 +3,7 @@ | ||||
| // ----- | ||||
| // File: output.dart | ||||
| // Created Date: 28/12/2021 13:31:39 | ||||
| // Last Modified: 28/12/2021 14:12:11 | ||||
| // Last Modified: 25/05/2022 16:39:39 | ||||
| // ----- | ||||
| // Copyright (c) 2021 | ||||
| 
 | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user