diff --git a/.gitignore b/.gitignore index 040b55ac..bb3fe209 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ debug debug.test main +*.qtr diff --git a/example/main.go b/example/main.go index 1fe59b56..007fe77d 100644 --- a/example/main.go +++ b/example/main.go @@ -10,16 +10,19 @@ import ( "log" "mime/multipart" "net/http" + "os" "strconv" "strings" "sync" _ "net/http/pprof" + "github.com/lucas-clemente/quic-go" "github.com/lucas-clemente/quic-go/http3" "github.com/lucas-clemente/quic-go/integrationtests/tools/testserver" "github.com/lucas-clemente/quic-go/internal/testdata" "github.com/lucas-clemente/quic-go/internal/utils" + "github.com/lucas-clemente/quic-go/quictrace" ) type binds []string @@ -38,8 +41,49 @@ type Size interface { Size() int64 } +var tracer quictrace.Tracer + func init() { - http.HandleFunc("/demo/tile", func(w http.ResponseWriter, r *http.Request) { + tracer = quictrace.NewTracer() +} + +func exportTraces() error { + traces := tracer.GetAllTraces() + if len(traces) != 1 { + return errors.New("expected exactly one trace") + } + for _, trace := range traces { + f, err := os.Create("trace.qtr") + if err != nil { + return err + } + if _, err := f.Write(trace); err != nil { + return err + } + f.Close() + fmt.Println("Wrote trace to", f.Name()) + } + return nil +} + +type tracingHandler struct { + handler http.Handler +} + +var _ http.Handler = &tracingHandler{} + +func (h *tracingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + h.handler.ServeHTTP(w, r) + if err := exportTraces(); err != nil { + panic(err) + } +} + +func setupHandler(www string, trace bool) http.Handler { + mux := http.NewServeMux() + + mux.Handle("/", http.FileServer(http.Dir(www))) + mux.HandleFunc("/demo/tile", func(w http.ResponseWriter, r *http.Request) { // Small 40x40 png w.Write([]byte{ 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, @@ -52,7 +96,7 @@ func init() { }) }) - http.HandleFunc("/demo/tiles", func(w http.ResponseWriter, r *http.Request) { + mux.HandleFunc("/demo/tiles", func(w http.ResponseWriter, r *http.Request) { io.WriteString(w, "
") for i := 0; i < 200; i++ { fmt.Fprintf(w, `