Created
March 9, 2014 23:56
-
-
Save hogedigo/9457009 to your computer and use it in GitHub Desktop.
go study(implementing apache benchmark)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"flag" | |
"time" | |
) | |
type result struct { | |
seq int | |
start int64 | |
end int64 | |
} | |
func main() { | |
c := flag.Int("c", 3, "concurrency") | |
n := flag.Int("n", 10, "number of requests") | |
fmt.Printf("concurrency:%d\n", *c) | |
fmt.Printf("number of requests:%d\n", *n) | |
seqCh := sequence(*n) | |
resultCh := make(chan *result, *c) | |
totalStart := time.Now().UnixNano() | |
for i := 0; i < *c; i++ { | |
go test(resultCh, seqCh) | |
} | |
numDone := 0 | |
for result := range(resultCh) { | |
if result == nil { | |
numDone++ | |
if numDone >= *c { | |
break | |
} | |
continue | |
} | |
fmt.Printf("result(%d)\n", result.seq) | |
} | |
totalEnd := time.Now().UnixNano() | |
fmt.Printf("start=%d, end=%d\n", totalStart, totalEnd) | |
fmt.Printf("total(ms)=%d\n", (totalEnd - totalStart) / 1000000) | |
} | |
func test(resultCh chan *result, seqCh chan int) { | |
for { | |
seq, ok := <-seqCh | |
if !ok { | |
resultCh<-nil | |
return | |
} | |
start := time.Now().UnixNano() | |
<-time.After(time.Second * 1) | |
end := time.Now().UnixNano() | |
resultCh<-&result{seq, start, end} | |
} | |
} | |
func sequence(max int) chan int { | |
ch := make(chan int) | |
go func() { | |
for i := 1; i <= max; i++ { | |
ch<-i | |
} | |
close(ch) | |
}() | |
return ch | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
つくりかけ