Rob--W / cors-anywhere

CORS Anywhere is a NodeJS reverse proxy which adds CORS headers to the proxied request.
MIT License
8.64k stars 6.05k forks source link

Connection speed #4

Open ghost opened 10 years ago

ghost commented 10 years ago

I ran a quick test, and I noticed that the speed at

allow-any-origin.appspot.com

is much faster. Can anything be done about this?

$ set https://s.ytimg.com/yts/jsbin/html5player-vfl4AMHqP.js

$ time curl -Os -H 'origin: *' https://cors-anywhere.herokuapp.com/$1

real    0m6.159s
user    0m0.093s
sys     0m0.015s

$ time curl -Os -H 'origin: *' https://allow-any-origin.appspot.com/$1

real    0m0.878s
user    0m0.171s
sys     0m0.061s
Rob--W commented 10 years ago

From one curl command, you cannot deduce anything. Either server could have been lucky to have encountered an intermediate proxy that cached the result, thus provided faster access to the data. To create a representative test case, you need to send many more requests.

Below, I show two small-sample test results of querying either web service 10x. First I just request the front page to measure the connectoon speed. Then I try to retrieve the URL you've posted in your ticket. These tests are not representative. You need to send many more requests to different servers across the globe to get a trustworthy result.

At my current geographical location (The Netherlands, near Eindhoven), the latency between appspot or herokuapp is comparable:

ApacheBench, 10x

Not really representative, but better than one single request. I'm measuring the speed of connecting to the server and getting a reply back. Note that I'm setting up a non-SSL connection because there's something with allow-any-origin's SSL certificate that prevents ApacheBench from establishing a connection.

CORS Anywhere

$ ab -n 10 -H 'origin: *' http://cors-anywhere.herokuapp.com/
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking cors-anywhere.herokuapp.com (be patient).....done

Server Software:        
Server Hostname:        cors-anywhere.herokuapp.com
Server Port:            80

Document Path:          /
Document Length:        1410 bytes

