-
Notifications
You must be signed in to change notification settings - Fork 0
/
parallelism_test.go
79 lines (64 loc) · 1.74 KB
/
parallelism_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
package gojsmodule
import (
"fmt"
"math/big"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
var (
num = 20000
)
func factorial(n int) (*big.Int, error) {
result := big.NewInt(1)
for i := 2; i <= n; i++ {
result.Mul(result, big.NewInt(int64(i)))
}
return result, nil
}
func getHeavyJob(isAddErrorFunc bool) []PromiseParams[*big.Int] {
bp, loop := []PromiseParams[*big.Int]{}, 10
for i := 0; i < loop; i++ {
bp = append(bp, PromiseParams[*big.Int]{
fnName: fmt.Sprintf("factorial-%d", i),
fn: func() (*big.Int, error) {
return factorial(num + ((i + 1) * 10000))
},
})
}
return bp
}
// 순차처리 방식
func executeSimple[T any](fns []PromiseParams[T]) ([]fnResParams[T], int64) {
start := time.Now()
fnResult := []fnResParams[T]{}
for _, attr := range fns {
res, err, durtaion := execute(attr.fn)
fnResult = append(fnResult, fnResParams[T]{
fnName: attr.fnName,
result: res,
err: err,
executionTime: durtaion,
})
}
end := time.Now()
return fnResult, int64(end.Sub(start).Milliseconds())
}
// ////////////////////////////////////////////// Testing ////////////////////////////////////////////////
func Test_FactorialDuration(t *testing.T) {
start := time.Now()
factorial(num)
end := time.Now()
duration := end.Sub(start).Milliseconds()
assert.Equal(t, duration > 1, true) // >= 0.001
}
func Test_benchmarkExecution(t *testing.T) {
_, simpleExecutionTime := executeSimple(getHeavyJob(false))
res2, promiseExecutionTime := PromiseAll(getHeavyJob(false))
assert.Equal(t, simpleExecutionTime > promiseExecutionTime, true)
for _, attr := range res2 {
if attr.result.Cmp(big.NewInt(1)) <= 0 {
t.Errorf("Expected result greater than 1, got: %v", attr.result)
}
}
}