Created
November 16, 2017 11:47
-
-
Save arunoda/7c977b5b722ffc4b83ea780d97d21219 to your computer and use it in GitHub Desktop.
Node.js HTTP client for ESP8266 Load Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const http = require('http'); | |
const printHeaders = (() => { | |
let printed = false | |
return () => { | |
if (printed) return | |
console.log('timestamp, kbps') | |
printed = true; | |
} | |
})(); | |
function start() { | |
const req = http.get('http://esp-load-test.local/', (res) => { | |
printHeaders(); | |
let dataSize = 0 | |
res.on('data', (data) => { | |
dataSize += data.length | |
}) | |
res.on('error', handleError) | |
const intervalHandler = setInterval(() => { | |
if (dataSize === 0) { | |
req.abort() | |
clearInterval(intervalHandler) | |
start() | |
} | |
const speed = dataSize/1024/10 | |
dataSize = 0 | |
console.log(`${Date.now()}, ${speed}`) | |
}, 1000 * 10) | |
}) | |
.on('error', handleError) | |
function handleError(err) { | |
console.error(err.message) | |
start() | |
} | |
} | |
start(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Excuse me, Sir, I want to make an APP that user press button on web page that created with node.js cloud server to turn on/off bulb controlled by ESP8266, how to write server side codes? I will be very appreciated if you can help... My Email: stephenlai2015@gmail.com