开发web服务程序
1、概述
开发简单 web 服务程序 cloudgo,了解 web 服务器工作原理。
任务目标
- 熟悉 go 服务器工作原理
- 基于现有 web 库,编写一个简单 web应用类似 cloudgo。
- 使用 curl 工具访问 web 程序
- 对 web 执行压力测试
2、任务要求
基本要求
-
编程 web 服务程序 类似 cloudgo 应用。
· 要求有详细的注释
· 是否使用框架、选哪个框架自己决定 请在 README.md 说明你决策的依据 -
使用 curl 测试,将测试结果写入 README.md
-
使用 ab 测试,将测试结果写入 README.md。并解释重要参数。
main函数:
main.go:
其主要功能为绑定端口,解析端口,启动server。
package main
import (
"os"
"web/service"
flag "github.com/spf13/pflag"
)
const (
//默认8080端口
PORT string = "8080"
)
func main() {
//默认8080端口
port := os.Getenv("PORT")
if len(port) == 0 {
port = PORT
}
//端口号的解析
pPort := flag.StringP("port", "p", PORT, "PORT for httpd listening")
flag.Parse()
if len(*pPort) != 0 {
port = *pPort
}
//启动server
service.NewServer(port)
}
service函数:
使用go语言中的martini框架里的函数定义main.go中具体操作。
package service
import (
"github.com/go-martini/martini"
)
func NewServer(port string) {
m := martini.Classic()
m.Get("/", func(params martini.Params) string {
return "hello world"
})
m.RunOnAddr(":"+port)
}
下载martini:
运行:
在浏览器可看到helloworld输出:
curl测试:
安装curl:
测试结果:
ab测试:
安装ab:
对http://localhost:8080/进行1000次请求, 100个并发请求:
压力测试结果:
测试结果重要参数:
Server Hostname: 服务器主机名
Server Port: 服务器端口
Document Path: 文件路径
Document Length: 文件大小
Concurrency Level: 并发等级
Requst per second: 平均每秒的请求个数。服务器并发处理能力的量化描述。
Time per request: 用户平均的等待时间。计算公式:总请求数/处理完成这些请求数所花费的时间.
Time per request:across all concurrent requests: 计算公式:处理完成所有请求数所花费的时间/总请求数
Connection Times: 表内描述了所有的过程中所消耗的最小、中位、最长时间。
Percentage of the requests served within a certain time: 每个百分段的请求完成所需的时间