generated from michielbdejong/node-typescript-boilerplate
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
078fddc
commit 27a1d39
Showing
5 changed files
with
153 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,6 +36,7 @@ | |
"author": "Michiel de Jong <[email protected]>", | ||
"license": "Apache-2.0", | ||
"dependencies": { | ||
"redis": "^4.7.0", | ||
"tslib": "~2.6" | ||
}, | ||
"volta": { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
|
||
import { readFileSync } from 'fs'; | ||
import cluster from 'node:cluster'; | ||
import { availableParallelism } from 'node:os'; | ||
import process from 'node:process'; | ||
|
||
const numCPUs = availableParallelism(); | ||
const TESTNET_CSV = '../strategy-pit/__tests__/fixtures/testnet-sarafu.csv'; | ||
|
||
if (cluster.isPrimary) { | ||
console.log(`Primary ${process.pid} is running`); | ||
|
||
// Fork workers. | ||
for (let i = 0; i < numCPUs; i++) { | ||
cluster.fork({ CHUNK: i }); | ||
} | ||
cluster.on('online', worker => { | ||
console.info(`worker process ${worker.process.pid} is online`) | ||
}) | ||
cluster.on('exit', (worker, code, signal) => { | ||
console.log(`worker ${worker.process.pid} exited ${code} ${signal}`); | ||
}); | ||
} else { | ||
let cumm = 0; | ||
const mod = parseInt(process.env.CHUNK); | ||
console.log(`Worker ${process.pid} started for chunk ${mod}`); | ||
console.log("Feeding the server..."); | ||
const data = readFileSync(TESTNET_CSV, 'utf8') | ||
const lines = data.split('\n').map(line => { | ||
const [ from, to, weight ] = line.split(' ') | ||
return { from, to, weight } | ||
}).filter(line => line.from !== 'from' && line.from !== ''); | ||
for (let lineNo = mod; lineNo < lines.length; lineNo += numCPUs) { | ||
// console.log(process.pid, mod, lineNo, cumm); | ||
const result = await fetch('http://localhost:8000', { | ||
method: 'POST', | ||
body: JSON.stringify(lines[lineNo]) | ||
}); | ||
cumm += parseFloat(await result.text()); | ||
} | ||
console.log(`Worker ${process.pid} done ${cumm}`); | ||
process.exit(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { createClient } from 'redis'; | ||
|
||
const client = createClient(); | ||
|
||
client.on('error', err => console.log('Redis Client Error', err)); | ||
|
||
await client.connect(); | ||
|
||
await client.set('key', 'value'); | ||
const value = await client.get('key'); | ||
console.log(value); | ||
await client.quit(); |