diff --git a/src/main/java/com/thebeastshop/liteflow/monitor/MonitorBus.java b/src/main/java/com/thebeastshop/liteflow/monitor/MonitorBus.java index ae740cc19..37007d587 100644 --- a/src/main/java/com/thebeastshop/liteflow/monitor/MonitorBus.java +++ b/src/main/java/com/thebeastshop/liteflow/monitor/MonitorBus.java @@ -20,6 +20,7 @@ import java.util.concurrent.ConcurrentHashMap; import org.slf4j.Logger; import org.slf4j.LoggerFactory; + import com.thebeastshop.liteflow.entity.data.DataBus; import com.thebeastshop.liteflow.entity.monitor.CompStatistics; import com.thebeastshop.liteflow.util.LimitQueue; @@ -28,6 +29,8 @@ public class MonitorBus { private static final int QUEUE_LIMIT_SIZE = 200; + private static final Logger LOG = LoggerFactory.getLogger(MonitorBus.class); + private static ConcurrentHashMap> statisticsMap = new ConcurrentHashMap>(); static{ @@ -36,7 +39,7 @@ public class MonitorBus { public void run() { MonitorBus.printStatistics(); } - }, 30*1000L, 10*60*1000L); + }, 30*1000L, 1*60*1000L); } public static void addStatistics(CompStatistics statistics){ @@ -50,32 +53,36 @@ public class MonitorBus { } public static void printStatistics(){ - Map compAverageTimeSpent = new HashMap(); - Map compAverageMemorySpent = new HashMap(); - - long totalTimeSpent = 0; - long totalMemorySpent = 0; - - for(Entry> entry : statisticsMap.entrySet()){ - for(CompStatistics statistics : entry.getValue()){ - totalTimeSpent += statistics.getTimeSpent(); - totalMemorySpent += statistics.getMemorySpent(); + try{ + Map compAverageTimeSpent = new HashMap(); + Map compAverageMemorySpent = new HashMap(); + + long totalTimeSpent = 0; + long totalMemorySpent = 0; + + for(Entry> entry : statisticsMap.entrySet()){ + for(CompStatistics statistics : entry.getValue()){ + totalTimeSpent += statistics.getTimeSpent(); + totalMemorySpent += statistics.getMemorySpent(); + } + compAverageTimeSpent.put(entry.getKey(), new BigDecimal(totalTimeSpent).divide(new BigDecimal(entry.getValue().size()), 2, RoundingMode.HALF_UP).longValue()); + compAverageMemorySpent.put(entry.getKey(), new BigDecimal(totalMemorySpent).divide(new BigDecimal(entry.getValue().size()), 2, RoundingMode.HALF_UP).longValue()); } - compAverageTimeSpent.put(entry.getKey(), new BigDecimal(totalTimeSpent).divide(new BigDecimal(entry.getValue().size()), 2, RoundingMode.HALF_UP).longValue()); - compAverageMemorySpent.put(entry.getKey(), new BigDecimal(totalMemorySpent).divide(new BigDecimal(entry.getValue().size()), 2, RoundingMode.HALF_UP).longValue()); + System.out.println("======================================================================================"); + System.out.println("===================================SLOT INFO=========================================="); + System.out.println("SLOT TOTAL SIZE : "+DataBus.SLOT_SIZE); + System.out.println("SLOT OCCUPY COUNT : "+DataBus.OCCUPY_COUNT); + System.out.println("===============================TIME AVERAGE SPENT====================================="); + for(Entry entry : compAverageTimeSpent.entrySet()){ + System.out.println("COMPONENT["+entry.getKey()+"] AVERAGE TIME SPENT : " + entry.getValue()); + } + System.out.println("==============================MEMORY AVERAGE SPENT===================================="); + for(Entry entry : compAverageMemorySpent.entrySet()){ + System.out.println("COMPONENT["+entry.getKey()+"] AVERAGE MEMORY SPENT : "+ new BigDecimal(entry.getValue()).divide(new BigDecimal(1024), 2, RoundingMode.HALF_UP) + "K"); + } + System.out.println("======================================================================================"); + }catch(Exception e){ + LOG.error("print statistics cause error",e); } - System.out.println("======================================================================================"); - System.out.println("===================================SLOT INFO=========================================="); - System.out.println("SLOT TOTAL SIZE : "+DataBus.SLOT_SIZE); - System.out.println("SLOT OCCUPY COUNT : "+DataBus.OCCUPY_COUNT); - System.out.println("===============================TIME AVERAGE SPENT====================================="); - for(Entry entry : compAverageTimeSpent.entrySet()){ - System.out.println("COMPONENT["+entry.getKey()+"] AVERAGE TIME SPENT : " + entry.getValue()); - } - System.out.println("==============================MEMORY AVERAGE SPENT===================================="); - for(Entry entry : compAverageMemorySpent.entrySet()){ - System.out.println("COMPONENT["+entry.getKey()+"] AVERAGE MEMORY SPENT : "+ new BigDecimal(entry.getValue()).divide(new BigDecimal(1024), 2, RoundingMode.HALF_UP) + "K"); - } - System.out.println("======================================================================================"); } } diff --git a/src/test/java/com/thebeastshop/liteflow/test/TestWithSpringMain.java b/src/test/java/com/thebeastshop/liteflow/test/TestWithSpringMain.java index 6ed6b1e05..55de356f1 100644 --- a/src/test/java/com/thebeastshop/liteflow/test/TestWithSpringMain.java +++ b/src/test/java/com/thebeastshop/liteflow/test/TestWithSpringMain.java @@ -1,5 +1,8 @@ package com.thebeastshop.liteflow.test; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + import javax.annotation.Resource; import org.junit.Test; @@ -20,7 +23,18 @@ public class TestWithSpringMain { @Test public void test1() throws Exception { - String response = flowExecutor.execute("chain2", "it's a request"); - System.out.println(response); + ExecutorService executorService = Executors.newFixedThreadPool(10); + + for(int i=0;i<100;i++){ + executorService.submit(new Thread(){ + @Override + public void run() { + String response = flowExecutor.execute("chain2", "it's a request"); + System.out.println(response); + } + }); + } + System.out.println("done!"); + System.in.read(); } }