Skip to content

Commit

Permalink
Benchmark moor_ffi
Browse files Browse the repository at this point in the history
  • Loading branch information
simolus3 committed Sep 24, 2019
1 parent 56d56a0 commit 42d889d
Show file tree
Hide file tree
Showing 11 changed files with 453 additions and 242 deletions.
1 change: 1 addition & 0 deletions android/gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
org.gradle.jvmargs=-Xmx1536M
android.enableR8=true
276 changes: 52 additions & 224 deletions lib/benchmark.dart
Original file line number Diff line number Diff line change
@@ -1,19 +1,33 @@
import 'dart:io';
import 'dart:math';

import 'package:hive/hive.dart';
import 'package:hive_benchmark/sqlite_store.dart';
import 'package:path/path.dart' as path;
import 'package:path_provider/path_provider.dart';
import 'package:hive_benchmark/runners/hive.dart';
import 'package:hive_benchmark/runners/moor_ffi.dart';
import 'package:hive_benchmark/runners/runner.dart';
import 'package:hive_benchmark/runners/shared_preferences.dart';
import 'package:hive_benchmark/runners/sqflite.dart';
import 'package:random_string/random_string.dart' as randStr;
import 'package:shared_preferences/shared_preferences.dart';

const String TABLE_NAME_STR = "kv_str";
const String TABLE_NAME_INT = "kv_int";

class Result {
final BenchmarkRunner runner;
int intTime;
int stringTime;

Result(this.runner);
}

final runners = [
HiveRunner(false),
HiveRunner(true),
// SqfliteRunner(),
// SharedPreferencesRunner(),
MoorFfiRunner(),
];

List<Result> _createResults() {
return runners.map((r) => Result(r)).toList();
}

