# Benchmark

* 新增檔案 名稱必須要以"\_test"為結尾  ex:main.test.go
* 需要可以運行
  * go test -bench //要加上 `-bench=.`，這樣才會跑 benchmark 部分，否則只有跑測試程式
  * go test -v -bench=. -run=none -benchmem .
* b.XXX Timer是於效能測試的計時器可避免for迴圈之外的程式碼干擾到效能測試的計算。

```

func BenchmarkToUserBrief(b *testing.B) {
	b.ResetTimer()
	user2 := UserBrief{}
	for i := 0; i < b.N; i++ {
		_ = toUserBrief(user1, &user2)
	}
	b.StopTimer()
	//fmt.Printf("user2 Type:%T V(%v) Name:%s \n", user2, user2, user2.Name)
}

```

#### ref

| ref.                                                                                |
| ----------------------------------------------------------------------------------- |
| [如何在 Go 語言內寫效能測試](https://blog.wu-boy.com/2018/06/how-to-write-benchmark-in-go/)    |
| [\[Golang\]效能測試(Benchmark)簡介-心智圖總結](https://ithelp.ithome.com.tw/articles/10254224) |
