Use lookup table when calculating CRC16 XMODEM

* I borrowed it from b921931480
This commit is contained in:
Jungtaek Lim
2014-09-12 17:50:43 +09:00
parent 37a3c7b835
commit ad10f91b60
3 changed files with 114 additions and 15 deletions

View File

@@ -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");
}
}

View File

@@ -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;
}
}