Go language test (test), Performance test (Benchmark) Learning notes

Source: Internet
Author: User
Tags benchmark
This is a creation in Article, where the information may have evolved or changed.

Brief introduction

    • Test

      Package testing provides support for automated testing of Go packages. It is intended to being used in concert with the ' Go Test ' command, which automates execution of any function of the form.


TestingThe package provides automated test support for GO packages. This is and Go TestCommand echoes the function, Go TestThe command executes automatically so that it conforms to the format
*testing
The function.
    • Benchmark

Functions of the form

*testing.B)

is considered benchmarks, and was executed by the ' Go Test ' command when >its-bench flag is provided. Benchmarks is run sequentially.

A function conforming to the format (see above) is considered a performance test program when carrying -bench= "." (Parameters must be there!) ) to execute the *go Test command, the performance test program is executed sequentially.

Simple instance Code

Lib.go

package libfuncintintint) {    z = x + y    return}typestruct {    int ;}func (this * ForTest) Loops() {    for i:=0 ; i  < 10000 ; i++ {        this.num++    }}

Lib_test.go

 PackageLibImport "Testing"typeAddArraystruct{Resultint; Add_1int; Add_2int;}funcBenchmarkloops (b *testing. B) {varTest fortest ptr: = &test//Must be cycled B.N times. This number B.N will be adjusted in the run so that the appropriate time consumption can eventually be achieved. Convenient to calculate reasonable data. (Lest the data be all 0)     forI=0; I&LT;B.N; i++ {ptr. Loops ()}}//test concurrency EfficiencyfuncBenchmarkloopsparallel (b *testing. B) {B.runparallel (func(Pb *testing. PB) {varTest fortest ptr: = &test forPb. Next () {ptr. Loops ()}}}funcTestadd (T * testing. T) {varTest_data =[3] AddArray {{2,1,1},        {5,2,3},        {4,2,2},    } for_, V: =RangeTest_data {ifV.result! = Add (v.add_1, v.add_2) {T.errorf ("Add (%d,%d)! =%d \ n", V.add_1, V.add_2, V.result); }    }}

Execute go test-bench= "." After the result:

// 表示测试全部通过>PASS                       // Benchmark 名字 - CPU            循环次数             平均每次执行时间 BenchmarkLoops-2                  100000             20628 ns/op     BenchmarkLoopsParallel-2          100000             10412 ns/op   //      哪个目录下执行go test         累计耗时ok      swap/lib                   2.279s

Go Testing Library testing. T and testing. B Introduction

Testing. T

    • Failed to determine interface
      • FailFailed to continue
      • FailNowFailure termination
    • Print Information interface
      • LogData flow ( cout similar)
      • LogfFormat ( printf similar)
    • SkipNowSkip Current Test
    • SkipedDetect if skipping

Integrated Interface Generation :

    • Error / ErrorfReport error continue [ Log / Logf + Fail ]
    • Fatel / FatelfReport error terminated [ Log / Logf + FailNow ]
    • Skip / SkipfReport and Skip [ Log / Logf + SkipNow ]

Testing. B

    • First, testing. B owns testing. all interfaces of T.
    • SetBytes( i uint64)Count the memory consumption if you need to.
    • SetParallelism(p int)Develop parallel numbers.
    • StartTimer / StopTimer / ResertTimerOperation Timer

Testing. PB

    • Next()Interface. Determine if the loop continues
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.