Skip to content

Commit

Permalink
perf[cache]: assert construct param
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysunxiao committed Mar 27, 2024
1 parent faace1d commit b3e6109
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.zfoo.protocol.model.Pair;
import com.zfoo.protocol.model.PairLong;
import com.zfoo.protocol.util.AssertionUtils;

import java.util.ArrayList;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -62,12 +63,15 @@ public static enum RemovalCause {
};

public LazyCache(int maximumSize, long expireAfterAccessMillis, long expireCheckIntervalMillis, BiConsumer<Pair<K, V>, RemovalCause> removeListener) {
AssertionUtils.ge1(maximumSize);
AssertionUtils.ge0(expireAfterAccessMillis);
AssertionUtils.ge0(expireCheckIntervalMillis);
this.maximumSize = maximumSize;
this.backPressureSize = Math.max(maximumSize, maximumSize + (int) (maximumSize * DEFAULT_BACK_PRESSURE_FACTOR));
this.expireAfterAccessMillis = expireAfterAccessMillis;
this.expireCheckIntervalMillis = expireCheckIntervalMillis;
this.expireCheckIntervalMillis = Math.max(1, expireCheckIntervalMillis);
this.minExpireTime = TimeUtils.now();
this.expireCheckTimeAtomic = new AtomicLong(TimeUtils.now() + expireCheckIntervalMillis);
this.expireCheckTimeAtomic = new AtomicLong(TimeUtils.now());
this.cacheMap = new ConcurrentHashMap<>(Math.max(maximumSize / 16, 512));
if (removeListener != null) {
this.removeListener = removeListener;
Expand Down
59 changes: 59 additions & 0 deletions scheduler/src/test/java/com/zfoo/scheduler/util/LazyCacheTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,65 @@ public void run() {
}
}

// check interval test
@Test
public void multipleThreadCheckIntervalTest() {
int threadNum = Runtime.getRuntime().availableProcessors() + 1;
ExecutorService[] executors = new ExecutorService[threadNum];
for (int i = 0; i < executors.length; i++) {
executors[i] = Executors.newSingleThreadExecutor();
}
var lazyCache = new LazyCache<Integer, String>(1_0000, 10000000 * TimeUtils.MILLIS_PER_SECOND, 0, myRemoveCallback);
for (int i = 0; i < executors.length; i++) {

var executor = executors[i];
int i1 = i;
executor.execute(new Runnable() {
@Override
public void run() {
var startIndex = i1 * 1_0000;
for (int j = i1 * 1_0000; j < startIndex + 1_0000; j++) {
lazyCache.put(j, String.valueOf(j));
}
}
});
}
for (int i = 0; i < 10000; i++) {
logger.info("cache size:[{}]", lazyCache.size());
ThreadUtils.sleep(1000);
}
}

// expire after access test
@Test
public void multipleThreadExpireAfterAccessTest() {
int threadNum = Runtime.getRuntime().availableProcessors() + 1;
ExecutorService[] executors = new ExecutorService[threadNum];
for (int i = 0; i < executors.length; i++) {
executors[i] = Executors.newSingleThreadExecutor();
}
var lazyCache = new LazyCache<Integer, String>(1_0000, 0, 0, null);
for (int i = 0; i < executors.length; i++) {

var executor = executors[i];
int i1 = i;
executor.execute(new Runnable() {
@Override
public void run() {
var startIndex = i1 * 1_0000;
for (int j = i1 * 1_0000; j < startIndex + 1_0000; j++) {
lazyCache.put(j, String.valueOf(j));
}
}
});
}
for (int i = 0; i < 10000; i++) {
lazyCache.get(-1);
logger.info("cache size:[{}]", lazyCache.size());
ThreadUtils.sleep(100);
}
}

// not expired test
@Test
public void multipleThreadNotExpiredTest() {
Expand Down

0 comments on commit b3e6109

Please sign in to comment.