1
0
mirror of https://github.com/xuthus83/pigallery2.git synced 2024-11-03 21:04:03 +08:00
pigallery2/benchmark/index.ts
Patrik J. Braun b6b576ba2f Implementing ContentWrapper packing.
This enables to extract common string into a map and only reference their values.
This is expected to bring a further 43% savings on search results. Altogether leading to a 50% reduction.
 #437
2022-06-24 18:05:45 +02:00

122 lines
4.1 KiB
TypeScript

import {Config} from '../src/common/config/private/Config';
import {ProjectPath} from '../src/backend/ProjectPath';
import {BenchmarkResult, BenchmarkRunner} from './BenchmarkRunner';
import {Utils} from '../src/common/Utils';
import {BMConfig} from './BMConfig';
import {DirectoryDTOUtils} from '../src/common/entities/DirectoryDTO';
Config.Server.Media.folder = BMConfig.path;
ProjectPath.reset();
const RUNS = BMConfig.RUNS;
let resultsText = '';
const printLine = (text: string) => {
resultsText += text + '\n';
};
const printHeader = async (statistic: string) => {
const dt = new Date();
printLine('## PiGallery2 v' + require('./../package.json').version +
', ' + Utils.zeroPrefix(dt.getDate(), 2) +
'.' + Utils.zeroPrefix(dt.getMonth() + 1, 2) +
'.' + dt.getFullYear());
if (Config.Server.Environment && Config.Server.Environment.buildCommitHash) {
printLine('**Version**: v' + Config.Server.Environment.appVersion + ', built at: ' + new Date(Config.Server.Environment.buildTime) + ', git commit:' + Config.Server.Environment.buildCommitHash);
}
printLine('**System**: ' + BMConfig.system);
printLine('\n**Gallery**: ' + statistic + '\n');
};
const printTableHeader = () => {
printLine('| Action | Sub action | Average Duration | Result |');
printLine('|:------:|:----------:|:----------------:|:-------:|');
};
const printExperimentResult = (result: BenchmarkResult, isSubResult = false) => {
console.log('benchmarked: ' + result.name);
const fileSize = (size: number): string => {
const postFixes = ['B', 'KB', 'MB', 'GB', 'TB'];
let index = 0;
while (size > 1000 && index < postFixes.length - 1) {
size /= 1000;
index++;
}
return size.toFixed(2) + postFixes[index];
};
let details = '-';
if (result.items) {
details = 'items: ' + result.items.length +
', size: ' + fileSize(JSON.stringify(result.items).length);
}
if (result.contentWrapper) {
if (result.contentWrapper.directory) {
details = 'media: ' + result.contentWrapper.directory.media.length +
', directories: ' + result.contentWrapper.directory.directories.length +
', size: ' + fileSize(JSON.stringify(DirectoryDTOUtils.removeReferences(result.contentWrapper.directory)).length);
} else {
details = 'media: ' + result.contentWrapper.searchResult.media.length +
', directories: ' + result.contentWrapper.searchResult.directories.length +
', size: ' + fileSize(JSON.stringify(result.contentWrapper.searchResult).length);
}
}
if (isSubResult) {
printLine('| | ' + result.name + ' | ' + (result.duration).toFixed(1) + ' ms | ' + details + ' |');
} else {
printLine('| **' + (result.experiment ? '`[' + result.experiment + ']`' : '') + result.name + '** | | **' + (result.duration).toFixed(1) + ' ms** | **' + details + '** |');
}
if (result.subBenchmarks && result.subBenchmarks.length > 1) {
for (const item of result.subBenchmarks) {
printExperimentResult(item, true);
}
}
};
const printResult = (results: BenchmarkResult[]) => {
for (const result of results) {
printExperimentResult(result);
}
};
const run = async () => {
console.log('Running, RUNS:' + RUNS);
const start = Date.now();
const bm = new BenchmarkRunner(RUNS);
// header
await printHeader(await bm.getStatistic());
printTableHeader();
if (BMConfig.Benchmarks.bmScanDirectory) {
printResult(await bm.bmScanDirectory());
}
if (BMConfig.Benchmarks.bmSaveDirectory) {
printResult(await bm.bmSaveDirectory());
}
if (BMConfig.Benchmarks.bmListDirectory) {
printResult(await bm.bmListDirectory());
}
if (BMConfig.Benchmarks.bmListPersons) {
printResult(await bm.bmListPersons());
}
if (BMConfig.Benchmarks.bmAllSearch) {
(await bm.bmAllSearch()).forEach(res => printResult(res.result));
}
if (BMConfig.Benchmarks.bmAutocomplete) {
printResult(await bm.bmAutocomplete('a'));
}
printLine('*Measurements run ' + RUNS + ' times and an average was calculated.');
console.log(resultsText);
console.log('run for : ' + ((Date.now() - start)).toFixed(1) + 'ms');
};
run().then(console.log).catch(console.error);