Skip to content

Commit

Permalink
[app_crypto] feat add perf test
Browse files Browse the repository at this point in the history
  • Loading branch information
alextekartik committed Jun 16, 2024
1 parent 358cf04 commit 1aebdc9
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 5 deletions.
6 changes: 5 additions & 1 deletion app_crypto/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
# tekartik strict extension
include: package:tekartik_lints/strict.yaml
include: package:tekartik_lints/strict.yaml

linter:
rules:
# avoid_print: true
4 changes: 3 additions & 1 deletion app_crypto/bin/generate_password.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'dart:io';

import 'package:tekartik_app_crypto/src/generate_password.dart';

Future<void> main() async {
print(generatePassword());
stdout.writeln(generatePassword());
}
2 changes: 1 addition & 1 deletion app_crypto/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions app_crypto/test/aes_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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++) {
Expand Down
93 changes: 93 additions & 0 deletions app_crypto/test/manual_perf_test_.dart
Original file line number Diff line number Diff line change
@@ -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);
}));
}
});
}
});
}

0 comments on commit 1aebdc9

Please sign in to comment.