Skip to content

Commit 63f8ec0

Browse files
committed
WIP
1 parent afa0e3c commit 63f8ec0

File tree

9 files changed

+125
-94
lines changed

9 files changed

+125
-94
lines changed

.babelrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"@babel/preset-env"
44
],
55
"plugins": [
6+
"@babel/plugin-transform-template-literals",
67
"@babel/plugin-transform-async-to-generator",
78
"@babel/plugin-proposal-class-properties"
89
]

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"@babel/node": "^7.10.5",
3939
"@babel/plugin-proposal-class-properties": "^7.10.4",
4040
"@babel/plugin-transform-async-to-generator": "^7.10.4",
41+
"@babel/plugin-transform-template-literals": "^7.10.5",
4142
"@babel/polyfill": "^7.11.5",
4243
"@babel/preset-env": "^7.11.5"
4344
}

src/clients/PerformanceTimer.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
const { PerformanceObserver, performance } = require('perf_hooks')
1+
import chalk from 'chalk'
2+
import { PerformanceObserver, performance } from 'perf_hooks'
3+
import { log, round } from './utils'
4+
5+
function ms2sec(ms, fraction) {
6+
return `${round(ms/1000, fraction)}s`
7+
}
28

39
function performanceObserverCallback(items) {
4-
console.log('Timer stopped. Measuring...')
5-
console.log(`Duration: ${items.getEntries()[0].duration}`)
10+
log('Timer stopped. Measuring...')
11+
log(`Duration: ${chalk.magenta(ms2sec(items.getEntries()[0].duration))}`)
612
performance.clearMarks()
713
}
814

src/clients/http/axios_client.js

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,32 @@
11
import axios from 'axios'
22
import { host, port, httpApi, iters } from '../config'
33
import PerformanceTimer from '../PerformanceTimer'
4-
import { randomName } from '../utils'
4+
import { randomName, log, logConnecting, logIterations } from '../utils'
55

66
export async function runTest() {
7-
console.log(`Axios client connecting to http://${host}:${port}`)
7+
logConnecting('Axios', `http://${host}:${port}`)
8+
logIterations(iters)
89

910
const timer = new PerformanceTimer()
10-
11-
console.log(`Running test with ${iters} iterations...`)
12-
1311
timer.start()
1412

15-
let i = iters
16-
await (async function asyncLoop() {
17-
const postData = { name: randomName() }
18-
const response = await axios.post(httpApi, postData)
19-
const { greeting } = response.data
20-
21-
if (--i === 0) {
22-
console.log(`Last greeting: ${greeting}`)
23-
return
24-
}
25-
26-
await asyncLoop()
27-
})()
13+
try {
14+
let i = iters
15+
await (async function asyncLoop() {
16+
const postData = { name: randomName() }
17+
const response = await axios.post(httpApi, postData)
18+
const { greeting } = response.data
19+
20+
if (--i === 0) {
21+
console.log(`Last greeting: ${greeting}`)
22+
return
23+
}
24+
25+
await asyncLoop()
26+
})()
27+
} catch (err) {
28+
log(`Error: ${err}`)
29+
}
2830

2931
timer.end()
3032
}

src/clients/http/fetch_client.js

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,38 @@
11
import fetch from 'node-fetch'
22
import { host, port, httpApi, iters } from '../config'
33
import PerformanceTimer from '../PerformanceTimer'
4-
import { randomName } from '../utils'
4+
import { randomName, logConnecting, logIterations } from '../utils'
55

