add MetroHash

This commit is contained in:
Looly
2022-02-11 17:35:05 +08:00
parent f239ba901f
commit 37f9220e50
14 changed files with 273 additions and 214 deletions

View File

@@ -0,0 +1,36 @@
package cn.hutool.core.lang.hash;
import cn.hutool.core.util.StrUtil;
import org.junit.Assert;
import org.junit.Test;
public class CityHashTest {
@Test
public void hash32Test() {
int hv = CityHash.hash32(StrUtil.utf8Bytes(""));
Assert.assertEquals(1290029860, hv);
hv = CityHash.hash32(StrUtil.utf8Bytes("你好"));
Assert.assertEquals(1374181357, hv);
hv = CityHash.hash32(StrUtil.utf8Bytes("见到你很高兴"));
Assert.assertEquals(1475516842, hv);
hv = CityHash.hash32(StrUtil.utf8Bytes("我们将通过生成一个大的文件的方式来检验各种方法的执行效率因为这种方式在结束的时候需要执行文件"));
Assert.assertEquals(0x51020cae, hv);
}
@Test
public void hash64Test() {
long hv = CityHash.hash64(StrUtil.utf8Bytes(""));
Assert.assertEquals(-4296898700418225525L, hv);
hv = CityHash.hash64(StrUtil.utf8Bytes("你好"));
Assert.assertEquals(-4294276205456761303L, hv);
hv = CityHash.hash64(StrUtil.utf8Bytes("见到你很高兴"));
Assert.assertEquals(272351505337503793L, hv);
hv = CityHash.hash64(StrUtil.utf8Bytes("我们将通过生成一个大的文件的方式来检验各种方法的执行效率因为这种方式在结束的时候需要执行文件"));
Assert.assertEquals(-8234735310919228703L, hv);
}
}

View File

