Windows and Node: Windows Performance Monitor

Alex R. Young





tutorials node windows windows-and-node

Windows and Node: Windows Performance Monitor

Posted by Alex R. Young on .

tutorials node windows windows-and-node

Windows and Node: Windows Performance Monitor

Posted by Alex R. Young on .

It's time to take a look at a Windows-oriented Node module to see how it works. Before getting messy with C, C++, and all that native module stuff, let's keep it simple by looking at something that communicates with Windows programs using Node's streams.

Windows Performance Monitor

Windows Performance Monitor provides extremely detailed logging, featuring a wide array of counters, event trace data, and configuration information. The command-line tool, typeperf.exe can write some of this data to a command window.

For example, typeperf "\Memory\Available bytes" "\processor(_total)\% processor time" will display processor and memory counters. The output looks like this on my computer:


Why do I mention this? Well, the Node perfmon module is a wrapper around typeperf that provides a ReadableStream-based API. This is an idiomatic module that demonstrates one way of working with an existing program that comes bundled with Windows.

The perfmon Module

To try it out, open a command window and make a directory:

cd Documents\Code\  
mkdir perf-example  
cd perf-example  

Then install perfmon:

npm install perfmon  

Once that's done, create a JavaScript file that uses the perfmon module to gather some data:

var perfmon = require('perfmon');

perfmon('\\processor(_total)\\% processor time', function(err, data) {  

Running it should display some logging. Press ctrl-c to stop it.

Child Processes

The author has written this by using spawn from the child_process module. In fact, we can execute any executable using this module. Try the following script:

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

exec('echo %PATH%', function(err, stdout, stderr) {  
  console.log('PATH:', stdout);

The output should look similar to the following screenshot, but it may vary depending on what you've got installed:


The child_process API is exactly the same in Unix, as we saw in the Unix and Node posts.


Does this mean Node programs are generally portable between Unix and Windows? Well, as you can see in this example, Node's standard libraries look the same. It's just the part that touches the operating system that may look different. In this example, the part that reads echo %PATH% would be different in Unix.

We'll look at other portability issues as this series progresses. For now, your homework is to find more Windows-oriented Node modules and see if you can understand the basic principles at work. The Node documentation will be indispensable for this!