refactor(example): update benchmark page

This commit is contained in:
Hugo Pointcheval 2022-05-25 23:27:22 +02:00
parent f592799970
commit 5729fff09b
Signed by: hugo
GPG Key ID: A9E8E9615379254F
2 changed files with 82 additions and 107 deletions

View File

@ -3,10 +3,11 @@
// ----- // -----
// File: benchmark_page.dart // File: benchmark_page.dart
// Created Date: 28/12/2021 15:12:39 // 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 // Copyright (c) 2021
import 'dart:math';
import 'dart:typed_data'; import 'dart:typed_data';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
@ -24,10 +25,12 @@ class BenchmarkPage extends ConsumerWidget {
final Output keyContent = Output(); final Output keyContent = Output();
final Output benchmarkStatus = Output(large: true); final Output benchmarkStatus = Output(large: true);
Future<void> _benchmarkEncryptionOnly( Future<void> _benchmark(
WidgetRef ref, WidgetRef ref,
Cipher cipher, Cipher cipher, {
) async { bool usePc = false,
bool encryptionOnly = false,
}) async {
Session state = ref.read(sessionProvider.state).state; Session state = ref.read(sessionProvider.state).state;
AesGcm pc = AesGcm(); AesGcm pc = AesGcm();
@ -37,102 +40,72 @@ class BenchmarkPage extends ConsumerWidget {
return; return;
} }
benchmarkStatus.print("Benchmark 2/4/8/16/32/64/128/256MB\n"); List<int> testedSizes = [2, 6, 10, 14, 18, 22, 26, 30, 34, 38, 42, 46, 50];
List<int> testedSizes = [2, 4, 8, 16, 32, 64, 128, 256]; int multiplier = pow(2, 20).toInt(); // MiB
String csv = "size;encryption time\n";
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(); var beforeBench = DateTime.now();
Cipher.bytesCountPerChunk = Cipher.bytesCountPerChunk;
benchmarkStatus
.append('[Benchmark] ${Cipher.bytesCountPerChunk} bytes/chunk \n');
for (int size in testedSizes) { for (int size in testedSizes) {
var b = Uint8List(size * 1000000); run++;
csv += "${size * 1000000};"; final StringBuffer csvLine = StringBuffer();
final dummyBytes = Uint8List(size * multiplier);
csvLine.write('$run;${size * multiplier};');
// Encryption // 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; Object encryptedBigFile;
var before = DateTime.now();
if (usePc) { if (usePc) {
encryptedBigFile = pc.encrypt(b, state.secretKey.bytes); encryptedBigFile = pc.encrypt(dummyBytes, state.secretKey.bytes);
} else { } else {
encryptedBigFile = await cipher.encrypt(b); encryptedBigFile = await cipher.encrypt(dummyBytes);
} }
var after = DateTime.now(); var after = DateTime.now();
var benchmark = var benchmark =
after.millisecondsSinceEpoch - before.millisecondsSinceEpoch; after.millisecondsSinceEpoch - before.millisecondsSinceEpoch;
benchmarkStatus.append('[$size MB] Encryption took $benchmark ms\n'); benchmarkStatus
.appendln('[Benchmark] ${size}MiB => Encryption took $benchmark ms');
csvLine.write('$benchmark');
csv += "$benchmark;"; if (!encryptionOnly) {
cryptoTime += benchmark; // Decryption
before = DateTime.now();
// Decryption if (usePc) {
before = DateTime.now(); pc.decrypt(encryptedBigFile as Uint8List, state.secretKey.bytes);
if (usePc) { } else {
pc.decrypt(encryptedBigFile as Uint8List, state.secretKey.bytes); await cipher.decrypt(encryptedBigFile as CipherText);
} else { }
await cipher.decrypt(encryptedBigFile as CipherText); after = DateTime.now();
benchmark =
after.millisecondsSinceEpoch - before.millisecondsSinceEpoch;
benchmarkStatus
.appendln('[Benchmark] ${size}MiB => Decryption took $benchmark ms');
csvLine.write(';$benchmark');
} }
after = DateTime.now(); csv += csvLine.toString() + '\n';
benchmark = after.millisecondsSinceEpoch - before.millisecondsSinceEpoch;
benchmarkStatus.append('[$size MB] Decryption took $benchmark ms\n');
csv += "$benchmark;";
cryptoTime += benchmark;
csv += "$cryptoTime\n";
} }
var afterBench = DateTime.now(); var afterBench = DateTime.now();
var benchmark = var benchmark =
afterBench.millisecondsSinceEpoch - beforeBench.millisecondsSinceEpoch; afterBench.millisecondsSinceEpoch - beforeBench.millisecondsSinceEpoch;
var sum = testedSizes.reduce((a, b) => a + b); var sum = testedSizes.reduce((a, b) => a + b);
benchmarkStatus.append( benchmarkStatus
'Benchmark finished.\nGenerated, encrypted and decrypted $sum MB in $benchmark ms'); .appendln('[Benchmark] Finished: ${sum}MiB in $benchmark ms');
debugPrint("[Benchmark cvs]\n$csv"); benchmarkStatus.appendln('[Benchmark] Check the console for csv data');
benchmarkStatus.appendln(csv);
print(csv);
} }
void _clear() { void _clear() {
@ -174,34 +147,36 @@ class BenchmarkPage extends ConsumerWidget {
alignment: Alignment.centerLeft, alignment: Alignment.centerLeft,
), ),
keyContent, keyContent,
Column( Wrap(
crossAxisAlignment: CrossAxisAlignment.start,
children: [ children: [
Row( Button(
mainAxisAlignment: MainAxisAlignment.spaceEvenly, () => _benchmark(ref, cipher),
children: [ "NativeCrypto",
Button(
() => _benchmark(ref, cipher),
"NativeCrypto",
),
Button(
() => _benchmark(ref, cipher, usePc: true),
"PointyCastle",
),
],
), ),
Row( const SizedBox(width: 8),
mainAxisAlignment: MainAxisAlignment.spaceEvenly, Button(
children: [ () => _benchmark(ref, cipher, usePc: true),
Button( "PointyCastle",
() => _benchmarkEncryptionOnly(ref, cipher), ),
"NC Persistence", const SizedBox(width: 8),
), Button(
Button( () => _benchmark(ref, cipher, encryptionOnly: true),
_clear, "NC Encryption Only",
"Clear", ),
), const SizedBox(width: 8),
], Button(
() => _benchmark(
ref,
cipher,
usePc: true,
encryptionOnly: true,
),
"PC Encryption Only",
),
const SizedBox(width: 8),
Button(
_clear,
"Clear",
), ),
], ],
), ),

View File

@ -3,7 +3,7 @@
// ----- // -----
// File: output.dart // File: output.dart
// Created Date: 28/12/2021 13:31:39 // 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 // Copyright (c) 2021