@@ -1,19 +1,45 @@
package cn.hutool.core.lang;
package cn.hutool.core.lang.hash;
import cn.hutool.core.lang.hash.CityHash;
import cn.hutool.core.lang.hash.MetroHash;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.HexUtil;
import cn.hutool.core.util.StrUtil;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import java.util.Random;
/**
* https://gitee.com/dromara/hutool/pulls/532
*/
public class MetroHashTest {
@Test
public void testEmpty() {
Assert.assertEquals("31290877cceaea29", HexUtil.toHex(MetroHash.hash64(StrUtil.utf8Bytes(""), 0)));
}
@Test
public void metroHash64Test() {
byte[] str = "我是一段测试123".getBytes(CharsetUtil.CHARSET_UTF_8);
final long hash64 = MetroHash.hash64(str);
Assert.assertEquals(62920234463891865L, hash64);
}
@Test
public void metroHash128Test() {
byte[] str = "我是一段测试123".getBytes(CharsetUtil.CHARSET_UTF_8);
final long[] hash128 = MetroHash.hash128(str).getLongArray();
Assert.assertEquals(4629350038757384271L, hash128[0]);
Assert.assertEquals(-1607305036506152112L, hash128[1]);
}
/**
* 数据量越大 MetroHash 优势越明显
*/
@Test
@Ignore
public void bulkHashing64Test() {
String[] strArray = getRandomStringArray(10000000);
long startCity = System.currentTimeMillis();
@@ -24,7 +50,7 @@ public class MetroHashTest {
long startMetro = System.currentTimeMillis();
for (String s : strArray) {
MetroHash.hash64(s);
MetroHash.hash64(StrUtil.utf8Bytes(s));
}
long endMetro = System.currentTimeMillis();
@@ -37,6 +63,7 @@ public class MetroHashTest {
* 数据量越大 MetroHash 优势越明显
*/
@Test
@Ignore
public void bulkHashing128Test() {
String[] strArray = getRandomStringArray(10000000);
long startCity = System.currentTimeMillis();
@@ -47,7 +74,7 @@ public class MetroHashTest {
long startMetro = System.currentTimeMillis();
for (String s : strArray) {
MetroHash.hash128(s);
MetroHash.hash128(StrUtil.utf8Bytes(s));
}
long endMetro = System.currentTimeMillis();

View File

@@ -0,0 +1,36 @@
package cn.hutool.core.lang.hash;
import cn.hutool.core.util.StrUtil;
import org.junit.Assert;
import org.junit.Test;
public class MurMurHashTest {
@Test
public void hash32Test() {
int hv = MurmurHash.hash32(StrUtil.utf8Bytes(""));
Assert.assertEquals(222142701, hv);
hv = MurmurHash.hash32(StrUtil.utf8Bytes("你好"));
Assert.assertEquals(1188098267, hv);
hv = MurmurHash.hash32(StrUtil.utf8Bytes("见到你很高兴"));
Assert.assertEquals(-1898490321, hv);
hv = MurmurHash.hash32(StrUtil.utf8Bytes("我们将通过生成一个大的文件的方式来检验各种方法的执行效率因为这种方式在结束的时候需要执行文件"));
Assert.assertEquals(-1713131054, hv);
}
@Test
public void hash64Test() {
long hv = MurmurHash.hash64(StrUtil.utf8Bytes(""));
Assert.assertEquals(-1349759534971957051L, hv);
hv = MurmurHash.hash64(StrUtil.utf8Bytes("你好"));
Assert.assertEquals(-7563732748897304996L, hv);
hv = MurmurHash.hash64(StrUtil.utf8Bytes("见到你很高兴"));
Assert.assertEquals(-766658210119995316L, hv);
hv = MurmurHash.hash64(StrUtil.utf8Bytes("我们将通过生成一个大的文件的方式来检验各种方法的执行效率因为这种方式在结束的时候需要执行文件"));
Assert.assertEquals(-7469283059271653317L, hv);
}
}

View File

@@ -10,9 +10,16 @@ public class ByteUtilTest {
@Test
public void intAndBytesLittleEndianTest() {
// 测试 int 转小端序 byte 数组
int int1 = RandomUtil.randomInt();
int int1 = RandomUtil.randomInt((Integer.MAX_VALUE));
ByteBuffer buffer = ByteBuffer.allocate(Integer.BYTES);
buffer.order(ByteOrder.LITTLE_ENDIAN);
buffer.putInt(int1);
byte[] bytesIntFromBuffer = buffer.array();
byte[] bytesInt = ByteUtil.intToBytes(int1, ByteOrder.LITTLE_ENDIAN);
Assert.assertArrayEquals(bytesIntFromBuffer, bytesInt);
int int2 = ByteUtil.bytesToInt(bytesInt, ByteOrder.LITTLE_ENDIAN);
Assert.assertEquals(int1, int2);
@@ -28,8 +35,14 @@ public class ByteUtilTest {
@Test
public void intAndBytesBigEndianTest() {
// 测试 int 转大端序 byte 数组
int int2 = RandomUtil.randomInt();
int int2 = RandomUtil.randomInt(Integer.MAX_VALUE);
ByteBuffer buffer = ByteBuffer.allocate(Integer.BYTES);
buffer.putInt(int2);
byte[] bytesIntFromBuffer = buffer.array();
byte[] bytesInt = ByteUtil.intToBytes(int2, ByteOrder.BIG_ENDIAN);
Assert.assertArrayEquals(bytesIntFromBuffer, bytesInt);
// 测试大端序 byte 数组转 int
int int3 = ByteUtil.bytesToInt(bytesInt, ByteOrder.BIG_ENDIAN);
@@ -39,9 +52,16 @@ public class ByteUtilTest {
@Test
public void longAndBytesLittleEndianTest() {
// 测试 long 转 byte 数组
long long1 = 2223;
long long1 = RandomUtil.randomLong(Long.MAX_VALUE);
ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
buffer.order(ByteOrder.LITTLE_ENDIAN);
buffer.putLong(long1);
byte[] bytesLongFromBuffer = buffer.array();
byte[] bytesLong = ByteUtil.longToBytes(long1, ByteOrder.LITTLE_ENDIAN);
Assert.assertArrayEquals(bytesLongFromBuffer, bytesLong);
long long2 = ByteUtil.bytesToLong(bytesLong, ByteOrder.LITTLE_ENDIAN);
Assert.assertEquals(long1, long2);
@@ -57,11 +77,16 @@ public class ByteUtilTest {
@Test
public void longAndBytesBigEndianTest() {
// 测试大端序 long 转 byte 数组
long long1 = 2223;
long long1 = RandomUtil.randomLong(Long.MAX_VALUE);
ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
buffer.putLong(long1);
byte[] bytesLongFromBuffer = buffer.array();
byte[] bytesLong = ByteUtil.longToBytes(long1, ByteOrder.BIG_ENDIAN);
long long2 = ByteUtil.bytesToLong(bytesLong, ByteOrder.BIG_ENDIAN);
Assert.assertArrayEquals(bytesLongFromBuffer, bytesLong);
long long2 = ByteUtil.bytesToLong(bytesLong, ByteOrder.BIG_ENDIAN);
Assert.assertEquals(long1, long2);
}