天天看點

hi-nginx-1.4.9正式釋出,支援javascript後端開發

hi-nginx-1.4.9已經釋出。

更新:

  • 支援javascript後端開發
  • 修複腳本搜尋的一個bug

從這一版開始,hi-nginx開始支援javascript,這意味着把javascript應用于後端開發,将不再隻有nodejs這唯一的途徑和方法。由于java本身對javascript的極好支援,使得在hi-nginx中,可以直接在javascript腳本中使用java——相當于把java嵌入了javascript。

為了開啟javascript支援,你隻需要開啟java支援即可:--enable-http-hi-java=YES。

例子:

if (typeof (Mustache) == 'undefined') {
    load('https://cdn.bootcss.com/mustache.js/2.3.0/mustache.min.js')
}


var list = java.util.Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
var template = "{{#list}}* {{.}}\n{{/list}}"
var key = 'test', output
if (hi_req.cache.containsKey(key)) {
    output = hi_req.cache.get(key)
} else {
    output = Mustache.render(template, {'list': JSON.parse(list.toString())})
    hi_res.cache.put(key, output)
}
hi_res.headers.get('Content-Type').set(0, 'text/plain;charset=UTF-8')
hi_res.content = output
hi_res.status = 200;      

比較:

helloworld:

fedora 25,4g,2核i5,筆記本

ab -c 1000 -n 500000 壓力測試,RPS:

  • nodejs(6.12.0):無法完成測試
  • hi-nginx-javascript :均值13000+

ab -c 1000 -n 100000 壓力測試,RPS:

  • nodejs(6.12.0):偶爾完成測試,最好成績接近9000+,均值6000-9000
  • hi-nginx-javascript :均值14000+

用法:

hi_java_classpath "-Djava.class.path=.:/usr/local/nginx/java:/usr/local/nginx/java/hi-nginx-java.jar";
        hi_java_options "-server -d64 -Xms512m -Xmx512m -Xmn170m -Dnashorn.args=--global-per-engine";
        hi_java_servlet_cache_expires 300s;
        hi_java_servlet_cache_size 10;
        hi_java_version 8;


        location / {
                hi_need_cache off;
                hi_cache_expires 5s;
                hi_need_kvdb off;
                hi_kvdb_size 10;
                hi_kvdb_expires 5s;
                hi_need_cookies on;
                hi_need_headers on;
                hi_need_session on;
                hi_session_expires 300s;
                hi_javascript_lang javascript;
                hi_javascript_extension js;
                #hi_javascript_script javascript/index.js;
                hi_javascript_content "hi_res.content='hello,world';hi_res.status=200;";
        }      
Server Software:        nginx/1.14.0
Server Hostname:        127.0.0.1
Server Port:            8086

Document Path:          /
Document Length:        11 bytes

Concurrency Level:      1000
Time taken for tests:   34.457 seconds
Complete requests:      500000
Failed requests:        0
Total transferred:      139000000 bytes
HTML transferred:       5500000 bytes
Requests per second:    14510.69 [#/sec] (mean)
Time per request:       68.915 [ms] (mean)
Time per request:       0.069 [ms] (mean, across all concurrent requests)
Transfer rate:          3939.43 [Kbytes/sec] received      

官網:

https://www.hi-nginx.com/

文檔:

https://doc.hi-nginx.com/

倉庫:

https://github.com/webcpp/hi-nginx