🩹 Remove v8 profiler (#719)
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
import { FastifyInstance, FastifyPluginCallback } from "fastify";
|
||||
import { DocumentsController } from "./controllers";
|
||||
import { createDocumentSchema, createVersionSchema, beginEditingSchema } from "./schemas";
|
||||
// import profilerPlugin from "../../../utils/profiler";
|
||||
|
||||
const baseUrl = "/companies/:company_id";
|
||||
const serviceUrl = `${baseUrl}/item`;
|
||||
@@ -10,11 +9,6 @@ const editingSessionBase = "/editing_session/:editing_session_key";
|
||||
const routes: FastifyPluginCallback = (fastify: FastifyInstance, _options, next) => {
|
||||
const documentsController = new DocumentsController();
|
||||
|
||||
// fastify.register(profilerPlugin, {
|
||||
// active: documentsController.profilingEnabled,
|
||||
// outputDir: "profiles",
|
||||
// });
|
||||
|
||||
fastify.route({
|
||||
method: "GET",
|
||||
url: `${serviceUrl}`,
|
||||
|
||||
@@ -1,61 +0,0 @@
|
||||
import fs from "fs";
|
||||
import fp from "fastify-plugin";
|
||||
import v8profiler from "v8-profiler-next";
|
||||
|
||||
export class Profiler {
|
||||
title: string;
|
||||
active: boolean;
|
||||
outputDir: string;
|
||||
constructor(options) {
|
||||
const { title, active, outputDir } = options;
|
||||
this.title = title;
|
||||
this.active = active;
|
||||
this.outputDir = outputDir;
|
||||
}
|
||||
|
||||
start() {
|
||||
if (this.active) {
|
||||
v8profiler.startProfiling(this.title, true);
|
||||
}
|
||||
}
|
||||
|
||||
finish() {
|
||||
if (this.active) {
|
||||
const profile = v8profiler.stopProfiling(this.title);
|
||||
if (profile === undefined) {
|
||||
console.log("profile is undefined: ", this.title);
|
||||
return;
|
||||
}
|
||||
profile.export((error, result) => {
|
||||
if (error) {
|
||||
console.log("Profiling error: ", error);
|
||||
} else {
|
||||
fs.writeFileSync(`${process.cwd()}/${this.outputDir}/${this.title}.cpuprofile`, result);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function profilerPlugin(fastify, options) {
|
||||
fastify.addHook("onRequest", async request => {
|
||||
const profiler = new Profiler({
|
||||
title: `${request.method}-${request.url}`,
|
||||
active: options.active,
|
||||
outputDir: options.outputDir || "profiles",
|
||||
});
|
||||
|
||||
profiler.start();
|
||||
|
||||
// Attach profiler to request for access in routes or other hooks
|
||||
request.profiler = profiler;
|
||||
});
|
||||
|
||||
fastify.addHook("onResponse", async request => {
|
||||
if (request.profiler) {
|
||||
request.profiler.finish();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export default fp(profilerPlugin);
|
||||
Reference in New Issue
Block a user