《Go語言從入門到精通》Go語言的測試與性能_第1頁
《Go語言從入門到精通》Go語言的測試與性能_第2頁
《Go語言從入門到精通》Go語言的測試與性能_第3頁
《Go語言從入門到精通》Go語言的測試與性能_第4頁
《Go語言從入門到精通》Go語言的測試與性能_第5頁
已閱讀5頁,還剩27頁未讀 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領

文檔簡介

Go語言的測試與性能Go語言從入門到精通了解Go語言中的功能測試01FUNCTION02PERFORMANCEtarget目標了解Go語言中的性能測試目錄導航17.1功能測試Contents功能測試

功能測試在編程開發(fā)中是指對某些具體功能進行針對性的測試以確認該功能是否被正確地實現(xiàn)當功能測試針對的是較小的代碼單位,例如包、對象類甚至是函數(shù)時,一般也稱作“單元測試”功能測試的目的功能測試主要測試功能是否正常實現(xiàn),是否存在代碼錯誤(一般常簡稱為“bug”)導致程序運行結果異常甚至崩潰,是否對于指定的輸入一定會得到預期的輸出等測試用例功能測試一般會針對某個功能設計一個或多個“測試用例”(testcase或usecase)

每個測試用例中有指定的輸入和預期的輸出

執(zhí)行測試的過程:依次(順序)或并行執(zhí)行每個測試用例,并記錄結果,最終形成完整的測試報告Go語言中測試代碼的寫法每個包中可以有專門用于測試的代碼文件,這些文件必須以“_test”作為文件名的結尾在該包的目錄下使用“gotest”命令時,會運行該目錄下所有符合條件的測試代碼文件進行測試

每個測試代碼文件中不能有主函數(shù),所有測試用例的函數(shù)需要以“Test”開始,并且后面的第一個字符不能是小寫字符測試代碼示例:tools_test.gopackage

tools

import

(

"testing")

func

TestGenerateRandomStringX(t

*testing.T)

{

rs

:=

GenerateRandomStringX(5,

8,

true,

true,

true,

false,

false,

false)

t.Logf("隨機字符串:%v",

rs)}測試效果用于測試的錯誤代碼//

StringToInt

轉(zhuǎn)換字符串為整數(shù)func

StringToInt(strA

string)

(int,

error)

{

nT,

errT

:=

strconv.ParseInt(strA,

0,

0)

if

errT

!=

nil

{

return

0,

errT

}

return

int(nT),

nil}功能測試不通過的情況func

TestStrToInt(t

*testing.T)

{

n1,

errT

:=

StringToInt("12")

if

errT

==

nil

&&

n1

!=

12

{

t.Logf("測試失?。簄1為%v(預期值:%v)",

n1,

12)

t.Fail()

}

n2,

errT

:=

StringToInt("012")

if

errT

==

nil

&&

n2

!=

12

{

t.Logf("測試失?。簄2為%v(預期值:%v)",

n2,

12)

t.Fail()

}

n3,

errT

:=

StringToInt("ABZ")

if

errT

==

nil

{

t.Logf("測試失?。篹rrT為nil(預期應不為nil),n3為%v",

n3)

t.Fail()

}}功能測試不通過的效果C:\goprjs\src\tools>gotest-v===RUNTestGenerateRandomStringX---PASS:TestGenerateRandomStringX(0.00s)tools_test.go:10:隨機字符串:94mxd8===RUNTestStrToInt---FAIL:TestStrToInt(0.00s)tools_test.go:25:測試失?。簄2為10(預期值:12)FAILexitstatus1FAILtools0.045s立即中止測試用例函數(shù)func

TestStrToInt(t

*testing.T)

{

n1,

errT

:=

StringToInt("12")

if

errT

==

nil

&&

n1

!=

12

{

t.Logf("測試失?。簄1為%v(預期值:%v)",

n1,

12)

t.Fail()

}

n2,

errT

:=

StringToInt("012")

if

errT

==

nil

&&

n2

!=

12

{

t.Logf("測試失敗:n2為%v(預期值:%v)",

n2,

12)

t.FailNow()

}

n3,

errT

:=

StringToInt("ABZ")

if

errT

==

nil

{

t.Logf("測試失?。篹rrT為nil(預期應不為nil),n3為%v",

n3)

t.Fail()

}}立即中止測試用例函數(shù)-2func

TestStrToInt(t

*testing.T)

{

n1,

errT

:=

StringToInt("12")

if

errT

==

nil

&&

n1

!=

12

{

t.Errorf("測試失敗:n1為%v(預期值:%v)",

n1,

12)

}

n2,

errT

:=

StringToInt("012")

if

errT

==

nil

&&

n2

!=

12

{

t.Fatalf("測試失?。簄2為%v(預期值:%v)",

n2,

12)

}

n3,

errT

:=

StringToInt("ABZ")

if

errT

==

nil

{

t.Errorf("測試失敗:errT為nil(預期應不為nil),n3為%v",

n3)

}

}跳過測試用例T.SkipNow函數(shù)相當于先調(diào)用T.Log再調(diào)用T.SkipNow函數(shù)T.Skipf函數(shù),相當于先調(diào)用T.Logf再調(diào)用T.SkipNow函數(shù)進行并發(fā)測試-1func

TestStrToIntParallel(t

*testing.T)

{

for

i

:=

0;

i

<

1000000;

i++

{

n1

:=

rand.Intn(500)

n2,

errT

:=

StringToInt(IntToString(n1))

if

errT

!=

nil

{

t.Fatalf("測試失?。簄1的值為%v,n2的值為%v,

errT為%v",

n1,

n2,

errT)

}

if

n1

!=

n2

{

t.Fatalf("測試失?。簄1的值為%v,n2的值為%v",

n1,

n2)

}

}}進行并發(fā)測試-2func

