Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// Something horrible happens and this maxes out at ~800 req/sec.
var TCP = process.binding('tcp_wrap').TCP;
var SlowBuffer = require('buffer').SlowBuffer;
var util = require('util');
var cntr = 0;
var err;
var headers = 'HTTP/1.1 200 OK\r\n' +
'Server: TCPTest\r\n' +
'Content-Type: text/plain; charset=latin-1\r\n' +
'Connection: keep-alive\r\n' +
'Content-Length: 12\r\n\r\n';
var body = 'hello world\n';
headers = new SlowBuffer(headers.length).fill(headers);
body = new SlowBuffer(body.length).fill(body);
function fail(err, syscall) {
throw util._errnoException(err, syscall);
}
var server = new TCP();
err = server.bind('127.0.0.1', 8012);
if (err)
fail(err, 'bind');
err = server.listen(511);
if (err)
fail(err, 'listen');
function afterWrite(err, handle, req) {
if (err)
fail(err, 'write');
}
// uncomment the following to see the speed increase
//var full_msg = Buffer.concat([headers, body]);
function onRead(nread, buffer) {
if (nread < 0)
var writeReq = { oncomplete: afterWrite }; return this.close();
// comment these out when using full_msg
this.writeBuffer(writeReq, headers);
this.writeBuffer(writeReq, body);
// uncomment this for perf difference
//this.writeBuffer(writeReq, full_msg);
}
server.onconnection = function onConnection(err, client) {
if (err)
fail(err, 'connect');
client.setKeepAlive(true, 10000);
client.onread = onRead;
client.readStart();
};
// Using the built in EventEmitter and Streams
// Max out at 20k req/sec
var net = require('net');
var headers = 'HTTP/1.1 200 OK\r\n' +
'Server: TCPTest\r\n' +
'Content-Type: text/plain; charset=latin-1\r\n' +
'Connection: keep-alive\r\n' +
'Content-Length: 12\r\n\r\n' +
'hello world!';
var b = new require('buffer').SlowBuffer(headers.length).fill(headers);
var server = net.createServer(function conn(c) {
c.on('error', gotError);
c.setKeepAlive(true, 10000);
c.end(b);
});
function gotError(err) {
if (err.message !== 'read ECONNRESET')
throw err;
}
server.listen(8000);
// Binding bare bones
// Max out at 125k req/sec
var TCP = process.binding('tcp_wrap').TCP;
var util = require('util');
var cntr = 0;
var err;
var headers = 'HTTP/1.1 200 OK\r\n' +
'Server: TCPTest\r\n' +
'Content-Type: text/plain; charset=latin-1\r\n' +
'Connection: keep-alive\r\n' +
'Content-Length: 12\r\n\r\n' +
'hello world!';
var data = require('buffer').SlowBuffer(headers.length).fill(headers);
function fail(err, syscall) {
throw util._errnoException(err, syscall);
}
var server = new TCP();
err = server.bind('127.0.0.1', 8012);
if (err)
fail(err, 'bind');
err = server.listen(511);
if (err)
fail(err, 'listen');
function afterWrite(err, handle, req) {
if (err)
fail(err, 'write');
}
function onRead(nread, buffer) {
if (nread < 0)
return this.close();
var writeReq = { oncomplete: afterWrite };
this.writeBuffer(writeReq, data);
}
server.onconnection = function onConnection(err, client) {
if (err)
fail(err, 'connect');
client.setKeepAlive(true, 10000);
client.onread = onRead;
client.readStart();
};
First 336 requests ordered by really-crapy-multi-write.js
onRead: 1
onRead: 2
onRead: 3
onRead: 4
onRead: 5
onRead: 6
onRead: 7
onRead: 8
onRead: 9
onRead: 10
onRead: 11
onRead: 12
onRead: 13
onRead: 14
onRead: 15
onRead: 16
afterWrite: 1
afterWrite: 1
afterWrite: 2
afterWrite: 2
afterWrite: 3
afterWrite: 3
afterWrite: 4
afterWrite: 4
afterWrite: 5
afterWrite: 5
afterWrite: 6
afterWrite: 6
afterWrite: 7
afterWrite: 7
afterWrite: 8
afterWrite: 8
afterWrite: 9
afterWrite: 9
afterWrite: 10
afterWrite: 10
afterWrite: 11
afterWrite: 11
afterWrite: 12
afterWrite: 12
afterWrite: 13
afterWrite: 13
afterWrite: 14
afterWrite: 14
afterWrite: 15
afterWrite: 15
afterWrite: 16
afterWrite: 16
onRead: 17
onRead: 18
onRead: 19
onRead: 20
onRead: 21
onRead: 22
onRead: 23
onRead: 24
onRead: 25
onRead: 26
onRead: 27
onRead: 28
onRead: 29
onRead: 30
onRead: 31
onRead: 32
onRead: 33
onRead: 34
onRead: 35
onRead: 36
onRead: 37
onRead: 38
onRead: 39
afterWrite: 17
afterWrite: 17
afterWrite: 18
afterWrite: 18
afterWrite: 19
afterWrite: 19
afterWrite: 20
afterWrite: 20
afterWrite: 21
afterWrite: 21
afterWrite: 22
afterWrite: 22
afterWrite: 23
afterWrite: 23
afterWrite: 24
afterWrite: 24
afterWrite: 25
afterWrite: 25
afterWrite: 26
afterWrite: 26
afterWrite: 27
afterWrite: 27
afterWrite: 28
afterWrite: 28
afterWrite: 29
afterWrite: 29
afterWrite: 30
afterWrite: 30
afterWrite: 31
afterWrite: 31
afterWrite: 32
afterWrite: 32
afterWrite: 33
afterWrite: 33
afterWrite: 34
afterWrite: 34
afterWrite: 35
afterWrite: 35
afterWrite: 36
afterWrite: 36
afterWrite: 37
afterWrite: 37
afterWrite: 38
afterWrite: 38
afterWrite: 39
afterWrite: 39
onRead: 40
onRead: 41
onRead: 42
onRead: 43
onRead: 44
onRead: 45
onRead: 46
onRead: 47
onRead: 48
onRead: 49
onRead: 50
onRead: 51
onRead: 52
onRead: 53
onRead: 54
onRead: 55
afterWrite: 40
afterWrite: 40
afterWrite: 41
afterWrite: 41
afterWrite: 42
afterWrite: 42
afterWrite: 43
afterWrite: 43
afterWrite: 44
afterWrite: 44
afterWrite: 45
afterWrite: 45
afterWrite: 46
afterWrite: 46
afterWrite: 47
afterWrite: 47
afterWrite: 48
afterWrite: 48
afterWrite: 49
afterWrite: 49
afterWrite: 50
afterWrite: 50
afterWrite: 51
afterWrite: 51
afterWrite: 52
afterWrite: 52
afterWrite: 53
afterWrite: 53
afterWrite: 54
afterWrite: 54
afterWrite: 55
afterWrite: 55
onRead: 56
onRead: 57
onRead: 58
onRead: 59
onRead: 60
onRead: 61
onRead: 62
onRead: 63
onRead: 64
afterWrite: 56
afterWrite: 56
afterWrite: 57
afterWrite: 57
afterWrite: 58
afterWrite: 58
afterWrite: 59
afterWrite: 59
afterWrite: 60
afterWrite: 60
afterWrite: 61
afterWrite: 61
afterWrite: 62
afterWrite: 62
afterWrite: 63
afterWrite: 63
afterWrite: 64
afterWrite: 64
onRead: 65
onRead: 66
afterWrite: 65
afterWrite: 65
afterWrite: 66
afterWrite: 66
onRead: 67
onRead: 68
onRead: 69
onRead: 70
onRead: 71
onRead: 72
onRead: 73
onRead: 74
onRead: 75
onRead: 76
onRead: 77
afterWrite: 67
afterWrite: 67
afterWrite: 68
afterWrite: 68
afterWrite: 69
afterWrite: 69
afterWrite: 70
afterWrite: 70
afterWrite: 71
afterWrite: 71
afterWrite: 72
afterWrite: 72
afterWrite: 73
afterWrite: 73
afterWrite: 74
afterWrite: 74
afterWrite: 75
afterWrite: 75
afterWrite: 76
afterWrite: 76
afterWrite: 77
afterWrite: 77
onRead: 78
onRead: 79
onRead: 80
afterWrite: 78
afterWrite: 78
afterWrite: 79
afterWrite: 79
afterWrite: 80
afterWrite: 80
onRead: 81
onRead: 82
onRead: 83
onRead: 84
onRead: 85
onRead: 86
onRead: 87
afterWrite: 81
afterWrite: 81
afterWrite: 82
afterWrite: 82
afterWrite: 83
afterWrite: 83
afterWrite: 84
afterWrite: 84
afterWrite: 85
afterWrite: 85
afterWrite: 86
afterWrite: 86
afterWrite: 87
afterWrite: 87
onRead: 88
onRead: 89
afterWrite: 88
afterWrite: 88
afterWrite: 89
afterWrite: 89
onRead: 90
onRead: 91
onRead: 92
onRead: 93
onRead: 94
onRead: 95
onRead: 96
afterWrite: 90
afterWrite: 90
afterWrite: 91
afterWrite: 91
afterWrite: 92
afterWrite: 92
afterWrite: 93
afterWrite: 93
afterWrite: 94
afterWrite: 94
afterWrite: 95
afterWrite: 95
afterWrite: 96
afterWrite: 96
onRead: 97
onRead: 98
onRead: 99
onRead: 100
onRead: 101
onRead: 102
onRead: 103
onRead: 104
afterWrite: 97
afterWrite: 97
afterWrite: 98
afterWrite: 98
afterWrite: 99
afterWrite: 99
afterWrite: 100
afterWrite: 100
afterWrite: 101
afterWrite: 101
afterWrite: 102
afterWrite: 102
afterWrite: 103
afterWrite: 103
afterWrite: 104
afterWrite: 104
onRead: 105
onRead: 106
onRead: 107
onRead: 108
onRead: 109
afterWrite: 105
afterWrite: 105
afterWrite: 106
afterWrite: 106
afterWrite: 107
afterWrite: 107
afterWrite: 108
afterWrite: 108
afterWrite: 109
afterWrite: 109
onRead: 110
onRead: 111
onRead: 112
onRead: 113
onRead: 114
afterWrite: 110
afterWrite: 110
afterWrite: 111
afterWrite: 111
afterWrite: 112
afterWrite: 112
afterWrite: 113
afterWrite: 113
afterWrite: 114
afterWrite: 114
onRead: 115
onRead: 116
onRead: 117
onRead: 118
onRead: 119
afterWrite: 115
afterWrite: 115
afterWrite: 116
afterWrite: 116
afterWrite: 117
afterWrite: 117
afterWrite: 118
afterWrite: 118
afterWrite: 119
afterWrite: 119
onRead: 120
onRead: 121
onRead: 122
afterWrite: 120
afterWrite: 120
afterWrite: 121
afterWrite: 121
afterWrite: 122
afterWrite: 122
onRead: 123
onRead: 124
onRead: 125
onRead: 126
onRead: 127
onRead: 128
afterWrite: 123
afterWrite: 123
afterWrite: 124
afterWrite: 124
afterWrite: 125
afterWrite: 125
afterWrite: 126
afterWrite: 126
afterWrite: 127
afterWrite: 127
afterWrite: 128
afterWrite: 128
onRead: 129
onRead: 130
onRead: 131
onRead: 132
onRead: 133
onRead: 134
onRead: 135
onRead: 136
onRead: 137
afterWrite: 129
afterWrite: 129
afterWrite: 130
afterWrite: 130
afterWrite: 131
afterWrite: 131
afterWrite: 132
afterWrite: 132
afterWrite: 133
afterWrite: 133
afterWrite: 134
afterWrite: 134
afterWrite: 135
afterWrite: 135
afterWrite: 136
afterWrite: 136
afterWrite: 137
afterWrite: 137
onRead: 138
onRead: 139
onRead: 140
onRead: 141
afterWrite: 138
afterWrite: 138
afterWrite: 139
afterWrite: 139
afterWrite: 140
afterWrite: 140
afterWrite: 141
afterWrite: 141
onRead: 142
onRead: 143
onRead: 144
onRead: 145
onRead: 146
onRead: 147
afterWrite: 142
afterWrite: 142
afterWrite: 143
afterWrite: 143
afterWrite: 144
afterWrite: 144
afterWrite: 145
afterWrite: 145
afterWrite: 146
afterWrite: 146
afterWrite: 147
afterWrite: 147
onRead: 148
onRead: 149
onRead: 150
onRead: 151
afterWrite: 148
afterWrite: 148
afterWrite: 149
afterWrite: 149
afterWrite: 150
afterWrite: 150
afterWrite: 151
afterWrite: 151
onRead: 152
afterWrite: 152
afterWrite: 152
onRead: 153
onRead: 154
onRead: 155
onRead: 156
onRead: 157
onRead: 158
onRead: 159
onRead: 160
afterWrite: 153
afterWrite: 153
afterWrite: 154
afterWrite: 154
afterWrite: 155
afterWrite: 155
afterWrite: 156
afterWrite: 156
afterWrite: 157
afterWrite: 157
afterWrite: 158
afterWrite: 158
afterWrite: 159
afterWrite: 159
afterWrite: 160
afterWrite: 160
onRead: 161
onRead: 162
onRead: 163
onRead: 164
onRead: 165
onRead: 166
onRead: 167
onRead: 168
onRead: 169
onRead: 170
afterWrite: 161
afterWrite: 161
afterWrite: 162
afterWrite: 162
afterWrite: 163
afterWrite: 163
afterWrite: 164
afterWrite: 164
afterWrite: 165
afterWrite: 165
afterWrite: 166
afterWrite: 166
afterWrite: 167
afterWrite: 167
afterWrite: 168
afterWrite: 168
afterWrite: 169
afterWrite: 169
afterWrite: 170
afterWrite: 170
onRead: 171
onRead: 172
onRead: 173
afterWrite: 171
afterWrite: 171
afterWrite: 172
afterWrite: 172
afterWrite: 173
afterWrite: 173
onRead: 174
onRead: 175
onRead: 176
onRead: 177
afterWrite: 174
afterWrite: 174
afterWrite: 175
afterWrite: 175
afterWrite: 176
afterWrite: 176
afterWrite: 177
afterWrite: 177
onRead: 178
onRead: 179
onRead: 180
onRead: 181
onRead: 182
onRead: 183
afterWrite: 178
afterWrite: 178
afterWrite: 179
afterWrite: 179
afterWrite: 180
afterWrite: 180
afterWrite: 181
afterWrite: 181
afterWrite: 182
afterWrite: 182
afterWrite: 183
afterWrite: 183
onRead: 184
onRead: 185
onRead: 186
afterWrite: 184
afterWrite: 184
afterWrite: 185
afterWrite: 185
afterWrite: 186
afterWrite: 186
onRead: 187
onRead: 188
onRead: 189
onRead: 190
onRead: 191
onRead: 192
afterWrite: 187
afterWrite: 187
afterWrite: 188
afterWrite: 188
afterWrite: 189
afterWrite: 189
afterWrite: 190
afterWrite: 190
afterWrite: 191
afterWrite: 191
afterWrite: 192
afterWrite: 192
onRead: 193
onRead: 194
onRead: 195
onRead: 196
onRead: 197
onRead: 198
onRead: 199
onRead: 200
afterWrite: 193
afterWrite: 193
afterWrite: 194
afterWrite: 194
afterWrite: 195
afterWrite: 195
afterWrite: 196
afterWrite: 196
afterWrite: 197
afterWrite: 197
afterWrite: 198
afterWrite: 198
afterWrite: 199
afterWrite: 199
afterWrite: 200
afterWrite: 200
onRead: 201
onRead: 202
onRead: 203
onRead: 204
onRead: 205
afterWrite: 201
afterWrite: 201
afterWrite: 202
afterWrite: 202
afterWrite: 203
afterWrite: 203
afterWrite: 204
afterWrite: 204
afterWrite: 205
afterWrite: 205
onRead: 206
onRead: 207
onRead: 208
afterWrite: 206
afterWrite: 206
afterWrite: 207
afterWrite: 207
afterWrite: 208
afterWrite: 208
onRead: 209
onRead: 210
onRead: 211
onRead: 212
onRead: 213
onRead: 214
onRead: 215
afterWrite: 209
afterWrite: 209
afterWrite: 210
afterWrite: 210
afterWrite: 211
afterWrite: 211
afterWrite: 212
afterWrite: 212
afterWrite: 213
afterWrite: 213
afterWrite: 214
afterWrite: 214
afterWrite: 215
afterWrite: 215
onRead: 216
afterWrite: 216
afterWrite: 216
onRead: 217
onRead: 218
onRead: 219
onRead: 220
onRead: 221
onRead: 222
onRead: 223
onRead: 224
afterWrite: 217
afterWrite: 217
afterWrite: 218
afterWrite: 218
afterWrite: 219
afterWrite: 219
afterWrite: 220
afterWrite: 220
afterWrite: 221
afterWrite: 221
afterWrite: 222
afterWrite: 222
afterWrite: 223
afterWrite: 223
afterWrite: 224
afterWrite: 224
onRead: 225
onRead: 226
onRead: 227
onRead: 228
onRead: 229
onRead: 230
onRead: 231
onRead: 232
onRead: 233
onRead: 234
afterWrite: 225
afterWrite: 225
afterWrite: 226
afterWrite: 226
afterWrite: 227
afterWrite: 227
afterWrite: 228
afterWrite: 228
afterWrite: 229
afterWrite: 229
afterWrite: 230
afterWrite: 230
afterWrite: 231
afterWrite: 231
afterWrite: 232
afterWrite: 232
afterWrite: 233
afterWrite: 233
afterWrite: 234
afterWrite: 234
onRead: 235
onRead: 236
onRead: 237
afterWrite: 235
afterWrite: 235
afterWrite: 236
afterWrite: 236
afterWrite: 237
afterWrite: 237
onRead: 238
onRead: 239
onRead: 240
afterWrite: 238
afterWrite: 238
afterWrite: 239
afterWrite: 239
afterWrite: 240
afterWrite: 240
onRead: 241
onRead: 242
onRead: 243
onRead: 244
onRead: 245
onRead: 246
onRead: 247
afterWrite: 241
afterWrite: 241
afterWrite: 242
afterWrite: 242
afterWrite: 243
afterWrite: 243
afterWrite: 244
afterWrite: 244
afterWrite: 245
afterWrite: 245
afterWrite: 246
afterWrite: 246
afterWrite: 247
afterWrite: 247
onRead: 248
afterWrite: 248
afterWrite: 248
onRead: 249
onRead: 250
onRead: 251
onRead: 252
onRead: 253
onRead: 254
onRead: 255
onRead: 256
afterWrite: 249
afterWrite: 249
afterWrite: 250
afterWrite: 250
afterWrite: 251
afterWrite: 251
afterWrite: 252
afterWrite: 252
afterWrite: 253
afterWrite: 253
afterWrite: 254
afterWrite: 254
afterWrite: 255
afterWrite: 255
afterWrite: 256
afterWrite: 256
onRead: 257
onRead: 258
onRead: 259
onRead: 260
onRead: 261
onRead: 262
onRead: 263
onRead: 264
onRead: 265
afterWrite: 257
afterWrite: 257
afterWrite: 258
afterWrite: 258
afterWrite: 259
afterWrite: 259
afterWrite: 260
afterWrite: 260
afterWrite: 261
afterWrite: 261
afterWrite: 262
afterWrite: 262
afterWrite: 263
afterWrite: 263
afterWrite: 264
afterWrite: 264
afterWrite: 265
afterWrite: 265
onRead: 266
onRead: 267
onRead: 268
onRead: 269
afterWrite: 266
afterWrite: 266
afterWrite: 267
afterWrite: 267
afterWrite: 268
afterWrite: 268
afterWrite: 269
afterWrite: 269
onRead: 270
onRead: 271
onRead: 272
onRead: 273
onRead: 274
onRead: 275
afterWrite: 270
afterWrite: 270
afterWrite: 271
afterWrite: 271
afterWrite: 272
afterWrite: 272
afterWrite: 273
afterWrite: 273
afterWrite: 274
afterWrite: 274
afterWrite: 275
afterWrite: 275
onRead: 276
onRead: 277
onRead: 278
onRead: 279
afterWrite: 276
afterWrite: 276
afterWrite: 277
afterWrite: 277
afterWrite: 278
afterWrite: 278
afterWrite: 279
afterWrite: 279
onRead: 280
afterWrite: 280
afterWrite: 280
onRead: 281
onRead: 282
onRead: 283
onRead: 284
onRead: 285
onRead: 286
onRead: 287
onRead: 288
afterWrite: 281
afterWrite: 281
afterWrite: 282
afterWrite: 282
afterWrite: 283
afterWrite: 283
afterWrite: 284
afterWrite: 284
afterWrite: 285
afterWrite: 285
afterWrite: 286
afterWrite: 286
afterWrite: 287
afterWrite: 287
afterWrite: 288
afterWrite: 288
onRead: 289
onRead: 290
onRead: 291
onRead: 292
onRead: 293
onRead: 294
onRead: 295
onRead: 296
onRead: 297
onRead: 298
onRead: 299
afterWrite: 289
afterWrite: 289
afterWrite: 290
afterWrite: 290
afterWrite: 291
afterWrite: 291
afterWrite: 292
afterWrite: 292
afterWrite: 293
afterWrite: 293
afterWrite: 294
afterWrite: 294
afterWrite: 295
afterWrite: 295
afterWrite: 296
afterWrite: 296
afterWrite: 297
afterWrite: 297
afterWrite: 298
afterWrite: 298
afterWrite: 299
afterWrite: 299
onRead: 300
onRead: 301
afterWrite: 300
afterWrite: 300
afterWrite: 301
afterWrite: 301
onRead: 302
onRead: 303
onRead: 304
afterWrite: 302
afterWrite: 302
afterWrite: 303
afterWrite: 303
afterWrite: 304
afterWrite: 304
onRead: 305
onRead: 306
onRead: 307
onRead: 308
onRead: 309
onRead: 310
onRead: 311
afterWrite: 305
afterWrite: 305
afterWrite: 306
afterWrite: 306
afterWrite: 307
afterWrite: 307
afterWrite: 308
afterWrite: 308
afterWrite: 309
afterWrite: 309
afterWrite: 310
afterWrite: 310
afterWrite: 311
afterWrite: 311
onRead: 312
afterWrite: 312
afterWrite: 312
onRead: 313
onRead: 314
onRead: 315
onRead: 316
onRead: 317
onRead: 318
onRead: 319
onRead: 320
afterWrite: 313
afterWrite: 313
afterWrite: 314
afterWrite: 314
afterWrite: 315
afterWrite: 315
afterWrite: 316
afterWrite: 316
afterWrite: 317
afterWrite: 317
afterWrite: 318
afterWrite: 318
afterWrite: 319
afterWrite: 319
afterWrite: 320
afterWrite: 320
onRead: 321
onRead: 322
onRead: 323
onRead: 324
onRead: 325
onRead: 326
onRead: 327
onRead: 328
onRead: 329
onRead: 330
onRead: 331
afterWrite: 321
afterWrite: 321
afterWrite: 322
afterWrite: 322
afterWrite: 323
afterWrite: 323
afterWrite: 324
afterWrite: 324
afterWrite: 325
afterWrite: 325
afterWrite: 326
afterWrite: 326
afterWrite: 327
afterWrite: 327
afterWrite: 328
afterWrite: 328
afterWrite: 329
afterWrite: 329
afterWrite: 330
afterWrite: 330
afterWrite: 331
afterWrite: 331
onRead: 332
onRead: 333
afterWrite: 332
afterWrite: 332
afterWrite: 333
afterWrite: 333
onRead: 334
onRead: 335
onRead: 336
afterWrite: 334
afterWrite: 334
afterWrite: 335
afterWrite: 335
afterWrite: 336
afterWrite: 336
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.