Skip to content

Instantly share code, notes, and snippets.

@unkstar
Last active August 29, 2015 14:01
Show Gist options
  • Save unkstar/d258527a303125c80733 to your computer and use it in GitHub Desktop.
Save unkstar/d258527a303125c80733 to your computer and use it in GitHub Desktop.
package main
import (
"flag"
"fmt"
"sync"
"sync/atomic"
"time"
"runtime"
"net/http"
"io/ioutil"
)
var loops = flag.Int("loop", 10, "requests per goroutine")
var goroutines = flag.Int("go", 10, "number of goroutines to spawn")
var url = flag.String("url", "", "url to query")
var reset uint32
func get() {
resp, err := http.Get(*url)
if err != nil {
atomic.AddUint32(&reset, 1)
return
}
_, err = ioutil.ReadAll(resp.Body)
resp.Body.Close()
}
func perf(id, times int, wg *sync.WaitGroup) {
for i:= 0; i < times; i++ {
get()
}
wg.Done()
}
func main() {
flag.Parse()
numCPU := runtime.NumCPU()
runtime.GOMAXPROCS(numCPU + 1)
fmt.Printf("Spawning %d goroutine, perform %d request each\n", *goroutines, *loops)
var wg sync.WaitGroup
wg.Add(*goroutines)
begin := time.Now()
for i := 0; i < *goroutines; i++ {
go perf(i, *loops, &wg)
}
wg.Wait()
dur := time.Since(begin)
fmt.Printf("%d request is reseted\n", reset)
fmt.Printf("Real time: durations= %d seconds\n", dur / time.Second)
fmt.Printf("Real time: %d ops/second\n", time.Duration(*loops * *goroutines) * time.Second / dur)
}
@diyism
Copy link

diyism commented May 23, 2014

看不懂, 这里get()是同步的还是异步的, 如果是同步的, 那得把goroutines设10000才是10000个concurrent连接

@diyism
Copy link

diyism commented May 23, 2014

你试下weightttp, 这个能设10000并发,跟go比较下哪个性能好:
weighttp -n 10000 -c 10000 -t 1 http://127.0.0.1:8888/

安装:
sudo apt-get install libev4 libev-dev
wget http://cgit.lighttpd.net/weighttp.git/snapshot/weighttp-master.tar.gz
tar -xzf weighttp-master.tar.gz
cd weighttp-master
./waf configure
./waf build
sudo ./waf install

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment