-
Notifications
You must be signed in to change notification settings - Fork 221
feat(pprof): Added pprof #2029
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
feat(pprof): Added pprof #2029
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| // SPDX-FileCopyrightText: 2025 The Kepler Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package server | ||
|
|
||
| import ( | ||
| "context" | ||
| "net/http" | ||
| "net/http/pprof" | ||
|
|
||
| "github.com/sustainable-computing-io/kepler/internal/service" | ||
| ) | ||
|
|
||
| type pp struct { | ||
| api APIService | ||
| } | ||
|
|
||
| var _ service.Service = (*pp)(nil) | ||
| var _ service.Initializer = (*pp)(nil) | ||
|
|
||
| func NewPprof(api APIService) *pp { | ||
| return &pp{ | ||
| api: api, | ||
| } | ||
| } | ||
|
|
||
| func (p *pp) Name() string { | ||
| return "pprof" | ||
| } | ||
|
|
||
| func (p *pp) Init(ctx context.Context) error { | ||
| return p.api.Register("/debug/pprof/", "pprof", "Profiling Data", handlers()) | ||
| } | ||
|
|
||
| func handlers() 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) | ||
|
|
||
| return mux | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| // SPDX-FileCopyrightText: 2025 The Kepler Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package server | ||
|
|
||
| import ( | ||
| "context" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/mock" | ||
| "net/http/pprof" | ||
| ) | ||
|
|
||
| // MockAPIService is an implementation of the APIService interface for testing. | ||
| type MockAPIService struct { | ||
| mock.Mock | ||
| } | ||
|
|
||
| func (m *MockAPIService) Register(path, name, description string, handler http.Handler) error { | ||
| args := m.Called(path, name, description, handler) | ||
| return args.Error(0) | ||
| } | ||
|
|
||
| func (m *MockAPIService) Name() string { | ||
| return "mockApiService" | ||
| } | ||
|
|
||
| // TestNewPprof tests the NewPprof constructor. | ||
| func TestNewPprof(t *testing.T) { | ||
| api := &MockAPIService{} | ||
| p := NewPprof(api) | ||
|
|
||
| assert.NotNil(t, p, "NewPprof should return a non-nil pointer") | ||
| assert.Equal(t, api, p.api, "NewPprof should set the api field correctly") | ||
| } | ||
|
|
||
| // TestPprofName tests the Name method. | ||
| func TestPprofName(t *testing.T) { | ||
| api := &MockAPIService{} | ||
| p := NewPprof(api) | ||
|
|
||
| name := p.Name() | ||
| assert.Equal(t, "pprof", name, "Name should return 'pprof'") | ||
| } | ||
|
|
||
| // TestPprofInit_Success tests the Init method when the API registration succeeds. | ||
| func TestPprofInit_Success(t *testing.T) { | ||
| api := &MockAPIService{} | ||
| p := NewPprof(api) | ||
|
|
||
| // Set up mock expectation | ||
| api.On("Register", "/debug/pprof/", "pprof", "Profiling Data", mock.AnythingOfType("*http.ServeMux")).Return(nil) | ||
|
|
||
| err := p.Init(context.Background()) | ||
| assert.NoError(t, err, "Init should not return an error when registration succeeds") | ||
| api.AssertExpectations(t) | ||
| } | ||
|
|
||
| // TestPprofInit_Failure tests the Init method when the API registration fails. | ||
| func TestPprofInit_Failure(t *testing.T) { | ||
| api := &MockAPIService{} | ||
| p := NewPprof(api) | ||
|
|
||
| // Set up mock expectation | ||
| expectedErr := assert.AnError | ||
| api.On("Register", "/debug/pprof/", "pprof", "Profiling Data", mock.AnythingOfType("*http.ServeMux")).Return(expectedErr) | ||
|
|
||
| err := p.Init(context.Background()) | ||
| assert.Error(t, err, "Init should return an error when registration fails") | ||
| assert.Equal(t, expectedErr, err, "Init should return the expected error") | ||
| api.AssertExpectations(t) | ||
| } | ||
|
|
||
| // TestPprofHandlers tests the handlers function to ensure it registers the correct pprof endpoints. | ||
| func TestPprofHandlers(t *testing.T) { | ||
| handler := handlers() | ||
| mux, ok := handler.(*http.ServeMux) | ||
| assert.True(t, ok, "handlers should return an http.ServeMux") | ||
|
|
||
| // Test cases for each pprof endpoint | ||
| tests := []struct { | ||
| path string | ||
| handlerFunc http.HandlerFunc | ||
| }{ | ||
| {"/debug/pprof/", pprof.Index}, | ||
| {"/debug/pprof/cmdline", pprof.Cmdline}, | ||
| {"/debug/pprof/profile", pprof.Profile}, | ||
| {"/debug/pprof/symbol", pprof.Symbol}, | ||
| {"/debug/pprof/trace", pprof.Trace}, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.path, func(t *testing.T) { | ||
| req := httptest.NewRequest("GET", tt.path, nil) | ||
| rr := httptest.NewRecorder() | ||
| mux.ServeHTTP(rr, req) | ||
| assert.NotEqual(t, http.StatusNotFound, rr.Code, "Handler for %s should be registered", tt.path) | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
question: What do you think about (separate PR / task)