Use lookup table when calculating CRC16 XMODEM
* I borrowed it from b921931480
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
package redis.clients.jedis.tests.benchmark;
|
||||
|
||||
import java.util.Calendar;
|
||||
|
||||
import redis.clients.util.JedisClusterCRC16;
|
||||
|
||||
public class CRC16Benchmark {
|
||||
private static final int TOTAL_OPERATIONS = 10000000;
|
||||
|
||||
private static String[] TEST_SET = {
|
||||
"", "123456789", "sfger132515", "hae9Napahngaikeethievubaibogiech",
|
||||
"AAAAAAAAAAAAAAAAAAAAAA", "Hello, World!"
|
||||
};
|
||||
|
||||
public static void main(String[] args) {
|
||||
long begin = Calendar.getInstance().getTimeInMillis();
|
||||
|
||||
for (int n = 0; n <= TOTAL_OPERATIONS; n++) {
|
||||
JedisClusterCRC16.getSlot(TEST_SET[n % TEST_SET.length]);
|
||||
}
|
||||
|
||||
long elapsed = Calendar.getInstance().getTimeInMillis() - begin;
|
||||
|
||||
System.out.println(((1000 * TOTAL_OPERATIONS) / elapsed) + " ops");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package redis.clients.jedis.tests.utils;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import redis.clients.util.JedisClusterCRC16;
|
||||
|
||||
public class JedisClusterCRC16Test {
|
||||
|
||||
@Test
|
||||
public void testGetCRC16() throws Exception {
|
||||
Map<String, Integer> solutions = prepareSolutionSet();
|
||||
|
||||
for (Entry<String, Integer> entry : solutions.entrySet()) {
|
||||
// string version
|
||||
assertEquals(entry.getValue().intValue(), JedisClusterCRC16.getCRC16(entry.getKey()));
|
||||
|
||||
// byte array version
|
||||
assertEquals(entry.getValue().intValue(), JedisClusterCRC16.getCRC16(entry.getKey().getBytes()));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetSlot() {
|
||||
assertEquals(7186, JedisClusterCRC16.getSlot("51"));
|
||||
}
|
||||
|
||||
private Map<String, Integer> prepareSolutionSet() {
|
||||
Map<String, Integer> solutionMap = new HashMap<String, Integer>();
|
||||
solutionMap.put("", 0x0);
|
||||
solutionMap.put("123456789", 0x31C3);
|
||||
solutionMap.put("sfger132515", 0xA45C);
|
||||
solutionMap.put("hae9Napahngaikeethievubaibogiech", 0x58CE);
|
||||
solutionMap.put("AAAAAAAAAAAAAAAAAAAAAA", 0x92cd);
|
||||
solutionMap.put("Hello, World!", 0x4FD6);
|
||||
return solutionMap;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user