Skip to content

Commit af27aa9

Browse files
committed
Generating random names and greetings
1 parent f99817f commit af27aa9

21 files changed

+137
-223
lines changed

experiments.ipynb

Lines changed: 0 additions & 154 deletions
This file was deleted.

package-lock.json

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
"chalk": "^4.1.0",
2424
"node-fetch": "^2.6.1",
2525
"socket.io-client": "^2.3.0",
26+
"text-encoding": "^0.7.0",
27+
"unique-names-generator": "^4.3.1",
2628
"websocket": "^1.0.32",
2729
"ws": "^7.3.1",
2830
"yargs": "^16.0.3"

src/clients/config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ export const {
66
iters = 10000
77
} = argv
88

9-
export const serverUrl = `http://${host}:${port}/hello`
9+
export const serverUrl = `http://${host}:${port}/greeting`

src/clients/http/axios_client.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import axios from 'axios'
22
import { host, port, serverUrl, iters } from '../config'
33
import PerformanceTimer from '../PerformanceTimer'
4+
import { randomName } from '../utils'
45

56
export async function runTest() {
67
console.log(`Axios client connecting to http://${host}:${port}`)
@@ -14,10 +15,14 @@ export async function runTest() {
1415
// async loop
1516
let i = iters
1617
await (async function asyncLoop() {
17-
const response = await axios.post(serverUrl, { name: 'Fran' })
18-
const { hello } = response.data
19-
20-
if (--i === 0) return
18+
const response = await axios.post(serverUrl, { name: randomName() })
19+
const { greeting } = response.data
20+
21+
if (--i === 0) {
22+
console.log(`Last greeting: ${greeting}`)
23+
return
24+
}
25+
2126
await asyncLoop()
2227
})()
2328

src/clients/http/fetch_client.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import fetch from 'node-fetch'
22
import { host, port, serverUrl, iters } from '../config'
33
import PerformanceTimer from '../PerformanceTimer'
4+
import { randomName } from '../utils'
45

56
export async function runTest() {
67
console.log(`Fetch client connecting to http://${host}:${port}`)
78

89
const requestInit = {
910
method: 'POST',
1011
headers: { 'Content-Type': 'application/json' },
11-
body: JSON.stringify({ name: 'Fran' })
12+
body: JSON.stringify({ name: randomName() })
1213
}
1314

1415
const timer = new PerformanceTimer()
@@ -22,9 +23,13 @@ export async function runTest() {
2223
await (async function asyncLoop() {
2324
const response = await fetch(serverUrl, requestInit)
2425
const data = await response.json()
25-
const { hello } = data
26-
27-
if (--i === 0) return
26+
const { greeting } = data
27+
28+
if (--i === 0) {
29+
console.log(`Last greeting: ${greeting}`)
30+
return
31+
}
32+
2833
await asyncLoop()
2934
})()
3035

src/clients/http/http_client.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import http from 'http'
22
import { host, port, serverUrl, iters } from '../config'
33
import PerformanceTimer from '../PerformanceTimer'
4+
import { randomName } from '../utils'
45

56
// Would be great to have Promise.defer :_(
67
class Deferred {
@@ -48,12 +49,12 @@ export async function runTest() {
4849
const reqOptions = {
4950
hostname: host,
5051
port: port,
51-
path: '/hello',
52+
path: '/greeting',
5253
method: 'POST',
5354
headers: { 'Content-Type': 'application/json' }
5455
}
5556

56-
const postData = JSON.stringify({ name: 'Fran' })
57+
const postData = JSON.stringify({ name: randomName() })
5758

5859
const timer = new PerformanceTimer()
5960

@@ -64,9 +65,13 @@ export async function runTest() {
6465
let i = iters
6566
await (async function asyncLoop() {
6667
const data = await request(reqOptions, postData)
67-
const { hello } = data
68+
const { greeting } = data
69+
70+
if (--i === 0) {
71+
console.log(`Last greeting: ${greeting}`)
72+
return
73+
}
6874

69-
if (--i === 0) return
7075
await asyncLoop()
7176
})()
7277

src/clients/utils.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import {
2+
uniqueNamesGenerator,
3+
adjectives,
4+
colors,
5+
animals
6+
} from 'unique-names-generator'
7+
8+
const rndNamesConfig = {
9+
dictionaries: [adjectives, colors, animals],
10+
separator: ' ',
11+
style: 'capital',
12+
}
13+
14+
export function randomName() {
15+
return uniqueNamesGenerator(rndNamesConfig)
16+
}

src/clients/ws/socketio_client.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,32 @@
22

33
const { PerformanceObserver, performance } = require('perf_hooks')
44
const io = require('socket.io-client')
5+
import { randomName } from '../utils'
56

67
const host = process.env.HOST || '0.0.0.0'
78
const port = process.env.PORT || 5000
89
const server = process.env.SERVER || 'unknown'
9-
const wsApi = `ws://${host}:${port}/hello`
10+
const wsApi = `ws://${host}:${port}/greeting`
1011

1112
let iters = 10000
1213

1314
async function runTest() {
14-
console.log(`SocketIO client <===> ${server} server on ws://${host}:${port}/hello`)
15+
console.log(`SocketIO client <===> ${server} server on ws://${host}:${port}/greeting`)
1516

1617
const socket = io(wsApi, { transports: ['websocket'] })
1718

1819
socket.on('connect', () => {
1920
console.log(`Running test with ${iters} iterations...`)
2021

21-
function requestHello() {
22-
socket.send({ name: 'Fran' })
22+
function requestGreeting() {
23+
socket.send({ name: randomName() })
2324
}
2425

2526
socket.on('message', data => {
26-
const { hello } = data
27+
const { greeting } = data
2728

2829
if (--iters > 0) {
29-
requestHello()
30+
requestGreeting()
3031
} else {
3132
performance.mark('END')
3233
performance.measure('START to END', 'START', 'END')
@@ -37,7 +38,7 @@ async function runTest() {
3738

3839
performance.mark('START')
3940

40-
requestHello()
41+
requestGreeting()
4142
})
4243
}
4344

0 commit comments

Comments
 (0)