From c237c839175b01dcca6816504909e3689b38863b Mon Sep 17 00:00:00 2001 From: BurnyLlama Date: Wed, 13 Oct 2021 20:27:07 +0200 Subject: [PATCH] Failed atempt for DB Multithread... --- index.js | 3 +++ libs/utils/multithread.js | 4 ++- libs/utils/multithread/db.test.js | 24 +++++++++++++++++ schemas/Finish-copy.js | 17 ++++++++++++ tests/multihread.test.js | 45 ++++++++++++++++++++----------- 5 files changed, 76 insertions(+), 17 deletions(-) create mode 100644 libs/utils/multithread/db.test.js create mode 100644 schemas/Finish-copy.js diff --git a/index.js b/index.js index d74b65e..c9002b2 100644 --- a/index.js +++ b/index.js @@ -10,6 +10,7 @@ import sqlite2mongo from './libs/database/sqlite2mongo.js' import databaseInit from './libs/database/init.js' import initLog from './libs/utils/log.js' import api from './api/api.js' +import multihreadTest from './tests/multihread.test.js' const log = initLog("[ MAIN ]") @@ -25,4 +26,6 @@ await databaseInit() if (process.env.LOAD_DB === "true") await sqlite2mongo() +multihreadTest() + Server.listen(process.env.PORT, () => log(`Server started and listening on port ${process.env.PORT}.`)) \ No newline at end of file diff --git a/libs/utils/multithread.js b/libs/utils/multithread.js index f64b369..af28b25 100644 --- a/libs/utils/multithread.js +++ b/libs/utils/multithread.js @@ -14,6 +14,8 @@ export function initWorkers(threads) { name: `Worker ${parseInt(i) + 1}` }) } + + console.log(`Initialised ${threads} workers!`) } let spreadIndex = 0 @@ -27,7 +29,7 @@ export function spread(script, data) { data }) - worker.once('message', result => resolve(result)) + worker.on('message', result => resolve(result)) ++spreadIndex if(spreadIndex >= workerFarm.length) diff --git a/libs/utils/multithread/db.test.js b/libs/utils/multithread/db.test.js new file mode 100644 index 0000000..72e6104 --- /dev/null +++ b/libs/utils/multithread/db.test.js @@ -0,0 +1,24 @@ +import Finish from '../../../schemas/Finish-copy.js' +import initLog from '../log.js' + +const log = initLog('DB TEST') + +export function main(data) { + return new Promise( + (resolve, reject) => { + const {err, finish} = data + + Finish.create({ + map: finish.Map, + time: finish.Time, + date: finish.Timestamp === '0000-00-00 00:00:00' ? new Date('January 1, 1970 00:00:00 UTC') : new Date(`${finish.Timestamp}+00:00`), + serverLocation: finish.Server ?? '', + player: finish.Name + }).then(() => { + log(`Added finish ~/~ -> At ${finish.Timestamp} «${finish.Name}» completed «${finish.Map}» in ${finish.Time} s`) + }) + + resolve(true) + } + ) +} \ No newline at end of file diff --git a/schemas/Finish-copy.js b/schemas/Finish-copy.js new file mode 100644 index 0000000..0a3bd54 --- /dev/null +++ b/schemas/Finish-copy.js @@ -0,0 +1,17 @@ +import mongoose from 'mongoose' + +const Finish = new mongoose.Schema({ + map: String, + time: Number, + date: Date, + serverLocation: String, + player: String +}) + +Finish.index({ player: 1, map: 1 }) + +/** + * This cotains the mongoose 'Finish' model. + * @module schemas/Finish + */ +export default mongoose.model("Finish-Copy", Finish) \ No newline at end of file diff --git a/tests/multihread.test.js b/tests/multihread.test.js index 4a192b9..9a9b117 100644 --- a/tests/multihread.test.js +++ b/tests/multihread.test.js @@ -1,21 +1,34 @@ import { initWorkers, spread } from '../libs/utils/multithread.js' +import { sqlite } from '../libs/database/init.js' +import Finish from '../schemas/Finish-copy.js' +import initLog from '../libs/utils/log.js' -initWorkers(10) +const log = initLog("sqlite2mongo") -let processes = 20 -let ran = 0 -for (let i = 0; i < processes; ++i) { - spread( - './fibonacci.js', - {} - ).then( - result => { - console.log(i) - - ++ran - if (ran === processes) { - process.exit(0) +initWorkers(6) + +export default async function() { + log("Checking for new finishes...") + + await Finish.deleteMany({}) + + let offset = -1 + while (offset < 10000000) { + await sqlite.each( + `SELECT * FROM race ORDER BY Timestamp LIMIT 5000 OFFSET ${offset + 1}`, + [], + (err, finish) => { + spread( + './db.test.js', + { + err, + finish, + } + ).then( + result => {} + ) } - } - ) + ) + offset += 5000 + } } \ No newline at end of file