Skip to content

Commit 17687f4

Browse files
committed
use doubles for startTime and endTime
don’t lose precision by doing integer arithmetic
1 parent ed5b7ce commit 17687f4

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

src/cpu_profile.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ namespace nodex {
8282
profile->Set(NanNew<String>("head"), head);
8383

8484
#if (NODE_MODULE_VERSION > 0x000B)
85-
Local<Value> start_time = NanNew<Number>(node->GetStartTime()/1000000);
86-
Local<Value> end_time = NanNew<Number>(node->GetEndTime()/1000000);
85+
Local<Value> start_time = NanNew<Number>((double)node->GetStartTime()/1000000.00);
86+
Local<Value> end_time = NanNew<Number>((double)node->GetEndTime()/1000000.00);
8787
Local<Array> samples = NanNew<Array>();
8888

8989
uint32_t count = node->GetSamplesCount();

test/cpu_cprofiler.js

+11
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,17 @@ describe('CPU', function() {
6767
expect(profile.samples.length > 0).to.equal(true);
6868
});
6969

70+
it('should record startTime and endTime with subsecond precision', function(done) {
71+
profiler.startProfiling('P');
72+
var delayInMilliseconds = 150;
73+
setTimeout(function() {
74+
var profile = profiler.stopProfiling();
75+
var elapsedTime = profile.endTime - profile.startTime;
76+
expect(elapsedTime).to.be.at.least(delayInMilliseconds / 1000);
77+
expect(elapsedTime).to.be.at.below(3 * delayInMilliseconds / 1000);
78+
done();
79+
}, delayInMilliseconds);
80+
});
7081
});
7182

7283
describe('Profile', function() {

v8-profiler.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,13 @@ var profiler = {
111111
get profiles() { return binding.cpu.profiles; },
112112

113113
startProfiling: function(name, recsamples) {
114-
startTime = Date.now();
114+
startTime = Date.now() / 1000;
115115
binding.cpu.startProfiling(name, recsamples);
116116
},
117117

118118
stopProfiling: function(name) {
119119
var profile = binding.cpu.stopProfiling(name);
120-
endTime = Date.now();
120+
endTime = Date.now() / 1000;
121121
profile.__proto__ = CpuProfile.prototype;
122122
if (!profile.startTime) profile.startTime = startTime;
123123
if (!profile.endTime) profile.endTime = endTime;

0 commit comments

Comments
 (0)