Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- name: golangci-lint
uses: golangci/golangci-lint-action@v2
with:
version: v1.43.0
version: v1.50.0

# Check #2: Test
- name: Test
Expand Down
23 changes: 23 additions & 0 deletions .github/workflows/demo-benchttp-action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Demo Benchttp Action

on: [pull_request]

jobs:
run-benchttp:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.17

- name: Start server in the background
run: go run main.go -port=9999 -ignoreStdin &

- name: Run Benchttp CLI
uses: benchttp/action@v1
with:
options: -configFile ./demo-benchttp-tests.yml
version: v0.1.0
3 changes: 0 additions & 3 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ linters:
disable-all: true
enable:
- bodyclose # enforce resp.Body.Close()
- deadcode
- dupl # duplicate code
- errcheck
- exportloopref
Expand All @@ -143,10 +142,8 @@ linters:
- prealloc # enforce capacity allocation when possible
- revive # golint enhancement
- staticcheck # go vet enhancement
- structcheck # unused struct fields
- testpackage # checks on tests (*_test)
- thelper # enforce t.Helper()
- varcheck # unused global var and const
- wastedassign
fast: false

Expand Down
20 changes: 20 additions & 0 deletions demo-benchttp-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
request:
url: http://localhost:9999?delay=200ms

runner:
requests: 10
concurrency: 2

tests:
- name: Delay is properly applied
field: ResponseTimes.Min
predicate: GTE
target: 200ms
- name: Delays are similar
field: ResponseTimes.StdDev
predicate: LT
target: 50ms
- name: 100% availability
field: RequestFailureCount
predicate: EQ
target: 0
18 changes: 12 additions & 6 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ import (
)

type Server struct {
port string
port string
ignoreStdin bool

requestCount int64
}

func New(port string) *Server {
return &Server{port: port}
func New(port string, ignoreStdin bool) *Server {
return &Server{
port: port,
ignoreStdin: ignoreStdin,
}
}

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
Expand All @@ -26,18 +30,20 @@ func (s *Server) ListenAndServe() error {
addr := "localhost:" + s.port
fmt.Printf("http://%s\n", addr)
go s.listenStdin()
return http.ListenAndServe(addr, s)
return http.ListenAndServe(addr, s) //nolint:gosec // local use only
}

func (s *Server) listenStdin() {
reader := bufio.NewReader(os.Stdin)
if s.ignoreStdin {
return
}

reader := bufio.NewReader(os.Stdin)
for {
line, _, err := reader.ReadLine()
if err != nil {
fmt.Println(err) // non critical
}

if string(line) == "debug" {
fmt.Printf("Total requests: %d\n", s.requestCount)
}
Expand Down
15 changes: 0 additions & 15 deletions internal/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,6 @@ func TestHandleRequest(t *testing.T) {
Run(t)
})

t.Run("request with fib param", func(t *testing.T) {
const (
fib = 35
expmin = 30 * time.Millisecond
expmax = 80 * time.Millisecond
)

r := httptest.NewRequest("", fmt.Sprintf("/?fib=%d", fib), nil)

testx.HTTPHandler(s).WithRequest(r).
Response(checkStatusCode(200)).
Duration(check.Duration.InRange(expmin, expmax)).
Run(t)
})

t.Run("request without params", func(t *testing.T) {
const expmax = 3 * time.Millisecond

Expand Down
7 changes: 5 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ import (

const defaultPort = "9999"

var port = flag.String("port", defaultPort, "listening port")
var (
port = flag.String("port", defaultPort, "listening port")
ignoreStdin = flag.Bool("ignoreStdin", false, "do not listen stdin while serving")
)

func main() {
flag.Parse()
s := server.New(*port)
s := server.New(*port, *ignoreStdin)
log.Fatal(s.ListenAndServe())
}