Skip to content

Commit 290b778

Browse files
committed
Added SuperAgent http client
1 parent af27aa9 commit 290b778

File tree

5 files changed

+207
-20
lines changed

5 files changed

+207
-20
lines changed

package-lock.json

Lines changed: 149 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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"scripts": {
66
"http-client": "babel-node src/clients/http/http_client.js",
77
"axios-client": "babel-node src/clients/http/axios_client.js",
8-
"fetch-client": "babel-node src/clients/http/fetch_client.js"
8+
"fetch-client": "babel-node src/clients/http/fetch_client.js",
9+
"superagent-client": "babel-node src/clients/http/superagent_client.js"
910
},
1011
"repository": {
1112
"type": "git",
@@ -23,6 +24,7 @@
2324
"chalk": "^4.1.0",
2425
"node-fetch": "^2.6.1",
2526
"socket.io-client": "^2.3.0",
27+
"superagent": "^6.1.0",
2628
"text-encoding": "^0.7.0",
2729
"unique-names-generator": "^4.3.1",
2830
"websocket": "^1.0.32",

src/clients/http/http_client.js

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +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'
5-
6-
// Would be great to have Promise.defer :_(
7-
class Deferred {
8-
resolve = null
9-
reject = null
10-
constructor() {
11-
this.promise = new Promise((resolve, reject) => {
12-
this.resolve = resolve
13-
this.reject = reject
14-
})
15-
}
16-
}
4+
import { Deferred, randomName } from '../utils'
175

186
async function request(reqOptions, postData) {
197
const deferred = new Deferred() // Promise.defer() => not supported

src/clients/http/superagent_client.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import superagent from 'superagent'
2+
import { host, port, serverUrl, iters } from '../config'
3+
import PerformanceTimer from '../PerformanceTimer'
4+
import { randomName } from '../utils'
5+
6+
export async function runTest() {
7+
console.log(`SuperAgent client connecting to http://${host}:${port}`)
8+
9+
const timer = new PerformanceTimer()
10+
11+
console.log(`Running test with ${iters} iterations...`)
12+
13+
timer.start()
14+
15+
// async loop
16+
let i = iters
17+
await (async function asyncLoop() {
18+
const response = await superagent
19+
.post(serverUrl)
20+
.send({ name: randomName() })
21+
const { greeting } = response.body
22+
23+
if (--i === 0) {
24+
console.log(`Last greeting: ${greeting}`)
25+
return
26+
}
27+
28+
await asyncLoop()
29+
})()
30+
31+
timer.end()
32+
}
33+
34+
if (require.main === module) {
35+
runTest()
36+
}

src/clients/utils.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import {
2-
uniqueNamesGenerator,
3-
adjectives,
4-
colors,
5-
animals
1+
import {
2+
uniqueNamesGenerator,
3+
adjectives,
4+
colors,
5+
animals
66
} from 'unique-names-generator'
77

88
const rndNamesConfig = {
@@ -12,5 +12,17 @@ const rndNamesConfig = {
1212
}
1313

1414
export function randomName() {
15-
return uniqueNamesGenerator(rndNamesConfig)
15+
return uniqueNamesGenerator(rndNamesConfig)
16+
}
17+
18+
// Would be great to have Promise.defer :_(
19+
export class Deferred {
20+
resolve = null
21+
reject = null
22+
constructor() {
23+
this.promise = new Promise((resolve, reject) => {
24+
this.resolve = resolve
25+
this.reject = reject
26+
})
27+
}
1628
}

0 commit comments

Comments
 (0)