Code Monkey home page Code Monkey logo

electron-profiler's Introduction

Electron main process profiler

An Electron main process profiler.

Build

npm install && npm run build

Usage

Sample code snippet

const fs = require('fs');
const path = require('path');
const { app, ipcMain, BrowserWindow, dialog } = require('electron');
const profiler = require('../lib/electron-profiler');

const fibo = n => {
    if (n == 0) return 0;
    if (n == 1) return 1;
    return fibo(n - 2) + fibo(n - 1);
}

let profile;
const profile_name = 'main_process_profile';
const startCpuProfiling = () => {
    profiler.cpu.startProfiling(profile_name, true);
    fibo(40);
    console.log('finish fibo');
}

const stopCpuProfiling = async () => {
    profile = profiler.cpu.stopProfiling(profile_name);
    let outputPath = dialog.showSaveDialogSync({
        filters: [{
            name: 'Cpu Profile',
            extensions: ['.cpuprofile']
        }]
    });
    if (outputPath) fs.writeFileSync(outputPath, JSON.stringify(profile));

}

const takeHeapSnapshot = () => {
    let snapshot = profiler.heap.takeSnapshot('heap_snapshot', () => { });
    let outputPath = dialog.showSaveDialogSync({
        filters: [{
            name: 'Heap Snapshot',
            extensions: ['.heapsnapshot']
        }]
    });

    if (outputPath) {
        let chunks = [];
        snapshot.serialize(
            (chunk, len) => chunks.push(chunk),
            () => fs.writeFileSync(outputPath, chunks.join(''))
        );
    }
}

const main = () => {
    let win = new BrowserWindow({
        width: 800,
        height: 600,
        webPreferences: {
            preload: path.resolve(__dirname, 'preload.js')
        }
    });
    win.webContents.loadFile(path.resolve(__dirname, 'index.html'));
    win.show();

    ipcMain.on('start-cpu-profiling', () => startCpuProfiling());
    ipcMain.on('stop-cpu-profiling', () => stopCpuProfiling());
    ipcMain.on('take-heap-snapshot', () => takeHeapSnapshot());
}

app.whenReady().then(() => {
    main();
})

This code snippet show how to create and save Cpu Profile or Heap Snapshot.

View cpu profile in DevTools

Open Chrome DevTools, DevTools -> More Tools -> Javascript Profiler -> Load, select saved cpu profile from disk. eg:

View heap snapshot in DevTools

Open Chrome DevTools, DevTools -> Memory -> Load, select saved heap snapshot from disk. eg:

electron-profiler's People

Contributors

sssooonnnggg avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Forkers

aprilandjan

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.