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

os: improve loadavg() performance #11516

Merged
merged 1 commit into from
Feb 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions benchmark/os/loadavg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

const common = require('../common.js');
const loadavg = require('os').loadavg;

const bench = common.createBenchmark(main, {
n: [5e6]
});

function main(conf) {
const n = +conf.n;

bench.start();
for (var i = 0; i < n; ++i)
loadavg();
bench.end(n);
}
8 changes: 7 additions & 1 deletion lib/os.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
'use strict';

const binding = process.binding('os');
const getLoadAvg = binding.getLoadAvg;
const constants = process.binding('constants').os;
const internalUtil = require('internal/util');
const isWindows = process.platform === 'win32';

exports.hostname = binding.getHostname;
exports.loadavg = binding.getLoadAvg;
exports.uptime = binding.getUptime;
exports.freemem = binding.getFreeMem;
exports.totalmem = binding.getTotalMem;
Expand All @@ -17,6 +17,12 @@ exports.networkInterfaces = binding.getInterfaceAddresses;
exports.homedir = binding.getHomeDirectory;
exports.userInfo = binding.getUserInfo;

const avgValues = new Float64Array(3);
exports.loadavg = function loadavg() {
getLoadAvg(avgValues);
return [avgValues[0], avgValues[1], avgValues[2]];
};

Object.defineProperty(exports, 'constants', {
configurable: false,
enumerable: true,
Expand Down
14 changes: 7 additions & 7 deletions src/node_os.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ namespace node {
namespace os {

using v8::Array;
using v8::ArrayBuffer;
using v8::Boolean;
using v8::Context;
using v8::Float64Array;
using v8::FunctionCallbackInfo;
using v8::Integer;
using v8::Local;
Expand Down Expand Up @@ -182,14 +184,12 @@ static void GetUptime(const FunctionCallbackInfo<Value>& args) {


static void GetLoadAvg(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
double loadavg[3];
CHECK(args[0]->IsFloat64Array());
Local<Float64Array> array = args[0].As<Float64Array>();
CHECK_EQ(array->Length(), 3);
Local<ArrayBuffer> ab = array->Buffer();
double* loadavg = static_cast<double*>(ab->GetContents().Data());
uv_loadavg(loadavg);
Local<Array> loads = Array::New(env->isolate(), 3);
loads->Set(0, Number::New(env->isolate(), loadavg[0]));
loads->Set(1, Number::New(env->isolate(), loadavg[1]));
loads->Set(2, Number::New(env->isolate(), loadavg[2]));
args.GetReturnValue().Set(loads);
}


Expand Down