just
just copied to clipboard
Apache benchmark not working.
Wanted to test http with ab.
ab -n 1 -c 1 http://localhost:8080/plaintext
using this :
const stringify = require('@stringify')
const http = require('@http')
const process = require('process')
const { createServer, responses } = http
const { sjs, attr } = stringify
const message = 'Hello, World!'
const json = { message }
function spawn (main) {
if (just.env()['WORKER']) return main()
const { watch, launch } = process
const processes = []
const cpus = parseInt(just.env().CPUS || just.sys.cpus, 10)
for (let i = 0; i < cpus; i++) {
just.sys.setenv('WORKER', i)
//const proc = launch(just.args[0], ['--trace-gc', ...just.args.slice(1)])
const proc = launch(just.args[0], just.args.slice(1))
processes.push(proc)
proc.stats = { user: 0, system: 0 }
}
return Promise.all(processes.map(p => watch(p)))
}
class Clock {
constructor () {
this.slots = new Map()
}
unset (callback, repeat = 1000) {
const current = this.slots.get(repeat)
if (!current) return
current.callbacks = current.callbacks.filter(cb => cb !== callback)
if (!current.callbacks.length) {
just.clearTimeout(current.timer)
this.slots.delete(repeat)
}
}
set (callback, repeat = 1000) {
let current = this.slots.get(repeat)
if (current) {
current.callbacks.push(callback)
return
}
current = {
callbacks: [callback],
timer: just.setInterval(() => current.callbacks.forEach(cb => cb()), repeat)
}
this.slots.set(repeat, current)
}
}
async function main() {
const sJSON = sjs({ message: attr('string') })
const server = createServer()
.get('/plaintext', res => res.text(message))
.get('/json', res => res.utf8(sJSON(json), responses.json))
.listen('0.0.0.0', 8080)
const clock = new Clock()
clock.set(() => {
server.update()
})
}
spawn(main)
.catch(err => just.error(err.stack))
@billywhizz ?
I think this is related to limitation of apache benchmark for newer tech : http2 + , websockets