開發簡單 web 服務程序 cloudgo

1. 框架選擇:Martini

Martini是快速構建模塊化web應用與服務的開發框架。其特點是模塊化,方便與其他框架結合,同時其路由設計十分簡潔實用,而靈活的中間件則給我們更多的自由發揮空間。
服務器代碼:

package main

import (
	"github.com/go-martini/martini"
	flag "github.com/spf13/pflag"
	"fmt"
)

func main() {
  m := martini.Classic()
  port := flag.String("port", "", "port:default is 3000") //傳入端口號
  flag.Parse()
  m.Get("/", func() string {
    return "Hello world!"
  })
  m.Get("/hello/(?P<name>[a-zA-Z]+)", func(params martini.Params) string {
	return fmt.Sprintf ("Hello %s\n", params["name"])
  })
  if *port == "" {
	m.Run()
  } else {
	m.RunOnAddr(":" + *port)
  }
}

2. curl 測試

1)運行服務器:
命令:go run server.go --port 9090
結果:
在這裏插入圖片描述
2)瀏覽器訪問測試:
在這裏插入圖片描述
3)curl命令測試:
命令:curl -v http://localhost:9090/hello/testuser
結果:
在這裏插入圖片描述

3. ab 測試

1)測試環境:
Ubuntu
2)測試命令:
ab -n 1000 -c 100 http://localhost:9090/hello/your
3)測試結果:
在這裏插入圖片描述
4)重要參數解釋:

字段 含義
Server Software 服務器軟件名
Server Hostname 服務器主機名
Server Port 服務器端口
Document Path 文件相對路徑
Document Length 文件大小
Concurrency Level 併發等級
Time taken for tests 測試一共花的時間
Complete requests 完成的測試數
Failed requests 失敗的測試數
Total transferred 傳輸的總的字節大小
HTML transferred 傳輸的HTML文件大小
Requst per second 平均每秒的請求個數
Time per request 平均每個請求花的時間
Transfer rate 傳輸速度
Connection Times 表內描述了所有的過程中所消耗的最小、中位、最長時間。
Percentage of the requests served within a certain time 每個百分段的請求完成所需的時間
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章