Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
A Micro-framework for node.js
branch: partials

This branch is 66 commits behind dantebronto:master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
picard/lib
sample_app
sinatra_app_for_benchmarks
README.textile

README.textile

Picard: A Micro-framework for node.js

If you want to check it out, install Node and start the sample app with `node app.js`. There are no other dependencies.

Picard uses Haml thanks to creationix’s haml-js project.

A sample application:

  require('./config/env')

  get('/', function(){
    return { text: 'Hello Universe' }
  })

  get('/haml', function(){
    var scope = {
      template: 'index.haml', 
      print_date: function () {
        return (new Date()).toDateString();
      },
      current_user: {
        name: "Jean-Luc Picard",
        bio: "Captain of the USS Enterprise"
      }
    };
    return scope
  })

  get('/json', function(){
    return {
      type: 'application/json',
      body: JSON.stringify( 
        [ { command_1: 'Make it so' },
          { command_2: 'You have the bridge, Number One' } ] )
    }
  })

  post('/order', function(){
    return { text: 'Tea, Earl Grey, Hot' }
  })

  // simulate form params with `curl -d "foo=bar&baz=bat" http://localhost:9900/with_params`
  post('/with_params', function(params){
    return { text: '<h1>' + params.foo + ' ' + params.baz + '</h1>' }
  })

  put('/weapon/:id', function(params){
    return { text: '<p>Phaser with id #' + params.id + ' set to stun</p>' }
  })

  del('/fire/:number', function(params){

    var text = '<p>Borg cube destroyed using ' + params.number + ' photon torpedoes</p>'

    if (  Number(params.number) > 12 )
      text = '<h1>Maximum yield, full spread!</h1>'

    return { text: text }
  })

  get(/\/regex\/(.*)\/(.*)/, function(params){
    return { text: params.captures[0] + ' ' + params.captures[1] }
  })

See the sample app for more examples, including how to handle asynchronous processing.

Benchmarks

The included sinatra_app_for_benchmarks is a Ruby port of the Picard sample_app. According to these benchmarks, the Picard app on node.js runs about twice as fast as the Sinatra app on Thin and can handle around 10x as many requests per second (due to the asynchronous nature of node.js). Below is a sample output of the benchmarks:

  $ cd sinatra_app_for_benchmarks
  $ ruby benchmark.rb

  Running Sinatra specs 10 times:

  Finished in 0.083148 seconds
  Finished in 0.083371 seconds
  Finished in 0.112633 seconds
  Finished in 0.083442 seconds
  Finished in 0.083424 seconds
  Finished in 0.082644 seconds
  Finished in 0.083286 seconds
  Finished in 0.084136 seconds
  Finished in 0.113771 seconds
  Finished in 0.08323 seconds
  Average time was 0.0893085 seconds

  Running Picard specs 10 times:

  Finished in 0.040487 seconds
  Finished in 0.040587 seconds
  Finished in 0.041176 seconds
  Finished in 0.042727 seconds
  Finished in 0.04083 seconds
  Finished in 0.040875 seconds
  Finished in 0.040128 seconds
  Finished in 0.039876 seconds
  Finished in 0.040797 seconds
  Finished in 0.041551 seconds
  Average time was 0.0409034 seconds

  Picard was 2.18 times faster!

Apache Benchmark Test for Sinatra app:

  $ ab -n 5000 -c 50 http://127.0.0.1:3000/haml
  
  ...
  
  Server Software:        thin
  Server Hostname:        127.0.0.1
  Server Port:            3000

  Document Path:          /haml
  Document Length:        697 bytes

  Concurrency Level:      50
  Time taken for tests:   52.174 seconds
  Complete requests:      5000
  Failed requests:        0
  Write errors:           0
  Total transferred:      4175000 bytes
  HTML transferred:       3485000 bytes
  Requests per second:    95.83 [#/sec] (mean)
  Time per request:       521.740 [ms] (mean)
  Time per request:       10.435 [ms] (mean, across all concurrent requests)
  Transfer rate:          78.15 [Kbytes/sec] received

  Connection Times (ms)
                min  mean[+/-sd] median   max
  Connect:        0    0   0.9      0      15
  Processing:   189  520  33.6    517     794
  Waiting:      100  418  33.2    420     701
  Total:        191  520  33.6    518     794

  Percentage of the requests served within a certain time (ms)
    50%    518
    66%    521
    75%    524
    80%    525
    90%    528
    95%    532
    98%    615
    99%    670
   100%    794 (longest request)

Apache Benchmark Test for Picard app:

  $ ab -n 5000 -c 50 http://127.0.0.1:9900/haml
  
  ...
  
  Server Software:        Picard
  Server Hostname:        127.0.0.1
  Server Port:            9900

  Document Path:          /haml
  Document Length:        547 bytes

  Concurrency Level:      50
  Time taken for tests:   5.058 seconds
  Complete requests:      5000
  Failed requests:        0
  Write errors:           0
  Total transferred:      3490000 bytes
  HTML transferred:       2735000 bytes
  Requests per second:    988.58 [#/sec] (mean)
  Time per request:       50.578 [ms] (mean)
  Time per request:       1.012 [ms] (mean, across all concurrent requests)
  Transfer rate:          673.85 [Kbytes/sec] received

  Connection Times (ms)
                min  mean[+/-sd] median   max
  Connect:        0    2   1.5      1      10
  Processing:    10   49  13.0     45     102
  Waiting:        5   35  13.1     33      91
  Total:         10   50  12.7     46     102

  Percentage of the requests served within a certain time (ms)
    50%     46
    66%     49
    75%     51
    80%     55
    90%     68
    95%     84
    98%     91
    99%     94
   100%    102 (longest request)

988.58 requests per second for Picard verses 95.83 for Sinatra!

Specs: MacBook Pro 2 × 2.53 GHz, 4 GB RAM, Ruby v1.8.7 on Thin 1.2.5, node.js v0.1.18

Note: The Sinatra app is not event driven, so to be fair I should really be testing Picard verses Sinatra + EventMachine. However, this illustrates the fact that node.js is evented out of the box, and obviates the need for separate libraries like EventMachine.

License

Copyright © 2009 Kellen Presley

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Something went wrong with that request. Please try again.