1
0
mirror of https://github.com/bpatrik/pigallery2.git synced 2024-12-23 01:27:14 +02:00
pigallery2/benchmark/BenchmarkRunner.ts

345 lines
11 KiB
TypeScript
Raw Normal View History

2020-12-30 22:13:19 +02:00
import {Config} from '../src/common/config/private/Config';
import {ObjectManagers} from '../src/backend/model/ObjectManagers';
import {DiskMangerWorker} from '../src/backend/model/threading/DiskMangerWorker';
2023-01-06 00:11:58 +02:00
import {IndexingManager} from '../src/backend/model/database/IndexingManager';
2020-12-30 22:54:07 +02:00
import * as path from 'path';
2021-01-04 11:32:19 +02:00
import * as fs from 'fs';
2020-12-30 22:13:19 +02:00
import {Utils} from '../src/common/Utils';
import {DatabaseType, ReIndexingSensitivity} from '../src/common/config/private/PrivateConfig';
2020-12-30 22:13:19 +02:00
import {ProjectPath} from '../src/backend/ProjectPath';
import {Benchmark} from './Benchmark';
import {IndexingJob} from '../src/backend/model/jobs/jobs/IndexingJob';
import {IJob} from '../src/backend/model/jobs/jobs/IJob';
import {JobProgressStates} from '../src/common/entities/job/JobProgressDTO';
import {JobProgress} from '../src/backend/model/jobs/jobs/JobProgress';
import {ContentWrapper} from '../src/common/entities/ConentWrapper';
2023-01-06 00:11:58 +02:00
import {GalleryManager} from '../src/backend/model/database/GalleryManager';
import {PersonManager} from '../src/backend/model/database/PersonManager';
2020-12-31 13:35:28 +02:00
import {GalleryRouter} from '../src/backend/routes/GalleryRouter';
import {Express} from 'express';
import {PersonRouter} from '../src/backend/routes/PersonRouter';
import {
ANDSearchQuery,
ORSearchQuery,
SearchQueryDTO,
SearchQueryTypes,
SomeOfSearchQuery,
TextSearch,
TextSearchQueryMatchTypes,
TextSearchQueryTypes
} from '../src/common/entities/SearchQueryDTO';
import {QueryKeywords, SearchQueryParser} from '../src/common/SearchQueryParser';
import {ParentDirectoryDTO} from '../src/common/entities/DirectoryDTO';
2020-12-30 22:13:19 +02:00
export interface BenchmarkResult {
name: string;
experiment?: string;
2020-12-30 22:13:19 +02:00
duration: number;
contentWrapper?: ContentWrapper;
items?: any[];
2020-12-30 22:13:19 +02:00
subBenchmarks?: BenchmarkResult[];
}
export class BMIndexingManager extends IndexingManager {
2021-06-27 19:33:37 +02:00
public async saveToDB(scannedDirectory: ParentDirectoryDTO): Promise<void> {
2020-12-30 22:13:19 +02:00
return super.saveToDB(scannedDirectory);
}
}
2020-12-31 13:35:28 +02:00
class BMGalleryRouter extends GalleryRouter {
public static addDirectoryList(app: Express): void {
2020-12-31 13:35:28 +02:00
GalleryRouter.addDirectoryList(app);
}
public static addSearch(app: Express): void {
2020-12-31 13:35:28 +02:00
GalleryRouter.addSearch(app);
}
public static addAutoComplete(app: Express): void {
2020-12-31 13:35:28 +02:00
GalleryRouter.addAutoComplete(app);
}
}
class BMPersonRouter extends PersonRouter {
public static addGetPersons(app: Express): void {
2020-12-31 13:35:28 +02:00
PersonRouter.addGetPersons(app);
}
}
2020-12-30 22:13:19 +02:00
export class BenchmarkRunner {
2020-12-30 22:54:07 +02:00
inited = false;
2020-12-31 13:35:28 +02:00
private biggestDirPath: string = null;
private readonly requestTemplate: any = {
requestPipe: null,
params: {},
query: {},
session: {}
};
2020-12-30 22:13:19 +02:00
constructor(public RUNS: number) {
2022-12-28 20:12:18 +02:00
Config.Users.authenticationRequired = false;
2020-12-30 22:13:19 +02:00
}
async bmSaveDirectory(): Promise<BenchmarkResult[]> {
2020-12-31 13:35:28 +02:00
const dir = await DiskMangerWorker.scanDirectory(this.biggestDirPath);
const bm = new Benchmark('Saving directory to DB', null,
(): Promise<void> => this.resetDB(), null,
async (): Promise<void> => {
await this.init();
await this.setupDB();
});
2020-12-30 22:13:19 +02:00
bm.addAStep({
name: 'Saving directory to DB',
fn: (): Promise<void> => {
2020-12-30 22:13:19 +02:00
const im = new BMIndexingManager();
2021-06-27 19:33:37 +02:00
return im.saveToDB(dir as ParentDirectoryDTO);
2020-12-30 22:13:19 +02:00
}
});
return await bm.run(this.RUNS);
}
async bmScanDirectory(): Promise<BenchmarkResult[]> {
2020-12-30 22:54:07 +02:00
await this.init();
const bm = new Benchmark('Scanning directory', {}, null,
null,
async (): Promise<void> => {
await this.init();
});
2020-12-30 22:13:19 +02:00
bm.addAStep({
name: 'Scanning directory',
fn: async (): Promise<ContentWrapper> => new ContentWrapper(await DiskMangerWorker.scanDirectory(this.biggestDirPath))
2020-12-30 22:13:19 +02:00
});
return await bm.run(this.RUNS);
}
async bmListDirectory(): Promise<BenchmarkResult[]> {
2020-12-31 13:35:28 +02:00
const req = Utils.clone(this.requestTemplate);
req.params.directory = this.biggestDirPath;
const bm = new Benchmark('List directory', req,
async (): Promise<void> => {
2020-12-30 22:13:19 +02:00
await ObjectManagers.reset();
await ObjectManagers.InitSQLManagers();
}, null,
async (): Promise<void> => {
2022-12-28 20:12:18 +02:00
Config.Indexing.reIndexingSensitivity = ReIndexingSensitivity.low;
await this.init();
await this.setupDB();
2020-12-30 22:13:19 +02:00
});
2020-12-31 13:35:28 +02:00
BMGalleryRouter.addDirectoryList(bm.BmExpressApp);
2020-12-30 22:13:19 +02:00
return await bm.run(this.RUNS);
}
async bmListPersons(): Promise<BenchmarkResult[]> {
const bm = new Benchmark('Listing Faces', Utils.clone(this.requestTemplate), async (): Promise<void> => {
await ObjectManagers.reset();
await ObjectManagers.InitSQLManagers();
}, null,
async (): Promise<void> => {
2022-12-28 20:12:18 +02:00
Config.Indexing.reIndexingSensitivity = ReIndexingSensitivity.low;
await this.setupDB();
});
2020-12-31 13:35:28 +02:00
BMPersonRouter.addGetPersons(bm.BmExpressApp);
2020-12-30 22:13:19 +02:00
return await bm.run(this.RUNS);
}
async bmAllSearch(): Promise<{ result: BenchmarkResult[], searchQuery: SearchQueryDTO }[]> {
2020-12-30 22:13:19 +02:00
await this.setupDB();
const queryKeywords: QueryKeywords = {
NSomeOf: 'of',
and: 'and',
or: 'or',
from: 'after',
to: 'before',
landscape: 'landscape',
maxRating: 'max-rating',
maxResolution: 'max-resolution',
minRating: 'min-rating',
minResolution: 'min-resolution',
orientation: 'orientation',
any_text: 'any-text',
keyword: 'keyword',
caption: 'caption',
directory: 'directory',
file_name: 'file-name',
person: 'person',
portrait: 'portrait',
position: 'position',
someOf: 'some-of',
kmFrom: 'km-from'
};
const queryParser = new SearchQueryParser(queryKeywords);
const names = (await ObjectManagers.getInstance().PersonManager.getAll()).sort((a, b) => b.count - a.count);
const queries: { query: SearchQueryDTO, description: string }[] = TextSearchQueryTypes.map(t => {
const q = {
type: t, text: 'a'
};
return {
query: q, description: queryParser.stringify(q)
};
});
// searching for everything
queries.push({
query: {
type: SearchQueryTypes.any_text, text: '.'
} as TextSearch, description: queryParser.stringify({
type: SearchQueryTypes.any_text, text: '.'
} as TextSearch)
});
if (names.length > 0) {
queries.push({
query: {
type: SearchQueryTypes.person, text: names[0].name,
matchType: TextSearchQueryMatchTypes.exact_match
} as TextSearch, description: '<Most common name>'
});
}
if (names.length > 1) {
queries.push({
query: {
type: SearchQueryTypes.AND, list: [
{
type: SearchQueryTypes.person, text: names[0].name,
matchType: TextSearchQueryMatchTypes.exact_match
} as TextSearch,
{
type: SearchQueryTypes.person, text: names[1].name,
matchType: TextSearchQueryMatchTypes.exact_match
} as TextSearch
]
} as ANDSearchQuery, description: '<Most AND second common names>'
});
queries.push({
query: {
type: SearchQueryTypes.OR, list: [
{
type: SearchQueryTypes.person, text: names[0].name,
matchType: TextSearchQueryMatchTypes.exact_match
} as TextSearch,
{
type: SearchQueryTypes.person, text: names[1].name,
matchType: TextSearchQueryMatchTypes.exact_match
} as TextSearch
]
} as ORSearchQuery, description: '<Most OR second common names>'
});
queries.push({
query: {
type: SearchQueryTypes.SOME_OF,
min: 2,
list: names.map(n => ({
type: SearchQueryTypes.person, text: n.name,
matchType: TextSearchQueryMatchTypes.exact_match
} as TextSearch))
} as SomeOfSearchQuery, description: '<Contain at least 2 out of all names>'
});
}
const results: { result: BenchmarkResult[], searchQuery: SearchQueryDTO }[] = [];
for (const entry of queries) {
2020-12-31 13:35:28 +02:00
const req = Utils.clone(this.requestTemplate);
req.params.searchQueryDTO = JSON.stringify(entry.query);
const bm = new Benchmark('Searching for `' + entry.description + '`', req, null, null,
async (): Promise<void> => {
await this.setupDB();
});
2020-12-31 13:35:28 +02:00
BMGalleryRouter.addSearch(bm.BmExpressApp);
results.push({result: await bm.run(this.RUNS), searchQuery: entry.query});
2020-12-30 22:13:19 +02:00
}
return results;
}
async bmAutocomplete(text: string): Promise<BenchmarkResult[]> {
2020-12-31 13:35:28 +02:00
const req = Utils.clone(this.requestTemplate);
req.params.text = text;
const bm = new Benchmark('Auto complete for `' + text + '`', req, null, null,
async (): Promise<void> => {
await this.setupDB();
});
2020-12-31 13:35:28 +02:00
BMGalleryRouter.addAutoComplete(bm.BmExpressApp);
2020-12-30 22:13:19 +02:00
return await bm.run(this.RUNS);
}
async getStatistic(): Promise<string> {
2020-12-30 22:54:07 +02:00
await this.setupDB();
const gm = new GalleryManager();
const pm = new PersonManager();
2020-12-31 13:35:28 +02:00
2020-12-30 22:54:07 +02:00
return 'directories: ' + await gm.countDirectories() +
', photos: ' + await gm.countPhotos() +
', videos: ' + await gm.countVideos() +
2020-12-31 13:35:28 +02:00
', diskUsage : ' + Utils.renderDataSize(await gm.countMediaSize()) +
2020-12-30 22:54:07 +02:00
', persons : ' + await pm.countFaces() +
', unique persons (faces): ' + (await pm.getAll()).length;
}
private async init(): Promise<string> {
2020-12-30 22:54:07 +02:00
if (this.inited === false) {
await this.setupDB();
const gm = new GalleryManager();
let biggest = 0;
let biggestPath = '/';
const queue = ['/'];
while (queue.length > 0) {
const dirPath = queue.shift();
const dir = await gm.listDirectory(dirPath);
dir.directories.forEach((d): number => queue.push(path.join(d.path + d.name)));
2020-12-30 22:54:07 +02:00
if (biggest < dir.media.length) {
biggestPath = path.join(dir.path + dir.name);
biggest = dir.media.length;
}
}
2020-12-31 13:35:28 +02:00
this.biggestDirPath = biggestPath;
console.log('updating path of biggest dir to: ' + this.biggestDirPath);
2020-12-30 22:54:07 +02:00
this.inited = true;
}
2020-12-31 13:35:28 +02:00
return this.biggestDirPath;
2020-12-30 22:54:07 +02:00
}
2020-12-30 22:13:19 +02:00
private resetDB = async (): Promise<void> => {
2020-12-30 22:13:19 +02:00
Config.Server.Threading.enabled = false;
await ObjectManagers.reset();
await fs.promises.rm(ProjectPath.DBFolder, {recursive: true, force: true});
2022-12-28 20:12:18 +02:00
Config.Database.type = DatabaseType.sqlite;
Config.Jobs.scheduled = [];
2020-12-30 22:13:19 +02:00
await ObjectManagers.InitSQLManagers();
};
2021-01-04 11:32:19 +02:00
private async setupDB(): Promise<void> {
2020-12-30 22:13:19 +02:00
Config.Server.Threading.enabled = false;
2021-01-04 11:32:19 +02:00
await this.resetDB();
await new Promise<void>((resolve, reject): void => {
2020-12-30 22:13:19 +02:00
try {
const indexingJob = new IndexingJob();
indexingJob.JobListener = {
onJobFinished: (job: IJob<any>, state: JobProgressStates, soloRun: boolean): void => {
2020-12-30 22:13:19 +02:00
resolve();
},
onProgressUpdate: (progress: JobProgress): void => {
2022-12-28 20:12:18 +02:00
// empty
2020-12-30 22:13:19 +02:00
}
};
indexingJob.start({indexChangesOnly: false}).catch(console.error);
2020-12-30 22:13:19 +02:00
} catch (e) {
console.error(e);
reject(e);
}
});
}
}