<commit_before>
const profiler = require('v8-profiler');
const path = require('path');
const client = require('prom-client');
const registry = new client.Registry();
let gauge = null;
let timeoutProfiling = null;
const timeoutProfilingDuration = 1000 * 60 * 5;

const resetProfiling = () => {
    if (timeoutProfiling)
        clearTimeout(timeoutProfiling);
    timeoutProfiling = setTimeout(resetProfiling, timeoutProfilingDuration);
    const profile = profiler.stopProfiling();
    profile.delete();
    // due to memory leaks
    profiler.deleteAllProfiles();
    profiler.startProfiling();
    return profile;
}

const initProfiling = () => {
    profiler.setSamplingInterval(100);
    profiler.startProfiling();
    timeoutProfiling = setTimeout(resetProfiling, timeoutProfilingDuration);
};

const clear = () => {
    registry.clear();
    gauge = new client.Gauge({ name: 'profiling', help: 'test', labelNames: [ 'signature' ], registers: [ registry ] });
};

const describe = (doc, parents, depth) => {
    if (doc.functionName) {
        if (parents + doc.functionName === "(root)#(idle)")
            return;
        if (parents + doc.functionName === "(root)#(garbage collector)")
            return;
        if (parents + doc.functionName === "(root)#(program)")
            return;
        if (doc.functionName === "slash_metrics_handler")
            return;

        //        console.log('-'.repeat(depth) + ' ' + doc.signature, doc.hitCount);
        //        console.log(parents + doc.functionName, doc.hitCount);
        gauge.set({ signature: parents + doc.functionName }, doc.hitCount);
        parents += doc.functionName + "#";
    }
//    else
//        console.log('-'.repeat(depth) + ' ' + doc.url, doc.hitCount);

    if (doc.children)
        for (var i = 0; i < doc.children.length; i++)
            describe(doc.children[i], parents, depth + 1);
};






const slash_metrics_handler = () => {
    const profile = resetProfiling();
    clear();
    describe(profile.head, "", 1);
    return registry.metrics();
}

const hostname = "0.0.0.0"
const port = 9142;

const http = require('http');

const server = http.createServer((req, res) => {
    const { method, url } = req;

    if (method === "GET" && url === "/metrics") {
        res.statusCode = 200;
        const metrics = slash_metrics_handler();
        res.end(metrics);
        return;
    } else if (method === "GET" && url === "/") {
        res.writeHead(301, {
            'Location': '/metrics'
        });
        res.end(`<html>
<head><title>Node profiling exporter</title></head>
<body>
<h1>Node profiling exporter</h1>
<p><a href="/metrics">Metrics</a></p>
</body>
</html>`);
	return;
    }

    res.statusCode = 404;
    res.end();
    return;
});

server.on('clientError', (err, socket) => {
  socket.end('HTTP/1.1 400 Bad Request\r\n\r\n');
});

const executeApp = () => {
    const appModule = path.resolve(path.join(process.argv[2]));
    console.info("\n");
    console.info("*********************************************");
    console.info('*****  Profiler listening on ' + hostname + ":" + port);
    console.info("*****  Executing:", appModule);
    console.info("*********************************************");
    console.info("\n");
    process.argv.shift();
    process.argv.shift();
    require(appModule);
}
<commit_msg>clean<commit_after>
const profiler = require('v8-profiler');
const path = require('path');
const client = require('prom-client');
const registry = new client.Registry();
let gauge = null;
let timeoutProfiling = null;
const timeoutProfilingDuration = 1000 * 60 * 5;

const resetProfiling = () => {
    if (timeoutProfiling)
        clearTimeout(timeoutProfiling);
    timeoutProfiling = setTimeout(resetProfiling, timeoutProfilingDuration);
    const profile = profiler.stopProfiling();
    profile.delete();
    profiler.startProfiling();
    return profile;
}

const initProfiling = () => {
    profiler.setSamplingInterval(100);
    profiler.startProfiling();
    timeoutProfiling = setTimeout(resetProfiling, timeoutProfilingDuration);
};

const clear = () => {
    registry.clear();
    gauge = new client.Gauge({ name: 'profiling', help: 'test', labelNames: [ 'signature' ], registers: [ registry ] });
};

const describe = (doc, parents, depth) => {
    if (doc.functionName) {
        // @todo: add a promfiler argument for verbose mode
        if (parents + doc.functionName === "(root)#(idle)")
            return;
        if (parents + doc.functionName === "(root)#(garbage collector)")
            return;
        if (parents + doc.functionName === "(root)#(program)")
            return;
        if (doc.functionName === "slash_metrics_handler")
            return;

        gauge.set({ signature: parents + doc.functionName }, doc.hitCount);
        parents += doc.functionName + "#";
    }

    if (doc.children)
        for (var i = 0; i < doc.children.length; i++)
            describe(doc.children[i], parents, depth + 1);
};






const slash_metrics_handler = () => {
    const profile = resetProfiling();
    clear();
    describe(profile.head, "", 1);
    return registry.metrics();
}

const hostname = "0.0.0.0"
const port = 9142;

const http = require('http');

const server = http.createServer((req, res) => {
    const { method, url } = req;

    if (method === "GET" && url === "/metrics") {
        res.statusCode = 200;
        const metrics = slash_metrics_handler();
        res.end(metrics);
        return;
    } else if (method === "GET" && url === "/") {
        res.writeHead(301, {
            'Location': '/metrics'
        });
        res.end(`<html>
<head><title>Node profiling exporter</title></head>
<body>
<h1>Node profiling exporter</h1>
<p><a href="/metrics">Metrics</a></p>
</body>
</html>`);
        return;
    }

    res.statusCode = 404;
    res.end();
    return;
});

server.on('clientError', (err, socket) => {
  socket.end('HTTP/1.1 400 Bad Request\r\n\r\n');
});

const executeApp = () => {
    const appModule = path.resolve(path.join(process.argv[2]));
    console.info("\n");
    console.info("*********************************************");
    console.info('*****  Profiler listening on ' + hostname + ":" + port);
    console.info("*****  Executing:", appModule);
    console.info("*********************************************");
    console.info("\n");
    process.argv.shift();
    process.argv.shift();
    require(appModule);
}

{'fraction_non_alphanumeric': 0.19807213930348258, 'fraction_numerical': 0.012126865671641791, 'mean_word_length': 3.071518987341772, 'pattern_counts': {'":': 2, '<': 33, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6280381', 'n_tokens_mistral': 2030, 'n_tokens_neox': 1925, 'n_words': 423}