An Interest In:
Web News this Week
- April 23, 2024
- April 22, 2024
- April 21, 2024
- April 20, 2024
- April 19, 2024
- April 18, 2024
- April 17, 2024
Golang Profiling
Golang Profiling
Profiling is a form of analyzing the program for optimizable code or functions. In software engineering, it is an essential task since optimization is a key factor when developing an application.
Avoiding memory leaks and optimizing for better performance is almost always a target for enterprise-level software.
Profiling is an important task that cannot be avoided for larger applications. Profiling helps us understand CPU and memory intensive code and helps us write better code for optimization
To create any profile first we need to have a test file. Here we are going to use the Fibonacci function to see profiles of it
// main.gopackage mainfunc Fib2(n int) uint64 { if n == 0 { return 0 } else if n == 1 { return 1 } else { return Fib2(n-1) + Fib2(n-2) }}func main() { // fmt.Println(Fib2(30)) // 832040}
Now, the test file is:
// main_test.gopackage mainimport "testing"func TestGetVal(t *testing.T) { for i := 0; i < 1000; i++ { // running it a 1000 times if Fib2(30) != 832040 { t.Error("Incorrect!") } }}
Now when we run the go test we got the below output
output:go testPassOk
It took almost 7.25s to complete. Now lets create a CPU profile. We will use this command shown below to generate a profile file.
go test -cpuprofile cpu.prof -bench .
which will return the below output
PS C:\work\latest\go-tutorial\profile> go test -cpuprofile cpu.prof -bench .PASSok profile 8.052s
Now, we will view it using the pprof tool. The command will be:
go tool pprof cpu.prof
When we run the above commands we got the below output
PS C:\work\latest\go-tutorial\profile> go tool pprof cpu.profType: cpuTime: Feb 22, 2022 at 10:53am (IST)Duration: 7.64s, Total samples = 4.86s (63.60%)Entering interactive mode (type "help" for commands, "o" for options")
Typing help will show all commands available. We will run the following command
top5 -cum
The topN function shown top N entries and the -cum flag shows the cumulative time taken
(pprof) top5 -cumShowing nodes accounting for 4700ms, 96.71% of 4860ms totalDropped 33 nodes (cum <= 24.30ms)Showing top 5 nodes out of 15 flat flat% sum% cum cum% 4700ms 96.71% 96.71% 4700ms 96.71% profile.Fib2 0 0% 96.71% 4700ms 96.71% profile.TestGetVal 0 0% 96.71% 4700ms 96.71% testing.tRunner 0 0% 96.71% 90ms 1.85% runtime.mcall 0 0% 96.71% 90ms 1.85% runtime.park_m
based on the above results we will optimize our code.
To create a memory profile we simply use this command:
go test -memprofile mem.prof -bench .
which will return the below output
PS C:\work\latest\go-tutorial\profile> go test -memprofile mem.prof -bench .PASSok profile 0.644s
We can generate both profiles (main, test) using the following command
go test -cpuprofile cpu.prof -memprofile mem.prof -bench .
which will return the below output
PS C:\work\latest\go-tutorial\profile> go test -cpuprofile cpu.prof -memprofile mem.prof -bench .PASSok profile 0.655s
Original Link: https://dev.to/krpmuruga/-golang-profiling-3j
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To