1
0
mirror of https://github.com/xuthus83/pigallery2.git synced 2024-11-03 21:04:03 +08:00
pigallery2/benchmark/index.ts

78 lines
2.7 KiB
TypeScript
Raw Normal View History

2019-12-10 17:44:35 +08:00
import {Config} from '../src/common/config/private/Config';
2019-01-27 07:03:40 +08:00
import * as path from 'path';
2019-12-10 17:44:35 +08:00
import {ProjectPath} from '../src/backend/ProjectPath';
2019-01-27 07:03:40 +08:00
import {BenchmarkResult, Benchmarks} from './Benchmarks';
2019-12-10 17:44:35 +08:00
import {SearchTypes} from '../src/common/entities/AutoCompleteItem';
import {Utils} from '../src/common/Utils';
import {DiskMangerWorker} from '../src/backend/model/threading/DiskMangerWorker';
2019-01-27 07:03:40 +08:00
const config: { path: string, system: string } = require(path.join(__dirname, 'config.json'));
Config.Server.Media.folder = config.path;
2019-01-27 07:03:40 +08:00
const dbPath = path.join(__dirname, 'test.db');
ProjectPath.reset();
2019-01-27 07:28:04 +08:00
const RUNS = 50;
2019-01-27 07:03:40 +08:00
let resultsText = '';
const printLine = (text: string) => {
resultsText += text + '\n';
};
const printHeader = async () => {
const dt = new Date();
printLine('## PiGallery2 v' + require('./../package.json').version +
2019-01-27 12:47:43 +08:00
', ' + Utils.zeroPrefix(dt.getDate(), 2) +
2019-01-27 07:03:40 +08:00
'.' + Utils.zeroPrefix(dt.getMonth() + 1, 2) +
'.' + dt.getFullYear());
printLine('**System**: ' + config.system);
const dir = await DiskMangerWorker.scanDirectory('./');
2019-01-27 07:28:04 +08:00
printLine('**Gallery**: directories: ' +
dir.directories.length +
' media: ' + dir.media.length +
// @ts-ignore
', faces: ' + dir.media.reduce((p, c) => p + (c.metadata.faces || []).length, 0));
2019-01-27 07:03:40 +08:00
};
const printTableHeader = () => {
printLine('| action | action details | average time | details |');
printLine('|:------:|:--------------:|:------------:|:-------:|');
};
const printResult = (result: BenchmarkResult, action: string, actionDetails: string = '') => {
2019-01-27 07:28:04 +08:00
console.log('benchmarked: ' + action);
2019-01-27 07:03:40 +08:00
let details = '-';
if (result.items) {
details = 'items: ' + result.items;
}
if (result.media) {
details = 'media: ' + result.media + ', directories:' + result.directories;
}
printLine('| ' + action + ' | ' + actionDetails +
2019-01-27 07:28:04 +08:00
' | ' + (result.duration).toFixed(1) + 'ms | ' + details + ' |');
2019-01-27 07:03:40 +08:00
};
const run = async () => {
2019-01-27 07:28:04 +08:00
const start = Date.now();
2019-01-27 07:03:40 +08:00
const bm = new Benchmarks(RUNS, dbPath);
// header
await printHeader();
printTableHeader();
printResult(await bm.bmScanDirectory(), 'Scanning directory');
printResult(await bm.bmSaveDirectory(), 'Saving directory');
printResult(await bm.bmListDirectory(), 'Listing Directory');
(await bm.bmAllSearch('a')).forEach(res => {
if (res.searchType !== null) {
printResult(res.result, 'searching', '`a` as `' + SearchTypes[res.searchType] + '`');
} else {
printResult(res.result, 'searching', '`a` as `any`');
}
});
printResult(await bm.bmInstantSearch('a'), 'instant search', '`a`');
printResult(await bm.bmAutocomplete('a'), 'auto complete', '`a`');
console.log(resultsText);
2019-01-27 07:28:04 +08:00
console.log('run for : ' + ((Date.now() - start)).toFixed(1) + 'ms');
2019-01-27 07:03:40 +08:00
};
run();