Comparing version 0.3.2 to 0.4.0
@@ -24,2 +24,8 @@ declare namespace contextor { | ||
get: (key: string, defaultValue: any, allowUndefinedContext?: boolean) => any; | ||
/** | ||
* Get an object describing memory usage of the contextor and process | ||
* in order to help debugging potential memory leaks. | ||
* @returns {object} Memory usage description. | ||
*/ | ||
getMemoryUsage: () => object; | ||
} | ||
@@ -26,0 +32,0 @@ } |
39
index.js
@@ -172,1 +172,40 @@ 'use strict'; | ||
}; | ||
/** | ||
* Get an object describing memory usage of the contextor and process | ||
* in order to help debugging potential memory leaks. | ||
* @returns {object} Memory usage description. | ||
*/ | ||
exports.getMemoryUsage = function getMemoryUsage() { | ||
const resourceTreeSize = Object.getOwnPropertyNames(resourceTree).length; | ||
const contextsSize = Object.getOwnPropertyNames(contexts).length; | ||
const childResourcesSize = Object.getOwnPropertyNames(childResources).length; | ||
const parentResourcesSize = Object.getOwnPropertyNames(parentResources).length; | ||
const destroyedResourcesSize = Object.getOwnPropertyNames(destroyedResources).length; | ||
const memoryUsage = process.memoryUsage(); | ||
const processMemory = Object.getOwnPropertyNames(memoryUsage).reduce((map, item) => { | ||
const mbSize = (Math.round((memoryUsage[item] / 1024 / 1024) * 100) / 100).toFixed(2); | ||
// eslint-disable-next-line no-param-reassign | ||
map[item] = `${mbSize} MB`; | ||
return map; | ||
}, {}); | ||
return { | ||
processMemory, | ||
sizes: { | ||
resourceTree: resourceTreeSize, | ||
contexts: contextsSize, | ||
childResources: childResourcesSize, | ||
parentResources: parentResourcesSize, | ||
destroyedResources: destroyedResourcesSize | ||
}, | ||
contents: { | ||
resourceTree, | ||
contexts, | ||
childResources, | ||
parentResources, | ||
destroyedResources | ||
} | ||
}; | ||
}; |
{ | ||
"name": "contextor", | ||
"description": "Package allowing to pass a context along an asynchronous process", | ||
"version": "0.3.2", | ||
"version": "0.4.0", | ||
"author": "Thomas Prelot <tprelot@gmail.com> (https://github.com/Gnucki)", | ||
@@ -6,0 +6,0 @@ "contributors": [], |
@@ -65,2 +65,3 @@ # contextor | ||
- [Get a value in the current context](#get-a-value-in-the-current-context) | ||
- [Debugging](#debugging) | ||
- [Testing](#testing) | ||
@@ -99,3 +100,3 @@ - [Contributing](#contributing) | ||
### Get a value in the current context | ||
You can get a value of the current context. | ||
You can get a value of the current context: | ||
```js | ||
@@ -111,2 +112,17 @@ contextor.get('foo'); | ||
### Debugging | ||
Contextor create context for async hooks. A bad usage can lead to memory leaks.<br> | ||
Function `getMemoryUsage` has been build to help you investigate that kind of issue: | ||
```js | ||
const { inspect } = require('util'); | ||
const memoryUsage = contextor.getMemoryUsage(); | ||
console.log(inspect(memoryUsage, { | ||
compact: false, | ||
colors: true, | ||
depth: 6, | ||
})); | ||
``` | ||
## Testing | ||
@@ -113,0 +129,0 @@ Many `npm` scripts are available to help testing: |
21
test.js
@@ -117,2 +117,23 @@ 'use strict'; | ||
}); | ||
it('should be debuggable', () => { | ||
const memoryUsage = contextor.getMemoryUsage(); | ||
expect(Object.getOwnPropertyNames(memoryUsage)).to.deep.equal(['processMemory', 'sizes', 'contents']); | ||
expect(Object.getOwnPropertyNames(memoryUsage.processMemory)).to.deep.equal(['rss', 'heapTotal', 'heapUsed', 'external']); | ||
expect(Object.getOwnPropertyNames(memoryUsage.sizes)).to.deep.equal([ | ||
'resourceTree', | ||
'contexts', | ||
'childResources', | ||
'parentResources', | ||
'destroyedResources' | ||
]); | ||
expect(Object.getOwnPropertyNames(memoryUsage.contents)).to.deep.equal([ | ||
'resourceTree', | ||
'contexts', | ||
'childResources', | ||
'parentResources', | ||
'destroyedResources' | ||
]); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
19075
334
165