Skip to content

Commit

Permalink
Add time statistics in benchmark.
Browse files Browse the repository at this point in the history
  • Loading branch information
cyfdecyf committed Jan 13, 2013
1 parent 700edb0 commit 325222e
Showing 1 changed file with 53 additions and 9 deletions.
62 changes: 53 additions & 9 deletions cmd/shadowsocks-httpget/httpget.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import (
"fmt"
ss "github.com/shadowsocks/shadowsocks-go/shadowsocks"
"io"
"math"
"net"
"net/http"
"os"
"runtime"
"strconv"
"strings"
"time"
)

var config struct {
Expand Down Expand Up @@ -39,13 +41,17 @@ func doOneRequest(client *http.Client, url string, buf []byte) (err error) {
}
if err != io.EOF {
fmt.Printf("Read %s response error: %v\n", url, err)
} else {
err = nil
}
return
}

func get(connid int, url, serverAddr string, enctbl *ss.EncryptTable, done chan byte) {
func get(connid int, url, serverAddr string, enctbl *ss.EncryptTable, done chan []time.Duration) {
reqDone := 0
reqTime := make([]time.Duration, config.nreq, config.nreq)
defer func() {
done <- 1
done <- reqTime[:reqDone]
}()
tr := &http.Transport{
Dial: func(net, addr string) (c net.Conn, err error) {
Expand All @@ -55,10 +61,15 @@ func get(connid int, url, serverAddr string, enctbl *ss.EncryptTable, done chan

buf := make([]byte, 8192)
client := &http.Client{Transport: tr}
for i := 1; i <= config.nreq; i++ {
doOneRequest(client, url, buf)
if i%1000 == 0 {
fmt.Printf("conn %d finished %d get request\n", connid, i)
for ; reqDone < config.nreq; reqDone++ {
start := time.Now()
if err := doOneRequest(client, url, buf); err != nil {
return
}
reqTime[reqDone] = time.Now().Sub(start)

if (reqDone+1)%1000 == 0 {
fmt.Printf("conn %d finished %d get request\n", connid, reqDone+1)
}
}
}
Expand All @@ -82,18 +93,51 @@ func main() {

runtime.GOMAXPROCS(config.core)
url := flag.Arg(0)
if !strings.HasPrefix(url, "http://") && !strings.HasPrefix(url, "https://") {
if !strings.HasPrefix(url, "http://") {
url = "http://" + url
}

enctbl := ss.GetTable(config.passwd)
serverAddr := net.JoinHostPort(config.server, strconv.Itoa(config.port))

done := make(chan byte)
done := make(chan []time.Duration)
for i := 1; i <= config.nconn; i++ {
go get(i, url, serverAddr, enctbl, done)
}

// collect request finish time
reqTime := make([]int64, config.nconn*config.nreq)
reqDone := 0
for i := 1; i <= config.nconn; i++ {
<-done
rt := <-done
for _, t := range rt {
reqTime[reqDone] = int64(t)
reqDone++
}
}

fmt.Println("number of total requests:", config.nconn*config.nreq)
fmt.Println("number of finished requests:", reqDone)
if reqDone == 0 {
return
}

// calculate average an standard deviation
reqTime = reqTime[:reqDone]
var sum int64
for _, d := range reqTime {
sum += d
}
avg := float64(sum) / float64(reqDone)

varSum := float64(0)
for _, d := range reqTime {
di := math.Abs(float64(d) - avg)
di *= di
varSum += di
}
stddev := math.Sqrt(varSum / float64(reqDone))
fmt.Println("\ntotal time used:", time.Duration(sum))
fmt.Println("average time per request:", time.Duration(avg))
fmt.Println("standard deviation:", time.Duration(stddev))
}

0 comments on commit 325222e

Please sign in to comment.