-
Notifications
You must be signed in to change notification settings - Fork 13
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
Set up a pprof server with optional mutex profiling in cmdutil #216
Merged
Merged
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ebb724d
Set up a pprof server with optional mutex profiling in cmdutil
bd669c7
Set up a ReadHeaderTimeout
850c21a
Put the method in debug package and improve it
3f6f3f6
Make PProfServer a seperate server and update the logging info
9b39c0d
Remove unnecessary changes
51a1892
Run golangci-lint
1a67670
Updated PProfServerConfig, used pprof.Handler and included Run() err…
a7b309c
Updated mutex and used pprof.Index
37fed8c
Run unit test
ad16243
Update unit test
60faef3
Considered pprofServer as nil and simplified test path for HTTP requests
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 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 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,109 @@ | ||
package debug | ||
|
||
import ( | ||
"net/http" | ||
"runtime" | ||
"testing" | ||
"time" | ||
|
||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
func TestNewPProfServer(t *testing.T) { | ||
logger := logrus.New() | ||
|
||
tests := []struct { | ||
name string | ||
config PProfServerConfig | ||
expectedAddr string | ||
expectedMutexFraction int | ||
}{ | ||
{ | ||
name: "DefaultAddr", | ||
config: PProfServerConfig{}, | ||
expectedAddr: "127.0.0.1:9998", | ||
expectedMutexFraction: defaultMutexProfileFraction, | ||
}, | ||
{ | ||
name: "CustomAddr", | ||
config: PProfServerConfig{Addr: "127.0.0.1:9090"}, | ||
expectedAddr: "127.0.0.1:9090", | ||
expectedMutexFraction: defaultMutexProfileFraction, | ||
}, | ||
{ | ||
name: "CustomMutexProfileFraction", | ||
config: PProfServerConfig{MutexProfileFraction: 5}, | ||
expectedAddr: "127.0.0.1:9998", | ||
expectedMutexFraction: 5, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
server := NewPProfServer(tt.config, logger) | ||
|
||
// Check server address | ||
if server.addr != tt.expectedAddr { | ||
t.Errorf("NewPProfServer() addr = %v, want %v", server.addr, tt.expectedAddr) | ||
} | ||
|
||
// Start the server | ||
go func() { | ||
if err := server.Run(); err != nil { | ||
t.Errorf("NewPProfServer() run error = %v", err) | ||
} | ||
}() | ||
|
||
// Give the server a moment to start | ||
time.Sleep(100 * time.Millisecond) | ||
|
||
// Check mutex profile fraction | ||
if got := runtime.SetMutexProfileFraction(0); got != tt.expectedMutexFraction { | ||
t.Errorf("runtime.SetMutexProfileFraction() = %v, want %v", got, tt.expectedMutexFraction) | ||
} | ||
runtime.SetMutexProfileFraction(tt.expectedMutexFraction) // Reset to the expected value | ||
|
||
urls := []string{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think verifying the paths is probably overkill since that is functionality within |
||
"http://" + server.addr + "/debug/pprof/", | ||
"http://" + server.addr + "/debug/pprof/heap", | ||
"http://" + server.addr + "/debug/pprof/goroutine", | ||
"http://" + server.addr + "/debug/pprof/threadcreate", | ||
"http://" + server.addr + "/debug/pprof/block", | ||
"http://" + server.addr + "/debug/pprof/mutex", | ||
} | ||
|
||
// Perform HTTP GET requests to ensure the server is running and all handlers respond correctly | ||
client := &http.Client{} | ||
for _, url := range urls { | ||
t.Run("GET "+url, func(t *testing.T) { | ||
req, err := http.NewRequest("GET", url, nil) | ||
if err != nil { | ||
t.Errorf("http.NewRequest(%s) error = %v", url, err) | ||
} | ||
|
||
resp, err := client.Do(req) | ||
if err != nil { | ||
t.Errorf("http.Client.Do() error = %v", err) | ||
} | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
t.Errorf("http.Client.Do() status = %v, want %v", resp.StatusCode, http.StatusOK) | ||
} | ||
|
||
resp.Body.Close() | ||
}) | ||
} | ||
|
||
// Stop the server | ||
server.Stop(nil) | ||
|
||
// Ensure the server is stopped | ||
select { | ||
case <-server.done: | ||
// success | ||
case <-time.After(1 * time.Second): | ||
t.Fatal("server did not stop in time") | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.
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.
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.
If it is nil, I think we can return an error.