6-
export async function runTest() {
7-
console.log(`Fetch client connecting to http://${host}:${port}`)
6+
const requestInit = {
7+
method: 'POST',
8+
headers: { 'Content-Type': 'application/json' }
9+
}
810

9-
const requestInit = {
10-
method: 'POST',
11-
headers: { 'Content-Type': 'application/json' }
12-
}
11+
export async function runTest() {
12+
logConnecting('Fetch', `http://${host}:${port}`)
13+
logIterations(iters)
1314

1415
const timer = new PerformanceTimer()
15-
16-
console.log(`Running test with ${iters} iterations...`)
17-
1816
timer.start()
1917

20-
let i = iters
21-
await (async function asyncLoop() {
22-
const body = JSON.stringify({ name: randomName() })
23-
const response = await fetch(httpApi, {...requestInit, body})
24-
const data = await response.json()
25-
const { greeting } = data
26-
27-
if (--i === 0) {
28-
console.log(`Last greeting: ${greeting}`)
29-
return
30-
}
31-
32-
await asyncLoop()
33-
})()
18+
try {
19+
let i = iters
20+
await (async function asyncLoop() {
21+
const body = JSON.stringify({ name: randomName() })
22+
const response = await fetch(httpApi, {...requestInit, body})
23+
const data = await response.json()
24+
const { greeting } = data
25+
26+
if (--i === 0) {
27+
console.log(`Last greeting: ${greeting}`)
28+
return
29+
}
30+
31+
await asyncLoop()
32+
})()
33+
} catch (err) {
34+
log(`Error: ${err}`)
35+
}
3436

3537
timer.end()
3638
}

src/clients/http/http_client.js

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import http from 'http'
22
import { host, port, iters } from '../config'
33
import PerformanceTimer from '../PerformanceTimer'
4-
import { Deferred, randomName } from '../utils'
4+
import { Deferred, randomName, logConnecting, logIterations } from '../utils'
55

66
async function request(reqOptions, postData) {
77
const deferred = new Deferred() // Promise.defer() => not supported
@@ -31,36 +31,38 @@ async function request(reqOptions, postData) {
3131
return deferred.promise
3232
}
3333

34-
export async function runTest() {
35-
console.log(`Http client connecting to http://${host}:${port}`)
34+
const reqOptions = {
35+
hostname: host,
36+
port: port,
37+
path: '/greeting',
38+
method: 'POST',
39+
headers: { 'Content-Type': 'application/json' }
40+
}
3641

37-
const reqOptions = {
38-
hostname: host,
39-
port: port,
40-
path: '/greeting',
41-
method: 'POST',
42-
headers: { 'Content-Type': 'application/json' }
43-
}
42+
export async function runTest() {
43+
logConnecting('Http', `http://${host}:${port}`)
44+
logIterations(iters)
4445

4546
const timer = new PerformanceTimer()
46-
47-
console.log(`Running test with ${iters} iterations...`)
48-
4947
timer.start()
5048

51-
let i = iters
52-
await (async function asyncLoop() {
53-
const postData = JSON.stringify({ name: randomName() })
54-
const data = await request(reqOptions, postData)
55-
const { greeting } = data
56-
57-
if (--i === 0) {
58-
console.log(`Last greeting: ${greeting}`)
59-
return
60-
}
61-
62-
await asyncLoop()
63-
})()
49+
try {
50+
let i = iters
51+
await (async function asyncLoop() {
52+
const postData = JSON.stringify({ name: randomName() })
53+
const data = await request(reqOptions, postData)
54+
const { greeting } = data
55+
56+
if (--i === 0) {
57+
console.log(`Last greeting: ${greeting}`)
58+
return
59+
}
60+
61+
await asyncLoop()
62+
})()
63+
} catch (err) {
64+
log(`Error: ${err}`)
65+
}
6466

6567
timer.end()
6668
}

src/clients/http/superagent_client.js

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,34 @@
11
import superagent from 'superagent'
22
import { host, port, httpApi, iters } from '../config'
33
import PerformanceTimer from '../PerformanceTimer'
4-
import { randomName } from '../utils'
4+
import { randomName, logConnecting, logIterations } from '../utils'
55

66
export async function runTest() {
7-
console.log(`SuperAgent client connecting to http://${host}:${port}`)
7+
logConnecting('SuperAgent', `http://${host}:${port}`)
8+
logIterations(iters)
89

910
const timer = new PerformanceTimer()
10-
11-
console.log(`Running test with ${iters} iterations...`)
12-
1311
timer.start()
1412

15-
let i = iters
16-
await (async function asyncLoop() {
17-
const postData = { name: randomName() }
18-
const response = await superagent
19-
.post(httpApi)
20-
.send(postData)
21-
const { greeting } = response.body
22-
23-
if (--i === 0) {
24-
console.log(`Last greeting: ${greeting}`)
25-
return
26-
}
27-
28-
await asyncLoop()
29-
})()
13+
try {
14+
let i = iters
15+
await (async function asyncLoop() {
16+
const postData = { name: randomName() }
17+
const response = await superagent
18+
.post(httpApi)
19+
.send(postData)
20+
const { greeting } = response.body
21+
22+
if (--i === 0) {
23+
console.log(`Last greeting: ${greeting}`)
24+
return
25+
}
26+
27+
await asyncLoop()
28+
})()
29+
} catch (err) {
30+
log(`Error: ${err}`)
31+
}
3032

3133
timer.end()
3234
}

src/clients/socketio/socketio_client.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
import io from 'socket.io-client'
44
import { host, port, httpApi, iters } from '../config'
55
import PerformanceTimer from '../PerformanceTimer'
6-
import { Deferred, randomName, createRequester } from '../utils'
6+
import { Deferred, randomName, createRequester, logConnecting, logIterations } from '../utils'
77

88
async function runTest() {
9-
console.log(`SocketIO client connecting to http://${host}:${port}`)
9+
logConnecting('SocketIO', `http://${host}:${port}`)
1010

1111
const timer = new PerformanceTimer()
1212
const connect = new Deferred()
@@ -18,7 +18,7 @@ async function runTest() {
1818
try {
1919
await connect.promise
2020

21-
console.log(`Running test with ${iters} iterations...`)
21+
logIterations(iters)
2222

2323
const requester = createRequester(data => socket.send(data))
2424
socket.on('message', data => requester.incoming.resolve(data))

src/clients/utils.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import chalk from 'chalk'
12
import {
23
uniqueNamesGenerator,
34
adjectives,
@@ -39,4 +40,18 @@ export function createRequester(sender) {
3940
}
4041

4142
return requester
43+
}
44+
45+
export function round(number, fraction=2) {
46+
return number.toFixed(fraction)
47+
}
48+
49+
export const log = console.log
50+
51+
export function logConnecting(client, url) {
52+
log(`${chalk.green(client)} client connecting to ${chalk.yellow(url)}`)
53+
}
54+
55+
export function logIterations(iters) {
56+
log(`Running test with ${chalk.cyan(`${iters} iterations`)}`)
4257
}

0 commit comments

Comments
 (0)