Map<String, int> generateIntEntries(int count) {
Expand All @@ -37,256 +51,70 @@ Map<String, String> generateStringEntries(int count) {
return map;
}

Future prepareHive() async {
var dir = await getApplicationDocumentsDirectory();
var homePath = path.join(dir.path, 'hive');
if (await Directory(homePath).exists()) {
await Directory(homePath).delete(recursive: true);
}
await Directory(homePath).create();
Hive.init(homePath);
}

Future<SqfliteStore> getSqliteStore() async {
var store = SqfliteStore();
await store.init();
return store;
}

Future prepareSharedPrefs() async {
var prefs = await SharedPreferences.getInstance();
await prefs.clear();
}

Future<int> hiveBatchRead(List<String> keys, bool lazy) async {
var box = await Hive.openBox('box', lazy: lazy);
var s = Stopwatch()..start();
for (var key in keys) {
box.get(key);
}
s.stop();
await box.close();
return s.elapsedMilliseconds;
}

Future<int> sqliteBatchReadInt(SqfliteStore store, List<String> keys) async {
var s = Stopwatch()..start();
for (var key in keys) {
await store.getInt(key);
}
s.stop();
return s.elapsedMilliseconds;
}

Future<int> sqliteBatchReadString(SqfliteStore store, List<String> keys) async {
var s = Stopwatch()..start();
for (var key in keys) {
await store.getString(key);
}
s.stop();
return s.elapsedMilliseconds;
}

Future<int> sharedPrefsBatchReadInt(List<String> keys) async {
var prefs = await SharedPreferences.getInstance();
var s = Stopwatch()..start();
for (var key in keys) {
prefs.getInt(key);
}
s.stop();
return s.elapsedMilliseconds;
}

Future<int> sharedPrefsReadString(List<String> keys) async {
var prefs = await SharedPreferences.getInstance();
var s = Stopwatch()..start();
for (var key in keys) {
prefs.getString(key);
}
s.stop();
return s.elapsedMilliseconds;
}

Future<List<Result>> benchmarkRead(int count) async {
var results = [Result(), Result(), Result(), Result()];
await prepareHive();
var store = await getSqliteStore();
await prepareSharedPrefs();
var results = _createResults();

var intEntries = generateIntEntries(count);
var intKeys = intEntries.keys.toList()..shuffle();
await hiveBatchWrite(intEntries);
await sqliteBatchWriteInt(store, intEntries);
await sharedPrefsBatchWriteInt(intEntries);
results[0].intTime = await hiveBatchRead(intKeys, false);
results[1].intTime = await hiveBatchRead(intKeys, true);
results[2].intTime = await sqliteBatchReadInt(store, intKeys);
results[3].intTime = await sharedPrefsBatchReadInt(intKeys);

await prepareHive();
await prepareSharedPrefs();
for (var result in results) {
await result.runner.setUp();
await result.runner.batchWriteInt(intEntries);
result.intTime = await result.runner.batchReadInt(intKeys);
}

var stringEntries = generateStringEntries(count);
var stringKeys = stringEntries.keys.toList()..shuffle();
await hiveBatchWrite(stringEntries);
await sqliteBatchWriteString(store, stringEntries);
await sharedPrefsBatchWriteString(stringEntries);
results[0].stringTime = await hiveBatchRead(stringKeys, false);
results[1].stringTime = await hiveBatchRead(stringKeys, true);
results[2].stringTime = await sqliteBatchReadString(store, stringKeys);
results[3].stringTime = await sharedPrefsReadString(stringKeys);

await Hive.close();
await store.close();

return results;
}

Future<int> hiveBatchWrite(Map<String, dynamic> entries) async {
var box = await Hive.openBox('box', lazy: true);
var s = Stopwatch()..start();
for (var key in entries.keys) {
await box.put(key, entries[key]);
}
s.stop();
await box.close();
return s.elapsedMilliseconds;
}

Future<int> sqliteBatchWriteInt(
SqfliteStore store, Map<String, int> entries) async {
var s = Stopwatch()..start();
for (var key in entries.keys) {
await store.putInt(key, entries[key]);
for (var result in results) {
await result.runner.batchWriteString(stringEntries);
result.stringTime = await result.runner.batchReadString(stringKeys);
}
s.stop();
return s.elapsedMilliseconds;
}

Future<int> sqliteBatchWriteString(
SqfliteStore store, Map<String, String> entries) async {
var s = Stopwatch()..start();
for (var key in entries.keys) {
await store.putString(key, entries[key]);
for (var result in results) {
await result.runner.tearDown();
}
s.stop();
return s.elapsedMilliseconds;
}

Future<int> sharedPrefsBatchWriteInt(Map<String, int> entries) async {
var s = Stopwatch()..start();
var prefs = await SharedPreferences.getInstance();
for (var key in entries.keys) {
await prefs.setInt(key, entries[key]);
}
s.stop();
return s.elapsedMilliseconds;
}

Future<int> sharedPrefsBatchWriteString(Map<String, String> entries) async {
var s = Stopwatch()..start();
var prefs = await SharedPreferences.getInstance();
for (var key in entries.keys) {
await prefs.setString(key, entries[key]);
}
s.stop();
return s.elapsedMilliseconds;
return results;
}

Future<List<Result>> benchmarkWrite(int count) async {
var results = [Result(), Result(), Result()];
await prepareHive();
var store = await getSqliteStore();
await prepareSharedPrefs();

final results = _createResults();
var intEntries = generateIntEntries(count);
results[0].intTime = await hiveBatchWrite(intEntries);
results[1].intTime = await sqliteBatchWriteInt(store, intEntries);
results[2].intTime = await sharedPrefsBatchWriteInt(intEntries);

await prepareHive();
await prepareSharedPrefs();

var stringEntries = generateStringEntries(count);
results[0].stringTime = await hiveBatchWrite(stringEntries);
results[1].stringTime = await sqliteBatchWriteString(store, stringEntries);
results[2].stringTime = await sharedPrefsBatchWriteString(stringEntries);

await Hive.close();
await store.close();

return results;
}

Future<int> hiveBatchDelete(List<String> keys) async {
var box = await Hive.openBox('box', lazy: true);
var s = Stopwatch()..start();
for (var key in keys) {
await box.delete(key);
}
s.stop();
await box.close();
return s.elapsedMilliseconds;
}
for (var result in results) {
await result.runner.setUp();
result.intTime = await result.runner.batchWriteInt(intEntries);
result.stringTime = await result.runner.batchWriteString(stringEntries);

Future<int> sqliteBatchDeleteInt(SqfliteStore store, List<String> keys) async {
var s = Stopwatch()..start();
for (var key in keys) {
await store.deleteInt(key);
await result.runner.tearDown();
}
s.stop();
return s.elapsedMilliseconds;
}

Future<int> sqliteBatchDeleteString(
SqfliteStore store, List<String> keys) async {
var s = Stopwatch()..start();
for (var key in keys) {
await store.deleteString(key);
}
s.stop();
return s.elapsedMilliseconds;
}

Future<int> sharedPrefsBatchDelete(List<String> keys) async {
var s = Stopwatch()..start();
var prefs = await SharedPreferences.getInstance();
for (var key in keys) {
await prefs.remove(key);
}
s.stop();
return s.elapsedMilliseconds;
return results;
}

Future<List<Result>> benchmarkDelete(int count) async {
var results = [Result(), Result(), Result()];
await prepareHive();
var store = await getSqliteStore();
await prepareSharedPrefs();
final results = _createResults();

var intEntries = generateIntEntries(count);
var intKeys = intEntries.keys.toList()..shuffle();
await hiveBatchWrite(intEntries);
await sqliteBatchWriteInt(store, intEntries);
await sharedPrefsBatchWriteInt(intEntries);
results[0].intTime = await hiveBatchDelete(intKeys);
results[1].intTime = await sqliteBatchDeleteInt(store, intKeys);
results[2].intTime = await sharedPrefsBatchDelete(intKeys);

await prepareHive();
await prepareSharedPrefs();
for (var result in results) {
await result.runner.setUp();
await result.runner.batchWriteInt(intEntries);
result.intTime = await result.runner.batchDeleteInt(intKeys);
}

var stringEntries = generateStringEntries(count);
var stringKeys = stringEntries.keys.toList()..shuffle();
await hiveBatchWrite(stringEntries);
await sqliteBatchWriteString(store, stringEntries);
await sharedPrefsBatchWriteString(stringEntries);
results[0].stringTime = await hiveBatchDelete(stringKeys);
results[1].stringTime = await sqliteBatchDeleteString(store, stringKeys);
results[2].stringTime = await sharedPrefsBatchDelete(stringKeys);
for (var result in results) {
await result.runner.batchWriteString(stringEntries);
result.stringTime = await result.runner.batchDeleteString(stringKeys);
}

await Hive.close();
await store.close();
for (var result in results) {
await result.runner.tearDown();
}

return results;
}
8 changes: 1 addition & 7 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import 'dart:collection';
import 'dart:io';
import 'dart:math';

import 'package:fl_chart/fl_chart.dart';
Expand Down Expand Up @@ -178,11 +176,7 @@ class BenchmarkResult extends StatelessWidget {
BenchmarkResult(this.results);

List<String> get labels {
if (results.length == 3) {
return ['Hive', 'SQLite', 'S.Prefs'];
} else {
return ['Hive', 'Lazy Hive', 'SQLite', 'S.Prefs'];
}
return results.map((r) => r.runner.name).toList();
}

int get maxResultTime {
Expand Down
Loading

0 comments on commit 42d889d

Please sign in to comment.