Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: Run checks
on: push
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 18
- name: Install modules
run: npm install
- name: Run Mocha
run: npm run test
1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nodejs 18.8.0
28 changes: 28 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Mocha Tests",
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
"args": [
"--timeout",
"60000",
"--reporter",
"dot",
"--slow",
"5000",
"--colors",
"${workspaceFolder}/test/**/*.test.js",
],
"internalConsoleOptions": "openOnSessionStart",
"skipFiles": [
"<node_internals>/**"
]
}
]
}
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Does not only support spotting and fixing memory leaks, but writing tests also e
## Table of Contents

- [Installation](#installation)
- [Development](#development)
- [Usage](#usage)
- [Memory Management in JS?](#memory-management-in-js)
- [API](#api)
Expand All @@ -32,6 +33,21 @@ npm install --save-dev leakage
yarn --dev leakage
```

## Development

To build locally, you will need asdf (https://asdf-vm.com/). Installation instructions vary by operating system but are fairly self-explanatory.

You will also need asdf-nodejs (https://github.com/asdf-vm/asdf-nodejs).

Once you've installed the tooling, run

```
asdf install nodejs 16.15.0 && asdf reshim nodejs
```

To doublecheck that you have the correct node, run `node --version` and verify that the output is v16.15.0 (or at least consistent with what is in .tool-versions).

You should then be able to `npm install` and plug away at the codebase.

## Usage

Expand Down
16 changes: 8 additions & 8 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/

const fs = require('fs')
const memwatch = require('@aidemaster/node-memwatch')
const memwatch = require('@airbnb/node-memwatch')
const minimist = require('minimist')
const path = require('path')
const { createResult } = require('./result')
Expand All @@ -44,7 +44,7 @@ function iterate (iteratorFn, options = {}) {
for (let index = 0; index < iterations; index++) {
const result = iteratorFn()
if (result && typeof result.then === 'function') {
throw new Error(`Tried to use iterate() on an async function. Use iterate.async() instead.`)
throw new Error('Tried to use iterate() on an async function. Use iterate.async() instead.')
}
}
return heapDiff.end()
Expand All @@ -54,7 +54,7 @@ function iterate (iteratorFn, options = {}) {
const heapDiffs = new Array(gcollections)

for (let gcIndex = 0; gcIndex < gcollections; gcIndex++) {
heapDiffs[ gcIndex ] = runAndHeapDiff()
heapDiffs[gcIndex] = runAndHeapDiff()
}

if (argv['heap-file']) {
Expand Down Expand Up @@ -107,7 +107,7 @@ iterate.async = function iterateAsync (iteratorFn, options = {}) {
},
onHeapDiff () {
memwatch.gc()
heapDiffs[ runner.gcIndex ] = runner.heapDiff.end()
heapDiffs[runner.gcIndex] = runner.heapDiff.end()
runner.gcIndex++

if (runner.gcIndex === gcollections) {
Expand Down Expand Up @@ -143,9 +143,9 @@ iterate.async = function iterateAsync (iteratorFn, options = {}) {

if (currentlyRunningTests > 0) {
return reject(new Error(
`Detected concurrently running tests. ` +
`This will render the heap snapshots unusable. ` +
`Make sure the tests are run strictly sequentially.`
'Detected concurrently running tests. ' +
'This will render the heap snapshots unusable. ' +
'Make sure the tests are run strictly sequentially.'
))
}
currentlyRunningTests++
Expand All @@ -154,7 +154,7 @@ iterate.async = function iterateAsync (iteratorFn, options = {}) {
const promise = iteratorFn()

if (!promise || typeof promise.then !== 'function') {
return reject(new Error(`Tried to use iterate.async() on a synchronous function. Use iterate() instead.`))
return reject(new Error('Tried to use iterate.async() on a synchronous function. Use iterate() instead.'))
}

promise.then(
Expand Down
2 changes: 1 addition & 1 deletion lib/result.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Result {
const minimum = changesInBytes.reduce((min, change) => change < min ? change : min, Infinity)
const maximum = changesInBytes.reduce((max, change) => change > max ? change : max, -Infinity)

log(title ? `Leak test summary - ${title}:` : `Leak test summary:`)
log(title ? `Leak test summary - ${title}:` : 'Leak test summary:')
log(` Did ${this.gcollections} heap diffs, iterating ${this.iterations} times each.`)
log(` Heap diff summary: ${formatDiffSize(average)} avg, ${formatDiffSize(minimum)} min, ${formatDiffSize(maximum)} max`)
log(` Heap diffs: ${this.heapDiffs.map(heapDiff => formatDiffSize(heapDiff.change.size_bytes))}`)
Expand Down
4 changes: 2 additions & 2 deletions lib/testConstantHeapSize.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function testConstantHeapSize (heapDiffs, { iterations, gcollections, sensitivit
const throwOnSubsequentHeapGrowths = Math.floor(heapDiffs.length * 2 / 3)

if (subsequentHeapGrowths.length > throwOnSubsequentHeapGrowths) {
const lastHeapDiff = subsequentHeapGrowths[ subsequentHeapGrowths.length - 1 ]
const lastHeapDiff = subsequentHeapGrowths[subsequentHeapGrowths.length - 1]
const heapGrowthIterations = Math.round(subsequentHeapGrowths.length * iterations)

const growthInBytes = subsequentHeapGrowths
Expand All @@ -25,7 +25,7 @@ function testConstantHeapSize (heapDiffs, { iterations, gcollections, sensitivit
`(${formatInteger(heapGrowthIterations)} of ${iterations * gcollections} iterations) ` +
`by ${prettyBytes(growthInBytes)}.\n\n` +
` Iterations between GCs: ${formatInteger(iterations)}\n\n` +
` Final GC details:\n` +
' Final GC details:\n' +
` ${prettyHeapContents(lastHeapDiff).trimLeft()}\n`
)
} else {
Expand Down
Loading