File tree Expand file tree Collapse file tree 3 files changed +31
-8
lines changed Expand file tree Collapse file tree 3 files changed +31
-8
lines changed Original file line number Diff line number Diff line change 1+ 'use strict' ;
2+
3+ const common = require ( '../common.js' ) ;
4+ const loadavg = require ( 'os' ) . loadavg ;
5+
6+ const bench = common . createBenchmark ( main , {
7+ n : [ 5e6 ]
8+ } ) ;
9+
10+ function main ( conf ) {
11+ const n = + conf . n ;
12+
13+ bench . start ( ) ;
14+ for ( var i = 0 ; i < n ; ++ i )
15+ loadavg ( ) ;
16+ bench . end ( n ) ;
17+ }
Original file line number Diff line number Diff line change 11'use strict' ;
22
33const binding = process . binding ( 'os' ) ;
4+ const getLoadAvg = binding . getLoadAvg ;
45const constants = process . binding ( 'constants' ) . os ;
56const internalUtil = require ( 'internal/util' ) ;
67const isWindows = process . platform === 'win32' ;
78
89exports . hostname = binding . getHostname ;
9- exports . loadavg = binding . getLoadAvg ;
1010exports . uptime = binding . getUptime ;
1111exports . freemem = binding . getFreeMem ;
1212exports . totalmem = binding . getTotalMem ;
@@ -17,6 +17,12 @@ exports.networkInterfaces = binding.getInterfaceAddresses;
1717exports . homedir = binding . getHomeDirectory ;
1818exports . userInfo = binding . getUserInfo ;
1919
20+ const avgValues = new Float64Array ( 3 ) ;
21+ exports . loadavg = function loadavg ( ) {
22+ getLoadAvg ( avgValues ) ;
23+ return [ avgValues [ 0 ] , avgValues [ 1 ] , avgValues [ 2 ] ] ;
24+ } ;
25+
2026Object . defineProperty ( exports , 'constants' , {
2127 configurable : false ,
2228 enumerable : true ,
Original file line number Diff line number Diff line change @@ -28,8 +28,10 @@ namespace node {
2828namespace os {
2929
3030using v8::Array;
31+ using v8::ArrayBuffer;
3132using v8::Boolean;
3233using v8::Context;
34+ using v8::Float64Array;
3335using v8::FunctionCallbackInfo;
3436using v8::Integer;
3537using v8::Local;
@@ -182,14 +184,12 @@ static void GetUptime(const FunctionCallbackInfo<Value>& args) {
182184
183185
184186static void GetLoadAvg (const FunctionCallbackInfo<Value>& args) {
185- Environment* env = Environment::GetCurrent (args);
186- double loadavg[3 ];
187+ CHECK (args[0 ]->IsFloat64Array ());
188+ Local<Float64Array> array = args[0 ].As <Float64Array>();
189+ CHECK_EQ (array->Length (), 3 );
190+ Local<ArrayBuffer> ab = array->Buffer ();
191+ double * loadavg = static_cast <double *>(ab->GetContents ().Data ());
187192 uv_loadavg (loadavg);
188- Local<Array> loads = Array::New (env->isolate (), 3 );
189- loads->Set (0 , Number::New (env->isolate (), loadavg[0 ]));
190- loads->Set (1 , Number::New (env->isolate (), loadavg[1 ]));
191- loads->Set (2 , Number::New (env->isolate (), loadavg[2 ]));
192- args.GetReturnValue ().Set (loads);
193193}
194194
195195
You can’t perform that action at this time.
0 commit comments