Test001(t

*testing.T)

{

t.Parallel()

for

i

:=

0;

i

<

5;

i++

{

t.Run("并發(fā)測試",

TestStrToIntParallel)

}}

func

Test002(t

*testing.T)

{

t.Parallel()

for

i

:=

0;

i

<

5;

i++

{

t.Run("并發(fā)測試",

TestStrToIntParallel)

}}T.Parallel與T.Run結合使用在測試用例函數(shù)中調(diào)用T.Parallel函數(shù)的作用是聲明該測試用例函數(shù)可以與其他測試用例函數(shù)并發(fā)執(zhí)行

T.Run函數(shù)可以調(diào)用另一個測試用例函數(shù)作為它的子測試用例,并等待該子測試用例執(zhí)行完畢再繼續(xù)執(zhí)行其他代碼指定運行的測試用例gotestgotest-run=gotest-run=.gotest-run=Test0gotest-run=00\d目錄導航17.2性能測試Contents基本的性能測試–待測試函數(shù)//

CalPi

是使用隨機落點法計算圓周率Π值的函數(shù)//

一般來說,輸入?yún)?shù)pointCountA的值越大,計算結果越準,但耗費時間也越多func

CalPi(pointCountA

int)

float64

{

inCircleCount

:=

0

var

x,

y

float64

var

Pi

float64

for

i

:=

0;

i

<

pointCountA;

i++

{

x

=

rand.Float64()

y

=

rand.Float64()

if

x*x+y*y

<

1

{

inCircleCount++

}

}

Pi

=

(4.0

*

float64(inCircleCount))

/

float64(pointCountA)

return

Pi}基本的性能測試–進行測試package

tools

import

(

"testing")

func

BenchmarkCalPi(b

*testing.B)

{

for

i

:=

1;

i

<

b.N;

i++

{

rs

:=

CalPi(i)

b.Logf("Pi值:%v",

rs)

}}基本的性能測試–測試效果C:\goprjs\src\tools>gotest-v-bench=.-run=Benchmarkgoos:windowsgoarch:amd64pkg:toolsBenchmarkCalPi-810000226170ns/op---BENCH:BenchmarkCalPi-8benchmark_test.go:10:Pi值:0benchmark_test.go:10:Pi值:4benchmark_test.go:10:Pi值:4benchmark_test.go:10:Pi值:4benchmark_test.go:10:Pi值:2.4benchmark_test.go:10:Pi值:4benchmark_test.go:10:Pi值:4benchmark_test.go:10:Pi值:2benchmark_test.go:10:Pi值:3.5555555555555554benchmark_test.go:10:Pi值:3.6...[outputtruncated]PASSoktools2.314s根據(jù)性能測試結果進行優(yōu)化和比對//

CalPiX

是使用隨機落點法計算圓周率Π值的函數(shù)//

與CalPi唯一的不同是使用了更快的隨機數(shù)發(fā)生器//

但有可能不是并發(fā)安全的,建議僅在單線程中使用func

CalPiX(pointCountA

int)

float64

{

inCircleCount

:=

0

var

x,

y

float64

var

Pi

float64

r

:=

tk.NewRandomGenerator()

for

i

:=

0;

i

<

pointCountA;

i++

{

x

=

r.Float64()

y

=

r.Float64()

if

x*x+y*y

<

1

{

inCircleCount++

}

}

Pi

=

(4.0

*

float64(inCircleCount))

/

float64(pointCountA)

return

Pi}效果C:\goprjs\src\tools>gotest-v-bench=.-run=Benchmarkgoos:windowsgoarch:amd64pkg:toolsBenchmarkCalPi-810000223870ns/op---BENCH:BenchmarkCalPi-8benchmark_test.go:10:Pi值:0benchmark_test.go:10:Pi值:4benchmark_test.go:10:Pi值:2.4benchmark_test.go:10:Pi值:2benchmark_test.go:10:Pi值:3.5555555555555554benchmark_test.go:10:Pi值:3.6...[outputtruncated]BenchmarkCalPiX-830000132331ns/op---BENCH:BenchmarkCalPiX-8benchmark_test.go:17:Pi值:0benchmark_test.go:17:Pi值:1benchmark_test.go:17:Pi值:1.6benchmark_test.go:17:Pi值:2benchmark_test.go:17:Pi值:1.7142857142857142benchmark_test.go:17:Pi值:2.2222222222222223...[outputtruncated]PASSoktools6.716s指定性能測試的時長gotest-v-bench=.-run=Benchmark-benchtime=5s手動控制測試計時testing.B對象的ResetTimer、StartTimer、StopTimer函數(shù)測試內(nèi)存分配的情況//

Fibonacci

計算斐波那契數(shù)列func

Fibonacci(c

int64)

int64

{

if

c

<

2

{

return

c

}

return

Fibonacci(c-2)

+

Fibonacci(c-1)}func

BenchmarkFibo38(b

*testing.B)

{

rs

:=

Fibonacci(38)

b.Logf("斐波那契38結果值:%v",

rs)}

func

BenchmarkFibo48(b

*testing.B)

{

rs

:=

Fibonacci(48)

b.Logf("斐波那契48結果值:%v",

rs)}測試效果C:\goprjs\src\tools>gotest-v-bench=Fibo-run=Benchmark-benchmemgoos:windowsgoarch:amd64pkg:toolsBenchmarkFibo38-810000000000.25ns/op0B/op0allocs/op---BENCH:BenchmarkFibo38-8benchmark_test.go:23:斐波那契38結果值:39088169benchmark_test.go:23:斐波那契38結果值:39088169benchmark_test.g

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論