1
1
package us .codecraft .webmagic .scheduler ;
2
2
3
+ import org .junit .Ignore ;
3
4
import org .junit .Test ;
4
5
import us .codecraft .webmagic .Request ;
5
6
import us .codecraft .webmagic .scheduler .component .BloomFilterDuplicateRemover ;
7
+ import us .codecraft .webmagic .scheduler .component .DuplicateRemover ;
8
+ import us .codecraft .webmagic .scheduler .component .HashSetDuplicateRemover ;
6
9
7
10
import static org .assertj .core .api .Assertions .assertThat ;
8
11
@@ -24,4 +27,54 @@ public void testRemove() throws Exception {
24
27
assertThat (isDuplicate ).isTrue ();
25
28
26
29
}
30
+
31
+ @ Ignore ("long time" )
32
+ @ Test
33
+ public void testMemory () throws Exception {
34
+ int times = 5000000 ;
35
+ DuplicateRemover duplicateRemover = new BloomFilterDuplicateRemover (times ,0.005 );
36
+ long freeMemory = Runtime .getRuntime ().freeMemory ();
37
+ long time = System .currentTimeMillis ();
38
+ for (int i = 0 ; i < times ; i ++) {
39
+ duplicateRemover .isDuplicate (new Request (String .valueOf (i )), null );
40
+ }
41
+ System .out .println ("Time used by bloomfilter:" + (System .currentTimeMillis () - time ));
42
+ System .out .println ("Memory used by bloomfilter:" + (freeMemory - Runtime .getRuntime ().freeMemory ()));
43
+
44
+ duplicateRemover = new HashSetDuplicateRemover ();
45
+ System .gc ();
46
+ freeMemory = Runtime .getRuntime ().freeMemory ();
47
+ time = System .currentTimeMillis ();
48
+ for (int i = 0 ; i < times ; i ++) {
49
+ duplicateRemover .isDuplicate (new Request (String .valueOf (i )), null );
50
+ }
51
+ System .out .println ("Time used by hashset:" + (System .currentTimeMillis () - time ));
52
+ System .out .println ("Memory used by hashset:" + (freeMemory - Runtime .getRuntime ().freeMemory ()));
53
+ }
54
+
55
+ @ Ignore ("long time" )
56
+ @ Test
57
+ public void testMissHit () throws Exception {
58
+ int times = 5000000 ;
59
+ DuplicateRemover duplicateRemover = new BloomFilterDuplicateRemover (times , 0.01 );
60
+ int right = 0 ;
61
+ int wrong = 0 ;
62
+ int missCheck = 0 ;
63
+ for (int i = 0 ; i < times ; i ++) {
64
+ boolean duplicate = duplicateRemover .isDuplicate (new Request (String .valueOf (i )), null );
65
+ if (duplicate ) {
66
+ wrong ++;
67
+ } else {
68
+ right ++;
69
+ }
70
+ duplicate = duplicateRemover .isDuplicate (new Request (String .valueOf (i )), null );
71
+ if (!duplicate ) {
72
+ missCheck ++;
73
+ }
74
+ }
75
+
76
+ System .out .println ("Right count: " + right + " Wrong count: " + wrong + " Miss check: " + missCheck );
77
+ }
78
+
79
+
27
80
}
0 commit comments