Skip to content

Commit

Permalink
cosmetics + speed up ci by removing benchs and adding back package-lock
Browse files Browse the repository at this point in the history
Avoid using require() in the main function

Improve method readability

Add back examples

Add package-lock back to speed up ci
  • Loading branch information
abluchet committed Mar 19, 2018
1 parent 4789ae9 commit 8dbad5b
Show file tree
Hide file tree
Showing 13 changed files with 8,067 additions and 160 deletions.
130 changes: 2 additions & 128 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,129 +1,3 @@

# Created by https://www.gitignore.io/api/linux,macos,windows,node

### Linux ###
*~

# temporary files which can be created if a process still has a handle open of a deleted file
.fuse_hidden*

# KDE directory preferences
.directory

# Linux trash folder which might appear on any partition or disk
.Trash-*

# .nfs files are created when an open file is removed but is still being accessed
.nfs*

### macOS ###
*.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon

# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk

### Node ###
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
node_modules
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Typescript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env


### Windows ###
# Windows thumbnail cache files
Thumbs.db
ehthumbs.db
ehthumbs_vista.db

# Folder config file
Desktop.ini

# Recycle Bin used on file shares
$RECYCLE.BIN/

# Windows Installer files
*.cab
*.msi
*.msm
*.msp

# Windows shortcuts
*.lnk


# End of https://www.gitignore.io/api/linux,macos,windows,node
coverage
1 change: 0 additions & 1 deletion .npmrc

This file was deleted.

2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,3 @@ os:
cache:
directories:
- node_modules
after_success:
- npm run bench
18 changes: 15 additions & 3 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
### 2.0

There is no BC break but because the code got refactored a lot and that the default retrieval method has changed we're bumping a major version.

- allow multiple pids
- remove `advanced` option
- `start` result is now a `Date` instance
- don't use `/proc` files anymore but use `ps` instead
- more tests
- API change no more `stat` method, module exports a single function
- no more `unmonitor` method, this is handed internally
- the default call now returns more data:

```
{
cpu: 10.0, // percentage (it may happen to be greater than 100%)
memory: 357306368, // bytes
ppid: 312, // PPID
pid: 727, // PID
ctime: 867000, // ms user + system time
elapsed: 6650000, // ms since the start of the process
timestamp: 864000000 // ms since epoch
}
```

### 1.2.0

Expand Down
2 changes: 0 additions & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,3 @@ install:
- npm install
test_script:
- npm test
after_test:
- npm run bench
11 changes: 11 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Example section

## server.js

Used to be tested with an HTTP benchmark tool like this one: https://github.com/wg/wrk.

Start the server, run the tests on `localhost:8020`

## stresstest.js

Just start node stresstest.js (but be carefull though...)
21 changes: 21 additions & 0 deletions examples/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
var http = require('http')
var pidusage = require('../')

http.createServer(function (req, res) {
res.writeHead(200)
res.end('hello world\n')
}).listen(8020)

var interval = setInterval(function () {
pidusage(process.pid, function (err, stat) {
if (err) {
throw err
}

console.log(stat)
})
}, 100)

process.on('exit', function () {
clearInterval(interval)
})
83 changes: 83 additions & 0 deletions examples/stresstest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
var pusage = require('../')

// stress test to compare with top or another tool
console.log('This is my PID: %s', process.pid)

// classic "drop somewhere"... yeah I'm a lazy guy
var formatBytes = function (bytes, precision) {
var kilobyte = 1024
var megabyte = kilobyte * 1024
var gigabyte = megabyte * 1024
var terabyte = gigabyte * 1024

if ((bytes >= 0) && (bytes < kilobyte)) {
return bytes + ' B '
} else if ((bytes >= kilobyte) && (bytes < megabyte)) {
return (bytes / kilobyte).toFixed(precision) + ' KB '
} else if ((bytes >= megabyte) && (bytes < gigabyte)) {
return (bytes / megabyte).toFixed(precision) + ' MB '
} else if ((bytes >= gigabyte) && (bytes < terabyte)) {
return (bytes / gigabyte).toFixed(precision) + ' GB '
} else if (bytes >= terabyte) {
return (bytes / terabyte).toFixed(precision) + ' TB '
} else {
return bytes + ' B '
}
}

var i = 0
var bigMemoryLeak = []

var stress = function (cb) {
var j = 500
var arr = []

while (j--) {
arr[j] = []

for (var k = 0; k < 1000; k++) {
arr[j][k] = {lorem: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum non odio venenatis, pretium ligula nec, fringilla ipsum. Sed a erat et sem blandit dignissim. Pellentesque sollicitudin felis eu mattis porta. Nullam nec nibh nisl. Phasellus convallis vulputate massa vitae fringilla. Etiam facilisis lectus in odio lacinia rutrum. Praesent facilisis vitae urna a suscipit. Aenean lacinia blandit lorem, et ullamcorper metus sagittis faucibus. Nam porta eros nisi, at adipiscing quam varius eu. Vivamus sed sem quis lorem varius posuere ut quis elit.'}
}
}

bigMemoryLeak.push(arr)

pusage(process.pid, function (err, stat) {
if (err) {
throw err
}

console.log('Pcpu: %s', stat.cpu)
console.log('Mem: %s', formatBytes(stat.memory))

if (i === 100) {
return cb(null, true)
} else if (stat.memory > 3e8) {
console.log("That's enough right?")
cb(null, true)
}

i++
return cb(null, false)
})
}

var interval = function () {
return setTimeout(function () {
stress(function (err, stop) {
if (err) {
throw err
}

if (stop) {
process.exit()
} else {
return interval()
}
})
}, 400)
}

setTimeout(function () {
interval()
}, 2000)
27 changes: 15 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@
'use strict'

function pify (fn, arg1) {
return new Promise(function (resolve, reject) {
fn(arg1, function (err, data) {
if (err) return reject(err)
resolve(data)
})
})
}

var stats = require('./lib/stats')

/**
* Get pid informations.
* @public
* @param {Number|Number[]|String|String[]} pids A pid or a list of pids.
* @param {Object} [options={}] Options object
* @param {Function} [callback=undefined] Called when the statistics are ready.
* If not provided a promise is returned instead.
* @returns {Promise.<Object>} Only when the callback is not provided.
*/
function pidusage (pids, callback) {
function pidusage (pids, options, callback) {
if (typeof options === 'function') {
callback = options
options = {}
}

if (typeof callback === 'function') {
stats(pids, callback)
stats(pids, options, callback)
return
}
return pify(stats, pids)

return new Promise(function (resolve, reject) {
stats(pids, options, function (err, data) {
if (err) return reject(err)
resolve(data)
})
})
}

module.exports = pidusage
12 changes: 7 additions & 5 deletions lib/history.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ function sheduleInvalidator () {
if (interval === null) {
interval = setInterval(runInvalidator, maxage / 2)
}
} else {
if (interval !== null) {
clearInterval(interval)
interval = null
}

return
}

if (interval !== null) {
clearInterval(interval)
interval = null
}
}

Expand Down
Loading

0 comments on commit 8dbad5b

Please sign in to comment.