realm-js/examples/ReactNativeBenchmarks/benchmarks.js

502 lines
14 KiB
JavaScript
Raw Normal View History

2016-05-09 19:36:25 +00:00
////////////////////////////////////////////////////////////////////////////
//
// Copyright 2016 Realm Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////
2016-02-15 21:52:38 +00:00
'use strict';
2016-05-16 21:15:56 +00:00
import React from 'react';
import {
2016-02-15 21:52:38 +00:00
Component,
StyleSheet,
Text,
View,
ListView,
TouchableHighlight
} from 'react-native';
import Store from 'react-native-store';
import SQLite from 'react-native-sqlite-storage';
import Realm from 'realm';
2016-02-15 21:52:38 +00:00
// Make SQLite module use Promises.
SQLite.enablePromise(true);
const TestObjectSchema = {
name: "TestObject",
properties: {
int: "int",
double: "double",
date: "date",
string: "string"
}
}
2016-02-16 20:50:01 +00:00
const numTestObjects = 100;
const numBatchTestObjects = numTestObjects * 1000;
const numRepeats = 1;
2016-02-16 21:15:50 +00:00
const numQueryBuckets = 100;
2016-02-15 21:52:38 +00:00
2016-02-16 01:43:43 +00:00
const tests = ["insertions", "binsertions", "enumeration", "querycount", "queryenum"];
const expectedCounts = {
insertions: numTestObjects,
binsertions: numBatchTestObjects,
2016-02-16 01:43:43 +00:00
enumeration: numBatchTestObjects,
querycount: numBatchTestObjects,
queryenum: numBatchTestObjects
};
const expectedResults = {
insertions: numTestObjects,
binsertions: numBatchTestObjects,
2016-02-16 01:43:43 +00:00
enumeration: numBatchTestObjects,
querycount: numBatchTestObjects / (numQueryBuckets * 2),
queryenum: numBatchTestObjects / (numQueryBuckets * 2)
};
2016-02-15 21:52:38 +00:00
class Tests {
async setup(testName) {
var count = await this.count();
if (testName == 'enumeration' || testName == 'querycount' || testName == 'queryenum') {
if (count != expectedCounts[testName]) {
throw "Incorrect count " + count + " for test " + testName;
}
2016-02-15 21:52:38 +00:00
}
else {
if (count != 0) {
throw "Initial count should be 0 for insertion tests";
}
2016-02-15 21:52:38 +00:00
}
}
async binsertions() {
return await this.batchInsert(this.testObjects(numBatchTestObjects));
2016-02-15 21:52:38 +00:00
}
objectValues(object) {
return Object.keys(TestObjectSchema.properties).map((prop) => object[prop])
}
testObjects(count) {
var objects = [];
2016-02-15 21:52:38 +00:00
for (let i = 0; i < count; i++) {
objects.push({ int: i % numQueryBuckets, double: i, date: new Date(i), string: "" + i });
}
return objects;
}
}
class RealmTests extends Tests {
constructor() {
super();
this.name = 'Realm';
}
async setup(testName) {
if (testName == "insertions" || testName == "binsertions") {
Realm.clearTestState();
}
this.realm = new Realm({schema: [TestObjectSchema]});
2016-02-15 21:52:38 +00:00
await super.setup(testName);
}
async insertions() {
var realm = this.realm;
2016-02-16 20:50:01 +00:00
var objects = this.testObjects(numTestObjects);
for (var i = 0; i < objects.length; i++) {
var obj = objects[i];
realm.write(() => {
realm.create("TestObject", obj);
});
}
return numTestObjects;
2016-02-15 21:52:38 +00:00
}
async batchInsert(objects) {
var realm = this.realm;
realm.write(() => {
2016-02-16 20:50:01 +00:00
for (var i = 0; i < objects.length; i++) {
var obj = objects[i];
realm.create("TestObject", obj);
}
2016-02-15 21:52:38 +00:00
});
return objects.length;
2016-02-15 21:52:38 +00:00
}
async enumeration() {
let objects = this.realm.objects('TestObject');
let len = objects.length;
for (let i = 0; i < len; i++) {
var obj = objects[i];
2016-02-15 21:52:38 +00:00
obj.int;
obj.double;
obj.date;
obj.string;
}
return len;
2016-02-15 21:52:38 +00:00
}
async querycount() {
2016-02-18 05:23:43 +00:00
let objects = this.realm.objects('TestObject').filtered('int = 0 and double < ' + numBatchTestObjects / 2);
return objects.length;
2016-02-15 21:52:38 +00:00
}
async queryenum() {
2016-02-18 05:23:43 +00:00
let objects = this.realm.objects('TestObject').filtered('int = 0 and double < ' + numBatchTestObjects / 2);
2016-02-15 21:52:38 +00:00
let len = objects.length;
for (let i = 0; i < len; i++) {
var obj = objects[i];
2016-02-15 21:52:38 +00:00
obj.int;
obj.double;
obj.date;
obj.string;
}
return len;
2016-02-15 21:52:38 +00:00
}
async count() {
return this.realm.objects('TestObject').length;
}
2016-02-15 21:52:38 +00:00
}
class RNStoreTests extends Tests {
constructor() {
super();
this.name = "RNStore";
}
async setup(testName) {
this.db = Store.model('objects');
if (testName == "insertions" || testName == "binsertions") {
await this.db.destroy();
}
2016-02-15 21:52:38 +00:00
await super.setup(testName);
}
async insertions() {
2016-02-16 20:50:01 +00:00
var objects = this.testObjects(numTestObjects);
for (var i = 0; i < objects.length; i++) {
var obj = objects[i];
2016-02-16 01:52:40 +00:00
obj.date = obj.date.getTime();
2016-02-15 21:52:38 +00:00
await this.db.add(obj);
}
return numTestObjects;
2016-02-15 21:52:38 +00:00
}
async batchInsert(objects) {
await this.db.multiAdd(objects);
2016-02-16 20:50:01 +00:00
for (var i = 0; i < objects.length; i++) {
var obj = objects[i];
2016-02-16 01:52:40 +00:00
obj.date = obj.date.getTime();
}
return objects.length;
2016-02-15 21:52:38 +00:00
}
async enumeration() {
let objects = await this.db.find();
let len = objects.length;
for (let i = 0; i < len; i++) {
var obj = objects[i];
2016-02-15 21:52:38 +00:00
obj.int;
obj.double;
2016-02-16 01:52:40 +00:00
new Date(obj.date);
2016-02-15 21:52:38 +00:00
obj.string;
}
return len;
2016-02-15 21:52:38 +00:00
}
async querycount() {
let objects = await this.db.find({
where: {
and: [{ int: 0 }, { double: { lt: numBatchTestObjects / 2 } }]
2016-02-15 21:52:38 +00:00
},
order: {
age: 'ASC',
}
});
return objects.length;
2016-02-15 21:52:38 +00:00
}
async queryenum() {
let objects = await this.db.find({
where: {
and: [{ int: 0 }, { double: { lt: numBatchTestObjects / 2 } }]
2016-02-15 21:52:38 +00:00
},
order: {
age: 'ASC',
}
});
let len = objects.length;
for (let i = 0; i < len; i++) {
var obj = objects[i];
2016-02-15 21:52:38 +00:00
obj.int;
obj.double;
obj.date;
obj.string;
}
return len
2016-02-15 21:52:38 +00:00
}
async count() {
let objects = await this.db.find();
return objects != undefined ? objects.length : 0;
}
2016-02-15 21:52:38 +00:00
}
class RNSqliteTests extends Tests {
constructor() {
super();
this.name = "RNSqlite";
}
async setup(testName) {
if (testName == "insertions" || testName == "binsertions") {
try {
await SQLite.deleteDatabase('test.db');
} catch (e) {}
}
2016-02-15 21:52:38 +00:00
this.db = await SQLite.openDatabase("test.db", "1.0", "Test Database", 200000);
await this.db.transaction((tx) => {
tx.executeSql('CREATE TABLE IF NOT EXISTS t1 (string VARCHAR(100), int INTEGER, double REAL, date INTEGER);');
2016-02-15 21:52:38 +00:00
});
await super.setup(testName);
}
async insertions() {
2016-02-16 20:50:01 +00:00
var objects = this.testObjects(numTestObjects);
for (var i = 0; i < objects.length; i++) {
var obj = objects[i];
let values = [obj.string, obj.int, obj.double, obj.date.getTime()];
await this.db.transaction((tx) => {
2016-02-15 21:52:38 +00:00
tx.executeSql('INSERT INTO t1 (string, int, double, date) VALUES (?,?,?,?);', values);
});
}
return numTestObjects;
2016-02-15 21:52:38 +00:00
}
async batchInsert(objects) {
await this.db.transaction((tx) => {
2016-02-16 20:50:01 +00:00
for (var i = 0; i < objects.length; i++) {
var obj = objects[i];
let values = [obj.string, obj.int, obj.double, obj.date.getTime()];
2016-02-15 21:52:38 +00:00
tx.executeSql('INSERT INTO t1 (string, int, double, date) VALUES (?,?,?,?);', values);
}
});
return objects.length;
2016-02-15 21:52:38 +00:00
}
async enumeration() {
var len;
2016-02-15 21:52:38 +00:00
await this.db.readTransaction(async (tx) => {
let [, results] = await tx.executeSql('SELECT * FROM t1;')
len = results.rows.length;
2016-02-15 21:52:38 +00:00
for (let i = 0; i < len; i++) {
var row = results.rows.item(i);
2016-02-15 21:52:38 +00:00
row.int;
row.double;
row.string;
new Date(row.date);
}
});
return len;
2016-02-15 21:52:38 +00:00
}
async querycount() {
var len;
2016-02-16 00:52:10 +00:00
await this.db.readTransaction(async (tx) => {
let [, results] = await tx.executeSql('SELECT * FROM t1 WHERE int = 0 AND double < ' + numBatchTestObjects/2 + ';');
len = results.rows.length;
2016-02-15 21:52:38 +00:00
});
return len;
2016-02-15 21:52:38 +00:00
}
async queryenum() {
var len;
2016-02-15 21:52:38 +00:00
await this.db.readTransaction(async (tx) => {
let [, results] = await tx.executeSql('SELECT * FROM t1 WHERE int = 0 AND double < ' + numBatchTestObjects/2 + ';');
len = results.rows.length;
2016-02-15 21:52:38 +00:00
for (let i = 0; i < len; i++) {
var row = results.rows.item(i);
2016-02-15 21:52:38 +00:00
row.int;
row.double;
row.string;
new Date(row.date);
}
});
return len;
2016-02-15 21:52:38 +00:00
}
async count() {
var len;
await this.db.readTransaction(async (tx) => {
let [, results] = await tx.executeSql('SELECT * FROM t1;')
len = results.rows.length;
});
return len;
}
2016-02-15 21:52:38 +00:00
}
const apiTests = [new RealmTests, new RNSqliteTests, new RNStoreTests];
export default class ReactNativeBenchmarks extends Component {
2016-02-15 21:52:38 +00:00
constructor(props) {
super(props);
this.state = {
dataSource: new ListView.DataSource({
rowHasChanged: (row1, row2) => row1 !== row2,
}),
running: false,
};
this._renderRow = this._renderRow.bind(this);
this._runTests = this._runTests.bind(this);
}
render() {
return (
<View style={styles.container}>
<Text style={styles.title}>
ReactNative Storage Benchmarks
</Text>
<TouchableHighlight style={styles.button} onPress={this._runTests}>
<Text style={styles.buttonText}>
Run
</Text>
</TouchableHighlight>
<ListView contentContainerStyle={styles.list}
dataSource={this.state.dataSource}
renderRow={this._renderRow}
/>
</View>
);
}
_renderRow(rowData) {
return (
<Text style={styles.item}>{rowData.join('\t\t')}</Text>
2016-02-15 21:52:38 +00:00
);
}
async _runTests() {
if (this.state.running) {
console.log("DISABLED");
return;
}
this.setState({running: true});
try {
await this._runTestsAsync();
} catch (e) {
console.error('Error running tests:', e);
}
this.setState({running: false});
}
async _runTestsAsync() {
var data = [apiTests.map((api) => api.name)];
data[0].splice(0, 0, "\t\t");
2016-02-16 20:50:01 +00:00
for (var i = 0; i < tests.length; i++) {
var test = tests[i];
2016-02-15 21:52:38 +00:00
data.push([test]);
this.setState({
dataSource: this.state.dataSource.cloneWithRows(data),
});
2016-02-16 20:50:01 +00:00
for (var j = 0; j < apiTests.length; j++) {
var apiTest = apiTests[j];
2016-02-15 21:52:38 +00:00
var totalTime = 0;
console.log("Running " + apiTest.name + "." + test);
2016-02-16 20:50:01 +00:00
for (var k = 0; k < numRepeats; k++) {
2016-02-15 21:52:38 +00:00
await apiTest.setup(test);
var startTime = Date.now();
var result = await apiTest[test]();
2016-02-15 21:52:38 +00:00
var endTime = Date.now();
if (result != expectedResults[test]) {
throw "Incorrect result " + result + " for test " + apiTest.name + "." + test;
}
var count = await apiTest.count();
if (count != expectedCounts[test]) {
throw "Incorrect count " + count + " for test " + apiTest.name + "." + test;
}
2016-02-15 21:52:38 +00:00
var time = endTime - startTime
console.log("finished in " + time);
totalTime += time;
}
data = data.slice();
let last = data.length-1;
data[last] = data[last].slice();
data[last].push(totalTime / numRepeats);
this.setState({
dataSource: this.state.dataSource.cloneWithRows(data),
});
}
}
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
title: {
fontSize: 20,
textAlign: 'center',
margin: 20,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
list: {
justifyContent: 'center',
flexDirection: 'column',
flexWrap: 'wrap',
},
button: {
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#CCC',
width: 100,
height: 40
},
row: {
flexDirection: 'row',
},
item: {
textAlign: 'center',
padding: 3,
margin: 3,
fontSize: 12
}
});