Skip to content

Commit

Permalink
Merge pull request #45 from IO-Design-Team/feature/better-storage-ben…
Browse files Browse the repository at this point in the history
…chmark

Better storage benchmark
  • Loading branch information
Rexios80 authored Nov 5, 2024
2 parents 63f7fe1 + 0bf1aa4 commit 38050a5
Show file tree
Hide file tree
Showing 21 changed files with 115 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
# Created by `dart pub`
.dart_tool/

hive_registrar.g.dart
hive_registrar.g.dart
results.csv
File renamed without changes.
80 changes: 80 additions & 0 deletions benchmarks/storage/bin/bench.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import 'dart:io';

import 'package:csv/csv.dart';
import 'package:hive/hive.dart' as v4;
import 'package:hive_ce/hive.dart' as ce;
import 'package:hive_storage_benchmark/bench_result.dart';
import 'package:hive_storage_benchmark/benchmark.dart';
import 'package:hive_storage_benchmark/db_type.dart';
import 'package:hive_storage_benchmark/test_model.dart';
import 'package:isar/isar.dart';

const benchmarks = [
10,
100,
1000,
10000,
100000,
1000000,
];

void main() async {
ce.Hive
..init('.')
..registerAdapter(TestModelAdapter());

await Isar.initialize('assets/libisar_macos.dylib');

v4.Hive.defaultDirectory = '.';
v4.Hive.registerAdapter('TestModel', (json) => TestModel.fromJson(json));

final ceResults = <BenchResult>[];
final v4Results = <BenchResult>[];

for (final operations in benchmarks) {
final ceResult = await runBenchmark(
operations: operations,
type: DbType.hive,
openBox: ce.Hive.openBox,
);

final v4Result = await runBenchmark(
operations: operations,
type: DbType.isar,
openBox: (name) => v4.Hive.box(name: name, maxSizeMiB: 1024),
);

ceResults.add(ceResult);
v4Results.add(v4Result);
}

final csv = const ListToCsvConverter().convert([
[
'Operations',
'Hive CE Time',
'Hive CE Size',
'Hive v4 Time',
'Hive v4 Size',
],
for (var i = 0; i < benchmarks.length; i++)
[
benchmarks[i],
formatTime(ceResults[i].time),
formatSize(ceResults[i].size),
formatTime(v4Results[i].time),
formatSize(v4Results[i].size),
],
]);

File('results.csv').writeAsStringSync(csv);
}

// Format a duration to "00.00 s"
String formatTime(Duration duration) {
final seconds = duration.inMilliseconds / 1000;
return '${seconds.toStringAsFixed(2)} s';
}

String formatSize(double size) {
return '${size.toStringAsFixed(2)} MB';
}
3 changes: 0 additions & 3 deletions benchmarks/storage/hive_ce/.gitignore

This file was deleted.

1 change: 0 additions & 1 deletion benchmarks/storage/hive_ce/analysis_options.yaml

This file was deleted.

15 changes: 0 additions & 15 deletions benchmarks/storage/hive_ce/bin/benchmark.dart

This file was deleted.

17 changes: 0 additions & 17 deletions benchmarks/storage/hive_ce/pubspec.yaml

This file was deleted.

2 changes: 0 additions & 2 deletions benchmarks/storage/hive_ce/puby.yaml

This file was deleted.

3 changes: 0 additions & 3 deletions benchmarks/storage/hive_v4/.gitignore

This file was deleted.

1 change: 0 additions & 1 deletion benchmarks/storage/hive_v4/analysis_options.yaml

This file was deleted.

17 changes: 0 additions & 17 deletions benchmarks/storage/hive_v4/bin/benchmark.dart

This file was deleted.

14 changes: 0 additions & 14 deletions benchmarks/storage/hive_v4/pubspec.yaml

This file was deleted.

2 changes: 0 additions & 2 deletions benchmarks/storage/hive_v4/puby.yaml

This file was deleted.

9 changes: 9 additions & 0 deletions benchmarks/storage/lib/bench_result.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class BenchResult {
final Duration time;
final double size;

const BenchResult({
required this.time,
required this.size,
});
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import 'dart:async';
import 'dart:io';

import 'package:common_storage_benchmark/db_type.dart';
import 'package:common_storage_benchmark/test_model.dart';
import 'package:hive_storage_benchmark/bench_result.dart';
import 'package:hive_storage_benchmark/db_type.dart';
import 'package:hive_storage_benchmark/test_model.dart';

const _boxName = 'test_box';
const _operations = 1000000;
const _model = TestModel(
testModelFieldZero: 0,
testModelFieldOne: 1,
Expand All @@ -19,29 +19,35 @@ const _model = TestModel(
testModelFieldNine: 9,
);

Future<void> runBenchmark({
Future<BenchResult> runBenchmark({
required int operations,
required DbType type,
required FutureOr<dynamic> Function(String name) openBox,
}) async {
print('');
print('Running $operations operation ${type.name} benchmark...');

var box = await openBox(_boxName);
await box.deleteFromDisk();
box = await openBox(_boxName);

final stopwatch = Stopwatch()..start();
for (var i = 0; i < _operations; i++) {
for (var i = 0; i < operations; i++) {
if (i % 10000 == 0) {
print('Operation: $i');
}
await box.add(_model);
}

final elapsed = stopwatch.elapsed;

final boxFile = File(type.boxFileName(_boxName));
final size = boxFile.lengthSync();
final megabytes = (size / 1024 / 1024).toStringAsFixed(2);
final megabytes = size / 1024 / 1024;
final megabytesString = megabytes.toStringAsFixed(2);

print('');
print('DB Type: $type');
print('Operations: $_operations');
print('Time: ${stopwatch.elapsed}');
print('Size: $megabytes MB');
print('Time: $elapsed');
print('Size: $megabytesString MB');

return BenchResult(time: elapsed, size: megabytes);
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: common_storage_benchmark
name: hive_storage_benchmark
publish_to: none

environment:
Expand All @@ -7,6 +7,9 @@ environment:
dependencies:
json_annotation: ^4.9.0
hive_ce: any
hive: ^4.0.0-dev.2
isar: ^4.0.0-dev.14
csv: ^6.0.0

dev_dependencies:
rexios_lints: ^8.2.0
Expand All @@ -16,6 +19,6 @@ dev_dependencies:

dependency_overrides:
hive_ce:
path: ../../../hive
path: ../../hive
hive_ce_generator:
path: ../../../hive_generator
path: ../../hive_generator
2 changes: 1 addition & 1 deletion hive/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ This is a comparison of the time to complete a given number of write operations

Database size in Hive v4 is directly affected by the length of field names in model classes which is not ideal. Also Hive v4 is much slower than Hive CE for large numbers of operations.

The benchmark was performed on an M3 Max MacBook Pro. You can [see the benchmark code here](../benchmarks/storage/common/lib/benchmark.dart).
The benchmark was performed on an M3 Max MacBook Pro. You can [see the benchmark code here](../benchmarks/storage/bin/bench.dart).

## Migrating from Hive v2

Expand Down

0 comments on commit 38050a5

Please sign in to comment.