-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper_test.go
131 lines (120 loc) · 2.81 KB
/
helper_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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package cpuprofile
import (
"context"
"math/rand"
"runtime/pprof"
"sort"
"strconv"
"sync"
)
const primeBound = 10000
const minBound = 10
const arrayLen = 500000
func selectPrime(ctx context.Context, c chan int, wg *sync.WaitGroup, enableProfile bool) {
if enableProfile {
pprof.SetGoroutineLabels(ctx)
}
defer wg.Done()
prime, ok := <-c
if !ok {
return
}
// fmt.Println(prime)
newChan := make(chan int)
newWg := sync.WaitGroup{}
newWg.Add(1)
go selectPrime(ctx, newChan, &newWg, enableProfile)
for n := range c {
if n%prime != 0 {
newChan <- n
}
}
close(newChan)
newWg.Wait()
}
func prime(ctx context.Context, labelKey string, labelValue string, enableProfile bool, externWg *sync.WaitGroup) {
// 筛法求素数, 用作测试任务
defer externWg.Done()
if enableProfile {
defer pprof.SetGoroutineLabels(ctx)
ctx = pprof.WithLabels(ctx, pprof.Labels(labelKey, labelValue))
pprof.SetGoroutineLabels(ctx)
}
wg := sync.WaitGroup{}
wg.Add(1)
c := make(chan int)
n := primeBound
go selectPrime(ctx, c, &wg, enableProfile)
for i := 2; i <= n; i++ {
c <- i
}
close(c)
wg.Wait()
}
func parallelStartNprime(ctx context.Context, number int, wg *sync.WaitGroup) {
for i := 0; i < number; i++ {
wg.Add(1)
go prime(ctx, "prime", strconv.Itoa(number)+"xload", true, wg)
}
}
func parallelMergeSort(ctx context.Context, array []int, enableProfile bool) {
if enableProfile {
pprof.SetGoroutineLabels(ctx)
}
if len(array) <= minBound {
sort.Ints(array)
return
}
wg := sync.WaitGroup{}
wg.Add(2)
leftArray := make([]int, len(array)/2)
rightArray := make([]int, len(array)-len(array)/2)
copy(leftArray[0:], array[0:len(array)/2])
copy(rightArray[0:], array[len(array)/2:])
go func() {
parallelMergeSort(ctx, leftArray, enableProfile)
wg.Done()
}()
go func() {
parallelMergeSort(ctx, rightArray, enableProfile)
wg.Done()
}()
wg.Wait()
cnt := 0
i := 0
j := 0
for i < len(array)/2 && j < len(array)-len(array)/2 {
if leftArray[i] < rightArray[j] {
array[cnt] = leftArray[i]
i++
} else {
array[cnt] = rightArray[j]
j++
}
cnt++
}
if i < len(array)/2 {
copy(array[cnt:], leftArray[i:])
} else {
copy(array[cnt:], rightArray[j:])
}
}
func MergeSort(ctx context.Context, labelKey string, labelValue string, enableProfile bool, externWg *sync.WaitGroup) {
defer externWg.Done()
if enableProfile {
defer pprof.SetGoroutineLabels(ctx)
ctx = pprof.WithLabels(ctx, pprof.Labels(labelKey, labelValue))
pprof.SetGoroutineLabels(ctx)
}
array := make([]int, arrayLen)
for i := range array {
array[i] = rand.Int()
}
parallelMergeSort(ctx, array, enableProfile)
}
func parallelStartNMergeSort(ctx context.Context, number int, wg *sync.WaitGroup) {
for i := 0; i < number; i++ {
wg.Add(1)
go MergeSort(ctx, "mergeSort", strconv.Itoa(number)+"xload", true, wg)
}
}