ãã®è¨äºã¯ãMerpay Tech Openness Month 2020 ã®6æ¥ç®ã®è¨äºã§ãã
ã¡ã«ãã¤ã§Backendã¨ã³ã¸ãã¢ããã¦ããæ´ç°ï¼@yoshiki_shibataï¼ã§ãããã®è¨äºã§ã¯ãGoè¨èªã®testing
ããã±ã¼ã¸ã«ç¨æããã¦ãã並ååã®æ©è½ã«ã¤ãã¦èª¬æãã¾ãã
Goè¨èªã§ã¯ããã¹ãã³ã¼ããä½æããããã®testing
ããã±ã¼ã¸ãç¨æããã¦ãã¾ããä¸è¬ã«éçºããã½ããã¦ã§ã¢ã®è¦æ¨¡ã大ãããªãã«å¾ã£ã¦ãä½æããããã¹ãã³ã¼ãã®éãå¤ããªãããã¹ã¦ã®ãã¹ããçµäºããã¾ã§ã®æéãé·ããªã£ã¦ããã¾ããç¹ã«ããã¼ã¿ãã¼ã¹ã¸ã¢ã¯ã»ã¹ãããããªãã¹ãã§ã¯ããã¼ã¿ãã¼ã¹ã¸ã®éä¿¡æéããã¹ãæéã®å¤ãå ãã¾ãã®ã§ããã¹ãã³ã¼ããé次å®è¡ããããã¯ä¸¦åå®è¡ãããã¨ã§ããã¹ãæéãç縮ã§ãã¾ãï¼å³å¯ã«ã¯ç¨èªã並è¡ãã§ãããt.Parallel()
ã¡ã½ããã®èª¬æãªã®ã§ããã®è¨äºã§ã¯ç¨èªã並åãã使ã£ã¦ãã¾ãï¼ã
ãã®è¨äºã§ã¯ã*testing.T
ã®Parallel()
ã¡ã½ããã«ã¤ãã¦è§£èª¬ãã¾ãã
è¤æ°ããã±ã¼ã¸ã®ãã¹ãã並åã«å®è¡ãã
testing
ããã±ã¼ã¸ã使ã£ããã¹ãã³ã¼ãã®å®è¡ã¯ãããã©ã«ãã§ã¯é次çã§ãããã ããé次çãªã®ã¯ãããç¹å®ã®ããã±ã¼ã¸å
ã®ãã¹ãã«å¯¾ãã¦ã§ãã
è¤æ°ã®ããã±ã¼ã¸ã®ãã¹ããæå®ããå ´åãããã±ã¼ã¸åä½ã§ãã¹ãã並åã«å®è¡ããã¾ãããã¨ãã°ãa
ããã±ã¼ã¸ã¨b
ããã±ã¼ã¸ããã£ãå ´åãa
ããã±ã¼ã¸å
ã®ãã¹ãã³ã¼ãã¯é次å®è¡ãããb
ããã±ã¼ã¸å
ã®ãã¹ãã³ã¼ããé次å®è¡ããã¾ããããããa
ããã±ã¼ã¸ã¨b
ããã±ã¼ã¸ã®ãã¹ãã¯ä¸¦åã«å®è¡ããã¾ãããã®å ´åãã©ã®ããã«ãã¦ä¸¦åã«å®è¡ããã¦ãããã次ã«èª¬æãã¾ãã
è¤æ°ã®ããã±ã¼ã¸ãæå®ããå ´åï¼ãããã¯ã./...
ã¨ãã¹ã¦ã®ããã±ã¼ã¸ãæå®ããå ´åï¼ãããã¤ã®ããã±ã¼ã¸ã並åã«ãã¹ããå®è¡ããããã¯ãgo test
ã³ãã³ãã§æå®ãã-p
ãã©ã°ï¼å®éã«ã¯ãbuild
ãã©ã°ï¼ã§æ±ºã¾ãã¾ããgo help build
ã§-p
ãã©ã°ã®èª¬æã表示ããã¨ã次ã®ããã«è¡¨ç¤ºããã¾ãã
-p n
the number of programs, such as build commands or
test binaries, that can be run in parallel.
The default is the number of CPUs available.
並åã«å®è¡å¯è½ãªbuildã³ãã³ããããã¯ãã¹ããã¤ããªã¨ãã£ãããã°ã©ã ã®æ°ãããã©ã«ãã¯å©ç¨å¯è½ãªCPUã®æ°ã
ã¤ã¾ãããã¹ãã«é¢ãã¦ã¯ã-p
ãã©ã°ã§æå®ãããå¤ã®ããã»ã¹æ°ã¾ã§ãã¹ããã¤ããªã¼ãè¤æ°ããã»ã¹ã¨ãã¦ï¼ä¸¦åã«ï¼å®è¡ãã¾ãã-p
ãã©ããæå®ããªããã°ãCPUã®æ°ããã®å¤ã¨ãªãã¾ããã¾ããã©ã®ããã±ã¼ã¸ããã¹ããããã¯èªåçã«å²ãæ¯ããã¾ããã¤ã¾ããåããã»ã¹å
ã§ã¯ãé次çã«ä¸ã¤ã®ããã±ã¼ã¸ã®ãã¹ããå®è¡ããã¦ãããã¨ã«ãªãã¾ããä»®ã«-p=1
ã¨æå®ããã¨ããã¹ããå®è¡ãã¦ããããã»ã¹ã¯ä¸ã¤ã ãã§ãã®ã§ããã¹ã¦ã®ãã¹ããããã±ã¼ã¸ãã¨ã«é次å®è¡ããããã¨ã«ãªãã¾ãã
è£è¶³ï¼
-p
ãã©ã°ã§1ãã大ããªå¤ãæå®ãã¦ãè¤æ°ã®ããã±ã¼ã¸ãæå®ï¼ãããã¯ã./...
ï¼ãã¦ãã¹ããå®è¡ãã¦ããã¹ãå®è¡ä¸ã«å¥ã®ã¿ã¼ããã«ããps
ã³ãã³ããå®è¡ããã°ãããã±ã¼ã¸ãã¨ã®ãã¹ãç¨ãã¤ããªãä½ãããªãããããã±ã¼ã¸ãã¨ã«ãã¹ããè¡ããã¦ããã®ãåããã¾ãã
-p
ãã©ã°ã§å¤§ããªå¤ãæå®ããã°ããã®æå®ããå¤ã®åæ°åã®ãã¹ãããã»ã¹ãçæããã¦ãã¹ãã®ä¸¦åæ§ã¯åä¸ãã¾ããããããããã¯è¤æ°ã®ããã±ã¼ã¸ã®ãã¹ãã並åã«å®è¡ãããã ãã§ãããåã
ã®ããã±ã¼ã¸å
ã®ãã¹ã群ã並åã«å®è¡ããããã¨ã«ã¯ãªãã¾ãããããã±ã¼ã¸å
ã®ãã¹ãã§ããç´°ããªä¸¦åæ§ãåä¸ãè¡ãããã«ã¯ãt.Parallel()
ã¡ã½ããã使ãã¾ãã
t.Parallel()
ã¡ã½ãã
*testing.T
ã«ã¯Parallel()
ã¡ã½ãããããã¾ããt.Parallel()
ã¡ã½ããã®ä½¿ãæ¹ã¯ãå°ãåããã«ããã§ãããæ£ãã使ãã«ã¯ãã¡ãã¨ç解ããå¿
è¦ãããã¾ãã
Parallel()
ã¡ã½ããã®ä»æ§ã¯ã次ã®éãã§ãã
func (t *T) Parallel()
Parallel signals that this test is to be run in parallel with (and only
with) other parallel tests. When a test is run multiple times due to use of
-test.count or -test.cpu, multiple instances of a single test never run in
parallel with each other.
Parallel
ã¯ããã®ãã¹ããä»ã®ä¸¦åã®ãã¹ãã¨ã ã並åã«å®è¡ããããã¨ãéç¥ãã¾ãã-test.count
ãããã¯-test.cpu
ã使ã£ã¦ãã¹ããè¤æ°åå®è¡ãããå ´åã«ã¯ãããåä¸ã®ãã¹ãããã®ãã¹ãã¨ä¸¦åã«åä½ãããã¨ã¯ããã¾ããã
ã§ã¯ãç°¡åãªä¾ãã説æãã¾ãã
testing
ããã±ã¼ã¸ã使ã£ããã¹ãã³ã¼ãã«ã¯ãfunc TestXXX(t *testing.T)
ã®ã·ã°ããã£ãæã¤ãããã¬ãã«ã®ãã¹ãé¢æ°ã¨ããããã¬ãã«ã®ãã¹ãé¢æ°å
ã§ãt.Run()
ãç¨ãã¦è¨è¿°ãããµããã¹ãé¢æ°ãããã¾ããã¾ãã¯ããããã¬ãã«é¢æ°ã ãã«t.Parallel()
ã¡ã½ãããå¼ã³åºããå ´åã®åä½ã説æãã¾ãã
次ã®ã³ã¼ããè¦ã¦ãã ããã
package main
import (
"fmt"
"testing"
)
func trace(name string) func() {
fmt.Printf("%s entered\n", name)
return func() {
fmt.Printf("%s returned\n", name)
}
}
func Test_Func1(t *testing.T) {
defer trace("Test_Func1")()
// ...
}
func Test_Func2(t *testing.T) {
defer trace("Test_Func2")()
t.Parallel()
// ...
}
func Test_Func3(t *testing.T) {
defer trace("Test_Func3")()
// ...
}
func Test_Func4(t *testing.T) {
defer trace("Test_Func4")()
t.Parallel()
// ...
}
func Test_Func5(t *testing.T) {
defer trace("Test_Func5")()
// ...
}
äºã¤ã®ãã¹ãé¢æ°ããããTest_Func1
ãTest_Func3
ãTest_Func5
ãæ®éã®ãã¹ãé¢æ°ã§ããTest_Func2
ã¨Test_Func4
ãt.Parallel()
ã¡ã½ãããå¼ã³åºãã¦ãã¾ããgo test
ã³ãã³ãã§å®è¡ããã¨ã次ã®ããã«å®è¡ããã¾ãã
Test_Func1
ãå®è¡ãããå¦çãå®äºãã¾ãã- 次ã«ã
Test_Func2
ã®å®è¡ã«ç§»ãã¾ãããt.Parallel()
ã¡ã½ãããå¼ã³åºããã¨ããã§ä¸æåæ¢ãã¾ãã Test_Func2
ã®å®è¡ãåæ¢ããç¶æ ã§ãTest_Func3
ãå®è¡ãããå¦çãå®äºãã¾ãã- 次ã«ã
Test_Func4
ã®å®è¡ã«ç§»ãã¾ãããt.Parallel()
ã¡ã½ãããå¼ã³åºãããã¨ãã¨ã§ä¸æåæ¢ãã¾ãã Test_Fun4
ã®å®è¡ãåæ¢ããç¶æ ã§ãTest_Func5
ãå®è¡ãããå¦çãå®äºãã¾ãã
t.Parallel()
ã¡ã½ãããå¼ã³åºãã¦ããªãTest_Func1
ãTest_Func3
ãTest_Func5
ãé ã«ãã¹ã¦å®è¡ãããã¨ãt.Parallel()
ã¡ã½ãããå¼ã³åºãã¦ããTest_Func2
ã¨Test_Func4
ã®å¦çã並åã«åéãã¦ãå¦çãå®äºãã¾ãã
å®è¡çµæã¯æ¬¡ã®éãã§ãã
=== RUN Test_Func1
Test_Func1 entered
Test_Func1 returned <- 1 ï¼å®äºï¼
--- PASS: Test_Func1 (0.00s)
=== RUN Test_Func2
Test_Func2 entered
=== PAUSE Test_Func2 <- 2 (ä¸æåæ¢ï¼
=== RUN Test_Func3
Test_Func3 entered
Test_Func3 returned <- 3 ï¼å®äºï¼
--- PASS: Test_Func3 (0.00s)
=== RUN Test_Func4
Test_Func4 entered
=== PAUSE Test_Func4 <- 4 (ä¸æåæ¢ï¼
=== RUN Test_Func5
Test_Func5 entered
Test_Func5 returned <- 5 ï¼å®äºï¼
--- PASS: Test_Func5 (0.00s)
=== CONT Test_Func2 <- å¦çãåé
Test_Func2 returned <- å®äº
=== CONT Test_Func4 <- å¦çãåé
Test_Func4 returned <- å®äº
--- PASS: Test_Func2 (0.00s)
--- PASS: Test_Func4 (0.00s)
PASS
ãã®å®è¡çµæããåããããã«ãt.Parallel()
ã¡ã½ããã®å¼ã³åºãã¯ããä¸æåæ¢ãã¦ããåéããããã¨ã«æ³¨æãã¦ãã ãããä¸æåæ¢ããå ´åã=== PAUSE
ã¨è¡¨ç¤ºãããå¦çãåéããå ´åã=== CONT
ã¨è¡¨ç¤ºããã¾ãã
t.Parallel()
ã¡ã½ãããå¼ã³åºãã¦ä¸æåæ¢ãã¦ãããã¹ãã®å¦çãåéããæ¡ä»¶ã¯ã次ã®åä½1ã§ãã
åä½1ï¼t.Parallel()
ã¡ã½ãããå¼ã³åºãã¦ããªãï¼ããã±ã¼ã¸å
ã®ï¼ãã¹ã¦ã®ãããã¬ãã«ã®ãã¹ãé¢æ°ãçµäºãã¦ãããt.Parallel()
ã¡ã½ãããå¼ã³åºãã¦ãããããã¬ãã«ã®ãã¹ãé¢æ°ã®å¦çãåéãã¦ä¸¦åã«å®è¡ããã¾ãã
ãã®åä½1ã¯ããããã¬ãã«ã®ãã¹ãé¢æ°ã
t.Parallel()
ã¡ã½ãããå¼ã³åºãã¦ããªããããã®ä¸ã«å«ã¾ããt.Run()
ã«ãããµããã¹ãé¢æ°ã§t.Parallel()
ã¡ã½ãããå¼ã³åºãã¦ããã¨ãã¦ãããããã®ãµããã¹ãé¢æ°ã®å®è¡ãçµããã¾ã§ã次ã®ãããã¬ãã«ã®ãã¹ãé¢æ°ã®å®è¡ã«ã¯ç§»ããªããã¨ãæå³ãã¾ãã
ãã¨ãã°ãTest_Func1
ã次ã®ããã«æ¸ãç´ããã¨ãã¾ãï¼ã³ã¼ãï¼ã
func Test_Func1(t *testing.T) {
defer trace("Test_Func1")()
t.Run("Func1_Sub1", func(t *testing.T) {
defer trace("Func1_Sub1")()
t.Parallel()
// ...
})
t.Run("Func1_Sub2", func(t *testing.T) {
defer trace("Func1_Sub2")()
t.Parallel()
// ...
})
// ...
}
äºã¤ã®ãµããã¹ãé¢æ°ã追å ãããããããããt.Parallel()
ã¡ã½ãããå¼ã³åºãã¦ãã¾ãã
å®è¡ããã¨ã次ã®ããã«ãªãã¾ãã
=== RUN Test_Func1
Test_Func1 entered
=== RUN Test_Func1/Func1_Sub1
Func1_Sub1 entered <- Func1_Sub1ãéå§
=== PAUSE Test_Func1/Func1_Sub1 <- Func1_Sub1ãä¸æåæ¢
=== RUN Test_Func1/Func1_Sub2
Func1_Sub2 entered <- Func1_Sub2ãéå§
=== PAUSE Test_Func1/Func1_Sub2 <- Func1_Sub2ãä¸æåæ¢
Test_Func1 returned <- Test_Func1ã®å¼ã³åºãæ»ãï¼ï¼ï¼
=== CONT Test_Func1/Func1_Sub1 <- Func1_Sub1ãåé
Func1_Sub1 returned <- Func1_Sub1ãå®äº
=== CONT Test_Func1/Func1_Sub2 <- Func1_Sub2ãåé
Func1_Sub2 returned <- Func1_Sub2ãå®äº
--- PASS: Test_Func1 (0.00s) <- Test_Func1ã®çµæ表示
--- PASS: Test_Func1/Func1_Sub1 (0.00s)
--- PASS: Test_Func1/Func1_Sub2 (0.00s)
=== RUN Test_Func2 <- ããã¾ã§Test_Func2ã¯å®è¡ãããªã
Test_Func2 entered
=== PAUSE Test_Func2
=== RUN Test_Func3
Test_Func3 entered
Test_Func3 returned
--- PASS: Test_Func3 (0.00s)
=== RUN Test_Func4
Test_Func4 entered
=== PAUSE Test_Func4
=== RUN Test_Func5
Test_Func5 entered
Test_Func5 returned
--- PASS: Test_Func5 (0.00s)
=== CONT Test_Func2
Test_Func2 returned
=== CONT Test_Func4
Test_Func4 returned
--- PASS: Test_Func4 (0.00s)
--- PASS: Test_Func2 (0.00s)
PASS
å®è¡çµæããåããããã«ãTest_Func1
ã§ã¯t.Parallel()
ã¡ã½ãããå¼ã³åºãã¦ããªãã®ã§ãããã«å«ã¾ãããã¹ã¦ã®ãã¹ããå®äºããã¾ã§ã次ã®Test_Func2
ã®å¦çã¯è¡ããã¾ãããè¨ãæããã¨ããããã¬ãã«ã®ãã¹ãé¢æ°ãå
¨ãt.Parallel()
ã¡ã½ãããå¼ã³åºãã¦ããªãã¨ããã®ããã±ã¼ã¸ã®ãã¹ãã¯ããããã¬ãã«ã®ãã¹ãé¢æ°ãä¸ã¤ãã¤é次å®è¡ããã¾ãããã¡ããããããã¬ãã«ã®ãã¹ãé¢æ°ã®ä¸ã§t.Run()
ã«ãããµããã¹ãé¢æ°ãt.Parallel()
ã¡ã½ãããå¼ã³åºãã¦ããã°ãå«ã¾ãã¦ãããµããã¹ãé¢æ°ã¯ä¸¦åå®è¡ããã¾ãã
ãã®å®è¡çµæã«ã¯ãããã«å¥ã®æ³¨ç®ãã¹ãç¹ãããã¾ãã
åä½2ï¼t.Run()
ã«ãããµããã¹ãé¢æ°ãt.Parallel()
ã¡ã½ãããå¼ã³åºãã¦ããå ´åããã®è¦ªã®ãããã¬ãã«ã®ãã¹ãé¢æ°ããçµäºãã¦æ»ããã¾ã§ããµããã¹ãé¢æ°ã¯t.Parallel()
ã¡ã½ããã®å¼ã³åºãã§ä¸æåæ¢ãã¦ããã ï¼ãªãããã®åä½ã¯ã親ã®ãããã¬ãã«ã®ãã¹ãé¢æ°ãt.Parallel()
ã¡ã½ãããå¼ã³åºãã¦ãã¦ããå¼ã³åºãã¦ããªãã¦ãåãï¼
è¨ãæããã¨ãåä½2ã¯æ¬¡ã®ããã«ã表ç¾ã§ãã¾ãã
åä½2ï¼å¥è¡¨ç¾ï¼ï¼t.Run()
ã«ãããµããã¹ãé¢æ°ãt.Parallel()
ã¡ã½ãããå¼ã³åºãã¦ããå ´åãt.Parallel()
ã¡ã½ããã§ä¸æåæ¢ãã¦ãããµããã¹ãé¢æ°ãåéãããã®ã¯ã親ã®ãããã¬ãã«ã®ãã¹ãé¢æ°ããçµäºãã¦æ»ã£ããå¾ã§ããã
åä½1ã¨åä½2ãåãããã¨ãã並åæ§ãæ大éã«åä¸ãããã«ã¯ããããã¬ãã«ã®ãã¹ãé¢æ°ã¨ãã®ä¸ã®ãµããã¹ãé¢æ°ã®ä¸¡æ¹ã§ãt.Parallel()
ã¡ã½ãããå¼ã³åºãå¿
è¦ãããããã¨ã«ãªãã¾ãããããããã¨ã§ãããã±ã¼ã¸å
ã®t.Parallel()
ã¡ã½ãããå¼ã³åºãã¦ãããã¹ã¦ã®ãµããã¹ãé¢æ°ããä¸æã«ä¸¦åã«åä½ãããã¨ã«ãªãã¾ãã
並åã¬ãã«
ä¸æã«ä¸¦åã«åä½ããã¨è¿°ã¹ã¾ããããå®éã«ã¯åæã«åä½ããåæ°ã¯å¶éããã¦ãã¾ããã©ãã ãã®åæ°ã®ãã¹ãé¢æ°ã並åã«åä½ãããã¯ã-parallel
ãã©ã°ã§æå®ãã¾ãã
-parallel n
Allow parallel execution of test functions that call t.Parallel.
The value of this flag is the maximum number of tests to run
simultaneously; by default, it is set to the value of GOMAXPROCS.
Note that -parallel only applies within a single test binary.
The 'go test' command may run tests for different packages
in parallel as well, according to the setting of the -p flag
(see 'go help build').
t.Parallel
ãå¼ã³åºãã¦ãããã¹ãé¢æ°ã®ä¸¦åå®è¡ãå¯è½ã«ãã¾ãããã®ãã©ã°ã®å¤ã¯ãåæã«å®è¡ããããã¹ãã®åæ°ã®æ大å¤ã§ããããã©ã«ãã§ã¯ãGOMAXPROCS
ã®å¤ã«è¨å®ããã¾ãã-parallel
ã¯ãåä¸ã®ãã¹ããã¤ããªå ã ãã«é©ç¨ããããã¨ã«æ³¨æãã¦ãã ãããgo test
ã³ãã³ãã¯ã-p
ãã©ã°ã®è¨å®ã«å¿ãã¦ãç°ãªãããã±ã¼ã¸ã«å¯¾ãããã¹ãã並åã«å®è¡ãã¾ãã
æ示çã«æå®ãããªãå ´åã«ã¯ãç°å¢å¤æ°GOMAXPROCS
ã®å¤ã¨ãªãã¾ããGOMAXPROCS
ã®å¤ã¯ãæ示çã«è¨å®ããã¦ããªããã°ãï¼è¦ããä¸ã®ï¼CPUã®åæ°ã§ãã
ãµããã¹ããå«ããã¹ãã®å¤ãããã¼ã¿ãã¼ã¹ã¸ã¢ã¯ã»ã¹ãã¦ããã®ã§ããã°ã並åã¬ãã«ã¯æ示çã«CPUã®åæ°ãã大ããå¤ãæå®ããæ¹ãããã§ãããªããªããéä¿¡å¾ ã¡ã¨ãªã£ã¦ãããã¨ãå¤ãããã§ããéã«ããã¹ãå 容ãCPUã§ã®å¦çãå¤ãå¿ è¦ã¨ããè¨ç®ãè¡ã£ã¦ããã®ã§ããã°ã大ããªå¤ãæå®ãã¦ãæ§è½ã¯åä¸ããªããã¨ãªãã¾ãã
defer
æã¨t.Cleanup()
ã¡ã½ãã
ãã¹ãçµäºæã«å¾å¦çãè¡ãå ´åãdefer
æã使ãã®ãããããã¯ãt.Cleanup()
ã¡ã½ããã使ãã®ãã«ã¤ãã¦ã¯æ³¨æãå¿
è¦ã§ãããããã¬ãã«ã®ãã¹ãé¢æ°ã«é¢ãã¦ã¯ãåºæ¬ã¯æ¬¡ã®éãã§ãã
- ãããã¬ãã«ã®ãã¹ãé¢æ°ãã
t.Run()
ã¡ã½ããã«ãããµããã¹ãé¢æ°ãå«ãã§ã§ããªããã°ãdefer
æãããã¯t.Cleanup()
ã¡ã½ããã®ã©ã¡ãã§å¾å¦çãè¨è¿°ãã¦ãããã - ãããã¬ãã«ã®ãã¹ãé¢æ°ãã
t.Run()
ã¡ã½ããã«ãããµããã¹ãé¢æ°ã¯å«ãã§ãã¦ããã®ãã¹ã¦ã®ãµããã¹ãé¢æ°ãt.Parallel()
ã¡ã½ãããå¼ã³åºãã¦ããªãå ´åãdefer
æãããã¯t.Cleanup()
ã¡ã½ããã®ã©ã¡ãã§å¾å¦çãè¨è¿°ãã¦ãããã - ãããã¬ãã«ã®ãã¹ãé¢æ°ãã
t.Run()
ã¡ã½ããã«ãããµããã¹ãé¢æ°ãå«ãã§ãã¦ãå°ãªãã¨ããã®ä¸ã¤ã®ãµããã¹ãé¢æ°ãt.Parallel()
ã¡ã½ãããå¼ã³åºãã¦ããå ´åãt.Cleanup()
ã¡ã½ããã§å¾å¦çãè¨è¿°ããã
defer
æã¯ããã®æãå«ã¾ããé¢æ°ãæ»ãéã«å¼ã³åºããã¾ããåè¿°ã®ã³ã¼ãã®å®è¡ä¾ãè¦ç´ãã¦ã¿ã¦ãã ãããTest_Func1
é¢æ°ã¯ãFunc1_Sub1
ã¨Func1_Sub2
ã®ãµããã¹ãé¢æ°ãçµäºããåã«ãæ»ã£ã¦ãã¾ãï¼åä½2ï¼ããã®ãããTest_Func1
é¢æ°ã«å«ã¾ããdefer
æã§é
延ãæå®ãããé¢æ°ã¯ãFunc1_Sub1
ã¨Func1_Sub2
ã®ãµããã¹ãé¢æ°ãä¸æåæ¢å¾ã«å¦çãåéããåã«å¼ã³åºããã¾ãï¼ä¸è¨ã®å®è¡çµæä¸ã§ãTest_Func1 returned
ã¨è¡¨ç¤ºããã¦ããä½ç½®ã«æ³¨æãã¦ãã ããï¼ã
ãã¨ãã°ããµããã¹ãé¢æ°ãä½æãããã¼ãã«ã®ã¬ã³ã¼ããåé¤ããã¨ãã£ãå¾å¦çã®å¼ã³åºããããããã¬ãã«ã®ãã¹ãé¢æ°ãdefer
æã§é
延ããã¦ãããµããã¹ãé¢æ°ãt.Parallel()
ã¡ã½ãããå¼ã³åºãã¦ããã¨ããµããã¹ãé¢æ°ã®å®è¡ã«å
ç«ã£ã¦ãdefer
æã§æå®ãããå¾å¦çã®é¢æ°ãå¼ã³åºããã¦ãã¾ãã¾ãããã®ãããªå ´åãdefer
æã§ã¯ãªããt.Cleanup()
ã¡ã½ããã使ã£ã¦ãå¾å¦çãè¨è¿°ãã¾ãã
t.Cleanup()
ã¡ã½ããã®ä»æ§ã¯æ¬¡ã®éãã§ãã
func (c *T) Cleanup(f func())
Cleanup registers a function to be called when the test and all its subtests
complete. Cleanup functions will be called in last added, first called
order.
Cleanup
ã¯ããã®ãã¹ãã¨ãã®ãµããã¹ãã®ãã¹ã¦ãå®äºããã¨ãã«å¼ã³åºãããé¢æ°ãç»é²ãã¾ããCleanup
ã§ç»é²ãããé¢æ°ã¯ãæå¾ã«è¿½å ãããé¢æ°ããé ã«å¼ã³åºããã¾ãã
t.Cleanup()
ã¡ã½ããã§ç»é²ããé¢æ°ã¯ããã¹ã¦ã®ãµããã¹ããçµäºããæç¹ã§å¼ã³åºãããã¨è¨è¿°ããã¦ãã¾ãã
ã§ã¯ãt.Run()
ã¡ã½ããã§è¨è¿°ãããµããã¹ãé¢æ°å
ã§ã®å¾å¦çã¯ã©ããªããã¨ããã¨ã以ä¸ã®éãã§ãï¼åºæ¬çã«ã¯ä¸è¨ã®3é
ç®ã¨åãã§ãã主èªãç°ãªã£ã¦ãã¾ãï¼ã
- ãµããã¹ãé¢æ°ããããã«ãã¹ããã
t.Run()
ã¡ã½ããã«ãããµããµããã¹ãé¢æ°ãå«ãã§ããªããã°ãdefer
æãããã¯t.Cleanup()
ã¡ã½ããã®ã©ã¡ãã§å¾å¦çãè¨è¿°ãã¦ãããã - ãµããã¹ãé¢æ°ããããã«ãã¹ããã
t.Run()
ã¡ã½ããã«ãããµããµããã¹ãé¢æ°ãå«ãã§ãã¦ããã®ãã¹ã¦ã®ãµããµããã¹ãé¢æ°ãt.Parallel()
ã¡ã½ãããå¼ã³åºãã¦ããªãå ´åãdefer
æãããã¯t.Cleanup()
ã¡ã½ããã®ã©ã¡ãã§å¾å¦çãè¨è¿°ãã¦ãããã - ãµããã¹ãé¢æ°ããããã«ãã¹ããã
t.Run()
ã¡ã½ããã«ãããµããµããã¹ãé¢æ°ãå«ãã§ãã¦ãå°ãªãã¨ãä¸ã¤ã®ãµããµããã¹ãé¢æ°ãt.Parallel()
ã¡ã½ãããå¼ã³åºãã¦ããå ´åãt.Cleanup()
ã¡ã½ããã§å¾å¦çãè¨è¿°ããã
ãããã¬ãã«ã®ãã¹ãé¢æ°ã¨ãµããã¹ãé¢æ°ã«é¢ããä¸è¨ã®å
ã¤ã®é
ç®ãè¦ããã®ãé¢åã§ããã°ãããã¸ã§ã¯ãã«ãã£ã¦ã¯ããt.Parallel()
ã¡ã½ããã使ã£ã¦ãããã¹ãã³ã¼ãã§ã¯ãå¾å¦çã¯t.Cleanup()
ã§æ¸ããã¨æ±ºãã¦ãããããããã¾ããã
ã¾ã¨ã
t.Parallel()
ã¡ã½ãããå¼ã³åºãã°é©å½ã«ä¸¦ååãããã¨èãã¦ãã¾ãããããã¾ããããæ大éã«æ£ãã並ååããã«ã¯ããã®è¨äºã§èª¬æãããã¨ããã¡ãã¨ç解ãã¦ããå¿
è¦ãããã¾ãã
ã¾ã¨ããã¨ä»¥ä¸ã®éãã§ãã
-p
ãã©ã°ã«ããæå®ã¯ãè¤æ°ã®ããã±ã¼ã¸ã®ãã¹ãã並åã«ç°ãªãããã»ã¹ã¨ãã¦å®è¡ãããã¨ãæå®ããã-p=1
ã§ã¯ãããã±ã¼ã¸ãä¸ã¤ãã¤å®è¡ããããã¨ã«ãªããt.Parallel()
ã¡ã½ããã®å¼ã³åºãã§ãããã±ã¼ã¸å ã®ãããã¬ãã«ã®ãã¹ãé¢æ°ããµããã¹ãé¢æ°ã並åã«å®è¡ããããã¨ã«ãªããt.Parallel()
ã¡ã½ãããå¼ã³åºãã¦ããï¼ãããã¬ãã«ãå«ãï¼ãã¹ãé¢æ°ã¯ããã®è¦ªã®ãã¹ãé¢æ°ã®å¼ã³åºããæ»ãã¾ã§ãt.Parallel()
ã¡ã½ããå¼ã³åºãã«ããä¸æåæ¢ã®ç¶æ ããå¦çãåéããªããt.Parallel()
ã¡ã½ããã«ãã並åã¬ãã«ã¯ãããã©ã«ãã§GOMAXPROCS
ã®å¤ã§ãããæ示çã«å¤æ´ããã«ã¯ã-parallel
ãã©ã°ã§æå®ããããç°å¢å¤æ°GOMAXPROCS
ã§è¨å®ããã- ãã¹ãé¢æ°å
ã§ã®å¾å¦çã¯ã
t.Cleanup
ã¡ã½ãããããã¯defer
æã使ããã¯ãå«ã¾ãããµããã¹ãé¢æ°ãt.Parallel()
ã¡ã½ãããå¼ã³åºãã¦ãããããªããã§ä½¿ãåããå¿ è¦ãããã t.Parallel()
ã¡ã½ããã使ã£ã¦ãã¦ããè¤æ°ã®ããã±ã¼ã¸ã®ãã¹ããåæã«ä¸ã¤ã®ãã¹ãããã»ã¹å ã§å®è¡ããããã¨ã¯ãªãã
ãã®è¨äºã§ã¯ãç§èªèº«ãã¡ã«ãã¤å ã®ï¼ãããã¤ã¯ããµã¼ãã¹ã®ï¼ããããã±ã¼ã¸ã®ãã¹ããæ大éã«ä¸¦ååããçµé¨ãéãã¦ã調æ»ããäºæãã¾ã¨ãã¦ã¿ã¾ãããå®éããã®ããã±ã¼ã¸ã®ãã¹ãæéã¯10%以ä¸ã¨ãªããããªãçããªãã¾ãããä»åã¯èª¬æãã¦ãã¾ããããå®éã«ä¸¦ååããããã®ããã°ã©ãã³ã°ä¸ã®æ³¨æç¹ã«ã¤ãã¦ã¯ãæ©ä¼ãããã°å¥éæ¸ãããã¨æãã¾ãã