From 747ef0acf98c3365f9e3663f763d256a7377a3bc Mon Sep 17 00:00:00 2001 From: Fabian Seluska Date: Wed, 13 Oct 2021 16:40:18 +0200 Subject: [PATCH] Started experimenting with multithreading. --- index.js | 2 +- libs/database/init.js | 2 +- libs/database/sqlite2mongo.js | 2 +- libs/database/sqlite2mongo/checks.js | 2 +- libs/database/sqlite2mongo/postTasks.js | 2 +- libs/{utills => utils}/log.js | 0 libs/utils/multithread.js | 36 +++++++++++++++++++++++++ libs/utils/multithread/fibonacci.js | 23 ++++++++++++++++ libs/utils/multithread/genericWorker.js | 31 +++++++++++++++++++++ package.json | 2 +- tests/multihread.test.js | 3 +++ 11 files changed, 99 insertions(+), 6 deletions(-) rename libs/{utills => utils}/log.js (100%) create mode 100644 libs/utils/multithread.js create mode 100644 libs/utils/multithread/fibonacci.js create mode 100644 libs/utils/multithread/genericWorker.js create mode 100644 tests/multihread.test.js diff --git a/index.js b/index.js index 24c361a..d74b65e 100644 --- a/index.js +++ b/index.js @@ -8,7 +8,7 @@ import express from 'express' import dotenv from 'dotenv' import sqlite2mongo from './libs/database/sqlite2mongo.js' import databaseInit from './libs/database/init.js' -import initLog from './libs/utills/log.js' +import initLog from './libs/utils/log.js' import api from './api/api.js' const log = initLog("[ MAIN ]") diff --git a/libs/database/init.js b/libs/database/init.js index 3141a2f..72877dd 100644 --- a/libs/database/init.js +++ b/libs/database/init.js @@ -1,4 +1,4 @@ -import initLog from '../utills/log.js' +import initLog from '../utils/log.js' import mongoose from 'mongoose' import sqlite3 from 'sqlite3' import { open } from 'sqlite' diff --git a/libs/database/sqlite2mongo.js b/libs/database/sqlite2mongo.js index 01d6208..7a3f21e 100644 --- a/libs/database/sqlite2mongo.js +++ b/libs/database/sqlite2mongo.js @@ -1,4 +1,4 @@ -import initLog from '../utills/log.js' +import initLog from '../utils/log.js' import { checkForFinishes, checkForMaps, checkForPlayers } from './sqlite2mongo/checks.js' import postTasks from './sqlite2mongo/postTasks.js' diff --git a/libs/database/sqlite2mongo/checks.js b/libs/database/sqlite2mongo/checks.js index bd2b71e..7f8f069 100644 --- a/libs/database/sqlite2mongo/checks.js +++ b/libs/database/sqlite2mongo/checks.js @@ -2,7 +2,7 @@ import { sqlite } from "../init.js" import Level from '../../../schemas/Level.js' import Player from '../../../schemas/Player.js' import Finish from '../../../schemas/Finish.js' -import initLog from '../../utills/log.js' +import initLog from '../../utils/log.js' const log = initLog("sqlite2mongo") diff --git a/libs/database/sqlite2mongo/postTasks.js b/libs/database/sqlite2mongo/postTasks.js index 18e58d3..b97faef 100644 --- a/libs/database/sqlite2mongo/postTasks.js +++ b/libs/database/sqlite2mongo/postTasks.js @@ -1,6 +1,6 @@ import Level from '../../../schemas/Level.js' import Player from '../../../schemas/Player.js' -import initLog from '../../utills/log.js' +import initLog from '../../utils/log.js' import decodeMsgpack from './decodeMsgpack.js' const log = initLog("sqlite2mongo") diff --git a/libs/utills/log.js b/libs/utils/log.js similarity index 100% rename from libs/utills/log.js rename to libs/utils/log.js diff --git a/libs/utils/multithread.js b/libs/utils/multithread.js new file mode 100644 index 0000000..56128b4 --- /dev/null +++ b/libs/utils/multithread.js @@ -0,0 +1,36 @@ +import { Worker } from 'worker_threads' + +let workerFarm = [] + +export function initWorkers(threads) { + for (let i = 0; i < threads; ++i) { + workerFarm.push(new Worker('./libs/utils/multithread/genericWorker.js')) + } + + for (const i in workerFarm) { + const worker = workerFarm[i] + worker.postMessage({ + type: 'setName', + name: `Worker ${parseInt(i) + 1}` + }) + } +} + +export function spread(threads, script, data, shouldEval) { + return new Promise((resolve, reject) => { + const worker = new Worker( + script, + { + workerData: data, + eval: shouldEval ?? false + } + ) + + worker.on('message', resolve) + worker.on('error', reject) + worker.on('exit', (code) => { + if (code !== 0) + reject(new Error(`Process stopped with code: ${code}`)) + }) + }) +} \ No newline at end of file diff --git a/libs/utils/multithread/fibonacci.js b/libs/utils/multithread/fibonacci.js new file mode 100644 index 0000000..c577302 --- /dev/null +++ b/libs/utils/multithread/fibonacci.js @@ -0,0 +1,23 @@ +import { workerData, parentPort } from 'worker_threads' + +let nums = [1, 1] + +for (let i = 0; i < 42; i++) { + nums.push(nums[nums.length - 2] + nums[nums.length - 1]) +} + +let primes = [] +for (const num of nums) { + let isPrime = true + for (let i = 2; i < num / 2; ++i) { + if (num % i === 0) + isPrime = false + } + + if (isPrime) + primes.push(num) +} + +console.log("DONE!") + +parentPort.postMessage(primes) \ No newline at end of file diff --git a/libs/utils/multithread/genericWorker.js b/libs/utils/multithread/genericWorker.js new file mode 100644 index 0000000..73a4ff4 --- /dev/null +++ b/libs/utils/multithread/genericWorker.js @@ -0,0 +1,31 @@ +import { workerData, parentPort } from 'worker_threads' +import initLog from '../log.js' + +let script = '' +let myName = '' + +let log = initLog(myName) + +parentPort.on( + 'message', + message => { + switch (message.type) { + case 'setName': + myName = message.name + log = initLog(myName) + log(`Changed name to '${myName}'.`) + break + + case 'setScript': + script = message.script + log = initLog(myName) + log(`Changed name to '${myName}'.`) + break + + + default: + + break + } + } +) \ No newline at end of file diff --git a/package.json b/package.json index dc91659..adf9577 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,6 @@ "express": "^4.17.1", "mongoose": "^6.0.7", "sqlite": "^4.0.23", - "sqlite3": "^5.0.2" + "sqlite3": "^4.2.0" } } diff --git a/tests/multihread.test.js b/tests/multihread.test.js new file mode 100644 index 0000000..71c2f1d --- /dev/null +++ b/tests/multihread.test.js @@ -0,0 +1,3 @@ +import { initWorkers, spread } from '../libs/utils/multithread.js' + +initWorkers(8) \ No newline at end of file