Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@
"jest": true
},
"parser": "@typescript-eslint/parser",
"parserOptions": {
"project": "tsconfig.json",
"sourceType": "module"
},
"plugins": [
"import"
],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:prettier/recommended",
"prettier"
],
"plugins": [
"import"
],
"parserOptions": {
"project": "tsconfig.json",
"sourceType": "module"
},
"rules": {
"linebreak-style": ["error", "unix"],
"no-empty": 1,
Expand All @@ -39,6 +39,7 @@
"message": "Use `globalThis` instead"
}
],
"prefer-rest-params": 0,
"require-yield": 0,
"eqeqeq": ["error", "smart"],
"spaced-comment": [
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tag.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: "CI / Tag"
npname: "CI / Tag"

on:
push:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ npm install
# build the dist
npm run build
# run the repl (this allows you to import from ./src)
npm run ts-node
npm run tsx
# run the tests
npm run test
# lint the source code
Expand Down
54 changes: 26 additions & 28 deletions benches/crypto_100KiB.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,35 @@
import type { EFSWorkerModule } from '@/workers';
import os from 'os';
import path from 'path';
import type { EFSWorker } from '#efsWorker.js';
import os from 'node:os';
import path from 'node:path';
import url from 'node:url';
import { Worker } from 'node:worker_threads';
import b from 'benny';
import { spawn, Worker, Transfer } from 'threads';
import Logger, { LogLevel, StreamHandler } from '@matrixai/logger';
import { WorkerManager } from '@matrixai/workers';
import * as utils from '@/utils';
import { suiteCommon } from './utils';
import { suiteCommon } from './utils/index.js';
import * as utils from '#utils.js';
import efsWorker from '#efsWorker.js';

const logger = new Logger('crypto100KiB Bench', LogLevel.WARN, [
new StreamHandler(),
]);
const filename = url.fileURLToPath(new URL(import.meta.url));
const workerPath = path.join(filename, '../../dist/efsWorker');

async function main() {
const cores = os.cpus().length;
logger.warn(`Cores: ${cores}`);
const workerManager =
await WorkerManager.createWorkerManager<EFSWorkerModule>({
workerFactory: () => spawn(new Worker('../src/workers/efsWorker')),
cores: 1,
logger,
});
const workerManager = await WorkerManager.createWorkerManager<EFSWorker>({
workerFactory: () => new Worker(workerPath),
manifest: efsWorker,
cores: 1,
logger,
});
const key = utils.generateKeySync(256);
const plain100KiB = utils.getRandomBytesSync(1024 * 100);
const cipher100KiB = await utils.encrypt(key, plain100KiB);
const summary = await b.suite(
path.basename(__filename, path.extname(__filename)),
path.basename(filename, path.extname(filename)),
b.add('encrypt 100 KiB of data', async () => {
await utils.encrypt(key, plain100KiB);
}),
Expand All @@ -35,25 +39,19 @@ async function main() {
b.add('encrypt 100 KiB of data with workers', async () => {
const keyAB = utils.toArrayBuffer(key);
const plainTextAB = utils.toArrayBuffer(plain100KiB);
const cipherTextAB = await workerManager.call(async (w) => {
return await w.encrypt(
Transfer(keyAB),
// @ts-ignore: threads.js types are wrong
Transfer(plainTextAB),
);
});
const { data: cipherTextAB } = await workerManager.methods.encrypt(
{ key: keyAB, plainText: plainTextAB },
[keyAB, plainTextAB],
);
utils.fromArrayBuffer(cipherTextAB);
}),
b.add('decrypt 100 KiB of data with workers', async () => {
const keyAB = utils.toArrayBuffer(key);
const cipherTextAB = cipher100KiB.slice(0);
const decrypted = await workerManager.call(async (w) => {
return await w.decrypt(
Transfer(keyAB),
// @ts-ignore: threads.js types are wrong
Transfer(cipherTextAB),
);
});
const { data: decrypted } = await workerManager.methods.decrypt(
{ key: keyAB, cipherText: cipherTextAB },
[keyAB, cipherTextAB],
);
if (decrypted != null) {
utils.fromArrayBuffer(decrypted);
}
Expand All @@ -64,7 +62,7 @@ async function main() {
return summary;
}

if (require.main === module) {
if (process.argv[1] === url.fileURLToPath(import.meta.url)) {
void main();
}

Expand Down
54 changes: 26 additions & 28 deletions benches/crypto_10KiB.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,35 @@
import type { EFSWorkerModule } from '@/workers';
import os from 'os';
import path from 'path';
import type { EFSWorker } from '#efsWorker.js';
import os from 'node:os';
import path from 'node:path';
import url from 'node:url';
import { Worker } from 'node:worker_threads';
import b from 'benny';
import { spawn, Worker, Transfer } from 'threads';
import Logger, { LogLevel, StreamHandler } from '@matrixai/logger';
import { WorkerManager } from '@matrixai/workers';
import * as utils from '@/utils';
import { suiteCommon } from './utils';
import { suiteCommon } from './utils/index.js';
import * as utils from '#utils.js';
import efsWorker from '#efsWorker.js';

const logger = new Logger('crypto10KiB Bench', LogLevel.WARN, [
new StreamHandler(),
]);
const filename = url.fileURLToPath(new URL(import.meta.url));
const workerPath = path.join(filename, '../../dist/efsWorker');

async function main() {
const cores = os.cpus().length;
logger.warn(`Cores: ${cores}`);
const workerManager =
await WorkerManager.createWorkerManager<EFSWorkerModule>({
workerFactory: () => spawn(new Worker('../src/workers/efsWorker')),
cores: 1,
logger,
});
const workerManager = await WorkerManager.createWorkerManager<EFSWorker>({
workerFactory: () => new Worker(workerPath),
manifest: efsWorker,
cores: 1,
logger,
});
const key = utils.generateKeySync(256);
const plain10KiB = utils.getRandomBytesSync(1024 * 10);
const cipher10KiB = await utils.encrypt(key, plain10KiB);
const summary = await b.suite(
path.basename(__filename, path.extname(__filename)),
path.basename(filename, path.extname(filename)),
b.add('encrypt 10 KiB of data', async () => {
await utils.encrypt(key, plain10KiB);
}),
Expand All @@ -35,25 +39,19 @@ async function main() {
b.add('encrypt 10 KiB of data with workers', async () => {
const keyAB = utils.toArrayBuffer(key);
const plainTextAB = utils.toArrayBuffer(plain10KiB);
const cipherTextAB = await workerManager.call(async (w) => {
return await w.encrypt(
Transfer(keyAB),
// @ts-ignore: threads.js types are wrong
Transfer(plainTextAB),
);
});
const { data: cipherTextAB } = await workerManager.methods.encrypt(
{ key: keyAB, plainText: plainTextAB },
[keyAB, plainTextAB],
);
utils.fromArrayBuffer(cipherTextAB);
}),
b.add('decrypt 10 KiB of data with workers', async () => {
const keyAB = utils.toArrayBuffer(key);
const cipherTextAB = cipher10KiB.slice(0);
const decrypted = await workerManager.call(async (w) => {
return await w.decrypt(
Transfer(keyAB),
// @ts-ignore: threads.js types are wrong
Transfer(cipherTextAB),
);
});
const { data: decrypted } = await workerManager.methods.decrypt(
{ key: keyAB, cipherText: cipherTextAB },
[keyAB, cipherTextAB],
);
if (decrypted != null) {
utils.fromArrayBuffer(decrypted);
}
Expand All @@ -64,7 +62,7 @@ async function main() {
return summary;
}

if (require.main === module) {
if (process.argv[1] === url.fileURLToPath(import.meta.url)) {
void main();
}

Expand Down
54 changes: 26 additions & 28 deletions benches/crypto_16KiB.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,35 @@
import type { EFSWorkerModule } from '@/workers';
import os from 'os';
import path from 'path';
import type { EFSWorker } from '#efsWorker.js';
import os from 'node:os';
import path from 'node:path';
import url from 'node:url';
import { Worker } from 'node:worker_threads';
import b from 'benny';
import { spawn, Worker, Transfer } from 'threads';
import Logger, { LogLevel, StreamHandler } from '@matrixai/logger';
import { WorkerManager } from '@matrixai/workers';
import * as utils from '@/utils';
import { suiteCommon } from './utils';
import { suiteCommon } from './utils/index.js';
import * as utils from '#utils.js';
import efsWorker from '#efsWorker.js';

const logger = new Logger('crypto16KiB Bench', LogLevel.WARN, [
new StreamHandler(),
]);
const filename = url.fileURLToPath(new URL(import.meta.url));
const workerPath = path.join(filename, '../../dist/efsWorker');

async function main() {
const cores = os.cpus().length;
logger.warn(`Cores: ${cores}`);
const workerManager =
await WorkerManager.createWorkerManager<EFSWorkerModule>({
workerFactory: () => spawn(new Worker('../src/workers/efsWorker')),
cores: 1,
logger,
});
const workerManager = await WorkerManager.createWorkerManager<EFSWorker>({
workerFactory: () => new Worker(workerPath),
manifest: efsWorker,
cores: 1,
logger,
});
const key = utils.generateKeySync(256);
const plain16KiB = utils.getRandomBytesSync(1024 * 16);
const cipher16KiB = await utils.encrypt(key, plain16KiB);
const summary = await b.suite(
path.basename(__filename, path.extname(__filename)),
path.basename(filename, path.extname(filename)),
b.add('encrypt 16 KiB of data', async () => {
await utils.encrypt(key, plain16KiB);
}),
Expand All @@ -35,25 +39,19 @@ async function main() {
b.add('encrypt 16 KiB of data with workers', async () => {
const keyAB = utils.toArrayBuffer(key);
const plainTextAB = utils.toArrayBuffer(plain16KiB);
const cipherTextAB = await workerManager.call(async (w) => {
return await w.encrypt(
Transfer(keyAB),
// @ts-ignore: threads.js types are wrong
Transfer(plainTextAB),
);
});
const { data: cipherTextAB } = await workerManager.methods.encrypt(
{ key: keyAB, plainText: plainTextAB },
[keyAB, plainTextAB],
);
utils.fromArrayBuffer(cipherTextAB);
}),
b.add('decrypt 16 KiB of data with workers', async () => {
const keyAB = utils.toArrayBuffer(key);
const cipherTextAB = cipher16KiB.slice(0);
const decrypted = await workerManager.call(async (w) => {
return await w.decrypt(
Transfer(keyAB),
// @ts-ignore: threads.js types are wrong
Transfer(cipherTextAB),
);
});
const { data: decrypted } = await workerManager.methods.decrypt(
{ key: keyAB, cipherText: cipherTextAB },
[keyAB, cipherTextAB],
);
if (decrypted != null) {
utils.fromArrayBuffer(decrypted);
}
Expand All @@ -64,7 +62,7 @@ async function main() {
return summary;
}

if (require.main === module) {
if (process.argv[1] === url.fileURLToPath(import.meta.url)) {
void main();
}

Expand Down
Loading