1
0
mirror of https://github.com/xuthus83/pigallery2.git synced 2025-01-14 14:43:17 +08:00

fixing experiment to regenerate db when switching sql driver #299

This commit is contained in:
Patrik J. Braun 2022-02-22 23:50:28 +01:00
parent b1f094c150
commit ca16ce0c83
4 changed files with 59 additions and 30 deletions

View File

@ -62,17 +62,23 @@ export class Benchmark {
request: any;
beforeEach: () => Promise<any>;
afterEach: () => Promise<any>;
beforeAll: () => Promise<any>;
afterAll: () => Promise<any>;
private readonly bmExpressApp: BMExpressApp;
constructor(name: string,
request: any = {},
beforeEach?: () => Promise<any>,
afterEach?: () => Promise<any>) {
afterEach?: () => Promise<any>,
beforeAll?: () => Promise<any>,
afterAll?: () => Promise<any>) {
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];

View File

@ -84,10 +84,13 @@ export class BenchmarkRunner {
}
async bmSaveDirectory(): Promise<BenchmarkResult[]> {
await this.init();
await this.resetDB();
const dir = await DiskMangerWorker.scanDirectory(this.biggestDirPath);
const bm = new Benchmark('Saving directory to DB', null, (): Promise<void> => this.resetDB());
const bm = new Benchmark('Saving directory to DB', null,
(): Promise<void> => this.resetDB(), null,
async (): Promise<void> => {
await this.init();
await this.setupDB();
});
bm.addAStep({
name: 'Saving directory to DB',
fn: (): Promise<void> => {
@ -100,7 +103,11 @@ export class BenchmarkRunner {
async bmScanDirectory(): Promise<BenchmarkResult[]> {
await this.init();
const bm = new Benchmark('Scanning directory');
const bm = new Benchmark('Scanning directory', {}, null,
null,
async (): Promise<void> => {
await this.init();
});
bm.addAStep({
name: 'Scanning directory',
fn: async (): Promise<ContentWrapper> => new ContentWrapper(await DiskMangerWorker.scanDirectory(this.biggestDirPath))
@ -109,27 +116,31 @@ export class BenchmarkRunner {
}
async bmListDirectory(): Promise<BenchmarkResult[]> {
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<void> => {
await ObjectManagers.reset();
await ObjectManagers.InitSQLManagers();
}, null,
async (): Promise<void> => {
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<BenchmarkResult[]> {
await this.setupDB();
Config.Server.Indexing.reIndexingSensitivity = ReIndexingSensitivity.low;
const bm = new Benchmark('Listing Faces', Utils.clone(this.requestTemplate), async (): Promise<void> => {
await ObjectManagers.reset();
await ObjectManagers.InitSQLManagers();
});
await ObjectManagers.reset();
await ObjectManagers.InitSQLManagers();
}, null,
async (): Promise<void> => {
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<void> => {
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<BenchmarkResult[]> {
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<void> => {
await this.setupDB();
});
BMGalleryRouter.addAutoComplete(bm.BmExpressApp);
return await bm.run(this.RUNS);
}

View File

@ -2,7 +2,6 @@ export const Experiments = {
db: {
name: 'SQlite',
groups: {
sqlite3: 'sqlite3',
betterSqlite: 'better-sqlite'
}
}

View File

@ -35,12 +35,6 @@ export class SQLConnection {
}
public static async getConnection(): Promise<Connection> {
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<Connection> {
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;