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
// 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,102 +40,72 @@ 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');
benchmarkStatus
.appendln('[Benchmark] ${size}MiB => Encryption took $benchmark ms');
csvLine.write('$benchmark');
csv += "$benchmark;";
cryptoTime += benchmark;
// Decryption
before = DateTime.now();
if (usePc) {
pc.decrypt(encryptedBigFile as Uint8List, state.secretKey.bytes);
} else {
await cipher.decrypt(encryptedBigFile as CipherText);
if (!encryptionOnly) {
// Decryption
before = DateTime.now();
if (usePc) {
pc.decrypt(encryptedBigFile as Uint8List, state.secretKey.bytes);
} 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();
benchmark = after.millisecondsSinceEpoch - before.millisecondsSinceEpoch;
benchmarkStatus.append('[$size MB] Decryption took $benchmark ms\n');
csv += "$benchmark;";
cryptoTime += benchmark;
csv += "$cryptoTime\n";
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,34 +147,36 @@ class BenchmarkPage extends ConsumerWidget {
alignment: Alignment.centerLeft,
),
keyContent,
Column(
crossAxisAlignment: CrossAxisAlignment.start,
Wrap(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Button(
() => _benchmark(ref, cipher),
"NativeCrypto",
),
Button(
() => _benchmark(ref, cipher, usePc: true),
"PointyCastle",
),
],
Button(
() => _benchmark(ref, cipher),
"NativeCrypto",
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Button(
() => _benchmarkEncryptionOnly(ref, cipher),
"NC Persistence",
),
Button(
_clear,
"Clear",
),
],
const SizedBox(width: 8),
Button(
() => _benchmark(ref, cipher, usePc: true),
"PointyCastle",
),
const SizedBox(width: 8),
Button(
() => _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",
),
],
),

View File

@ -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