-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathTestHistory.java
287 lines (245 loc) · 9.21 KB
/
TestHistory.java
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package edu.rice.historytree;
import java.util.Arrays;
import java.util.List;
import org.junit.Test;
import com.google.protobuf.InvalidProtocolBufferException;
import edu.rice.historytree.*;
import edu.rice.historytree.aggs.*;
import edu.rice.historytree.generated.Serialization;
import edu.rice.historytree.storage.AppendOnlyArrayStore;
import edu.rice.historytree.storage.ArrayStore;
import edu.rice.historytree.storage.HashStore;
import junit.framework.TestCase;
public class TestHistory extends TestCase {
public final String NAMES[] = {
"Alan","Bob","Charlie","Dan",
"Elen","Frank","Gordon","Helen",
"Isis","Jon","Kevin", "Laura"};
public String results[] = {
"A",
"[A,B]",
"[[A,B],[C,]]", "[[A,B],[C,D]]",
"[[[A,B],[C,D]],[[E,],]]",
"[[[A,B],[C,D]],[[E,F],]]",
"[[[A,B],[C,D]],[[E,F],[G,]]]",
"[[[A,B],[C,D]],[[E,F],[G,H]]]",
"[[[[A,B],[C,D]],[[E,F],[G,H]]],[[[I,],],]]",
"[[[[A,B],[C,D]],[[E,F],[G,H]]],[[[I,J],],]]",
"[[[[A,B],[C,D]],[[E,F],[G,H]]],[[[I,J],[K,]],]]",
"[[[[A,B],[C,D]],[[E,F],[G,H]]],[[[I,J],[K,L]],]]",
};
public HistoryTree<String,String> doTestAppendOnStore(HistoryDataStoreInterface<String,String> store) {
String mynames[] = Arrays.copyOf(NAMES,9);
AggregationInterface<String,String> aggobj = new ConcatAgg();
HistoryTree<String,String> histtree=new HistoryTree<String,String>(aggobj,store);
for (int i = 0 ; i <=4 ; i++) {
histtree.append(mynames[i]) ; assertEquals(results[i],histtree.agg());
}
assertEquals(4,histtree.version());
for (int i = 0 ; i <= histtree.version() ; i++)
assertEquals(results[i],histtree.aggV(i));
for (int i = 5 ; i <= 8 ; i++) {
histtree.append(mynames[i]) ; assertEquals(results[i],histtree.agg());
}
assertEquals(8,histtree.version());
for (int i = 0 ; i <= histtree.version() ; i++)
assertEquals(results[i],histtree.aggV(i));
return histtree;
}
@Test
public void testOnArrayStore() {
HistoryDataStoreInterface<String,String> store = new ArrayStore<String,String>();
doTestAppendOnStore(store);
}
@Test
public void testOnAppendArrayStore() {
HistoryDataStoreInterface<String,String> store = new AppendOnlyArrayStore<String,String>();
doTestAppendOnStore(store);
}
@Test
public void testOnHashStore() {
HistoryDataStoreInterface<String,String> store = new HashStore<String,String>();
doTestAppendOnStore(store);
}
HistoryTree<String, String> makeHistTree(int length) {
AggregationInterface<String,String> aggobj = new ConcatAgg();
HistoryDataStoreInterface<String,String> datastore = new AppendOnlyArrayStore<String,String>();
HistoryTree<String,String> histtree=new HistoryTree<String,String>(aggobj,datastore);
for (int i=0 ; i < length ; i++) {
histtree.append(NAMES[i]);
assertEquals(results[i],histtree.aggV(i));
}
return histtree;
}
void doTestMakePruned(int length, HistoryDataStoreInterface<String,String> datastore) throws ProofError {
HistoryTree<String,String> tree=makeHistTree(length);
HistoryTree<String,String> clone0=tree.makePruned(datastore);
assertEquals(clone0.version(),length-1);
assertEquals(clone0.aggV(tree.version()),clone0.agg());
for (int i=0; i <= length-1 ; i++) {
HistoryTree<String,String> clone=tree.makePruned(datastore);
clone.copyV(tree,i,false);
assertEquals(length-1,clone.version());
assertEquals(results[i],clone.aggV(i));
assertEquals(results[length-1],clone.aggV(length-1));
}
}
@Test
public void testPruned() throws ProofError {
// Try around powers of 2.
for (int i=1 ; i < 12 ; i++) {
HashStore<String,String> store = new HashStore<String,String>();
doTestMakePruned(i,store);
}
}
// TO WRITE TESTS BELOW HERE.
@Test
public void testSerialization() throws InvalidProtocolBufferException {
HistoryTree<String,String> histtree= makeHistTree(11);
byte[] serialized = histtree.serializeTree();
HistoryTree<String,String> tree2 = parseSerialization(serialized);
System.out.println(tree2.toString("Unserial:"));
tree2.aggV(3);
assertEquals(histtree.agg(),tree2.agg());
}
public HistoryTree<String,String> parseSerialization(byte serialized[]) throws InvalidProtocolBufferException {
Serialization.PrunedTree.Builder builder = Serialization.PrunedTree.newBuilder();
Serialization.PrunedTree pb = builder.mergeFrom(serialized).build();
//System.out.println(pb.toString());
HistoryTree<String,String> tree2= new HistoryTree<String,String>(new ConcatAgg(),new HashStore<String,String>());
tree2.updateTime(pb.getVersion());
tree2.parseTree(pb);
return tree2;
}
HistoryTree<byte[],byte[]>
makeShaHistTree() {
List<String> x = Arrays.asList("Alan","Bob","Charlie","Dan","Elen","Frank","Gordon","Helen","Isis","Jon","Kevin");
AggregationInterface<byte[],byte[]> aggobj = new SHA256AggB64();
ArrayStore<byte[],byte[]> datastore = new ArrayStore<byte[],byte[]>();
HistoryTree<byte[],byte[]> histtree=new HistoryTree<byte[],byte[]>(aggobj,datastore);
for (String s : x) {
histtree.append(s.getBytes());
System.out.println(aggobj.serializeAgg(histtree.agg()).toStringUtf8());
}
System.out.println(histtree.toString("Binary:"));
return histtree;
}
@Test
public void testMakeShaHistTree() {
makeShaHistTree();
}
public HistoryTree<byte[],byte[]> parseSerialization2(byte serialized[]) throws InvalidProtocolBufferException {
Serialization.PrunedTree.Builder builder = Serialization.PrunedTree.newBuilder();
Serialization.PrunedTree pb = builder.mergeFrom(serialized).build();
//System.out.println(pb.toString());
HistoryTree<byte[],byte[]> tree2= new HistoryTree<byte[],byte[]>(new SHA256AggB64(),new HashStore<byte[],byte[]>());
tree2.updateTime(pb.getVersion());
tree2.parseTree(pb);
return tree2;
}
public void benchTestCore(int keycount, int iter, boolean doGetAgg, boolean doGetAggV, boolean doMakePrune,
boolean doAddPruned, boolean doSerialize, boolean doDeserialize, boolean doVf) {
int LOOP = keycount; // TODO: BUGGY WITH THIS AN EXACT POWER OF 2.
HistoryTree<byte[],byte[]> histtree;
for (int i=0; i < iter ; i++) {
AggregationInterface<byte[],byte[]> aggobj = new SHA256AggB64();
ArrayStore<byte[],byte[]> datastore = new ArrayStore<byte[],byte[]>();
histtree=new HistoryTree<byte[],byte[]>(aggobj,datastore);
for (int j =0; j < LOOP ; j++) {
histtree.append(String.format("Foo%d",j).getBytes());
if (doGetAgg)
histtree.agg();
}
if (doGetAggV)
for (int j = 0 ; j < LOOP ; j++)
histtree.aggV(j);
if (doMakePrune||doAddPruned||doSerialize||doDeserialize||doVf) {
for (int j = 0 ; j < LOOP ; j++) {
HashStore<byte[],byte[]> datastore2 = new HashStore<byte[],byte[]>();
HistoryTree<byte[],byte[]> clone = histtree.makePruned(datastore2);
if (doAddPruned) {
try {
clone.copyV(histtree, j, true);
} catch (ProofError e) {
e.printStackTrace();
}
}
//System.out.print(clone.toString("Clone:"));
if (doDeserialize) {
byte[] data = clone.serializeTree();
if (doSerialize) {
HistoryTree<byte[],byte[]> parsed=null;
try {
parsed = parseSerialization2(data);
} catch (InvalidProtocolBufferException e) {
e.printStackTrace();
}
//System.out.print(parsed.toString("Parsed:"));
if (doVf) {
assertTrue(Arrays.equals(parsed.agg(),histtree.agg()));
assertTrue(Arrays.equals(parsed.aggV(j),histtree.aggV(j)));
}
}
}
}
}
}
}
final int LOOPCOUNT = 1;
@Test
public void testdoAppend() {
for (int i = 0 ; i < LOOPCOUNT ; i++) {
benchTestCore(8,10,false,false,false,false,false,false,false);
benchTestCore(13,10,false,false,false,false,false,false,false);
}
}
@Test
public void testdoGetAgg() {
for (int i = 0 ; i < LOOPCOUNT ; i++) {
benchTestCore(8,10,true,false,false,false,false,false,false);
benchTestCore(13,10,true,false,false,false,false,false,false);
}
}
@Test
public void testdoGetAggV() {
for (int i = 0 ; i < LOOPCOUNT ; i++) {
benchTestCore(8,10,false,true,false,false,false,false,false);
benchTestCore(13,10,false,true,false,false,false,false,false);
}
}
@Test
public void testdoSimplePruned() {
for (int i = 0 ; i < LOOPCOUNT ; i++) {
benchTestCore(8,10,false,false,true,false,false,false,false);
benchTestCore(13,10,false,false,true,false,false,false,false);
}
}
@Test
public void testdoAddPruned() {
for (int i = 0 ; i < LOOPCOUNT ; i++) {
benchTestCore(8,10,false,false,false, true,false,false,false);
benchTestCore(13,10,false,false,false, true,false,false,false);
}
}
@Test
public void testdoAddPrunedSerialize() {
for (int i = 0 ; i < LOOPCOUNT ; i++) {
benchTestCore(8,10,false,false,false, true,true,false,false);
benchTestCore(13,10,false,false,false, true,true,false,false);
}
}
@Test
public void testdoAddPrunedDeSerialize() {
for (int i = 0 ; i < LOOPCOUNT ; i++) {
benchTestCore(8,10,false,false,false,true,true,true,false);
benchTestCore(13,10,false,false,false,true,true,true,false);
}
}
@Test
public void testdoAddPrunedDeSerializeVf() {
for (int i = 0 ; i < LOOPCOUNT ; i++) {
benchTestCore(8,10,false,false,false,true,true,true,true);
benchTestCore(13,10,false,false,false,true,true,true,true);
}
}
}