Skip to content

Commit 2c1be7a

Browse files
committed
Finished http client
1 parent bff0560 commit 2c1be7a

File tree

2 files changed

+18
-19
lines changed

2 files changed

+18
-19
lines changed

src/clients/http/fetch_client.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ import fetch from 'node-fetch'
22
import { host, port, serverUrl, iters } from '../config'
33
import PerformanceTimer from '../PerformanceTimer'
44

5-
const requestInit = {
6-
method: 'POST',
7-
headers: { 'Content-Type': 'application/json' },
8-
body: JSON.stringify({ name: 'Fran' })
9-
}
10-
115
export async function runTest() {
126
console.log(`Fetch client connecting to http://${host}:${port}`)
137

8+
const requestInit = {
9+
method: 'POST',
10+
headers: { 'Content-Type': 'application/json' },
11+
body: JSON.stringify({ name: 'Fran' })
12+
}
13+
1414
const timer = new PerformanceTimer()
1515

1616
console.log(`Running test with ${iters} iterations...`)

src/clients/http/http_client.js

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,6 @@ import http from 'http'
22
import { host, port, serverUrl, iters } from '../config'
33
import PerformanceTimer from '../PerformanceTimer'
44

5-
const reqOptions = {
6-
hostname: host,
7-
port: port,
8-
path: '/hello',
9-
method: 'POST',
10-
headers: { 'Content-Type': 'application/json' }
11-
}
12-
13-
const postData = JSON.stringify({ name: 'Fran' })
14-
155
// Would be great to have Promise.defer :_(
166
class Deferred {
177
resolve = null
@@ -24,7 +14,7 @@ class Deferred {
2414
}
2515
}
2616

27-
async function request(reqOptions) {
17+
async function request(reqOptions, postData) {
2818
const deferred = new Deferred() // Promise.defer() => not supported
2919

3020
const req = http.request(reqOptions, resp => {
@@ -55,6 +45,16 @@ async function request(reqOptions) {
5545
export async function runTest() {
5646
console.log(`Http client connecting to http://${host}:${port}`)
5747

48+
const reqOptions = {
49+
hostname: host,
50+
port: port,
51+
path: '/hello',
52+
method: 'POST',
53+
headers: { 'Content-Type': 'application/json' }
54+
}
55+
56+
const postData = JSON.stringify({ name: 'Fran' })
57+
5858
const timer = new PerformanceTimer()
5959

6060
console.log(`Running test with ${iters} iterations...`)
@@ -63,9 +63,8 @@ export async function runTest() {
6363

6464
let i = iters
6565
await (async function asyncLoop() {
66-
const data = await request(serverUrl, reqOptions)
66+
const data = await request(reqOptions, postData)
6767
const { hello } = data
68-
console.log(hello)
6968

7069
if (--i === 0) return
7170
await asyncLoop()

0 commit comments

Comments
 (0)