mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-08-23 18:25:50 +00:00
ref[lpmap]: move lpmap from orm to protocol
This commit is contained in:
@@ -26,13 +26,14 @@ import java.util.concurrent.CountDownLatch;
|
||||
@Ignore
|
||||
public class ConcurrentTest {
|
||||
|
||||
private static final int EXECUTOR_SIZE = Runtime.getRuntime().availableProcessors();
|
||||
|
||||
@Test
|
||||
public void test() throws InterruptedException {
|
||||
var map = new CopyOnWriteHashMapLongObject<Integer>();
|
||||
var num = 1_0000;
|
||||
var executorSize = Runtime.getRuntime().availableProcessors();
|
||||
var countDownLatch = new CountDownLatch(executorSize);
|
||||
for (var i = 0; i < executorSize; i++) {
|
||||
var countDownLatch = new CountDownLatch(EXECUTOR_SIZE);
|
||||
for (var i = 0; i < EXECUTOR_SIZE; i++) {
|
||||
new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
@@ -46,8 +47,8 @@ public class ConcurrentTest {
|
||||
countDownLatch.await();
|
||||
Assert.assertEquals(map.size(), num);
|
||||
|
||||
var countDownLatch2 = new CountDownLatch(executorSize);
|
||||
for (var i = 0; i < executorSize; i++) {
|
||||
var countDownLatch2 = new CountDownLatch(EXECUTOR_SIZE);
|
||||
for (var i = 0; i < EXECUTOR_SIZE; i++) {
|
||||
new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
+73
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
* Copyright (C) 2020 The zfoo Authors
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
|
||||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.zfoo.protocol.collection.lpmap;
|
||||
|
||||
import com.zfoo.protocol.ProtocolManager;
|
||||
import com.zfoo.protocol.collection.lpmap.model.MyPacket;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
/**
|
||||
* @author godotg
|
||||
* @version 3.0
|
||||
*/
|
||||
@Ignore
|
||||
public class ConcurrentFileChannelMapTest {
|
||||
|
||||
private static final int EXECUTOR_SIZE = Runtime.getRuntime().availableProcessors();
|
||||
|
||||
@Test
|
||||
public void benchmarkTest() throws IOException, InterruptedException {
|
||||
ProtocolManager.initProtocol(Set.of(MyPacket.class));
|
||||
|
||||
var map = new ConcurrentFileChannelMap<MyPacket>("db", MyPacket.class);
|
||||
var atomicInt = new AtomicInteger(0);
|
||||
var count = 1000_0000;
|
||||
|
||||
var countdown = new CountDownLatch(EXECUTOR_SIZE);
|
||||
for (int i = 0; i < EXECUTOR_SIZE; i++) {
|
||||
new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
var key = atomicInt.getAndIncrement();
|
||||
while (key < count) {
|
||||
var myPacket = MyPacket.valueOf(key, String.valueOf(key));
|
||||
map.put(key, myPacket);
|
||||
key = atomicInt.getAndIncrement();
|
||||
}
|
||||
countdown.countDown();
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
countdown.await();
|
||||
for (var i = 0; i < count; i++) {
|
||||
var myPacket = MyPacket.valueOf(i, String.valueOf(i));
|
||||
var packet = map.get(i);
|
||||
Assert.assertEquals(myPacket, packet);
|
||||
}
|
||||
|
||||
map.close();
|
||||
var newMap = new ConcurrentFileChannelMap<MyPacket>("db", MyPacket.class);
|
||||
for (var i = 0; i < count; i++) {
|
||||
var myPacket = MyPacket.valueOf(i, String.valueOf(i));
|
||||
var packet = newMap.get(i);
|
||||
Assert.assertEquals(myPacket, packet);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
* Copyright (C) 2020 The zfoo Authors
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
|
||||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.zfoo.protocol.collection.lpmap;
|
||||
|
||||
import com.zfoo.protocol.collection.lpmap.model.MyPacket;
|
||||
import com.zfoo.protocol.ProtocolManager;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
/**
|
||||
* @author godotg
|
||||
* @version 3.0
|
||||
*/
|
||||
@Ignore
|
||||
public class ConcurrentHeapMapTest {
|
||||
|
||||
private static final int EXECUTOR_SIZE = Runtime.getRuntime().availableProcessors();
|
||||
|
||||
@Test
|
||||
public void putIfAbsentTest() {
|
||||
ProtocolManager.initProtocol(Set.of(MyPacket.class));
|
||||
var myPacket = new MyPacket();
|
||||
myPacket.setA(1);
|
||||
|
||||
var map = new ConcurrentHeapMap<MyPacket>();
|
||||
|
||||
var previous1 = map.put(1, myPacket);
|
||||
var previous2 = map.put(2, myPacket);
|
||||
var previous3 = map.put(2, new MyPacket());
|
||||
|
||||
Assert.assertNull(previous1);
|
||||
Assert.assertNull(previous2);
|
||||
Assert.assertEquals(previous3, myPacket);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void benchmarkTest() throws IOException, InterruptedException {
|
||||
ProtocolManager.initProtocol(Set.of(MyPacket.class));
|
||||
|
||||
var map = new ConcurrentHeapMap<MyPacket>();
|
||||
var atomicInt = new AtomicInteger(0);
|
||||
var count = 1000_0000;
|
||||
|
||||
var countdown = new CountDownLatch(EXECUTOR_SIZE);
|
||||
for (int i = 0; i < EXECUTOR_SIZE; i++) {
|
||||
new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
var key = atomicInt.getAndIncrement();
|
||||
while (key < count) {
|
||||
var myPacket = MyPacket.valueOf(key, String.valueOf(key));
|
||||
map.put(key, myPacket);
|
||||
key = atomicInt.getAndIncrement();
|
||||
}
|
||||
countdown.countDown();
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
countdown.await();
|
||||
for (var i = 0; i < count; i++) {
|
||||
var myPacket = MyPacket.valueOf(i, String.valueOf(i));
|
||||
var packet = map.get(i);
|
||||
Assert.assertEquals(myPacket, packet);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
/*
|
||||
* Copyright (C) 2020 The zfoo Authors
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
|
||||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.zfoo.protocol.collection.lpmap;
|
||||
|
||||
import com.zfoo.protocol.collection.lpmap.model.MyPacket;
|
||||
import com.zfoo.protocol.ProtocolManager;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @author godotg
|
||||
* @version 3.0
|
||||
*/
|
||||
@Ignore
|
||||
public class FileChannelMapTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
ProtocolManager.initProtocol(Set.of(MyPacket.class));
|
||||
|
||||
var map = new FileChannelMap<MyPacket>("db", MyPacket.class);
|
||||
var myPacket = new MyPacket();
|
||||
myPacket.setA(9999);
|
||||
|
||||
map.put(0, myPacket);
|
||||
map.put(1, myPacket);
|
||||
map.put(2, myPacket);
|
||||
map.put(3, myPacket);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readTest() {
|
||||
ProtocolManager.initProtocol(Set.of(MyPacket.class));
|
||||
|
||||
var map = new FileChannelMap<MyPacket>("db", MyPacket.class);
|
||||
|
||||
System.out.println(map.get(1));
|
||||
System.out.println(map.get(2));
|
||||
System.out.println(map.get(3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void channelHeapTest() {
|
||||
ProtocolManager.initProtocol(Set.of(MyPacket.class));
|
||||
|
||||
var map = new FileChannelHeapMap<MyPacket>("db", 1000, MyPacket.class);
|
||||
|
||||
System.out.println(map.get(1));
|
||||
System.out.println(map.get(2));
|
||||
System.out.println(map.get(3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void benchmarkTest() throws IOException {
|
||||
ProtocolManager.initProtocol(Set.of(MyPacket.class));
|
||||
|
||||
var map = new FileChannelMap<MyPacket>("db", MyPacket.class);
|
||||
var count = 1000_0000;
|
||||
for (var i = 0; i < count; i++) {
|
||||
var myPacket = MyPacket.valueOf(i, String.valueOf(i));
|
||||
map.put(i, myPacket);
|
||||
}
|
||||
|
||||
for (var i = 0; i < count; i++) {
|
||||
var myPacket = MyPacket.valueOf(i, String.valueOf(i));
|
||||
var packet = map.get(i);
|
||||
Assert.assertEquals(myPacket, packet);
|
||||
}
|
||||
map.close();
|
||||
map = new FileChannelMap<MyPacket>("db", MyPacket.class);
|
||||
for (var i = 0; i < count; i++) {
|
||||
var myPacket = MyPacket.valueOf(i, String.valueOf(i));
|
||||
var packet = map.get(i);
|
||||
Assert.assertEquals(myPacket, packet);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
/*
|
||||
* Copyright (C) 2020 The zfoo Authors
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
|
||||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.zfoo.protocol.collection.lpmap;
|
||||
|
||||
import com.zfoo.protocol.collection.lpmap.model.MyPacket;
|
||||
import com.zfoo.protocol.ProtocolManager;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @author godotg
|
||||
* @version 3.0
|
||||
*/
|
||||
@Ignore
|
||||
public class FileHeapMapTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
ProtocolManager.initProtocol(Set.of(MyPacket.class));
|
||||
|
||||
var map = new FileHeapMap<MyPacket>("db", MyPacket.class);
|
||||
var myPacket = new MyPacket();
|
||||
myPacket.setA(9999);
|
||||
|
||||
var packet = map.put(1, myPacket);
|
||||
Assert.assertNull(packet);
|
||||
|
||||
packet = map.put(2, myPacket);
|
||||
Assert.assertNull(packet);
|
||||
|
||||
packet = map.delete(4);
|
||||
Assert.assertNull(packet);
|
||||
|
||||
packet = map.delete(2);
|
||||
Assert.assertEquals(packet, myPacket);
|
||||
|
||||
map.put(1, myPacket);
|
||||
map.put(2, myPacket);
|
||||
map.put(3, myPacket);
|
||||
map.put(4, myPacket);
|
||||
map.put(5, myPacket);
|
||||
map.save();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void benchmarkTest() {
|
||||
ProtocolManager.initProtocol(Set.of(MyPacket.class));
|
||||
|
||||
var map = new FileHeapMap<MyPacket>("db", MyPacket.class);
|
||||
var count = 1000_0000;
|
||||
for (var i = 0; i < count; i++) {
|
||||
var myPacket = MyPacket.valueOf(i, String.valueOf(i));
|
||||
map.put(i, myPacket);
|
||||
}
|
||||
|
||||
for (var i = 0; i < count; i++) {
|
||||
var myPacket = MyPacket.valueOf(i, String.valueOf(i));
|
||||
var packet = map.get(i);
|
||||
Assert.assertEquals(myPacket, packet);
|
||||
}
|
||||
map.save();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void loadTest() {
|
||||
ProtocolManager.initProtocol(Set.of(MyPacket.class));
|
||||
|
||||
var map = new FileHeapMap<MyPacket>("db", MyPacket.class);
|
||||
var count = 1000_0000;
|
||||
|
||||
for (var i = 0; i < count; i++) {
|
||||
var myPacket = MyPacket.valueOf(i, String.valueOf(i));
|
||||
var packet = map.get(i);
|
||||
Assert.assertEquals(myPacket, packet);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Copyright (C) 2020 The zfoo Authors
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
|
||||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.zfoo.protocol.collection.lpmap;
|
||||
|
||||
import com.zfoo.protocol.collection.lpmap.model.MyPacket;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @author godotg
|
||||
* @version 3.0
|
||||
*/
|
||||
@Ignore
|
||||
public class HeapMapTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
var map = new HeapMap<MyPacket>();
|
||||
var myPacket = new MyPacket();
|
||||
|
||||
var packet = map.put(0, myPacket);
|
||||
Assert.assertNull(packet);
|
||||
|
||||
packet = map.put(3, myPacket);
|
||||
Assert.assertNull(packet);
|
||||
|
||||
packet = map.delete(4);
|
||||
Assert.assertNull(packet);
|
||||
|
||||
packet = map.delete(3);
|
||||
Assert.assertEquals(packet, myPacket);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void benchmarkTest() {
|
||||
var map = new HeapMap<MyPacket>();
|
||||
|
||||
var count = 1000_0000;
|
||||
for (var i = 0; i < count; i++) {
|
||||
var myPacket = MyPacket.valueOf(i, String.valueOf(i));
|
||||
map.put(i, myPacket);
|
||||
}
|
||||
|
||||
for (var i = 0; i < count; i++) {
|
||||
var myPacket = MyPacket.valueOf(i, String.valueOf(i));
|
||||
var packet = map.get(i);
|
||||
Assert.assertEquals(myPacket, packet);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
* Copyright (C) 2020 The zfoo Authors
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
|
||||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.zfoo.protocol.collection.lpmap.model;
|
||||
|
||||
import com.zfoo.protocol.IPacket;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* @author godotg
|
||||
* @version 3.0
|
||||
*/
|
||||
public class MyPacket implements IPacket {
|
||||
|
||||
public static final transient short PROTOCOL_ID = 1;
|
||||
|
||||
private int a;
|
||||
|
||||
private String b;
|
||||
|
||||
public static MyPacket valueOf(int a, String b) {
|
||||
var packet = new MyPacket();
|
||||
packet.a = a;
|
||||
packet.b = b;
|
||||
return packet;
|
||||
}
|
||||
|
||||
@Override
|
||||
public short protocolId() {
|
||||
return PROTOCOL_ID;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
MyPacket myPacket = (MyPacket) o;
|
||||
return a == myPacket.a && Objects.equals(b, myPacket.b);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(a, b);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
final StringBuilder sb = new StringBuilder("MyPacket{");
|
||||
sb.append("a=").append(a);
|
||||
sb.append(", b='").append(b).append('\'');
|
||||
sb.append('}');
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public int getA() {
|
||||
return a;
|
||||
}
|
||||
|
||||
public void setA(int a) {
|
||||
this.a = a;
|
||||
}
|
||||
|
||||
public String getB() {
|
||||
return b;
|
||||
}
|
||||
|
||||
public void setB(String b) {
|
||||
this.b = b;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user