Skip to content

Commit

Permalink
Feat: add test
Browse files Browse the repository at this point in the history
Signed-off-by: Yin Da <yd219913@alibaba-inc.com>
  • Loading branch information
Somefive committed Mar 16, 2023
1 parent 11aec68 commit 35e3981
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 13 deletions.
35 changes: 22 additions & 13 deletions util/perf/pprof.go → util/profiling/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,43 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package perf
package profiling

import (
"encoding/json"
"net/http"
"net/http/pprof"
"time"
"runtime"

"k8s.io/klog/v2"
)

func StartProfilingServer(pprofAddr string, errChan chan error) {
// Start pprof server if enabled
// NewProfilingHandler create a profiling handler
func NewProfilingHandler() http.Handler {
mux := http.NewServeMux()
mux.HandleFunc("/debug/pprof/", pprof.Index)
mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
pprofServer := http.Server{
Addr: pprofAddr,
Handler: mux,
ReadHeaderTimeout: 2 * time.Second,
}

klog.InfoS("Starting debug HTTP server", "addr", pprofServer.Addr)
mux.HandleFunc("/mem/stat", func(writer http.ResponseWriter, request *http.Request) {
var ms runtime.MemStats
runtime.ReadMemStats(&ms)
bs, _ := json.Marshal(ms)
_, _ = writer.Write(bs)
})
mux.HandleFunc("/gc", func(writer http.ResponseWriter, request *http.Request) {
runtime.GC()
})
return mux
}

if err := pprofServer.ListenAndServe(); err != nil {
klog.Error(err, "Failed to start debug HTTP server")
// StartProfilingServer listen to the pprofAddr and export the profiling results
// If the errChan is nil, this function will panic when the listening error occurred.
func StartProfilingServer(addr string, errChan chan error) {
klog.Infof("start profiling server at %s", addr)
if err := http.ListenAndServe(addr, NewProfilingHandler()); err != nil {
klog.ErrorS(err, "failed to start debug HTTP server")
if errChan != nil {
errChan <- err
} else {
Expand Down
37 changes: 37 additions & 0 deletions util/profiling/server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
Copyright 2023 The KubeVela Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package profiling_test

import (
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/require"

"github.com/kubevela/pkg/util/profiling"
)

func TestStartProfilingServer(t *testing.T) {
fn := profiling.NewProfilingHandler()
server := httptest.NewServer(fn)
defer server.Close()
_, err := http.Get(server.URL + "/mem/stat")
require.NoError(t, err)
_, err = http.Get(server.URL + "/gc")
require.NoError(t, err)
}

0 comments on commit 35e3981

Please sign in to comment.