Skip to content

Commit

Permalink
Merge pull request #5 from felixge/gh-3-fix-http
Browse files Browse the repository at this point in the history
fix: ServeHTTP was broken after refactor
  • Loading branch information
felixge authored Sep 21, 2022
2 parents 857467a + ef92e12 commit f46702d
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 107 deletions.
48 changes: 48 additions & 0 deletions fgtrace.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package fgtrace
import (
"bytes"
"errors"
"fmt"
"io"
"net/http"
"runtime"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -96,7 +98,53 @@ func (c Config) Trace() *Trace {
func (c Config) ServeHTTP(w http.ResponseWriter, r *http.Request) {
c = c.WithDefaults()
c.Dst = Writer(w)

params := []struct {
Name string
Fn func(val string) error
}{
{
Name: "seconds",
Fn: func(val string) error {
seconds, err := strconv.ParseFloat(val, 64)
if err != nil {
return err
} else if seconds <= 0 {
return errors.New("invalid value")
}
c.HTTPDuration = time.Duration(float64(time.Second) * seconds)
return nil
},
},
{
Name: "hz",
Fn: func(val string) error {
hz, err := strconv.Atoi(val)
if err != nil {
return err
} else if hz <= 0 {
return errors.New("invalid value")
}
c.Hz = hz
return nil
},
},
}

for _, p := range params {
val := r.URL.Query().Get(p.Name)
if val == "" {
continue
} else if err := p.Fn(val); err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "bad %s: %q: %s\n", p.Name, val, err)
return
}
}

defer c.Trace().Stop()
time.Sleep(c.HTTPDuration)

}

// File is a helper for Config.Dst that returns an io.WriteCloser that creates
Expand Down
29 changes: 29 additions & 0 deletions fgtrace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"math"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
Expand Down Expand Up @@ -182,6 +183,34 @@ func TestConfig(t *testing.T) {
})
})
})

t.Run("ServeHTTP", func(t *testing.T) {
t.Run("seconds", func(t *testing.T) {
for _, duration := range []time.Duration{
100 * time.Millisecond,
200 * time.Millisecond,
300 * time.Millisecond,
} {
rr := httptest.NewRecorder()
r := httptest.NewRequest("GET", fmt.Sprintf("/?seconds=%f", duration.Seconds()), nil)
start := time.Now()
Config{}.ServeHTTP(rr, r)
dt := time.Since(start)
require.InDelta(t, duration, dt, float64(25*time.Millisecond))
}
})

t.Run("hz", func(t *testing.T) {
hz := 123
rr := httptest.NewRecorder()
r := httptest.NewRequest("GET", fmt.Sprintf("/?hz=%d&seconds=0.1", hz), nil)
Config{}.ServeHTTP(rr, r)
data, err := internal.Unmarshal(rr.Body.Bytes())
require.NoError(t, err)
require.Equal(t, hz, data.MetaHz())
})

})
}

// workloadSimulator calls workloadA followed by workloadB in a loop. Each call
Expand Down
74 changes: 0 additions & 74 deletions handler.go

This file was deleted.

33 changes: 0 additions & 33 deletions handler_test.go

This file was deleted.

0 comments on commit f46702d

Please # to comment.