diff --git a/ChangeLog b/ChangeLog index e8ee9de..e86df19 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,6 @@ +Unreleased + * Fix windows compatibility of memwatch.cc + v2.0.0 * BREAKING: Drop support for Node <= 9. * Add support for Node >= 10. diff --git a/src/memwatch.cc b/src/memwatch.cc index 26b9c64..a922f55 100644 --- a/src/memwatch.cc +++ b/src/memwatch.cc @@ -16,7 +16,6 @@ #include // for pow #include // for time -#include using namespace v8; using namespace node; @@ -176,10 +175,7 @@ NAN_GC_CALLBACK(memwatch::after_gc) { Nan::GetHeapStatistics(&hs); - timeval tv; - gettimeofday(&tv, NULL); - - baton->gc_ts = (tv.tv_sec * 1000000) + tv.tv_usec; + baton->gc_ts = uv_hrtime() / 1000; baton->total_heap_size = hs.total_heap_size(); baton->total_heap_size_executable = hs.total_heap_size_executable(); diff --git a/tests.js b/tests.js index 2b8a93f..3c9e07d 100644 --- a/tests.js +++ b/tests.js @@ -2,6 +2,10 @@ const should = require('should'), memwatch = require('./'); +function hrtimeInMicroseconds() { + return process.hrtime.bigint() / 1000n; +} + describe('the library', function() { it('should export a couple functions', function(done) { should.exist(memwatch.gc); @@ -14,10 +18,23 @@ describe('the library', function() { }); describe('calling .gc()', function() { it('should cause a stats() event to be emitted', function(done) { + let timeBeforeGc; + memwatch.once('stats', function(s) { + const timeAfterEvent = hrtimeInMicroseconds(); + s.should.be.object; + + (typeof timeBeforeGc).should.equal("bigint"); + timeAfterEvent.should.be.greaterThan(timeBeforeGc); + + s.gc_ts.should.be.a.Number(); + s.gc_ts.should.be.within(timeBeforeGc, timeAfterEvent); + done(); }); + + timeBeforeGc = hrtimeInMicroseconds(); memwatch.gc(); }); });