forked from JCMais/node-libcurl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
181 lines (162 loc) · 3.76 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
const http = require('http')
const { curly, Curl, Easy } = require('node-libcurl')
const axios = require('axios')
const superagent = require('superagent')
const request = require('request')
const fetch = require('node-fetch')
const HOST = process.env.HOST || '127.0.0.1'
const PORT = process.env.PORT || '8080'
const URL = `http://${HOST}:${PORT}`
axios.defaults.baseURL = URL
const Benchmark = require('benchmark')
const suite = new Benchmark.Suite()
suite.add('node.js http.request - GET', {
defer: true,
fn: (defer) => {
http
.request({ host: HOST, port: PORT, path: '/' }, (res) => {
res.setEncoding('utf8')
let rawData = ''
res.on('data', (chunk) => {
rawData += chunk
})
res.on('end', () => {
defer.resolve()
})
})
.end()
},
})
suite.add('axios - GET', {
defer: true,
fn: (defer) => {
axios.get('/').then(() => defer.resolve())
},
})
suite.add('superagent - GET', {
defer: true,
fn: (defer) => {
superagent.get(URL).end(() => {
defer.resolve()
})
},
})
suite.add('request - GET', {
defer: true,
fn: (defer) => {
request(URL, () => defer.resolve())
},
})
suite.add('fetch - GET', {
defer: true,
fn: (defer) => {
fetch(URL)
.then((res) => res.text())
.then((_body) => defer.resolve())
},
})
suite.add('node-libcurl curly - GET', {
defer: true,
fn: (defer) => {
curly.get(URL).then((_result) => defer.resolve())
},
})
suite.add('node-libcurl Curl - GET', {
defer: true,
fn: (defer) => {
const curl = new Curl()
curl.setOpt('URL', URL)
const onEnd = () => {
curl.close()
defer.resolve()
}
const onError = (error) => {
curl.close()
throw error
}
curl.on('end', onEnd)
curl.on('error', onError)
curl.perform()
},
})
let curlReuse = null
suite.add('node-libcurl Curl - reusing instance - GET', {
defer: true,
setup: () => {
curlReuse = new Curl()
curlReuse.setOpt('URL', URL)
},
fn: (defer) => {
const onEnd = () => {
curlReuse.off('end', onEnd)
curlReuse.off('error', onError)
defer.resolve()
}
const onError = (error) => {
curlReuse.off('end', onEnd)
curlReuse.off('error', onError)
curlReuse.close()
throw error
}
curlReuse.on('end', onEnd)
curlReuse.on('error', onError)
curlReuse.perform()
},
teardown: () => {
curlReuse.close()
},
})
suite.add('node-libcurl Easy - GET', {
fn: () => {
const easy = new Easy()
let headers = ''
let body = ''
easy.setOpt('URL', URL)
easy.setOpt('HEADERFUNCTION', (data, size, nmemb) => {
headers += data.toString('utf8')
return size * nmemb
})
easy.setOpt('WRITEFUNCTION', (data, size, nmemb) => {
body += data.toString('utf8')
return size * nmemb
})
easy.perform()
easy.close()
},
})
let easyReuse = null
suite.add('node-libcurl Easy - reusing instance - GET', {
defer: true,
setup: () => {
easyReuse = new Easy()
easyReuse.setOpt('URL', URL)
},
fn: (defer) => {
let headers = ''
let body = ''
easyReuse.setOpt('URL', URL)
easyReuse.setOpt('HEADERFUNCTION', (data, size, nmemb) => {
headers += data.toString('utf8')
return size * nmemb
})
easyReuse.setOpt('WRITEFUNCTION', (data, size, nmemb) => {
body += data.toString('utf8')
return size * nmemb
})
easyReuse.perform()
defer.resolve()
},
teardown: () => {
easyReuse.close()
},
})
suite.on('complete', function (defer) {
console.log('Fastest is ' + this.filter('fastest').map('name'))
})
suite.on('cycle', function (event) {
console.log(String(event.target))
})
suite.on('error', function (error) {
console.error(error)
})
suite.run({ async: true })