Skip to content

Commit

Permalink
Rewrite profiles on object
Browse files Browse the repository at this point in the history
  • Loading branch information
3y3 committed Feb 8, 2016
1 parent 2cfaf74 commit 3370c3b
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 53 deletions.
31 changes: 13 additions & 18 deletions src/cpu_profile.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace nodex {
using v8::Value;

Nan::Persistent<ObjectTemplate> Profile::profile_template_;
Nan::Persistent<Array> Profile::profiles;
Nan::Persistent<Object> Profile::profiles;
uint32_t Profile::uid_counter = 0;

NAN_METHOD(Profile_EmptyMethod) {
Expand All @@ -36,19 +36,10 @@ namespace nodex {
NAN_METHOD(Profile::Delete) {
Local<Object> self = info.This();
void* ptr = Nan::GetInternalFieldPointer(self, 0);
Local<Array> profiles = Nan::New<Array>(Profile::profiles);

uint32_t count = profiles->Length();
for (uint32_t index = 0; index < count; index++) {
if (profiles->Get(index) == info.This()) {
Local<Value> argv[2] = {
Nan::New<Integer>(index),
Nan::New<Integer>(1)
};
Local<Function>::Cast(profiles->Get(Nan::New<String>("splice").ToLocalChecked()))->Call(profiles, 2, argv);
break;
}
}
Local<Object> profiles = Nan::New<Object>(Profile::profiles);
Local<Value> _uid = info.This()->Get(Nan::New<String>("uid").ToLocalChecked());
Local<String> uid = Nan::To<String>(_uid).ToLocalChecked();
profiles->Delete(uid);
static_cast<CpuProfile*>(ptr)->Delete();
}

Expand All @@ -64,15 +55,19 @@ namespace nodex {
Local<Object> profile = Nan::New(profile_template_)->NewInstance();
Nan::SetInternalFieldPointer(profile, 0, const_cast<CpuProfile*>(node));

const uint32_t uid_length = (((sizeof uid_counter) * 8) + 2)/3 + 2;
char _uid[uid_length];
sprintf(_uid, "%d", uid_counter);

Local<Value> CPU = Nan::New<String>("CPU").ToLocalChecked();
Local<Value> uid = Nan::New<Integer>(uid_counter);
Local<Value> uid = Nan::New<String>(_uid).ToLocalChecked();
#if (NODE_MODULE_VERSION >= 45)
Local<String> title = node->GetTitle();
#else
Local<String> title = Nan::New(node->GetTitle());
#endif
if (!title->Length()) {
char _title[32];
char _title[8 + uid_length];
sprintf(_title, "Profile %i", uid_counter);
title = Nan::New<String>(_title).ToLocalChecked();
}
Expand Down Expand Up @@ -101,8 +96,8 @@ namespace nodex {
profile->Set(Nan::New<String>("timestamps").ToLocalChecked(), timestamps);
#endif

Local<Array> profiles = Nan::New<Array>(Profile::profiles);
profiles->Set(profiles->Length(), profile);
Local<Object> profiles = Nan::New<Object>(Profile::profiles);
profiles->Set(uid, profile);

return scope.Escape(profile);
}
Expand Down
2 changes: 1 addition & 1 deletion src/cpu_profile.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace nodex {
class Profile {
public:
static v8::Local<v8::Value> New(const v8::CpuProfile* node);
static Nan::Persistent<v8::Array> profiles;
static Nan::Persistent<v8::Object> profiles;
private:
static NAN_METHOD(Delete);
static void Initialize();
Expand Down
4 changes: 2 additions & 2 deletions test/binding.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ describe('binding', function() {
it('should delete itself from profiler cache', function() {
cpu.startProfiling('', true);
var profile = cpu.stopProfiling();
var oldProfilesLength = cpu.profiles.length;
var oldProfilesLength = Object.keys(cpu.profiles).length;
profile.delete();
expect(cpu.profiles.length == oldProfilesLength - 1).to.equal(true);
expect(oldProfilesLength - Object.keys(cpu.profiles).length).to.equal(1);
});
});

Expand Down
6 changes: 3 additions & 3 deletions test/v8-profiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('v8-profiler', function() {
});

it('should cache profiles', function() {
expect(profiler.profiles).to.have.length(1);
expect(Object.keys(profiler.profiles)).to.have.length(1);
});

it('should replace profile title, if started with name argument', function() {
Expand Down Expand Up @@ -73,8 +73,8 @@ describe('v8-profiler', function() {
});

function deleteAllProfiles() {
profiler.profiles.slice().forEach(function(profile) {
profile.delete();
Object.keys(profiler.profiles).forEach(function(key) {
profiler.profiles[key].delete();
});
}
});
Expand Down
31 changes: 2 additions & 29 deletions v8-profiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,22 +152,6 @@ var profiler = {
return snapshot;
},

getSnapshot: function(index) {
var snapshot = binding.heap.snapshots[index];
if (!snapshot) return;
snapshot.__proto__ = Snapshot.prototype;
return snapshot;
},

findSnapshot: function(uid) {
var snapshot = binding.heap.snapshots.filter(function(snapshot) {
return snapshot.uid == uid;
})[0];
if (!snapshot) return;
snapshot.__proto__ = Snapshot.prototype;
return snapshot;
},

deleteAllSnapshots: function () {
Object.keys(binding.heap.snapshots).forEach(function(key) {
binding.heap.snapshots[key].delete();
Expand Down Expand Up @@ -258,20 +242,9 @@ var profiler = {
binding.cpu.setSamplingInterval(num);
},

getProfile: function(index) {
return binding.cpu.profiles[index];
},

findProfile: function(uid) {
var profile = binding.cpu.profiles.filter(function(profile) {
return profile.uid == uid;
})[0];
return profile;
},

deleteAllProfiles: function() {
binding.cpu.profiles.forEach(function(profile) {
profile.delete();
Object.keys(binding.cpu.profiles).forEach(function(key) {
binding.cpu.profiles[key].delete();
});
}
};
Expand Down

0 comments on commit 3370c3b

Please # to comment.