@depup/node-memwatch 1.0.1-depup.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,19 @@
1
+ #ifndef __PLATFORMCOMPAT_H
2
+ #define __PLATFORMCOMPAT_H
3
+
4
+ #if defined(_MSC_VER)
5
+ #include <float.h> //isinf, isnan
6
+ #include <stdlib.h> //min
7
+ #define ISINF _finite
8
+ #define ISNAN _isnan
9
+ #define FMIN __min
10
+ #define ROUND(x) floor(x + 0.5)
11
+ #else
12
+ #include <cmath> // round(), isinf, isnan
13
+ #define ISINF std::isinf
14
+ #define ISNAN std::isnan
15
+ #define FMIN fmin
16
+ #define ROUND round
17
+ #endif
18
+
19
+ #endif
package/src/util.cc ADDED
@@ -0,0 +1,46 @@
1
+ /*
2
+ * 2012|lloyd|http://wtfpl.org
3
+ */
4
+
5
+ #include "platformcompat.hh"
6
+ #include "util.hh"
7
+
8
+ #include <sstream>
9
+
10
+ #include <stdlib.h> // abs()
11
+
12
+ std::string
13
+ mw_util::niceSize(int bytes)
14
+ {
15
+ std::stringstream ss;
16
+
17
+ if (abs(bytes) > 1024 * 1024) {
18
+ ss << ROUND(bytes / (((double) 1024 * 1024 ) / 100)) / (double) 100 << " mb";
19
+ } else if (abs(bytes) > 1024) {
20
+ ss << ROUND(bytes / (((double) 1024 ) / 100)) / (double) 100 << " kb";
21
+ } else {
22
+ ss << bytes << " bytes";
23
+ }
24
+
25
+ return ss.str();
26
+ }
27
+
28
+ std::string
29
+ mw_util::niceDelta(int seconds)
30
+ {
31
+ std::stringstream ss;
32
+
33
+ if (seconds > (60*60)) {
34
+ ss << (seconds / (60*60)) << "h ";
35
+ seconds %= (60*60);
36
+ }
37
+
38
+ if (seconds > (60)) {
39
+ ss << (seconds / (60)) << "m ";
40
+ seconds %= (60);
41
+ }
42
+
43
+ ss << seconds << "s";
44
+
45
+ return ss.str();
46
+ }
package/src/util.hh ADDED
@@ -0,0 +1,16 @@
1
+ /*
2
+ * 2012|lloyd|http://wtfpl.org
3
+ */
4
+
5
+ #include <string>
6
+
7
+ namespace mw_util {
8
+ // given a size in bytes, return a human readable representation of the
9
+ // string
10
+ std::string niceSize(int bytes);
11
+
12
+ // given a delta in seconds, return a human redable representation
13
+ std::string niceDelta(int seconds);
14
+ };
15
+
16
+
package/tests.js ADDED
@@ -0,0 +1,64 @@
1
+ const
2
+ should = require('should'),
3
+ memwatch = require('./');
4
+
5
+ describe('the library', function() {
6
+ it('should export a couple functions', function(done) {
7
+ should.exist(memwatch.gc);
8
+ should.exist(memwatch.on);
9
+ should.exist(memwatch.once);
10
+ should.exist(memwatch.removeAllListeners);
11
+ should.exist(memwatch.HeapDiff);
12
+ done();
13
+ });
14
+ });
15
+ describe('calling .gc()', function() {
16
+ it('should cause a stats() event to be emitted', function(done) {
17
+ memwatch.once('stats', function(s) {
18
+ s.should.be.object;
19
+ done();
20
+ });
21
+ memwatch.gc();
22
+ });
23
+ });
24
+
25
+ describe('HeapDiff', function() {
26
+ it('should detect allocations', function(done) {
27
+ function LeakingClass() {};
28
+ var arr = [];
29
+ var hd = new memwatch.HeapDiff();
30
+ for (var i = 0; i < 100; i++) arr.push(new LeakingClass());
31
+ var diff = hd.end();
32
+ (Array.isArray(diff.change.details)).should.be.ok;
33
+ diff.change.details.should.be.an.instanceOf(Array);
34
+ // find the LeakingClass elem
35
+ var leakingReport;
36
+ diff.change.details.forEach(function(d) {
37
+ if (d.what === 'LeakingClass')
38
+ leakingReport = d;
39
+ });
40
+ should.exist(leakingReport);
41
+ ((leakingReport['+'] - leakingReport['-']) > 0).should.be.ok;
42
+ done();
43
+
44
+ });
45
+ });
46
+
47
+ describe('HeapDiff', function() {
48
+ it('double end should throw', function(done) {
49
+ var hd = new memwatch.HeapDiff();
50
+ var arr = [];
51
+ (function() { hd.end(); }).should.not.throw();
52
+ (function() { hd.end(); }).should.throw();
53
+ done();
54
+ });
55
+ });
56
+
57
+ describe('improper HeapDiff allocation', function() {
58
+ it('should throw an exception', function(done) {
59
+ // equivalent to "new require('memwatch').HeapDiff()"
60
+ // see issue #30
61
+ (function() { new (memwatch.HeapDiff()); }).should.throw();
62
+ done();
63
+ });
64
+ });