Skip to content

Instantly share code, notes, and snippets.

@aggre
Created March 11, 2017 08:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aggre/4541c4b510b3ba3c9bce7880d7460204 to your computer and use it in GitHub Desktop.
Save aggre/4541c4b510b3ba3c9bce7880d7460204 to your computer and use it in GitHub Desktop.
Performance measurement execution file of S3 database
const https = require( 'https' )
const http = require( 'http' )
const url = require( 'url' )
const target = process.argv[ 2 ] || ''
const repeat = process.argv[ 3 ] || 10
const protocol = () => target.replace( /(https?).*/, '$1' ) === 'https' ? https : http
const fetch = async ( requestOptions, requestBody, type ) => {
return await new Promise( resolve => {
const req = protocol().request( requestOptions, ( res ) => {
let body = ''
res.on( 'data', chunk => body += chunk )
res.on( 'end', () => {
console.timeEnd( type )
resolve( JSON.parse( body ) )
} )
} )
req.on( 'error', err => console.error( err ) )
if ( requestBody ) req.write( requestBody )
req.end()
console.time( type )
} )
}
const performance = async ( type, data, inbox ) => {
let count = 0
while ( count < repeat ) {
let body = null
let options = {
headers: {
'Content-Type': 'application/json'
}
}
switch ( type ) {
case 'post':
data.mail = `${ count }@${ Math.random() }`
body = JSON.stringify( data )
options.headers[ 'Content-Length' ] = body.length
break
case 'get':
options.headers[ 'Authorization' ] = `Bearer ${ inbox[ count ] }`
}
options.method = type
if ( target !== '' ) {
let parsed = url.parse( target )
options.host = parsed.hostname
options.path = parsed.pathname
if ( parsed.port ) options.port = parsed.port
}
let res = await fetch( options, body, type )
inbox.push( res.token )
count += 1
}
}
const run = async ( ...a ) => {
await performance( 'post', ...a )
await performance( 'get', ...a )
}
let payload = {
mail: '',
password: 'password'
}
let tokens = []
run( payload, tokens )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment