diff --git a/benchmark/Benchmark.ts b/benchmark/Benchmark.ts index 2c2000ca..e87a027f 100644 --- a/benchmark/Benchmark.ts +++ b/benchmark/Benchmark.ts @@ -62,17 +62,23 @@ export class Benchmark { request: any; beforeEach: () => Promise; afterEach: () => Promise; + beforeAll: () => Promise; + afterAll: () => Promise; private readonly bmExpressApp: BMExpressApp; constructor(name: string, request: any = {}, beforeEach?: () => Promise, - afterEach?: () => Promise) { + afterEach?: () => Promise, + beforeAll?: () => Promise, + afterAll?: () => Promise) { this.name = name; this.request = request; this.beforeEach = beforeEach; this.afterEach = afterEach; + this.beforeAll = beforeAll; + this.afterAll = afterAll; this.bmExpressApp = new BMExpressApp(this); } @@ -85,7 +91,13 @@ export class Benchmark { for (const exp of Object.values(Experiments)) { for (const group of Object.values(exp.groups)) { ActiveExperiments[exp.name] = group; + if (this.beforeAll) { + await this.beforeAll(); + } ret.push(await this.runAnExperiment(RUNS)); + if (this.afterAll) { + await this.afterAll(); + } ret[ret.length - 1].experiment = exp.name + '=' + group; } delete ActiveExperiments[exp.name]; diff --git a/benchmark/BenchmarkRunner.ts b/benchmark/BenchmarkRunner.ts index 82101a3c..352c4920 100644 --- a/benchmark/BenchmarkRunner.ts +++ b/benchmark/BenchmarkRunner.ts @@ -84,10 +84,13 @@ export class BenchmarkRunner { } async bmSaveDirectory(): Promise { - await this.init(); - await this.resetDB(); const dir = await DiskMangerWorker.scanDirectory(this.biggestDirPath); - const bm = new Benchmark('Saving directory to DB', null, (): Promise => this.resetDB()); + const bm = new Benchmark('Saving directory to DB', null, + (): Promise => this.resetDB(), null, + async (): Promise => { + await this.init(); + await this.setupDB(); + }); bm.addAStep({ name: 'Saving directory to DB', fn: (): Promise => { @@ -100,7 +103,11 @@ export class BenchmarkRunner { async bmScanDirectory(): Promise { await this.init(); - const bm = new Benchmark('Scanning directory'); + const bm = new Benchmark('Scanning directory', {}, null, + null, + async (): Promise => { + await this.init(); + }); bm.addAStep({ name: 'Scanning directory', fn: async (): Promise => new ContentWrapper(await DiskMangerWorker.scanDirectory(this.biggestDirPath)) @@ -109,27 +116,31 @@ export class BenchmarkRunner { } async bmListDirectory(): Promise { - Config.Server.Indexing.reIndexingSensitivity = ReIndexingSensitivity.low; - await this.init(); - await this.setupDB(); const req = Utils.clone(this.requestTemplate); req.params.directory = this.biggestDirPath; const bm = new Benchmark('List directory', req, async (): Promise => { await ObjectManagers.reset(); await ObjectManagers.InitSQLManagers(); + }, null, + async (): Promise => { + Config.Server.Indexing.reIndexingSensitivity = ReIndexingSensitivity.low; + await this.init(); + await this.setupDB(); }); BMGalleryRouter.addDirectoryList(bm.BmExpressApp); return await bm.run(this.RUNS); } async bmListPersons(): Promise { - await this.setupDB(); - Config.Server.Indexing.reIndexingSensitivity = ReIndexingSensitivity.low; const bm = new Benchmark('Listing Faces', Utils.clone(this.requestTemplate), async (): Promise => { - await ObjectManagers.reset(); - await ObjectManagers.InitSQLManagers(); - }); + await ObjectManagers.reset(); + await ObjectManagers.InitSQLManagers(); + }, null, + async (): Promise => { + Config.Server.Indexing.reIndexingSensitivity = ReIndexingSensitivity.low; + await this.setupDB(); + }); BMPersonRouter.addGetPersons(bm.BmExpressApp); return await bm.run(this.RUNS); } @@ -234,7 +245,10 @@ export class BenchmarkRunner { const req = Utils.clone(this.requestTemplate); req.params.searchQueryDTO = JSON.stringify(entry.query); - const bm = new Benchmark('Searching for `' + entry.description + '`', req); + const bm = new Benchmark('Searching for `' + entry.description + '`', req, null, null, + async (): Promise => { + await this.setupDB(); + }); BMGalleryRouter.addSearch(bm.BmExpressApp); results.push({result: await bm.run(this.RUNS), searchQuery: entry.query}); @@ -244,10 +258,12 @@ export class BenchmarkRunner { async bmAutocomplete(text: string): Promise { - await this.setupDB(); const req = Utils.clone(this.requestTemplate); req.params.text = text; - const bm = new Benchmark('Auto complete for `' + text + '`', req); + const bm = new Benchmark('Auto complete for `' + text + '`', req, null, null, + async (): Promise => { + await this.setupDB(); + }); BMGalleryRouter.addAutoComplete(bm.BmExpressApp); return await bm.run(this.RUNS); } diff --git a/benchmark/Experiments.ts b/benchmark/Experiments.ts index 89019796..4bfa5b7f 100644 --- a/benchmark/Experiments.ts +++ b/benchmark/Experiments.ts @@ -2,7 +2,6 @@ export const Experiments = { db: { name: 'SQlite', groups: { - sqlite3: 'sqlite3', betterSqlite: 'better-sqlite' } } diff --git a/src/backend/model/database/sql/SQLConnection.ts b/src/backend/model/database/sql/SQLConnection.ts index a082a609..b39f1e6c 100644 --- a/src/backend/model/database/sql/SQLConnection.ts +++ b/src/backend/model/database/sql/SQLConnection.ts @@ -35,12 +35,6 @@ export class SQLConnection { } public static async getConnection(): Promise { - if (ActiveExperiments[Experiments.db.name] === Experiments.db.groups.sqlite3) { - Config.Server.Database.type = DatabaseType.sqlite; - } - if (ActiveExperiments[Experiments.db.name] === Experiments.db.groups.betterSqlite) { - Config.Server.Database.type = DatabaseType.better_sqlite3; - } if (this.connection == null) { const options: any = this.getDriver(Config.Server.Database); // options.name = 'main'; @@ -62,7 +56,7 @@ export class SQLConnection { if (Config.Server.Log.sqlLevel !== SQLLogLevel.none) { options.logging = SQLLogLevel[Config.Server.Log.sqlLevel]; } - Logger.debug(LOG_TAG, 'Creating connection: ' + DatabaseType[Config.Server.Database.type]); + Logger.debug(LOG_TAG, 'Creating connection: ' + DatabaseType[Config.Server.Database.type], 'with:', options.type); this.connection = await this.createConnection(options); await SQLConnection.schemeSync(this.connection); } @@ -160,7 +154,7 @@ export class SQLConnection { } private static async createConnection(options: ConnectionOptions): Promise { - if (options.type === 'sqlite') { + if (options.type === 'sqlite' || options.type === 'better-sqlite3') { return await createConnection(options); } try { @@ -226,14 +220,22 @@ export class SQLConnection { charset: 'utf8mb4' }; } else if (config.type === DatabaseType.sqlite) { - driver = { - type: 'sqlite', - database: path.join(ProjectPath.getAbsolutePath(config.dbFolder), config.sqlite.DBFileName) - }; + + if (ActiveExperiments[Experiments.db.name] === Experiments.db.groups.betterSqlite) { + driver = { + type: 'better-sqlite3', + database: path.join(ProjectPath.getAbsolutePath(config.dbFolder), 'better_' + config.sqlite.DBFileName) + }; + } else { + driver = { + type: 'sqlite', + database: path.join(ProjectPath.getAbsolutePath(config.dbFolder), config.sqlite.DBFileName) + }; + } } else if (config.type === DatabaseType.better_sqlite3) { driver = { type: 'better-sqlite3', - database: path.join(ProjectPath.getAbsolutePath(config.dbFolder), 'better_', config.sqlite.DBFileName) + database: path.join(ProjectPath.getAbsolutePath(config.dbFolder), 'better_' + config.sqlite.DBFileName) }; } return driver;