单元测试 goroutine 数 与性能 CPU+1
1、
并发下载图片的goroutine数为CPU数加1
package goroutin import ( "fmt" "io" "net/http" "os" "sync" ) func dlImg(threadPool, batchNum int) { var wg sync.WaitGroup URL := "http://intranet/images/a.png" s := func(i interface{}) string { return fmt.Sprint(i) } dir := "img" + s(threadPool) + "-" + s(batchNum) err := os.Mkdir(dir, 0777) if err != nil { logHelper.Error(err) } for j := 0; j < threadPool; j++ { wg.Add(1) go func() { for i := 0; i < batchNum; i++ { resp, err := http.Get(URL) if err != nil { logHelper.Error(err) continue } defer resp.Body.Close() bs, err := io.ReadAll(resp.Body) if err != nil { logHelper.Error(err) continue } name := dir + "/" + s(i) + ".png" os.WriteFile(name, bs, 0777) } wg.Done() }() } wg.Wait() }
package goroutin import "testing" func Test_dlImg(t *testing.T) { type args struct { threadPool int batchNum int } tests := []struct { name string args args }{ // TODO: Add test cases. {name: "10-128", args: args{threadPool: 10, batchNum: 128}}, {name: "3-128", args: args{threadPool: 3, batchNum: 128}}, {name: "2-128", args: args{threadPool: 2, batchNum: 128}}, {name: "1-128", args: args{threadPool: 1, batchNum: 128}}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { dlImg(tt.args.threadPool, tt.args.batchNum) }) } }
测试结果
Running tool: C:\Program Files\Go\bin\go.exe test -timeout 30s -run ^Test_dlImg$ goroutin
=== RUN Test_dlImg
=== RUN Test_dlImg/10-128
=== RUN Test_dlImg/3-128
=== RUN Test_dlImg/2-128
=== RUN Test_dlImg/1-128
--- PASS: Test_dlImg (1.79s)
--- PASS: Test_dlImg/10-128 (0.65s)
--- PASS: Test_dlImg/3-128 (0.37s)
--- PASS: Test_dlImg/2-128 (0.51s)
--- PASS: Test_dlImg/1-128 (0.26s)
PASS
ok goroutin 2.010s
internet Img
--- PASS: Test_dlImg (16.33s)
--- PASS: Test_dlImg/10-128 (9.47s)
--- PASS: Test_dlImg/3-128 (2.94s)
--- PASS: Test_dlImg/2-128 (2.58s)
--- PASS: Test_dlImg/1-128 (1.34s)
PASS
--- PASS: Test_dlImg (17.33s)
--- PASS: Test_dlImg/10-128 (9.27s)
--- PASS: Test_dlImg/3-128 (3.22s)
--- PASS: Test_dlImg/2-128 (2.35s)
--- PASS: Test_dlImg/1-128 (2.48s)
PASS
--- PASS: Test_dlImg (19.78s)
--- PASS: Test_dlImg/10-128 (11.25s)
--- PASS: Test_dlImg/3-128 (3.38s)
--- PASS: Test_dlImg/2-128 (2.58s)
--- PASS: Test_dlImg/1-128 (2.56s)
PASS
--- PASS: Test_dlImg (19.22s)
--- PASS: Test_dlImg/10-256 (6.60s)
--- PASS: Test_dlImg/3-256 (4.26s)
--- PASS: Test_dlImg/2-256 (4.27s)
--- PASS: Test_dlImg/1-256 (4.09s)
PASS
intranet Img
--- PASS: Test_dlImg (5.10s)
--- PASS: Test_dlImg/10-512 (2.38s)
--- PASS: Test_dlImg/3-512 (0.97s)
--- PASS: Test_dlImg/2-512 (0.85s)
--- PASS: Test_dlImg/1-512 (0.91s)
PASS
ok goroutin 5.329s
--- PASS: Test_dlImg (9.60s)
--- PASS: Test_dlImg/10-1024 (3.25s)
--- PASS: Test_dlImg/3-1024 (2.92s)
--- PASS: Test_dlImg/2-1024 (1.85s)
--- PASS: Test_dlImg/1-1024 (1.57s)
PASS
--- PASS: Test_dlImg (24.36s)
--- PASS: Test_dlImg/10-2048 (10.53s)
--- PASS: Test_dlImg/3-2048 (5.27s)
--- PASS: Test_dlImg/2-2048 (4.60s)
--- PASS: Test_dlImg/1-2048 (3.96s)
PASS
--- PASS: Test_dlImg (20.01s)
--- PASS: Test_dlImg/3-4096 (6.90s)
--- PASS: Test_dlImg/2-4096 (6.43s)
--- PASS: Test_dlImg/1-4096 (6.69s)
PASS