FC - Hello World

main.go

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package main

import (
"fmt"
"net/http"
"os"
)

func handler(w http.ResponseWriter, req *http.Request) {
requestID := req.Header.Get("x-fc-request-id")

w.Write([]byte(fmt.Sprintf("Hello, World:\n %s", requestID)))
}

func main() {
fmt.Println("FunctionCompute go runtime inited.")
http.HandleFunc("/", handler)
port := os.Getenv("FC_SERVER_PORT") // 监听端口可以在函数配置里面修改
if port == "" {
port = "9000"
}
http.ListenAndServe("0.0.0.0:" + port, nil)
}

bootstrap

1
2
#!/bin/bash
./main

编译

1
$ env GOOS=linux GOARCH=amd64 go build -o main main.go

编译后将 mainbootstrap 压缩,压缩前需要赋予执行权限或 0777。

win 可以用 工具 来便捷增加权限。

现在需要去阿里云那边创建 FC 函数。

创建完成后,进入到底部代码执行。

能成功请求,那进行下压测。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ ./wrk -t20 -c200 -d10s --latency https://1391263125553677.cn-shenzhen.fc.aliyuncs.com/2016-08-15/proxy/test/go_http/
Running 10s test @ https://1391263125553677.cn-shenzhen.fc.aliyuncs.com/2016-08-15/proxy/test/go_http/
20 threads and 200 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 77.52ms 92.05ms 1.58s 87.54%
Req/Sec 178.25 84.11 405.00 65.20%
Latency Distribution
50% 36.63ms
75% 87.90ms
90% 193.38ms
99% 453.64ms
33085 requests in 10.10s, 18.21MB read
Requests/sec: 3275.75
Transfer/sec: 1.80MB
往上