From 1aebdc93dbf4d64925caaffa907d91523ef96b11 Mon Sep 17 00:00:00 2001 From: Alexandre Roux Date: Sun, 16 Jun 2024 11:13:48 +0200 Subject: [PATCH] [app_crypto] feat add perf test --- app_crypto/analysis_options.yaml | 6 +- app_crypto/bin/generate_password.dart | 4 +- app_crypto/pubspec.yaml | 2 +- app_crypto/test/aes_test.dart | 4 +- app_crypto/test/manual_perf_test_.dart | 93 ++++++++++++++++++++++++++ 5 files changed, 104 insertions(+), 5 deletions(-) create mode 100644 app_crypto/test/manual_perf_test_.dart diff --git a/app_crypto/analysis_options.yaml b/app_crypto/analysis_options.yaml index e684295..12e20d7 100644 --- a/app_crypto/analysis_options.yaml +++ b/app_crypto/analysis_options.yaml @@ -1,2 +1,6 @@ # tekartik strict extension -include: package:tekartik_lints/strict.yaml \ No newline at end of file +include: package:tekartik_lints/strict.yaml + +linter: + rules: + # avoid_print: true \ No newline at end of file diff --git a/app_crypto/bin/generate_password.dart b/app_crypto/bin/generate_password.dart index b8bb77f..4e583c8 100644 --- a/app_crypto/bin/generate_password.dart +++ b/app_crypto/bin/generate_password.dart @@ -1,5 +1,7 @@ +import 'dart:io'; + import 'package:tekartik_app_crypto/src/generate_password.dart'; Future main() async { - print(generatePassword()); + stdout.writeln(generatePassword()); } diff --git a/app_crypto/pubspec.yaml b/app_crypto/pubspec.yaml index 65af546..770ef97 100644 --- a/app_crypto/pubspec.yaml +++ b/app_crypto/pubspec.yaml @@ -31,7 +31,7 @@ dev_dependencies: build_test: any build_web_compilers: any process_run: '>=0.12.0+1' - + cryptography: '>=1.0.0' _dependency_overrides: encrypt: git: diff --git a/app_crypto/test/aes_test.dart b/app_crypto/test/aes_test.dart index 3798c8a..ae96c6d 100644 --- a/app_crypto/test/aes_test.dart +++ b/app_crypto/test/aes_test.dart @@ -9,7 +9,7 @@ void main() { void aesRoundTrip(String decoded, String password) { var encrypter = aesEncrypterFromPassword(password); var encrypted = encrypter.encrypt(decoded); - print('${decoded.length}:${encrypted.length}'); + // print('${decoded.length}:${encrypted.length}'); encrypter = aesEncrypterFromPassword(password); expect(encrypter.decrypt(encrypted), decoded); } @@ -75,7 +75,7 @@ void main() { for (var i = 0; i < count; i++) { aes.decrypt(aes.encrypt(textWithLength(count * 10))); } - print('aes round trip: ${sw.elapsedMilliseconds} ms'); + // print('aes round trip: ${sw.elapsedMilliseconds} ms'); sw = Stopwatch()..start(); for (var i = 0; i < count; i++) { diff --git a/app_crypto/test/manual_perf_test_.dart b/app_crypto/test/manual_perf_test_.dart new file mode 100644 index 0000000..7f532da --- /dev/null +++ b/app_crypto/test/manual_perf_test_.dart @@ -0,0 +1,93 @@ +import 'package:tekartik_app_crypto/encrypt.dart'; +import 'package:tekartik_common_utils/num_utils.dart'; +import 'package:test/test.dart'; + +class _TestEncrypter { + final String name; + final StringEncrypter encrypter; + _TestEncrypter(this.name, this.encrypter); +} + +class _TestAction { + final String name; + final void Function() action; + _TestAction(this.name, this.action); +} + +String textWithLength(int length) { + return List.generate(length, (i) => i.toString().substring(0, 1)).join(); +} + +void main() { + var password = encryptTextPassword16FromText('password'); + var testEncrypters = [ + _TestEncrypter('aes', aesEncrypterFromPassword(password)), + _TestEncrypter('salsa20', salsa20EncrypterFromPassword(password)), + ]; + + group('perf', () { + for (var testEncrypter in testEncrypters) { + var name = testEncrypter.name; + test(name, () { + var encrypter = testEncrypter.encrypter; + for (var data in [ + textWithLength(10), + textWithLength(1000), + textWithLength(10000) + ]) { + var encryptedData = encrypter.encrypt(data); + expect(encrypter.decrypt(encryptedData), data); + + var initialEstimationSize = 10; + + int actionsDurationMs(int count, void Function() action) { + var sw = Stopwatch()..start(); + for (var i = 0; i < count; i++) { + action(); + } + sw.stop(); + return sw.elapsedMilliseconds; + } + + void execute(_TestAction testAction) { + void action() { + testAction.action(); + } + + var count = initialEstimationSize; + int ms; + while (true) { + ms = actionsDurationMs(count, action); + // print('count: $count ms: $ms'); + if (ms < 50) { + count = (count * 1.2).toInt(); + } else { + break; + } + } + var countPerMs = count / ms; + var durationTestMs = 500; + var countTest = (countPerMs * durationTestMs).toInt(); + + // print('countTest: $countTest (${(countPerMs * 1000).toInt()} per/s)'); + for (var i = 0; i < 1; i++) { + encryptedData = encrypter.encrypt(data); + + var msTest = actionsDurationMs(countTest, action).boundedMin(1); + countPerMs = countTest / msTest; + print( + '$name ${data.length} ${testAction.name}: (${(countPerMs * 1000).toInt()} per/s)'); + } + } + + execute(_TestAction('encrypt', () { + encrypter.encrypt(data); + })); + execute(_TestAction('decrypt', () { + encrypter.decrypt(encryptedData); + })); + } + }); + } + }); +}