ddstats-server/tests/multihread.test.js
2021-10-20 10:03:48 +02:00

67 lines
1.6 KiB
JavaScript

import { initWorkers, spread } from '../libs/utils/multithread.js'
import Player from '../schemas/Player.js'
import decodeMsgpack from '../libs/database/sqlite2mongo/decodeMsgpack.js'
import initLog from '../libs/utils/log.js'
const log = initLog("Fib. Test")
initWorkers(3)
const jobs = 10
let completed = 0
for (let i = 0; i < jobs; ++i) {
spread(
'./fibonacci.js',
{}
).then(
result => {
++completed
log(`Completed job ${completed}/${jobs} -> ${result}`)
if (completed === jobs) process.exit(0)
}
)
}
export default async function() {
const cache = decodeMsgpack()
await Player.updateMany(
{},
{
pointsThisWeek: 0,
pointsThisMonth: 0
}
)
let processedPlayers = 0
const totalPlayers = cache.pointsRanks.length
const rawDbFunc = (name, points) => new Promise((resolve, reject) => Player.findOneAndUpdate({ name: name }, { points: points }).then(() => resolve()))
for (const entry of cache.pointsRanks) {
let data = new SharedArrayBuffer(1024 * 10)
// let data = new Array(buffer)
data[0] = 0
data[1] = entry[0]
data[2] = entry[1]
spread(
'./db.test.js',
data
).then(
result => {
console.log(data[0])
++processedPlayers
log(`Process player ${processedPlayers}/${totalPlayers} -> «${entry[0]}» with ${entry[1]} points!`)
if (processedPlayers === totalPlayers)
process.exit(0)
}
)
}
}