Our docs represent how we write code (#2925)

We want them to look like good code :-)
So brush the autoscale example
This commit is contained in:
Victor Agababov 2020-10-09 18:47:22 -07:00 committed by GitHub
parent cac638fd0f
commit 96cbd6664f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 19 additions and 18 deletions

View File

@ -3,6 +3,7 @@ package main
import ( import (
"flag" "flag"
"fmt" "fmt"
"log"
"net/http" "net/http"
"os" "os"
"sync/atomic" "sync/atomic"
@ -16,8 +17,8 @@ var (
ip = flag.String("ip", "", "ip address of knative ingress") ip = flag.String("ip", "", "ip address of knative ingress")
port = flag.String("port", "80", "port of call") port = flag.String("port", "80", "port of call")
host = flag.String("host", "autoscale-go.default.example.com", "host name of revision under test") host = flag.String("host", "autoscale-go.default.example.com", "host name of revision under test")
qps = flag.Int("qps", 10, "max requests per second") qps = flag.Int64("qps", 10, "max requests per second")
concurrency = flag.Int("concurrency", 10, "max in-flight requests") concurrency = flag.Int64("concurrency", 10, "max in-flight requests")
duration = flag.Duration("duration", time.Minute, "duration of the test") duration = flag.Duration("duration", time.Minute, "duration of the test")
verbose = flag.Bool("verbose", false, "verbose output for debugging") verbose = flag.Bool("verbose", false, "verbose output for debugging")
) )
@ -25,22 +26,21 @@ var (
type result struct { type result struct {
success bool success bool
statusCode int statusCode int
latency int64 latency time.Duration
} }
func get(url string, client *http.Client, report chan *result) { func get(url string, client *http.Client, report chan *result) {
start := time.Now() start := time.Now()
result := &result{} result := &result{}
defer func() { defer func() {
end := time.Now() result.latency = time.Since(start)
result.latency = end.UnixNano() - start.UnixNano()
report <- result report <- result
}() }()
req, err := http.NewRequest("GET", url, nil) req, err := http.NewRequest("GET", url, nil)
if err != nil { if err != nil {
if *verbose { if *verbose {
fmt.Printf("%v\n", err) fmt.Println("NewRequest:", err)
} }
return return
} }
@ -48,7 +48,7 @@ func get(url string, client *http.Client, report chan *result) {
res, err := client.Do(req) res, err := client.Do(req)
if err != nil { if err != nil {
if *verbose { if *verbose {
fmt.Printf("%v\n", err) fmt.Println("Do:", err)
} }
return return
} }
@ -56,7 +56,7 @@ func get(url string, client *http.Client, report chan *result) {
result.statusCode = res.StatusCode result.statusCode = res.StatusCode
if result.statusCode != http.StatusOK { if result.statusCode != http.StatusOK {
if *verbose { if *verbose {
fmt.Printf("%+v\n", res) fmt.Printf("Response: %+v\n", res)
} }
return return
} }
@ -66,10 +66,10 @@ func get(url string, client *http.Client, report chan *result) {
func reporter(stopCh <-chan time.Time, report chan *result, inflight *int64) { func reporter(stopCh <-chan time.Time, report chan *result, inflight *int64) {
tickerCh := time.NewTicker(time.Second).C tickerCh := time.NewTicker(time.Second).C
var ( var (
total int64 total int
count int64 count float64
nanoseconds int64 successful float64
successful int64 totalDuration time.Duration
) )
fmt.Println("REQUEST STATS:") fmt.Println("REQUEST STATS:")
for { for {
@ -79,17 +79,18 @@ func reporter(stopCh <-chan time.Time, report chan *result, inflight *int64) {
case <-tickerCh: case <-tickerCh:
fmt.Printf("Total: %v\tInflight: %v\tDone: %v ", total, atomic.LoadInt64(inflight), count) fmt.Printf("Total: %v\tInflight: %v\tDone: %v ", total, atomic.LoadInt64(inflight), count)
if count > 0 { if count > 0 {
fmt.Printf("\tSuccess Rate: %.2f%%\tAvg Latency: %.4f sec\n", float64(successful)/float64(count)*100, float64(nanoseconds)/float64(count)/(1000000000)) fmt.Printf("\tSuccess Rate: %.2f%%\tAvg Latency: %.4f sec\n",
successful/count*100, totalDuration.Seconds()/count)
} else { } else {
fmt.Printf("\n") fmt.Printf("\n")
} }
count = 0 count = 0
nanoseconds = 0 totalDuration = 0
successful = 0 successful = 0
case r := <-report: case r := <-report:
total++ total++
count++ count++
nanoseconds += r.latency totalDuration += r.latency
if r.success { if r.success {
successful++ successful++
} }
@ -104,7 +105,7 @@ func main() {
ip = &ipAddress ip = &ipAddress
} }
if *ip == "" { if *ip == "" {
panic("need either $IP_ADDRESS env var or --ip flag") log.Fatal("Need either $IP_ADDRESS env var or --ip flag")
} }
url := fmt.Sprintf( url := fmt.Sprintf(
"http://%v:%v?sleep=%v&prime=%v&bloat=%v", "http://%v:%v?sleep=%v&prime=%v&bloat=%v",
@ -117,13 +118,13 @@ func main() {
go reporter(stopCh, report, &inflight) go reporter(stopCh, report, &inflight)
qpsCh := time.NewTicker(time.Duration(time.Second.Nanoseconds() / int64(*qps))).C qpsCh := time.NewTicker(time.Duration(time.Second.Nanoseconds() / *qps)).C
for { for {
select { select {
case <-stopCh: case <-stopCh:
return return
case <-qpsCh: case <-qpsCh:
if atomic.LoadInt64(&inflight) < int64(*concurrency) { if atomic.LoadInt64(&inflight) < *concurrency {
atomic.AddInt64(&inflight, 1) atomic.AddInt64(&inflight, 1)
go func() { go func() {
get(url, client, report) get(url, client, report)