Skip to content

Commit

Permalink
Merge 8ab86b9 into 7e54940
Browse files Browse the repository at this point in the history
  • Loading branch information
Idrinth committed Apr 27, 2024
2 parents 7e54940 + 8ab86b9 commit 2885377
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 3 deletions.
4 changes: 3 additions & 1 deletion framework/integration/storage/mysql-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ describe('storage/mysql-storage', () => {
mysqlUser: 'idrinth-api-bench',
mysqlPassword: 'mysqlTestPassword',
mysqlDb: 'idrinth-api-bench',
containerName: 'storage-mysql',
defaultExternalPort: 3337,
},);
port = Number.parseInt(
database.databaseURL.replace(/\D/gui, '',),
Expand All @@ -30,7 +32,7 @@ describe('storage/mysql-storage', () => {
},);
after(async() => {
await delay(WAIT_MEDIUM,);
database.kill();
await database.kill();
},);
it('should be a class', () => {
expect(MysqlStorage,).to.be.a('function',);
Expand Down
55 changes: 55 additions & 0 deletions framework/integration/storage/storage-factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import MysqlStorage from '../../src/storage/mysql-storage.js';
import {
expect,
} from 'chai';
import 'mocha';
import storageFactory from '../../src/storage/storage-factory';
import getDatabase from '@databases/mysql-test';
import NoopStorage from '../../src/storage/noop-storage';
import delay from '../delay';

const WAIT_LONG = 25000;
const WAIT_MEDIUM = 1000;
const RADIX = 10;

describe('storage/storage-factory', () => {
it('should be a function', () => {
expect(storageFactory,).to.be.a('function',);
},);
it('(mysql) should not throw an error', async() => {
const database: {
databaseURL: string;
kill: () => Promise<void>;
} = await getDatabase.default({
mysqlUser: 'idrinth-api-bench',
mysqlPassword: 'mysqlTestPassword',
mysqlDb: 'idrinth-api-bench',
containerName: 'storage-factory-mysql',
defaultExternalPort: 3338,
},);
const port = Number.parseInt(
database.databaseURL.replace(/\D/gui, '',),
RADIX,
);
const storage = storageFactory({
databaseUser: 'idrinth-api-bench',
databasePassword: 'mysqlTestPassword',
databaseDatabase: 'idrinth-api-bench',
databasePort: port,
database: 'mysql',
cwd: '',
task: 'bench',
},) as MysqlStorage;
expect(storage,).to.be.an.instanceof(MysqlStorage,);
await delay(WAIT_MEDIUM,);
storage.close();
await database.kill();
},).timeout(WAIT_LONG,);
it('(noop) should not throw an error', function() {
const storage = storageFactory({
cwd: '',
task: 'bench',
},);
expect(storage,).to.be.an.instanceof(NoopStorage,);
},);
},);
7 changes: 5 additions & 2 deletions framework/src/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,22 @@ import run from '../main.js';
import pkg from '../../package.json' with {
type: 'json',
};
import factory from './config/factory.js';
import configFactory from './config/config-factory.js';
import storageFactory from '../storage/storage-factory.js';

// eslint-disable-next-line complexity
export default async(args: string[], cwd: string,): Promise<number> => {
const config = factory(cwd, args, process.env,);
const config = configFactory(cwd, args, process.env,);
resultStore.set(false,);
const storage = storageFactory(config,);
switch (config.task) {
case 'bench':
await run({
mode: 'benchmarking',
taskId: config.taskId,
language: config.language,
cwd: config.cwd,
resultStorage: storage,
}, config.threads, config.repetitions,);
break;
case 'content':
Expand Down
File renamed without changes.
8 changes: 8 additions & 0 deletions framework/src/cli/config/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
interface Config {
databasePassword?: string;
databaseDriver?: string;
databaseTrustedConnection?: string;
databaseDatabase?: string;
databasePort?: number;
databaseUser?: string;
databaseHost?: string;
cwd: string;
taskId?: string;
repetitions?: number;
Expand All @@ -8,5 +15,6 @@ interface Config {
increment?: number;
duration?: number;
task: string;
database?: 'mysql'|'mssql'|'postgres';
}
export default Config;
43 changes: 43 additions & 0 deletions framework/src/storage/storage-factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import MysqlStorage from './mysql-storage.js';
import PostgresStorage from './postgres-storage.js';
import MssqlStorage from './mssql-storage.js';
import NoopStorage from './noop-storage.js';
import Config from '../cli/config/config.js';
import Storage from './storage.js';

// eslint-disable-next-line complexity
export default (config: Config,): Storage => {
switch (config.database) {
case 'mysql':
return new MysqlStorage(
config.databaseHost,
config.databasePassword,
config.databasePort,
config.databaseUser ?? 'idrinth-api-bench',
config.databaseDatabase ?? 'idrinth-api-bench',
);
case 'postgres':
return new PostgresStorage(
config.databaseHost,
config.databasePassword,
config.databasePort,
config.databaseUser ?? 'idrinth-api-bench',
config.databaseDatabase ?? 'idrinth-api-bench',
);
case 'mssql':
return new MssqlStorage(
config.databaseHost,
config.databasePassword,
config.databasePort,
config.databaseDriver,
config.databaseUser ?? 'idrinth-api-bench',
config.databaseDatabase ?? 'idrinth-api-bench',
config.databaseTrustedConnection === 'true',
);
default:
if (config.database) {
throw new Error(`Database type ${ config.database } is unknown.`,);
}
return new NoopStorage();
}
};

0 comments on commit 2885377

Please sign in to comment.