diff --git a/src/main/java/cn/langpy/kotime/service/KoInvokedHandler.java b/src/main/java/cn/langpy/kotime/service/KoInvokedHandler.java index 1b5185b..26dfddd 100644 --- a/src/main/java/cn/langpy/kotime/service/KoInvokedHandler.java +++ b/src/main/java/cn/langpy/kotime/service/KoInvokedHandler.java @@ -4,9 +4,11 @@ import cn.langpy.kotime.annotation.KoListener; import cn.langpy.kotime.handler.InvokedHandler; import cn.langpy.kotime.model.ExceptionNode; import cn.langpy.kotime.model.MethodNode; +import cn.langpy.kotime.util.BoomFilter; import cn.langpy.kotime.util.Common; import cn.langpy.kotime.util.Context; +import javax.annotation.Resource; import java.lang.reflect.Parameter; import java.util.logging.Logger; @@ -14,12 +16,11 @@ import java.util.logging.Logger; public final class KoInvokedHandler implements InvokedHandler { private static Logger log = Logger.getLogger(KoInvokedHandler.class.toString()); - @Override public void onInvoked(MethodNode current, MethodNode parent, Parameter[] names, Object[] values) { GraphService graphService = GraphService.getInstance(); - graphService.addMethodNode(parent); - graphService.addMethodNode(current); + graphService.addMethodNode(filter(parent)); + graphService.addMethodNode(filter(current)); graphService.addMethodRelation(parent, current); if (Context.getConfig().getParamAnalyse()) { graphService.addParamAnalyse(current.getId(), names, values, current.getValue()); @@ -36,4 +37,13 @@ public final class KoInvokedHandler implements InvokedHandler { graphService.addExceptionNode(exception); graphService.addExceptionRelation(current, exception); } + + public MethodNode filter(MethodNode currentNode) { + if (BoomFilter.exists(currentNode.getId())) { + return null; + } else { + BoomFilter.add(currentNode.getId()); + return currentNode; + } + } } diff --git a/src/main/java/cn/langpy/kotime/util/BoomFilter.java b/src/main/java/cn/langpy/kotime/util/BoomFilter.java new file mode 100644 index 0000000..329727f --- /dev/null +++ b/src/main/java/cn/langpy/kotime/util/BoomFilter.java @@ -0,0 +1,51 @@ +package cn.langpy.kotime.util; + + +import java.util.ArrayList; +import java.util.BitSet; +import java.util.List; +import java.util.function.BiFunction; +import java.util.logging.Logger; + +public class BoomFilter { + private static Logger log = Logger.getLogger(BoomFilter.class.toString()); + + private final static int size = 1000000; + private final static BitSet bits = new BitSet(size); + private final static int[] hashSeeds = new int[]{3, 113, 919, 2203, 4013, 6011}; + + private final static List> hashFuncs = new ArrayList<>(); + + static { + for (int i = 0; i < hashSeeds.length; i++) { + hashFuncs.add((v, s) -> hash(v, s)); + } + } + + public static void add(String value) { + for (int i = 0; i < hashFuncs.size(); i++) { + BiFunction hashFunc = hashFuncs.get(i); + int hashSeed = hashSeeds[i]; + Integer bitHash = hashFunc.apply(value, hashSeed); + bits.set(bitHash, true); + } + } + + public static boolean exists(String value) { + for (int i = 0; i < hashFuncs.size(); i++) { + BiFunction hashFunc = hashFuncs.get(i); + int hashSeed = hashSeeds[i]; + Integer bitHash = hashFunc.apply(value, hashSeed); + boolean isTrue = bits.get(bitHash); + if (isTrue == false) { + return false; + } + } + return true; + } + + + public static int hash(String value, int seed) { + return Math.abs(value.hashCode() * seed + 33) % size; + } +}