Failed atempt for DB Multithread...

This commit is contained in:
BurnyLlama 2021-10-13 20:27:07 +02:00
parent 36ecb5c6a7
commit c237c83917
5 changed files with 76 additions and 17 deletions

View File

@ -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}.`))

View File

@ -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)

View File

@ -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)
}
)
}

17
schemas/Finish-copy.js Normal file
View File

@ -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)

View File

@ -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
}
}