-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmapreduce_test.go
167 lines (159 loc) · 3.73 KB
/
mapreduce_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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package gostream
import (
"bytes"
"encoding/gob"
"fmt"
"log"
"reflect"
"strconv"
"testing"
)
type IntSlice []interface{}
type IntSlice2 []interface{}
type Item int
func (s Item) HashCode() []byte {
return []byte(strconv.Itoa(int(s)))
}
func (s IntSlice) Len() int { return len(s) }
func (s IntSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s IntSlice) Less(i, j int) bool {
return s[i].(int) < s[j].(int)
}
func (s IntSlice2) Len() int { return len(s) }
func (s IntSlice2) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s IntSlice2) Less(i, j int) bool {
return s[i].(int) > s[j].(int)
}
func TestStream_Distinct(t *testing.T) {
a := []interface{}{Item(1), Item(2), Item(4), Item(4)}
s:= BuildStream(IntSlice(a))
s.Distinct()
fmt.Println(s.Collect())
}
func TestName(t *testing.T) {
/*var a []interface{}
for i:=0;i<100000000 ;i++ {
a=append(a,i)
}*/
a := []interface{}{4, 3, 2, 1}
s := BuildStream(IntSlice(a))
a1 := []interface{}{6, 5, 8, 7}
s1 := BuildStream(IntSlice(a1))
var types []int
s.Combine(s1).
Sorted().
Limit(6).
Map(types,func(i interface{}) interface{} {
return 1
}) //==20
//s.Limit(10)
fmt.Println("after arr: ", s.Collect())
//fmt.Println("after value: ", value)
}
func TestCopy(t *testing.T) {
a := []interface{}{4, 3, 2, 1}
s := BuildStream(IntSlice(a))
s1 := s.Copy()
var types []int
s1.Map(types,func(i interface{}) interface{} {
return i.(int) + 1
}).Sorted()
fmt.Println(s1.Collect())
fmt.Println(s.Collect())
}
type ForFoo struct {
Item int
}
func TestStream_Map(t *testing.T) {
a := []ForFoo{ForFoo{4}, ForFoo{3}, ForFoo{2}, ForFoo{1}}
s := BuildStream((a))
//s1 := s.Copy()
var types []int
s.Map(types,func(i interface{}) interface{} {
return i.(ForFoo).Item + 1
}).Sorted()
//fmt.Println(s1.Collect())
//_,ok:=s.Collect().([]interface{})
log.Println(reflect.ValueOf(s.Collect()).Type())
fmt.Println(s.Collect())
}
func BenchmarkCopyRegule(b *testing.B) {
/*
goos: windows
goarch: amd64
pkg: github.com/felix0080/gostream
BenchmarkCopy-4 33630 31917 ns/op
PASS
*/
for i := 0; i < b.N; i++ {
a1 := []interface{}{6, 5, 8, 7}
a2 := make([]interface{}, len(a1))
//fmt.Println(fmt.Sprintf("%p %p",a1,a2))
Clone(&a1, &a2)
//fmt.Println(a2)
}
}
func BenchmarkCopyReflect(b *testing.B) {
/*
使用反射前
goos: windows
goarch: amd64
pkg: github.com/felix0080/gostream
BenchmarkCopy1-4 10915362 101 ns/op
PASS
使用反射后
goos: windows
goarch: amd64
pkg: github.com/felix0080/gostream
BenchmarkCopy1-4 2914296 436 ns/op
PASS
总结有300ns的损耗,还不错,采纳反射
*/
for i := 0; i < b.N; i++ {
a1 := []interface{}{6, 5, "asd", 7}
move := a1
//a2:=make([]interface{},len(a1))
value := reflect.ValueOf(move)
a2 := reflect.MakeSlice(value.Type(), value.Len(), value.Cap())
reflect.Copy(a2, reflect.ValueOf(move))
}
}
// Clone 完整复制数据
func Clone(a, b interface{}) error {
buff := new(bytes.Buffer)
enc := gob.NewEncoder(buff)
dec := gob.NewDecoder(buff)
if err := enc.Encode(a); err != nil {
return err
}
if err := dec.Decode(b); err != nil {
return err
}
return nil
}
func TestStream_GroupBy(t *testing.T) {
/* var i []int
v := reflect.MakeSlice(reflect.TypeOf(i), 5, 5)
v=reflect.Append(v, reflect.ValueOf(1))
fmt.Println(v.Index(1))
fmt.Println(v.Len())*/
/*s := Student{
Name:"fanxing",
}
v:=reflect.ValueOf(s)
v1:=v.FieldByName("Name")
fmt.Println(v1.Kind()==reflect.String)*/
var s []Student
for i := 0; i < 5; i++ {
s=append(s,Student{strconv.Itoa(i)})
}
s=append(s, Student{strconv.Itoa(1)})
ss:=BuildStream(s)
sss:=ss.GroupByToStream("Name")
for index,value:=range sss{
fmt.Println(index,value.Collect())
}
}
type Student struct {
Name string
}