rxdb-server
Version:
RxDB Server Plugin
276 lines (266 loc) • 8.51 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.performanceTest = performanceTest;
var _core = require("rxdb/plugins/core");
var _index = require("./index.js");
var _index2 = require("../client-rest/index.js");
/**
* A reusable performance test suite for RxDB server adapters.
* This function can be exported and used by consumers who
* create custom adapters to verify their performance characteristics.
*/
/**
* Simple document schema for performance testing.
*/
var PERF_SCHEMA = {
version: 0,
primaryKey: 'id',
type: 'object',
properties: {
id: {
type: 'string',
maxLength: 100
},
name: {
type: 'string'
},
value: {
type: 'number'
}
},
required: ['id', 'name', 'value']
};
function generateDocs(count) {
var docs = [];
for (var i = 0; i < count; i++) {
docs.push({
id: (0, _core.randomToken)(12) + '-' + i,
name: 'doc-name-' + i + '-' + (0, _core.randomToken)(6),
value: i
});
}
return docs;
}
async function createCollectionWithDocs(db, docs) {
var colName = 'perf' + (0, _core.randomToken)(8).toLowerCase();
var collections = await db.addCollections({
[colName]: {
schema: PERF_SCHEMA
}
});
var col = collections[colName];
if (docs.length > 0) {
await col.bulkInsert(docs);
}
return col;
}
/**
* Runs a set of performance tests against the given adapter.
* Each test creates its own server and collection, measures
* the time for the operations, and tears down afterwards.
*
* Returns an array of results that callers can assert against
* or log for comparison purposes.
*/
async function performanceTest(options) {
var batchSize = options.batchSize ?? 30;
var results = [];
// ---------- Test 1: writes via REST /set ----------
{
var db = await options.createDatabase();
var col = await createCollectionWithDocs(db, []);
var port = await options.getPort();
var server = await (0, _index.createRxServer)({
adapter: options.adapter,
database: db,
port
});
var endpoint = await server.addRestEndpoint({
name: (0, _core.randomToken)(10),
collection: col
});
await server.start();
var client = (0, _index2.createRestClient)('http://localhost:' + port + '/' + endpoint.urlPath, {});
var docs = generateDocs(batchSize);
var start = performance.now();
await client.set(docs);
var timeMs = performance.now() - start;
results.push({
name: 'REST /set (bulk write ' + batchSize + ' docs)',
timeMs,
opsCount: batchSize,
opsPerSecond: Math.round(batchSize / timeMs * 1000)
});
await db.close();
}
// ---------- Test 2: query via REST /query ----------
{
var _docs = generateDocs(batchSize);
var _db = await options.createDatabase();
var _col = await createCollectionWithDocs(_db, _docs);
var _port = await options.getPort();
var _server = await (0, _index.createRxServer)({
adapter: options.adapter,
database: _db,
port: _port
});
var _endpoint = await _server.addRestEndpoint({
name: (0, _core.randomToken)(10),
collection: _col
});
await _server.start();
var _client = (0, _index2.createRestClient)('http://localhost:' + _port + '/' + _endpoint.urlPath, {});
var iterations = 10;
var _start = performance.now();
for (var i = 0; i < iterations; i++) {
await _client.query({
selector: {}
});
}
var _timeMs = performance.now() - _start;
results.push({
name: 'REST /query (' + iterations + ' queries, ' + batchSize + ' docs each)',
timeMs: _timeMs,
opsCount: iterations,
opsPerSecond: Math.round(iterations / _timeMs * 1000)
});
await _db.close();
}
// ---------- Test 3: get by IDs via REST /get ----------
{
var _docs2 = generateDocs(batchSize);
var _db2 = await options.createDatabase();
var _col2 = await createCollectionWithDocs(_db2, _docs2);
var _port2 = await options.getPort();
var _server2 = await (0, _index.createRxServer)({
adapter: options.adapter,
database: _db2,
port: _port2
});
var _endpoint2 = await _server2.addRestEndpoint({
name: (0, _core.randomToken)(10),
collection: _col2
});
await _server2.start();
var _client2 = (0, _index2.createRestClient)('http://localhost:' + _port2 + '/' + _endpoint2.urlPath, {});
var ids = _docs2.map(d => d.id);
var _iterations = 10;
var _start2 = performance.now();
for (var _i = 0; _i < _iterations; _i++) {
await _client2.get(ids);
}
var _timeMs2 = performance.now() - _start2;
results.push({
name: 'REST /get (' + _iterations + ' fetches, ' + batchSize + ' ids each)',
timeMs: _timeMs2,
opsCount: _iterations,
opsPerSecond: Math.round(_iterations / _timeMs2 * 1000)
});
await _db2.close();
}
// ---------- Test 4: delete via REST /delete ----------
{
var _docs3 = generateDocs(batchSize);
var _db3 = await options.createDatabase();
var _col3 = await createCollectionWithDocs(_db3, _docs3);
var _port3 = await options.getPort();
var _server3 = await (0, _index.createRxServer)({
adapter: options.adapter,
database: _db3,
port: _port3
});
var _endpoint3 = await _server3.addRestEndpoint({
name: (0, _core.randomToken)(10),
collection: _col3
});
await _server3.start();
var _client3 = (0, _index2.createRestClient)('http://localhost:' + _port3 + '/' + _endpoint3.urlPath, {});
var _ids = _docs3.map(d => d.id);
var _start3 = performance.now();
await _client3.delete(_ids);
var _timeMs3 = performance.now() - _start3;
results.push({
name: 'REST /delete (bulk delete ' + batchSize + ' docs)',
timeMs: _timeMs3,
opsCount: batchSize,
opsPerSecond: Math.round(batchSize / _timeMs3 * 1000)
});
await _db3.close();
}
// ---------- Test 5: sequential single-doc writes ----------
{
var _db4 = await options.createDatabase();
var _col4 = await createCollectionWithDocs(_db4, []);
var _port4 = await options.getPort();
var _server4 = await (0, _index.createRxServer)({
adapter: options.adapter,
database: _db4,
port: _port4
});
var _endpoint4 = await _server4.addRestEndpoint({
name: (0, _core.randomToken)(10),
collection: _col4
});
await _server4.start();
var _client4 = (0, _index2.createRestClient)('http://localhost:' + _port4 + '/' + _endpoint4.urlPath, {});
var count = batchSize;
var _start4 = performance.now();
for (var _i2 = 0; _i2 < count; _i2++) {
await _client4.set([{
id: (0, _core.randomToken)(12) + '-' + _i2,
name: 'seq-' + _i2,
value: _i2
}]);
}
var _timeMs4 = performance.now() - _start4;
results.push({
name: 'REST /set sequential (' + count + ' single-doc writes)',
timeMs: _timeMs4,
opsCount: count,
opsPerSecond: Math.round(count / _timeMs4 * 1000)
});
await _db4.close();
}
// ---------- Test 6: replication pull via HTTP ----------
{
var _docs4 = generateDocs(batchSize);
var _db5 = await options.createDatabase();
var _col5 = await createCollectionWithDocs(_db5, _docs4);
var _port5 = await options.getPort();
var _server5 = await (0, _index.createRxServer)({
adapter: options.adapter,
database: _db5,
port: _port5
});
var _endpoint5 = await _server5.addReplicationEndpoint({
name: (0, _core.randomToken)(10),
collection: _col5
});
await _server5.start();
var url = 'http://localhost:' + _port5 + '/' + _endpoint5.urlPath;
var _iterations2 = 10;
var _start5 = performance.now();
for (var _i3 = 0; _i3 < _iterations2; _i3++) {
var response = await fetch(url + '/pull?lwt=0&id=&limit=' + batchSize, {
method: 'GET',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
});
await response.json();
}
var _timeMs5 = performance.now() - _start5;
results.push({
name: 'Replication /pull (' + _iterations2 + ' pulls, ' + batchSize + ' docs each)',
timeMs: _timeMs5,
opsCount: _iterations2,
opsPerSecond: Math.round(_iterations2 / _timeMs5 * 1000)
});
await _db5.close();
}
return results;
}
//# sourceMappingURL=performance-test.js.map