spawn.js 882 B

1234567891011121314151617181920212223242526
  1. const { readFile } = require('fs')
  2. function spawn (source) {
  3. const shared = new SharedArrayBuffer(4)
  4. const u32 = new Uint32Array(shared)
  5. const tid = just.thread.spawn(source, just.args.slice(1), shared)
  6. const thread = { tid, u32 }
  7. threads.push(thread)
  8. return thread
  9. }
  10. function onTimer () {
  11. const { user, system } = just.cpuUsage()
  12. const { rss } = just.memoryUsage()
  13. let total = 0
  14. for (const thread of threads) {
  15. total += Atomics.load(thread.u32, 0)
  16. }
  17. just.print(`threads ${threads.length} total ${total} mem ${rss} cpu (${user.toFixed(2)}/${system.toFixed(2)}) ${(user + system).toFixed(2)} qps/core ${(total / (user + system)).toFixed(2)}`)
  18. }
  19. const source = readFile(just.args[2] || 'test.js')
  20. const cpus = parseInt(just.env().CPUS || just.sys.cpus, 10)
  21. const threads = []
  22. for (let i = 0; i < cpus; i++) spawn(source)
  23. just.setInterval(onTimer, 1000)