1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase.io.hfile;
21
22 import java.io.BufferedReader;
23 import java.io.FileReader;
24 import java.io.IOException;
25 import java.nio.ByteBuffer;
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.Random;
29
30 import org.apache.hadoop.conf.Configuration;
31 import org.apache.hadoop.fs.LocalFileSystem;
32 import org.apache.hadoop.fs.Path;
33 import org.apache.hadoop.fs.RawLocalFileSystem;
34 import org.apache.hadoop.hbase.io.hfile.HFile.Reader;
35 import org.apache.hadoop.hbase.util.Bytes;
36
37
38
39
40 public class RandomSeek {
41 private static List<String> slurp(String fname) throws IOException {
42 BufferedReader istream = new BufferedReader(new FileReader(fname));
43 String str;
44 List<String> l = new ArrayList<String>();
45 while ( (str=istream.readLine()) != null) {
46 String [] parts = str.split(",");
47 l.add(parts[0] + ":" + parts[1] + ":" + parts[2]);
48 }
49 istream.close();
50 return l;
51 }
52
53 private static String randKey(List<String> keys) {
54 Random r = new Random();
55
56 return "2" + Integer.toString(7+r.nextInt(2)) + Integer.toString(r.nextInt(100));
57
58 }
59
60 public static void main(String [] argv) throws IOException {
61 Configuration conf = new Configuration();
62 conf.setInt("io.file.buffer.size", 64*1024);
63 RawLocalFileSystem rlfs = new RawLocalFileSystem();
64 rlfs.setConf(conf);
65 LocalFileSystem lfs = new LocalFileSystem(rlfs);
66
67 Path path = new Path("/Users/ryan/rfile.big.txt");
68 long start = System.currentTimeMillis();
69 SimpleBlockCache cache = new SimpleBlockCache();
70 CacheConfig cacheConf = new CacheConfig(cache, true, false, false, false,
71 false, false, false);
72
73 Reader reader = HFile.createReader(lfs, path, cacheConf);
74 reader.loadFileInfo();
75 System.out.println(reader.getTrailer());
76 long end = System.currentTimeMillis();
77
78 System.out.println("Index read time: " + (end - start));
79
80 List<String> keys = slurp("/Users/ryan/xaa.50k");
81
82
83 HFileScanner scanner = reader.getScanner(false, true);
84 int count;
85 long totalBytes = 0;
86 int notFound = 0;
87
88 start = System.nanoTime();
89 for(count = 0; count < 500000; ++count) {
90 String key = randKey(keys);
91 byte [] bkey = Bytes.toBytes(key);
92 int res = scanner.seekTo(bkey);
93 if (res == 0) {
94 ByteBuffer k = scanner.getKey();
95 ByteBuffer v = scanner.getValue();
96 totalBytes += k.limit();
97 totalBytes += v.limit();
98 } else {
99 ++ notFound;
100 }
101 if (res == -1) {
102 scanner.seekTo();
103 }
104
105 for (int i = 0; i < 1000; ++i) {
106 if (!scanner.next())
107 break;
108 ByteBuffer k = scanner.getKey();
109 ByteBuffer v = scanner.getValue();
110 totalBytes += k.limit();
111 totalBytes += v.limit();
112 }
113
114 if ( count % 1000 == 0 ) {
115 end = System.nanoTime();
116
117 System.out.println("Cache block count: " + cache.size() + " dumped: "+ cache.dumps);
118
119 double msTime = ((end - start) / 1000000.0);
120 System.out.println("Seeked: "+ count + " in " + msTime + " (ms) "
121 + (1000.0 / msTime ) + " seeks/ms "
122 + (msTime / 1000.0) + " ms/seek");
123
124 start = System.nanoTime();
125 }
126 }
127 System.out.println("Total bytes: " + totalBytes + " not found: " + notFound);
128 }
129 }