Add the URL path prefix to the static module of connect.

Source: Internet
Author: User
ArticleDirectory
    • The purest official helloworld
    • The purest connect helloworld
    • Add the URL path prefix of a static file
    • Love

It is estimated that we will naturally use the static file module static according to the official example when using connect:

 
VaR connect = require ('connect '); Connect (connect. static (_ dirname), function (req, Res) {res. writehead (200, {'content-type': 'text/plain '}); Res. end ('Hello world \ n ');}). listen (8124); console. log ('server running at http: // 127.0.0.1: 8124 /');
Benchmark performance

To evaluateStaticPerformance, we need another benchmark comparison.

The purest official helloworld

We use the helloworld provided in the official nodejs documentation for the most basic reference:

Ar HTTP = require ('http'); http. createserver (function (request, response) {response. writehead (200, {'content-type': 'text/plain '}); response. end ('Hello world \ n ');}). listen (8124); console. log ('server running at http: // 127.0.0.1: 8124 /');
The purest connect helloworld

Do not use any middleware Module

 
VaR connect = require ('connect '); Connect (function (req, Res) {res. writehead (200, {'content-type': 'text/plain '}); Res. end ('Hello world \ n ');}). listen (8124); console. log ('server running at http: // 127.0.0.1: 8124 /');
Connect helloworld with domain Module
VaR connect = require ('connect '); var createdomain = require ('domain '). create; Connect (function (req, res, next) {var domain = createdomain (); domain. on ('error', function (ERR) {console. log ('errrrrr ', err); Res. status Code = 500; Res. end (err. message + '\ n'); domain. dispose () ;}); domain. run (next) ;}, function (req, res, next) {If (req. url = '/error') {process. nexttick (function () {res. end ('params: '+ req. query. ABC) ;}); return ;}res. writehead (200, {'content-type': 'text/plain '}); Res. end ('Hello world \ n ');}). listen (8124); console. log ('server running at http: // 127.0.0.1: 8124 /');
Test Results

Official purest helloworld: 7851.56 QPS

$ Siege-B-C10-t10s http: // 127.0.0.1: 8124/** siege 2.72 ** preparing 10 concurrent users for battle. the server is now under siege... lifting the server siege... done. transactions: 78123 hitsavailability: 100.00% elapsed time: 9.95 secsdata transferred: 0.89 mbresponse time: 0.00 secstransaction rate: 7851.56 TRANS/seconds: 0.09 MB/secconcurrency: 9.93 rows transactions: 78123 failed transactions: 0 longest transaction: 0.09 MAID: 0.00

Pure connect helloworld: 6808.19 QPS

$ Siege-B-C10-t10s http: // 127.0.0.1: 8124/** siege 2.72 ** preparing 10 concurrent users for battle. the server is now under siege... lifting the server siege... done. transactions: 78123 hitsavailability: 100.00% elapsed time: 9.95 secsdata transferred: 0.89 mbresponse time: 0.00 secstransaction rate: 7851.56 TRANS/seconds: 0.09 MB/secconcurrency: 9.93 rows transactions: 78123 failed transactions: 0 longest transaction: 0.09 MAID: 0.00

Connect helloworld: 5601.35 QPS using domain Module

Domain demo for Express

$ Siege-B-C10-t10s http: // 127.0.0.1: 8124/** siege 2.72 ** preparing 10 concurrent users for battle. the server is now under siege... lifting the server siege... done. transactions: 65699 hitsavailability: 100.00% elapsed time: 9.65 secsdata transferred: 0.75 mbresponse time: 0.00 secstransaction rate: 6808.19 TRANS/seconds: 0.08 MB/secconcurrency: 9.96 rows transactions: 65699 failed transactions: 0 longest transaction: 0.05 MAID: 0.00

Connect helloworld with static values: 3636.98 QPS

$ Siege-B-C10-t10s http: // 127.0.0.1: 8124/** siege 2.72 ** preparing 10 concurrent users for battle. the server is now under siege... lifting the server siege... done. transactions: 34915 hitsavailability: 100.00% elapsed time: 9.60 secsdata transferred: 0.40 mbresponse time: 0.00 secstransaction rate: 3636.98 TRANS/seconds: 0.04 MB/secconcurrency: 9.97 rows transactions: 34915 failed transactions: 0 longest transaction: 0.06 MAID: 0.00
Why is the performance reduced by 50%?

Dizzy, why is the performance reduced by 50% with the static module added?

View static. Send ()Source code:

// "Hidden" fileif (! Hidden &&'. '= basename (PATH) [0]) return next (); FS. stat (path, function (ERR, STAT) {// MIME type = mime. lookup (PATH); // ignore enoent if (ERR) {If (FN) return FN (ERR); Return ('enoent' = err. code | 'enabledlong' = err. code )? Next (): Next (ERR); // redirect directory in case index.html is present} else if (Stat. isdirectory () {If (! Redirect) return next (); Res. status Code = 301; Res. setheader ('location', URL. pathname + '/'); Res. end ('redirecting to '+ URL. pathname + '/'); return ;}

The static module needs file IO every time to determine whether the file exists. This is a waste of performance.

Add the URL path prefix of a static file

Now that the performance problem is located, the problem can be solved. The right remedy does not require all requests to be processed statically.

Add a URL prefix to the static statement, for example/Public/images/logo.jpgOnly URL requests with a prefix of/public need to be processed in the static module.

So what we improvedCodeIt should be like this:

VaR connect = require ('connect '); var APP = connect (); app. use ('/public', connect. static (_ dirname); app. use (function (req, Res) {res. writehead (200, {'content-type': 'text/plain '}); Res. end ('Hello world \ n ');}). listen (8124); console. log ('server running at http: // 127.0.0.1: 8124 /');

What is the performance? Wow 6749.03 QPS, almost the same as connect hellowrold. Done!

$ Siege-B-C10-t10s http: // 127.0.0.1: 8124/** siege 2.72 ** preparing 10 concurrent users for battle. the server is now under siege... lifting the server siege... done. transactions: 66073 hitsavailability: 100.00% elapsed time: 9.79 secsdata transferred: 0.76 mbresponse time: 0.00 secstransaction rate: 6749.03 TRANS/seconds: 0.08 MB/secconcurrency: 9.97 rows transactions: 66073 failed transactions: 0 longest transaction: 0.03 MAID: 0.00

To reproduce the previous performance problems, access/public/Foo.

$ Siege-B-C10-t10s http: // 127.0.0.1: 8124/public/Foo ** siege 2.72 ** preparing 10 concurrent users for battle. the server is now under siege... lifting the server siege... done. transactions: 37773 hitsavailability: 100.00% elapsed time: 9.59 secsdata transferred: 0.43 mbresponse time: 0.00 secstransaction rate: 3938.79 TRANS/seconds: 0.05 MB/secconcurrency: 9.97 rows transactions: 37773 failed transactions: 0 longest transaction: 0.05 MAID: 0.00
Love

Remember to add a URL path prefix to connect. Static!

^_^ I hope this article will be useful to you.

Original Link

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.