github.com/cloudwego/hertz@v0.9.3/pkg/protocol/http1/server_timing_test.go (about)

     1  /*
     2   * Copyright 2023 CloudWeGo Authors
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package http1
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"sync"
    23  	"testing"
    24  
    25  	inStats "github.com/cloudwego/hertz/internal/stats"
    26  	"github.com/cloudwego/hertz/pkg/app"
    27  	"github.com/cloudwego/hertz/pkg/common/test/mock"
    28  	"github.com/cloudwego/hertz/pkg/common/tracer/traceinfo"
    29  )
    30  
    31  func BenchmarkServer_Serve(b *testing.B) {
    32  	server := &Server{}
    33  	server.eventStackPool = &sync.Pool{
    34  		New: func() interface{} {
    35  			return &eventStack{}
    36  		},
    37  	}
    38  	server.EnableTrace = true
    39  	reqCtx := &app.RequestContext{}
    40  	server.Core = &mockCore{
    41  		ctxPool: &sync.Pool{New: func() interface{} {
    42  			ti := traceinfo.NewTraceInfo()
    43  			ti.Stats().SetLevel(2)
    44  			reqCtx.SetTraceInfo(&mockTraceInfo{ti})
    45  			return reqCtx
    46  		}},
    47  		controller: &inStats.Controller{},
    48  	}
    49  	err := server.Serve(context.TODO(), mock.NewConn("GET /aaa HTTP/1.1\nHost: foobar.com\n\n"))
    50  	if err != nil {
    51  		fmt.Println(err.Error())
    52  	}
    53  	for i := 0; i < b.N; i++ {
    54  		server.Serve(context.TODO(), mock.NewConn("GET /aaa HTTP/1.1\nHost: foobar.com\n\n"))
    55  	}
    56  }