<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                [TOC] ## 概述 ``` Benchmark_Division-4 ``` ### 參數 ``` -bench 指定為壓測 -bench=. 執行所有壓測 -bench='^BenchmarkFoo$'用正則指定類型 ``` ``` -cpu: 設置邏輯 CPU 的最大個數,多個用逗號隔開,如:-cpu=2 -cpu=2,4 -benchtime: 設置執行時間為5s,默認1s,如 -benchtime=5s -benchmem: 提供每次操作分配內存的次數,以及每次操作分配的字節數,如: -benchmem -count: 設置壓測執行次數 -count=5 -cpuprofile: 生成profile 文件,用于性能追蹤, -cpuprofile=cpu.profile -memprofil: 輸出 heap 的 pprof 信息文件。-memprofile mem.prof -blockprofile 阻塞分析,記錄 goroutine 阻塞等待同步(包括定時器通道)的位置 -block.prof -mutexprofile 互斥鎖分析,報告互斥鎖的競爭情況 -mutexprofile=mutex.prof ``` ## 實例 ### hello world <details> <summary>gotest.go</summary> ``` package main import "errors" func Division(a, b float64) (float64, error) { if b == 0 { return 0, errors.New("除數不能為0") } return a / b, nil } ``` </details> <br/> <details> <summary>gotest_bench_test.go</summary> ``` package main import "testing" func Benchmark_Division(b *testing.B) { for i := 0; i < b.N; i++ { //use b.N for looping Division(4, 5) } } ``` </details> <br/> ``` > go test -bench=Division 或 go test -bench=Benchmark_Division goos: windows goarch: amd64 pkg: bigant/test Benchmark_Division Benchmark_Division-4 1000000000 0.321 ns/op PASS ok bigant/test 0.558s ``` ### `Benchmark_xx-4` 含義 這里的-4中的4 表示最大 P 數量,最大 P 數量相當于可以同時運行 goroutine 的邏輯 CPU 的最大個數。不是實際cpu數目. 對應 golang 就是 GOMAXPROCS 的值。這個你可以自行設置,可以通過調用 runtime.GOMAXPROCS 函數改變最大P數量,也可以在命令行 go test 加入 -cpu=2 ### 重新設置壓測開始時間,用于壓測前準備 ``` func Benchmark_TimeConsumingFunction(b *testing.B) { b.StopTimer() //調用該函數停止壓力測試的時間計數 //初始化工作 b.StartTimer() //重新開始時間 for i := 0; i < b.N; i++ { Division(4, 5) } } ``` ### 壓測數據進行 pprof 生成可執行文件和 cpu.profile 文件 ``` go test -bench=. -cpuprofile=cpu.profile -bench=. "."為批量壓測,也可指定名稱,參考普通壓測 ``` 讀取 cpu.profile 文件 `go tool pprof test.test.exe cpu.profile`
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看