golang unit testing and performance analysis

Here's a brief introduction to how go writes unit tests, performance analysis of code, and code coverage. Usually we have written some methods, ...

Here's a brief introduction to how go writes unit tests, performance analysis of code, and code coverage.
Usually we have written some methods, and want to test the main function in the main package to call the function we have written, so the test is not very professional.Goang's own test tool is very useful. We can either write the test code manually or create it first by using shortcut keys in the ide. Let's use the following examples to illustrate code testing, performance pressure testing, performance analysis, and so on.

Example demo.go

package demo import "time" //String Length func strCount(str string) int { var l int l = len([]rune(str)) return l } //Calculating function func algorithm(a int,b int) int { time.Sleep(time.Millisecond*100) return a+b } type Demo struct { } // Notes // e.g. t.Test1(123) func(d *Demo) Test1(v int)int{ return v+v }

1. Test results

We need to know what parameters the test function passes and what parameters the value should return.Write test cases as needed.The file name ends with _test.go and the functions in the file begin with Test.Using the methods below testing.T, use the relevant methods for this package.
demo_test.go

1. Tabular Data Test
func Test_strCount(t *testing.T) { type args struct { str string } tests := []struct { name string args args want int }{ // TODO: Add test cases. {"name1", args{"Adu zhenxun"}, 9}, {"name2", args{"ado"}, 3}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if got := strCount(tt.args.str); got != tt.want { t.Errorf("strCount() = %v, want %v", got, tt.want) } }) } }

Run results in IDE

=== RUN Test_strCount --- PASS: Test_strCount (0.00s) === RUN Test_strCount/name1 --- PASS: Test_strCount/name1 (0.00s) === RUN Test_strCount/name2 --- PASS: Test_strCount/name2 (0.00s) PASS

If you don't want to write, you can also use the golang ide development tools to generate this code directly.Place the cursor on the method you want to test. mac press command+n, windown should be ctrl+n. The following picture will appear:

2. Simple example testing
// Test examples func ExampleDemo_Test1() { d := Demo{} fmt.Println(d.Test1(11)) fmt.Println(d.Test1(22)) // Output: // 11 // 44 }

Run results in IDE

=== RUN ExampleDemo_Test1 --- FAIL: ExampleDemo_Test1 (0.00s) got: 22 44 want: 11 44

Note: You can run go test directly under the named line to see the results, and other related commands can view go help test

Code Coverage

Select'...when running in golang's ide.with Coverage displays code coverage
Or execute the following two commands from the command line to view coverage in html

go test -coverprofile=c.out

go tool cover -html=c.out

3. Testing Performance

Performance testing We need to start the function with Benchmark, using the method below the testing.B structure

demo_test.go

package demo import ( "testing" ) func Benchmark_algorithm(b *testing.B) { args1 := 1 args2 := 2 want := 3 b.Run("name1", func(bb *testing.B) { for i := 0; i < 10; i++ { if got := algorithm(args1, args2); got != want { b.Errorf("algorithm() = %v, want %v", got, want) } } }) }

Run Results
You can use the go test-bench. -count=10 command to specify 10 executions

➜ test git:(master) ✗ go test -bench . -count=10 goos: darwin goarch: amd64 pkg: go-demo/Fundamentals/test Benchmark_algorithm/name1-8 1 1043681294 ns/op Benchmark_algorithm/name1-8 1 1044113890 ns/op Benchmark_algorithm/name1-8 1 1044500717 ns/op Benchmark_algorithm/name1-8 1 1044609517 ns/op Benchmark_algorithm/name1-8 1 1044535417 ns/op Benchmark_algorithm/name1-8 1 1044608038 ns/op Benchmark_algorithm/name1-8 1 1044479081 ns/op Benchmark_algorithm/name1-8 1 1044309623 ns/op Benchmark_algorithm/name1-8 1 1044603758 ns/op Benchmark_algorithm/name1-8 1 1044389802 ns/op PASS ok go-demo/Fundamentals/Test 10.456s

Because we slept in the function for 0.1 seconds.We call this function 10 times per test, and the execution results show that it is a little more than a second each time.Ten times for about 10 seconds.

4. Generate Code Performance PDF

Write a performance analysis code for strCount first, use go tool pprof to generate pdf file

func Benchmark_strCount(b *testing.B){ s:="Adu ado" for i:=0;i<25;i++{ s = s+s } //b.Logf("len(s) = %d",len(s)) b.ResetTimer() //String generation time is removed here for i:=0;i<b.N;i++{ strCount(s) } }

command

  1. First use the command go test-bench. -cpuprofile cpu.out to output the file cpu.out
  2. Run go tool pprof cpu.out A again to prompt for web input

Note: If output is not possible, install graphviz first
The official graphviz website http://www.graphviz.org/download

results of enforcement

➜ test git:(master) ✗ go test -bench . -cpuprofile cpu.out goos: darwin goarch: amd64 pkg: go-demo/Fundamentals/test Benchmark_strCount-8 2 705669566 ns/op PASS ok go-demo/Fundamentals/Test 2.824s ➜ test git:(master) ✗ go tool pprof cpu.out Type: cpu Time: Jan 31, 2020 at 2:15am (CST) Duration: 2.77s, Total samples = 2.64s (95.28%) Entering interactive mode (type "help" for commands, "o" for options) (pprof) pdf Generating report in profile001.pdf

Looking at profile001.pdf, runtime decoderune 1.32s (50.97%) was found, indicating that we took a long time in decoderune.

Is it much easier to optimize your code at this time?

duzhenxun Published 31 original articles. Accepted 3. Visited 10,000+ Private letter follow

31 January 2020, 21:27 | Views: 1801

Add new comment

For adding a comment, please log in
or create account

0 comments