Concurrency Level:      1
Time taken for tests:   2.614 seconds
Complete requests:      10
Failed requests:        0
Write errors:           0
Total transferred:      16100 bytes
HTML transferred:       14100 bytes
Requests per second:    3.83 [#/sec] (mean)
Time per request:       261.351 [ms] (mean)
Time per request:       261.351 [ms] (mean, across all concurrent requests)
Transfer rate:          6.02 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:      119  125  12.9    120     161
Processing:   125  136  14.5    133     171
Waiting:      125  135  12.6    131     165
Total:        245  261  25.8    255     332

Percentage of the requests served within a certain time (ms)
  50%    255
  66%    257
  75%    262
  80%    267
  90%    332
  95%    332
  98%    332
  99%    332
 100%    332 (longest request)

allow-any-origin

$ ab -n 10 -H 'origin: *' http://allow-any-origin.appspot.com/
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking allow-any-origin.appspot.com (be patient).....done

Server Software:        Google
Server Hostname:        allow-any-origin.appspot.com
Server Port:            80

Document Path:          /
Document Length:        2651 bytes

Concurrency Level:      1
Time taken for tests:   2.470 seconds
Complete requests:      10
Failed requests:        0
Write errors:           0
Total transferred:      28930 bytes
HTML transferred:       26510 bytes
Requests per second:    4.05 [#/sec] (mean)
Time per request:       246.997 [ms] (mean)
Time per request:       246.997 [ms] (mean, across all concurrent requests)
Transfer rate:          11.44 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:       34   43  22.0     35     105
Processing:   164  204  54.6    180     339
Waiting:      164  204  54.9    177     339
Total:        199  247  56.4    220     380

Percentage of the requests served within a certain time (ms)
  50%    220
  66%    267
  75%    280
  80%    280
  90%    380
  95%    380
  98%    380
  99%    380
 100%    380 (longest request)

ApacheBench, proxy request

Now we leverage the proxy capabilities of both web services. Again, this test is not representative. Sometimes I get numbers that are 4x as much as the numbers shown below.

CORS Anywhere

$ ab -n 10 -H 'origin: *' http://cors-anywhere.herokuapp.com/$1
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking cors-anywhere.herokuapp.com (be patient).....done

Server Software:        sffe
Server Hostname:        cors-anywhere.herokuapp.com
Server Port:            80

Document Path:          /https://s.ytimg.com/yts/jsbin/html5player-vfl4AMHqP.js
Document Length:        527217 bytes

Concurrency Level:      1
Time taken for tests:   15.154 seconds
Complete requests:      10
Failed requests:        0
Write errors:           0
Total transferred:      5278320 bytes
HTML transferred:       5272170 bytes
Requests per second:    0.66 [#/sec] (mean)
Time per request:       1515.402 [ms] (mean)
Time per request:       1515.402 [ms] (mean, across all concurrent requests)
Transfer rate:          340.15 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:      118  123   8.4    121     146
Processing:  1057 1393 196.0   1485    1604
Waiting:      146  162  27.1    154     235
Total:       1177 1515 197.4   1614    1725

Percentage of the requests served within a certain time (ms)
  50%   1614
  66%   1618
  75%   1632
  80%   1644
  90%   1725
  95%   1725
  98%   1725
  99%   1725
 100%   1725 (longest request)

allow-any-origin

$ set https://s.ytimg.com/yts/jsbin/html5player-vfl4AMHqP.js
$ ab -n 10 -H 'origin: *' http://allow-any-origin.appspot.com/$1
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking allow-any-origin.appspot.com (be patient).....done

Server Software:        Google
Server Hostname:        allow-any-origin.appspot.com
Server Port:            80

Document Path:          /https://s.ytimg.com/yts/jsbin/html5player-vfl4AMHqP.js
Document Length:        527217 bytes

Concurrency Level:      1
Time taken for tests:   9.290 seconds
Complete requests:      10
Failed requests:        0
Write errors:           0
Total transferred:      5276440 bytes
HTML transferred:       5272170 bytes
Requests per second:    1.08 [#/sec] (mean)
Time per request:       928.995 [ms] (mean)
Time per request:       928.995 [ms] (mean, across all concurrent requests)
Transfer rate:          554.66 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:       34   36   1.2     36      38
Processing:   611  893 215.0    842    1383
Waiting:      248  447 141.3    404     673
Total:        646  929 214.6    879    1417

Percentage of the requests served within a certain time (ms)
  50%    879
  66%    965
  75%   1060
  80%   1062
  90%   1417
  95%   1417
  98%   1417
  99%   1417
 100%   1417 (longest request)
ghost commented 10 years ago

I appreciate the fast and detailed reply. From your location, I agree the results are comparable between the two services. However I am based in the United States and I am consistently getting 1/3 or worse speed with CORS Anywhere.

Rob--W commented 10 years ago

Odd. I'd expect US-request to be faster since the apps are created in the US region by default (https://devcenter.heroku.com/articles/regions#select-a-region).

I did not see anything special on tracepath (I quickly hit a dead end with allow-any-origin.appspot.com, and ended up at Amazon for cors-anywhere.herokuapp.com).

The source code for cors-anywhere is public. Feel free to clone the app and host it on your own server or some other PaaS if that reduces the latency.

ghost commented 10 years ago

I am noobish at this, if you know any instructions online to set up Google App Engine, I would appreciate.

Rob--W commented 10 years ago

Google App Engine does not support Node.js, AFAIK.

See https://github.com/joyent/node/wiki/Node-Hosting for a list of free Node.js hosting service providers.

You could also try out Heroku (everythng you need is already in this repository), and see whether it increases the response times.

ghost commented 10 years ago

@Rob--W thanks again. I will look at that list. This is my logic at the moment

The Netherlands + Heroku         = good
United States   + Heroku         = bad
United States   + something else = good?
Rob--W commented 10 years ago

@svnpenn I don't think that there's anything wrong with US + Heroku.

Do you have ApacheBench? If so, try to profile a little bit more. -n specifies the number of requests, -c specifies the number of concurrent requests (default 1). For comparison, these are the results for me:

cors-anywhere.herokuapp.com

ab -n 1000 -c20 http://example.herokuapp.com/
...
Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:      118  146 105.1    130    1135
Processing:   126  185 115.9    153    1119
Waiting:      125  168  63.2    148     782
Total:        246  331 157.6    287    1383

Percentage of the requests served within a certain time (ms)
  50%    287
  66%    302
  75%    321
  80%    337
  90%    391
  95%    480
  98%   1019
  99%   1271
 100%   1383 (longest request)

allow-any-origin.appspot.com

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:       32   46  21.7     43     438
Processing:   155  259 176.7    179    1241
Waiting:      154  250 156.8    178    1169
Total:        191  305 177.0    227    1299

Percentage of the requests served within a certain time (ms)
  50%    227
  66%    248
  75%    317
  80%    384
  90%    501
  95%    636
  98%    917
  99%   1050
 100%   1299 (longest request)
ghost commented 10 years ago

Here is my (very truncated) report. It is encouraging, but still a clear difference between the two. I can paste the full report if wanted.

$ ab -n 10 -H 'origin: *' http://cors-anywhere.herokuapp.com/$1
Time per request:       1500.405 [ms] (mean)

$ ab -n 10 -H 'origin: *' http://allow-any-origin.appspot.com/$1
Time per request:       620.881 [ms] (mean)
Rob--W commented 10 years ago

Could you increase your sample size? E.g. to 1000 (possibly without $i):

ab -n 1000 -c20 http://cors-anywhere.herokuapp.com/

Or at least 100 with $i:

ab -n 100 -c3 http://cors-anywhere.herokuapp.com/$i
ghost commented 10 years ago

Hm, mixed results. Looking better each time

# ab -n 1000 -c20 http://cors-anywhere.herokuapp.com/
Time per request:       2303.383 [ms] (mean)

# ab -n 100 -c3 http://cors-anywhere.herokuapp.com/$1
Time per request:       380.017 [ms] (mean)

# ab -n 1000 -c20 http://allow-any-origin.appspot.com/
Time per request:       1581.342 [ms] (mean)

# ab -n 100 -c3 http://allow-any-origin.appspot.com/$1
Time per request:       997.047 [ms] (mean)