import { ApiCall } from "tsrpc";
import { Reqheapdump, Resheapdump } from "../../monopoly/protocols/games/Ptlheapdump";
var heapdump = require('heapdump');
import Inspector from 'inspector-api'
const inspector = new Inspector({ storage: { type: 'fs' } })

export default async function (call: ApiCall<Reqheapdump, Resheapdump>) {

    if(call.req.act == "heapdump"){
        let name = Date.now() + '.heapsnapshot'
        heapdump.writeSnapshot(name);
        call.succ({
            code:200,
            data:name
        })
    }

    if(call.req.act == "gc"){
        global.gc();
        call.succ({
            code:200,
            data:"gc..."
        })
    }

    if(call.req.act == "cpustart"){
        await inspector.profiler.enable()
        await inspector.profiler.start()
        console.log('CPU profile has been start')
        call.succ({
            code:200,
            data:"profiler.start"
        })
    }

    if(call.req.act == "cpustop"){
        await inspector.profiler.stop()
        await inspector.profiler.disable()
        console.log('CPU profile has been stop')
        call.succ({
            code:200,
            data:"profiler.cpustop"
        })
    }
}