Skip to content

Commit

Permalink
optimization with context
Browse files Browse the repository at this point in the history
  • Loading branch information
lv 周 authored and lv 周 committed May 14, 2022
1 parent 0021e72 commit b38c01e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
36 changes: 28 additions & 8 deletions http_bench.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"bytes"
"context"
"crypto/tls"
"encoding/json"
"flag"
Expand Down Expand Up @@ -1038,47 +1039,65 @@ func main() {
}
}

var mainServer *http.Server
_, mainCancel := context.WithCancel(context.Background())

if getEnv("BENCH_PROFILE") == "1" {
file, err := os.OpenFile("cpu.pprof", os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0644)
if err == nil {
fmt.Fprintf(os.Stdout, "== StartCPUProfile ==\n")
pprof.StartCPUProfile(file)
defer func() {
pprof.StopCPUProfile()
file.Close()
fmt.Fprintf(os.Stdout, "== StopCPUProfile ==\n")
}()

osSignal := make(chan os.Signal)
signal.Notify(osSignal, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-osSignal
pprof.StopCPUProfile()
file.Close()
fmt.Fprintf(os.Stdout, "== StopCPUProfile ==\n")
os.Exit(0)
if mainServer != nil {
mainServer.Shutdown(context.Background())
}
mainCancel()
}()
}
}

// decrease gc profile
if getEnv("BENCH_GC") == "1" {
debug.SetGCPercent(500)
debug.SetGCPercent(200)
}

if len(*listen) > 0 {
mux := http.NewServeMux()
mux.HandleFunc("/", handleWorker)
fmt.Fprintf(os.Stdout, "Worker listen %s\n", *listen)
if err := http.ListenAndServe(*listen, mux); err != nil {
mainServer = &http.Server{
Addr: *listen,
Handler: mux,
}
if err := mainServer.ListenAndServe(); err != nil {
fmt.Fprintf(os.Stderr, "ListenAndServe err: %s\n", err.Error())
}
} else if len(*web) > 0 {
mux := http.NewServeMux()
mux.Handle("/", http.FileServer(http.Dir("./")))
mux.HandleFunc("/api", handleWorker)
fmt.Fprintf(os.Stdout, "Web listen %s\n", *web)
if err := http.ListenAndServe(*web, mux); err != nil {
mainServer = &http.Server{
Addr: *web,
Handler: mux,
}
if err := mainServer.ListenAndServe(); err != nil {
fmt.Fprintf(os.Stderr, "ListenAndServe err: %s\n", err.Error())
}
} else {
if len(params.Urls) <= 0 || len(params.Urls[0]) <= 0 {
usageAndExit("url or url-file empty.")
}

params.SequenceId = time.Now().Unix()
params.Cmd = CMD_START
verbosePrint(VERBOSE_DEBUG, "Request params: %s\n", params.String())
Expand All @@ -1090,11 +1109,12 @@ func main() {

go func() {
<-stopSignal
verbosePrint(VERBOSE_INFO, "Recv stop signal")
verbosePrint(VERBOSE_INFO, "Recv stop signal\n")
params.Cmd = CMD_STOP
jsonBody, _ := json.Marshal(params)
requestWorkerList(jsonBody, stressTest)
stressTest.Stop(true) // Recv stop signal and Stop commands
mainCancel()
}()

if stressResult = execStress(params, &stressTest); stressResult != nil {
Expand Down
5 changes: 4 additions & 1 deletion test/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ go test -v echo_http2_test.go
```
../http_bench -d 10s -c 10 -m POST "http://127.0.0.1:18090" -body "{}"
../http_bench -d 10s -c 10 -http http2 -m POST "http://127.0.0.1:19090" -body "{}"
```
```

# TODO:
[] test script

0 comments on commit b38c01e

Please sign in to comment.