新聞中心
我使用Python工作已經(jīng)有幾年了,最近開始了一個關(guān)于GO的調(diào)查,主要看作是一個緩解瓶頸的實驗,還沒有大規(guī)模web服務(wù)器部署。

十載的中站網(wǎng)站建設(shè)經(jīng)驗,針對設(shè)計、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時及時工作處理。營銷型網(wǎng)站建設(shè)的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動調(diào)整中站建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計,從而大程度地提升瀏覽體驗。創(chuàng)新互聯(lián)公司從事“中站網(wǎng)站設(shè)計”,“中站網(wǎng)站推廣”以來,每個客戶項目都認(rèn)真落實執(zhí)行。
我用不同語言寫了一個簡單的REST服務(wù),使用ab工具檢測響應(yīng)速度。
Python
server.py
- from bottle import route, run
- @route('/')
- def home():
- article = {'name': 'A Royal Baby', 'body':'A slow news week'}
- return article
- def main():
- run(host='localhost', port=8081)
- if __name__ == '__main__':
- main()
Go
server.go
- package main
- import (
- "encoding/json"
- "fmt"
- "github.com/emicklei/go-restful"
- "io"
- "net/http"
- )
- func main() {
- ws := new(restful.WebService)
- ws.Route(ws.GET("/").To(hello))
- restful.Add(ws)
- fmt.Print("Server starting on port 8080\n")
- http.ListenAndServe(":8080", nil)
- }
- func hello(req *restful.Request, resp *restful.Response) {
- article := Article{"A Royal Baby", "A slow news week"}
- b, _ := json.Marshal(article)
- io.WriteString(resp, string(b))
- }
- type Article struct {
- Name string
- Body string
- }
Go語言的版本明顯比Python版的更詳細(xì),但我認(rèn)為它仍然非常言簡意賅。Python服務(wù)器使用bottle框架,指定的article字典作為響應(yīng)自動序列化返回。Go服務(wù)器使用go-restful包,這個包使得在Go中很容易構(gòu)建RESTful API。
測試基準(zhǔn)是在Macbook Pro上,CPU i7 2.4Ghz,16GB RAM。
使用下附命令:
ab -q -c 50 -n 1000 http://127.0.0.1:8080/ | grep "Requests per second"
結(jié)果
5次測試,1000次請求的平均每秒鐘完成請求次數(shù)
| Run | Python | Go |
|---|---|---|
| 1 | 1310.58 | 13568.34 |
| 2 | 1332.82 | 13092.95 |
| 3 | 1331.54 | 13479.45 |
| 4 | 1306.09 | 13271.58 |
| 5 | 1274.49 | 13873.09 |
Go平均完成13457次請求/秒,Python完成1311次請求/秒
在本次測試中,Go在完成JSON響應(yīng)方面比Python快10.26倍。
我知道這些腳本非常的簡單,而且缺少實際應(yīng)用中的邏輯——例如數(shù)據(jù)庫連接。也許有比ab更準(zhǔn)確的測試方法,但我認(rèn)為這些結(jié)果足以給你一個嘗試Go的理由。以我目前的經(jīng)驗,從Python到編譯型語言有很多樂趣。
做過Python/Go基準(zhǔn)測試的想一起分享么?
英文原文:Python vs Go - Requests per Second
譯文鏈接:http://www.oschina.net/translate/python-vs-go-requests-per-second
網(wǎng)站名稱:對比Python和Go語言的每秒請求數(shù)
本文路徑:http://fisionsoft.com.cn/article/dhhseei.html


咨詢
建站咨詢
