-
Notifications
You must be signed in to change notification settings - Fork 0
/
gomaxprocs_test.go
54 lines (43 loc) · 982 Bytes
/
gomaxprocs_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package golang_goroutine
import (
"fmt"
"runtime"
"sync"
"testing"
"time"
)
func TestGomaxProcs(t *testing.T) {
group := sync.WaitGroup{}
for i := 0; i < 100; i++ {
group.Add(1)
go func() {
time.Sleep(3 * time.Second)
group.Done()
}()
}
totalCPU := runtime.NumCPU()
fmt.Println("Total CPU", totalCPU)
totalThread := runtime.GOMAXPROCS(-1)
fmt.Println("Total thread", totalThread)
totalGoroutine := runtime.NumGoroutine()
fmt.Println("Total Goroutine", totalGoroutine)
group.Wait()
}
func TestChangeThreadNumber(t *testing.T) {
group := sync.WaitGroup{}
for i := 0; i < 100; i++ {
group.Add(1)
go func() {
time.Sleep(3 * time.Second)
group.Done()
}()
}
totalCPU := runtime.NumCPU()
fmt.Println("Total CPU", totalCPU)
runtime.GOMAXPROCS(20)
totalThread := runtime.GOMAXPROCS(-1)
fmt.Println("Total thread", totalThread)
totalGoroutine := runtime.NumGoroutine()
fmt.Println("Total Goroutine", totalGoroutine)
group.Wait()
}