/
parallel_test.go
152 lines (138 loc) · 4.01 KB
/
parallel_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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package parallel
import (
"fmt"
"github.com/eaciit/database/base"
"github.com/eaciit/database/mongodb"
"github.com/eaciit/toolkit"
"runtime"
"strconv"
"testing"
"time"
)
var e error
const (
pworkerCount = 100
pnumCount = 20000
)
func init() {
fmt.Println("Initiate Env")
runtime.GOMAXPROCS(runtime.NumCPU())
fmt.Println("")
}
func Test1(t *testing.T) {
t.Skip()
fmt.Println("Test 1 - Generate x Number")
numCount := 1000
workerCount := 100
idxs := make([]interface{}, numCount)
want := 0
for i := 1; i <= numCount; i++ {
idxs[i-1] = i
want += i
}
total := 0
fmt.Printf("Run Parallel %d job within %d pools \n", numCount, workerCount)
pr := Run(idxs, workerCount, func(js <-chan interface{}, rs chan<- *toolkit.Result) {
for j := range js {
time.Sleep(1 * time.Microsecond)
j2 := j.(int)
total += j2
r := new(toolkit.Result)
r.Status = toolkit.Status_OK
r.Data = j2
rs <- r
}
})
if total == want && pr.Success == 1000 {
fmt.Printf("Test OK in %v \n\n", pr.Duration)
} else {
fmt.Printf("Test Fail want %d got %d and has %d records\n", total, want, pr.Success)
}
}
func TestDb1(t *testing.T) {
//t.Skip()
t0 := time.Now()
fmt.Println("Test 2a - Insert Data (No Parallel)")
numCount := pnumCount
idxs := make([]interface{}, numCount)
for i := 1; i <= numCount; i++ {
idxs[i-1] = i
}
conn := mongodb.NewConnection("localhost:27888", "root", "Database.1", "ectest")
//conn := mongodb.NewConnection("localhost:27888", "", "", "ectest")
e = conn.Connect()
if e != nil {
t.Error("Unable to connect to Database | " + e.Error())
}
defer conn.Close()
conn.Execute("appusers", toolkit.M{"find": toolkit.M{}, "operation": base.DB_DELETE})
success := 0
fmt.Printf("Insert %d data with no pool \n", numCount)
for _, jb := range idxs {
j := jb.(int)
user := toolkit.M{}
userid := "User " + strconv.Itoa(j)
user.Set("_id", userid)
user.Set("fullname", "User "+strconv.Itoa(j))
user.Set("email", "user"+strconv.Itoa(j)+"@email.com")
//_, _, e = conn.Query().From("ORMUsers").Save(user).Run()
_, e := conn.Execute("appusers", toolkit.M{"find": toolkit.M{"_id": userid}, "operation": base.DB_SAVE, "data": user})
if e == nil {
success++
}
}
if success == numCount {
fmt.Printf("Test OK in %v \n\n", time.Since(t0))
} else {
fmt.Printf("Test Fail has %d records in %v \n\n", success, time.Since(t0))
}
}
func TestDb2(t *testing.T) {
fmt.Println("Test 2b - Insert Data (Parallel)")
numCount := pnumCount
workerCount := pworkerCount
idxs := make([]interface{}, numCount)
for i := 1; i <= numCount; i++ {
idxs[i-1] = i + 2000
}
conn := mongodb.NewConnection("localhost:27888", "root", "Database.1", "ectest")
//conn := mongodb.NewConnection("localhost:27888", "", "", "ectest")
e = conn.Connect()
if e != nil {
t.Error("Unable to connect to Database | " + e.Error())
return
}
defer conn.Close()
_, e = conn.Execute("appusers", toolkit.M{"find": toolkit.M{}, "operation": base.DB_DELETE})
if e != nil {
fmt.Println("Unable to delete: " + e.Error())
}
fmt.Printf("Insert %d data within %d pools \n", numCount, workerCount)
pr := Run(idxs, workerCount, func(js <-chan interface{}, rs chan<- *toolkit.Result) {
for jb := range js {
j := jb.(int)
user := toolkit.M{}
userid := "User " + strconv.Itoa(j)
user.Set("_id", userid)
user.Set("fullname", "User "+strconv.Itoa(j))
user.Set("email", "user"+strconv.Itoa(j)+"@email.com")
r := new(toolkit.Result)
//_, _, e = conn.Query().From("ORMUsers").Save(user).Run()
_, e := conn.Execute("appusers",
toolkit.M{"find": toolkit.M{"_id": userid}, "operation": base.DB_SAVE, "data": user})
if e == nil {
r.Status = toolkit.Status_OK
r.Data = user
} else {
r.Status = toolkit.Status_NOK
r.Message = "Error ID " + strconv.Itoa(j) + " " + e.Error()
}
rs <- r
}
})
if pr.Success == numCount {
fmt.Printf("Test OK in %v \n\n", pr.Duration)
} else {
fmt.Printf("Test Fail has %d records. \nErrors\n%v\nIn %v \n\n", pr.Success, toolkit.JsonString(pr.Errors[0]), pr.Duration)
}
}