Benchmark_Ruby_Go.md

1 min read Original article ↗

ruby 2.2.0p0 (2014-12-25 revision 49005) [x86_64-linux-gnu]

go version xgcc (Ubuntu 4.9.1-0ubuntu1) 4.9.1 linux/amd64

Sinatra

require 'sinatra'
require 'thin'

set :logging, false
set :bind, '0.0.0.0'

get '/' do
  '{"message":"Hello, World!"}'
end

rake

module MySinatra
  class Application
    def self.call(env)
      new.call(env)
    end 

    def call(env)
      headers = { 'Content-Type' => 'text/plain' }
      if env['PATH_INFO'] == '/' 
        status, body = 200, '{"message":"Hello, World!"}'
      else
        status, body = 404, "Sinatra doesn't know this ... ! "
      end 
      headers['Content-Length'] = body.length.to_s
      [status, headers, [body] ]
    end 
  end 
end

require 'thin'
Thin::Server.start MySinatra::Application, 4567

go bee (http://beego.me/quickstart)

package main

import (
    "github.com/astaxie/beego"
)

type MainController struct {
    beego.Controller
}

func (this *MainController) Get() {
    this.Ctx.WriteString("{\"message\":\"Hello, World!\"}")
}

func main() {
    beego.Router("/", &MainController{})
    beego.Run()
}

go net/http

package main

import (
  "io"
  "net/http"
  "log"
)

// hello world, the web server
func HelloServer(w http.ResponseWriter, req *http.Request) {
  io.WriteString(w, "{\"message\":\"Hello, World!\"}")
}

func main() {
  http.HandleFunc("/hello", HelloServer)
  err := http.ListenAndServe(":8080", nil)
  if err != nil {
    log.Fatal("ListenAndServe: ", err)
  }
}