Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting tasks is slow #4

Closed
iamdriz opened this issue Jan 7, 2016 · 20 comments
Closed

Getting tasks is slow #4

iamdriz opened this issue Jan 7, 2016 · 20 comments

Comments

@iamdriz
Copy link

iamdriz commented Jan 7, 2016

I'm using this module in an Electron/NodeJS powered application that can open other exe's.
I use it to see if the exe I am trying to open is already running before opening it again. However it can take this module up to 7 seconds before the tasklist has returned. Adding this to 2-3 seconds for the exe to be called/launched it causes applications to take up to 10 seconds to be launched. Is there any reason for the lag? Or ways it could be sped up?

@kevva
Copy link
Contributor

kevva commented Jan 7, 2016

I don't see any particular time consuming task in the code other than running tasklist. Iterating over the results shouldn't be heavy at all.

@iamdriz
Copy link
Author

iamdriz commented Jan 7, 2016

Here is what the code looks like. While a lot of the code below isn't directly relevant to this module.

The first two console logs returned under a second. But the console log inside the task list callback takes over 7 seconds.

If I remove the tasklist code completely and just run everything else, then the whole block completes in about 2 seconds. So the tasklist callback is where the delay is happening... so it seems like it's slow to get the list of tasks....

ipc.on('launch', function(event, arg) {

    console.log('launch called') // this happens under 1 second

    // get the windowTitle and exe path from the passed args
    var theApp = arg['appName'],
        theExe = arg['appExe'];

    var appList,
        openApp;

    console.log('tasks started') // this happens under 1 second

    tasklist(function(err, tasks) {

        console.log('tasks callback'); // it takes up to 7 seconds for this to fire

        if (err) throw err;
        // map the apps from the tasklist into a new array
        appList = tasks.map(function(task) {
            return {
                id   : task.pid,
                name : task.imageName,
                windowTitle: task.windowTitle
            };
        });

        // see if any open apps in the appList match the one we are requesting
        for( var i = 0; i<appList.length; i++)
        {
            if( appList[i]['windowTitle'] == theApp )
                openApp = appList[i]['id'];
        }

        // if app isn't open then launch it
        if( !openApp )
        {
            if( theExe == null )
            {
                var theCommand = __dirname + '/../../../' + theApp + '/' + theApp + '.exe';
            }
            else
            {
                var theCommand = '"' + theExe + '"';
            }
            exec(theCommand, function(error, stdout, stderr) {

            });
        }
        // else run the focus script passing the pid
        else
        {

            exec('wscript ' + __dirname + '/Focus.vbs ' + openApp, function(error, stdout, stderr) {

            });
        }

    }); // end tasklist

});

@kevva
Copy link
Contributor

kevva commented Jan 7, 2016

It feels like this is related to your code somehow. Just running this takes 0.5s on my machine:

const tasklist = require('tasklist');

tasklist().then(data => {
    console.log(data);
});

@SamVerschueren
Copy link

Probably not the issue, but you're working with an older version of tasklist which uses a callback function.

@iamdriz
Copy link
Author

iamdriz commented Jan 7, 2016

@kevva it took about 3-4 seconds when I ran you code on its own inside from the command prompt. So it seems it's taking longer on certain machines than others. However it's still much quicker than when I run it inside my Electron app. I wonder if NodeJS modules run slower from Electron...

@SamVerschueren I tested this with the new version and it's the same.

@kevva
Copy link
Contributor

kevva commented Jan 7, 2016

How long does running tasklist from the command line take?

@iamdriz
Copy link
Author

iamdriz commented Jan 7, 2016

@kevva About .5s

So in short I have this happening:

Native call: .5s
Command prompt call of module: 3-4s
Electron call of module: 7+s

It's still much slower when running the NodeJS version compared to the native.... So wonder if that's the issue and when running it inside Electron it just doubles up that time.

@sindresorhus
Copy link
Owner

That's really weird. Spawning comes with some overhead, but that should be 30-100ms, not seconds. I would recommend you create a super simple spawning Node.js example executable that just spawns tasklist.exe, nothing more, and open a Node.js issue if it's still that slow.

@iamdriz
Copy link
Author

iamdriz commented Jan 7, 2016

@sindresorhus Could you provide a quick example of exactly that so I can test if that is also slow. Unless you literally mean exec('tasklist.exe', function(... Thanks

@sindresorhus
Copy link
Owner

Unless you literally mean exec('tasklist.exe', function(...

I literally mean that.

@iamdriz
Copy link
Author

iamdriz commented Jan 7, 2016

@sindresorhus So this?

var exec = require('child_process').exec;

exec('tasklist.exe', function(error, stdout, stderr) {

});

@sindresorhus
Copy link
Owner

@iamdriz No, the same as this module.

childProcess.execFile('tasklist')

@iamdriz
Copy link
Author

iamdriz commented Jan 7, 2016

@sindresorhus Okay I have done that, but it just executes immediately and nothing is returned... Is this correct?

var childProcess = require('child_process');
childProcess.execFile('tasklist');

@kevva
Copy link
Contributor

kevva commented Jan 7, 2016

How long does it take to run?

@iamdriz
Copy link
Author

iamdriz commented Jan 7, 2016

@kevva It's pretty much instant.

@SamVerschueren
Copy link

@iamdriz execFile expects a callback.

var childProcess = require('child_process');
childProcess.execFile('tasklist', function(err, result) {
     console.log(result);
});

@iamdriz
Copy link
Author

iamdriz commented Jan 8, 2016

@SamVerschueren Again, this takes like 1 second. So not sure why tasklist module is taking 2-3 seconds longer and then when run inside Electron a further 4 seconds.

So to clarify:

This takes about 3 times longer:

var tasklist = require('tasklist');
tasklist(function(err, tasks) {
    console.log(tasks);
});

than this:

var childProcess = require('child_process');
childProcess.execFile('tasklist', function(err, result) {
     console.log(result);
});

So there is serious overhead with the module approach over the native call.

But when this same code is run inside Electron it then takes even longer, nearly doubling it.

@kevva
Copy link
Contributor

kevva commented Jan 8, 2016

So either this or this is a serious bottleneck, which sounds weird because it isn't any intensive stuff really. Can you try editing and removing any of them locally and see if it makes any difference?

@SamVerschueren
Copy link

@kevva You linked the same code block twice :)

@iamdriz you could clone the repository and use something like time-span to calculate some stuff on your machine. For instance, what's the time it takes to run (execFile) tasklist. What's the time it takes to process this block.

@sindresorhus
Copy link
Owner

Closing for the same reason as electron/electron#4020 (comment).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants