From aa94f7fe63038b6537e1e714523666ffc023fa79 Mon Sep 17 00:00:00 2001 From: "yong.you" Date: Tue, 26 Nov 2013 20:54:22 +0800 Subject: [PATCH] add the testcase --- README.md | 2 +- .../metric/MetricConfigManagerTest.java | 108 +---- .../metric/ProductLineConfigManagerTest.java | 2 +- .../cat/consumer/dump/DumpAnalyzer.java | 12 + .../com/dianping/cat/consumer/AllTests.java | 3 + .../consumer/RealtimeConfigConfiguration.java | 55 +++ .../cat/consumer/dump/DumpAnalyzerTest.java | 117 ++++++ .../problem/AggregationConfigManagerTest.java | 5 +- .../consumer/problem/ProblemAnalyzerTest.java | 11 + .../cat/consumer/state/StateAnalyzerTest.java | 1 - .../cat/consumer/problem/problem_analyzer.xml | 384 ++++++++++++++++++ .../test/java/com/dianping/cat/MockLog.java | 110 +++++ 12 files changed, 698 insertions(+), 112 deletions(-) create mode 100644 cat-consumer/src/test/java/com/dianping/cat/consumer/RealtimeConfigConfiguration.java create mode 100644 cat-consumer/src/test/java/com/dianping/cat/consumer/dump/DumpAnalyzerTest.java create mode 100644 cat-core/src/test/java/com/dianping/cat/MockLog.java diff --git a/README.md b/README.md index dbd31f626..26650af4d 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ CAT监控系统将每次URL、Service的请求内部执行情况都封装为一 ![Alt text](https://raw.github.com/dianping/cat/master/cat-home/src/main/webapp/images/logviewAll02.png) -分布式消息树【一台机器CALL另外一台机器】 +分布式消息树【一台机器调用另外一台机器】 --------------------- ![Alt text](https://raw.github.com/dianping/cat/master/cat-home/src/main/webapp/images/logviewAll03.png) diff --git a/cat-consumer-advanced/src/test/java/com/dianping/cat/consumer/metric/MetricConfigManagerTest.java b/cat-consumer-advanced/src/test/java/com/dianping/cat/consumer/metric/MetricConfigManagerTest.java index 1252d9a73..7b08db411 100644 --- a/cat-consumer-advanced/src/test/java/com/dianping/cat/consumer/metric/MetricConfigManagerTest.java +++ b/cat-consumer-advanced/src/test/java/com/dianping/cat/consumer/metric/MetricConfigManagerTest.java @@ -6,12 +6,12 @@ import java.util.List; import junit.framework.Assert; -import org.codehaus.plexus.logging.Logger; import org.junit.Test; import org.unidal.dal.jdbc.DalException; import org.unidal.dal.jdbc.Readset; import org.unidal.dal.jdbc.Updateset; +import com.dianping.cat.MockLog; import com.dianping.cat.advanced.metric.config.entity.MetricConfig; import com.dianping.cat.advanced.metric.config.entity.MetricItemConfig; import com.dianping.cat.consumer.metric.MetricAnalyzer.ConfigItem; @@ -140,110 +140,4 @@ public class MetricConfigManagerTest { } } - public static class MockLog implements Logger { - - @Override - public void debug(String message) { - - } - - @Override - public void debug(String message, Throwable throwable) { - - } - - @Override - public boolean isDebugEnabled() { - - return false; - } - - @Override - public void info(String message) { - - } - - @Override - public void info(String message, Throwable throwable) { - - } - - @Override - public boolean isInfoEnabled() { - - return false; - } - - @Override - public void warn(String message) { - - } - - @Override - public void warn(String message, Throwable throwable) { - - } - - @Override - public boolean isWarnEnabled() { - - return false; - } - - @Override - public void error(String message) { - - } - - @Override - public void error(String message, Throwable throwable) { - - } - - @Override - public boolean isErrorEnabled() { - - return false; - } - - @Override - public void fatalError(String message) { - - } - - @Override - public void fatalError(String message, Throwable throwable) { - - } - - @Override - public boolean isFatalErrorEnabled() { - - return false; - } - - @Override - public Logger getChildLogger(String name) { - - return null; - } - - @Override - public int getThreshold() { - - return 0; - } - - @Override - public void setThreshold(int threshold) { - - } - - @Override - public String getName() { - - return null; - } - - } } diff --git a/cat-consumer-advanced/src/test/java/com/dianping/cat/consumer/metric/ProductLineConfigManagerTest.java b/cat-consumer-advanced/src/test/java/com/dianping/cat/consumer/metric/ProductLineConfigManagerTest.java index 79cca66d9..005f80ee6 100644 --- a/cat-consumer-advanced/src/test/java/com/dianping/cat/consumer/metric/ProductLineConfigManagerTest.java +++ b/cat-consumer-advanced/src/test/java/com/dianping/cat/consumer/metric/ProductLineConfigManagerTest.java @@ -12,9 +12,9 @@ import org.unidal.dal.jdbc.DalNotFoundException; import org.unidal.dal.jdbc.Readset; import org.unidal.dal.jdbc.Updateset; +import com.dianping.cat.MockLog; import com.dianping.cat.consumer.company.model.entity.Company; import com.dianping.cat.consumer.company.model.entity.ProductLine; -import com.dianping.cat.consumer.metric.MetricConfigManagerTest.MockLog; import com.dianping.cat.core.config.Config; import com.dianping.cat.core.config.ConfigDao; diff --git a/cat-consumer/src/main/java/com/dianping/cat/consumer/dump/DumpAnalyzer.java b/cat-consumer/src/main/java/com/dianping/cat/consumer/dump/DumpAnalyzer.java index ac2bdb405..579e4f100 100644 --- a/cat-consumer/src/main/java/com/dianping/cat/consumer/dump/DumpAnalyzer.java +++ b/cat-consumer/src/main/java/com/dianping/cat/consumer/dump/DumpAnalyzer.java @@ -120,4 +120,16 @@ public class DumpAnalyzer extends AbstractMessageAnalyzer implements Log } } + public Map getOldVersionDomains() { + return m_oldVersionDomains; + } + + public void setServerStateManager(ServerStatisticManager serverStateManager) { + m_serverStateManager = serverStateManager; + } + + public void setBucketManager(LocalMessageBucketManager bucketManager) { + m_bucketManager = bucketManager; + } + } diff --git a/cat-consumer/src/test/java/com/dianping/cat/consumer/AllTests.java b/cat-consumer/src/test/java/com/dianping/cat/consumer/AllTests.java index 30026781e..551739c8f 100644 --- a/cat-consumer/src/test/java/com/dianping/cat/consumer/AllTests.java +++ b/cat-consumer/src/test/java/com/dianping/cat/consumer/AllTests.java @@ -9,6 +9,7 @@ import com.dianping.cat.consumer.core.GsonTest; import com.dianping.cat.consumer.core.NumberFormatTest; import com.dianping.cat.consumer.core.aggregation.CompositeFormatTest; import com.dianping.cat.consumer.core.aggregation.DefaultFormatTest; +import com.dianping.cat.consumer.dump.DumpAnalyzer; import com.dianping.cat.consumer.event.EventAnalyzerTest; import com.dianping.cat.consumer.event.EventReportMergerTest; import com.dianping.cat.consumer.heartbeat.HeartbeatAnalyzerTest; @@ -90,6 +91,8 @@ CompositeFormatTest.class, DefaultFormatTest.class, +DumpAnalyzer.class, + TransactionReportTypeAggergatorTest.class }) public class AllTests { diff --git a/cat-consumer/src/test/java/com/dianping/cat/consumer/RealtimeConfigConfiguration.java b/cat-consumer/src/test/java/com/dianping/cat/consumer/RealtimeConfigConfiguration.java new file mode 100644 index 000000000..1b96f599b --- /dev/null +++ b/cat-consumer/src/test/java/com/dianping/cat/consumer/RealtimeConfigConfiguration.java @@ -0,0 +1,55 @@ +package com.dianping.cat.consumer; + +import java.util.ArrayList; +import java.util.List; + +import org.unidal.lookup.configuration.AbstractResourceConfigurator; +import org.unidal.lookup.configuration.Component; + +import com.dianping.cat.analysis.MessageAnalyzer; +import com.dianping.cat.analysis.MessageAnalyzerManager; +import com.dianping.cat.consumer.event.EventAnalyzer; +import com.dianping.cat.consumer.top.TopAnalyzer; +import com.dianping.cat.consumer.transaction.Configurator; +import com.dianping.cat.consumer.transaction.TransactionAnalyzer; +import com.dianping.cat.consumer.transaction.TransactionAnalyzerTest; +import com.dianping.cat.consumer.transaction.Configurator.ExtendedTransactionDelegate; +import com.dianping.cat.consumer.transaction.Configurator.MockTransactionReportManager; +import com.dianping.cat.service.ReportDelegate; +import com.dianping.cat.service.ReportManager; + +public class RealtimeConfigConfiguration extends AbstractResourceConfigurator { + + public static void main(String[] args) { + generatePlexusComponentsXmlFile(new Configurator()); + } + + protected Class getTestClass() { + return TransactionAnalyzerTest.class; + } + + @Override + public List defineComponents() { + List all = new ArrayList(); + + return all; + } + + public static class MockMessageAnalyzerManager implements MessageAnalyzerManager { + + @Override + public List getAnalyzerNames() { + List list = new ArrayList(); + + list.add(TransactionAnalyzer.ID); + list.add(EventAnalyzer.ID); + list.add(TopAnalyzer.ID); + return list; + } + + @Override + public MessageAnalyzer getAnalyzer(String name, long startTime) { + return null; + } + } +} diff --git a/cat-consumer/src/test/java/com/dianping/cat/consumer/dump/DumpAnalyzerTest.java b/cat-consumer/src/test/java/com/dianping/cat/consumer/dump/DumpAnalyzerTest.java new file mode 100644 index 000000000..f334c5178 --- /dev/null +++ b/cat-consumer/src/test/java/com/dianping/cat/consumer/dump/DumpAnalyzerTest.java @@ -0,0 +1,117 @@ +package com.dianping.cat.consumer.dump; + +import java.io.IOException; + +import junit.framework.Assert; + +import org.junit.Test; + +import com.dianping.cat.MockLog; +import com.dianping.cat.message.Message; +import com.dianping.cat.message.internal.DefaultTransaction; +import com.dianping.cat.message.internal.MessageId; +import com.dianping.cat.message.spi.MessageTree; +import com.dianping.cat.message.spi.internal.DefaultMessageTree; +import com.dianping.cat.statistic.ServerStatisticManager; +import com.dianping.cat.storage.dump.LocalMessageBucketManager; + +public class DumpAnalyzerTest { + + @Test + public void test() throws Exception { + DumpAnalyzer analyzer = new DumpAnalyzer(); + int size = 1000; + MockLocalMessageBucketManager bucketManager = new MockLocalMessageBucketManager(); + + analyzer.setServerStateManager(new ServerStatisticManager()); + analyzer.setBucketManager(bucketManager); + analyzer.enableLogging(new MockLog()); + + for (int i = 0; i < size; i++) { + analyzer.process(generateMessageTree(i)); + } + + Assert.assertEquals(size, bucketManager.m_insert); + + analyzer.doCheckpoint(true); + Thread.sleep(1000); + + Assert.assertEquals(true, bucketManager.m_archive); + + for (int i = 0; i < size; i++) { + analyzer.process(generateOldMessageTree()); + } + + Assert.assertEquals(size, bucketManager.m_insert); + Assert.assertEquals(size, analyzer.getOldVersionDomains().get("Cat").intValue()); + } + + protected MessageTree generateOldMessageTree() { + DefaultMessageTree tree = (DefaultMessageTree) generateMessageTree(10); + + tree.setMessageId("Cat-0a010680-1385467200000-10"); + + return tree; + } + + protected MessageTree generateMessageTree(int i) { + MessageTree tree = new DefaultMessageTree(); + + tree.setDomain("Cat"); + tree.setHostName("group001"); + tree.setIpAddress("192.168.1.1"); + + DefaultTransaction t = new DefaultTransaction("A", "n" + i % 2, null); + DefaultTransaction t2 = new DefaultTransaction("A-1", "n" + i % 3, null); + + if (i % 2 == 0) { + t2.setStatus("ERROR"); + } else { + t2.setStatus(Message.SUCCESS); + } + + t2.complete(); + t2.setDurationInMillis(i); + + t.addChild(t2); + + if (i % 2 == 0) { + t.setStatus("ERROR"); + } else { + t.setStatus(Message.SUCCESS); + } + + t.complete(); + t.setDurationInMillis(i * 2); + long timestamp = System.currentTimeMillis() - System.currentTimeMillis() % (3600 * 1000); + + t.setTimestamp(timestamp + 1000); + t2.setTimestamp(timestamp + 2000); + tree.setMessage(t); + tree.setMessageId("Cat-0a010680-384852-" + i); + + return tree; + } + + public static class MockLocalMessageBucketManager extends LocalMessageBucketManager { + + protected boolean m_archive = false; + + protected int m_insert = 0; + + @Override + public MessageTree loadMessage(String messageId) throws IOException { + return new DefaultMessageTree(); + } + + @Override + public void storeMessage(MessageTree tree, MessageId id) throws IOException { + m_insert++; + } + + @Override + public void archive(long startTime) { + m_archive = true; + } + } +} diff --git a/cat-consumer/src/test/java/com/dianping/cat/consumer/problem/AggregationConfigManagerTest.java b/cat-consumer/src/test/java/com/dianping/cat/consumer/problem/AggregationConfigManagerTest.java index adac29cc1..407d8224c 100644 --- a/cat-consumer/src/test/java/com/dianping/cat/consumer/problem/AggregationConfigManagerTest.java +++ b/cat-consumer/src/test/java/com/dianping/cat/consumer/problem/AggregationConfigManagerTest.java @@ -6,6 +6,7 @@ import junit.framework.Assert; import org.junit.Test; import org.unidal.dal.jdbc.DalException; +import org.unidal.dal.jdbc.DalNotFoundException; import org.unidal.dal.jdbc.Readset; import org.unidal.dal.jdbc.Updateset; @@ -80,8 +81,8 @@ public class AggregationConfigManagerTest { public static class MockConfigDao2 extends MockConfigDao1 { @Override - public Config findByName(String name, Readset readset) throws DalException { - throw new DalException("this is test exception, please ignore it!"); + public Config findByName(String name, Readset readset) throws DalNotFoundException { + throw new DalNotFoundException("this is test exception, please ignore it!"); } } diff --git a/cat-consumer/src/test/java/com/dianping/cat/consumer/problem/ProblemAnalyzerTest.java b/cat-consumer/src/test/java/com/dianping/cat/consumer/problem/ProblemAnalyzerTest.java index a49bbfc8d..efe77aa9d 100644 --- a/cat-consumer/src/test/java/com/dianping/cat/consumer/problem/ProblemAnalyzerTest.java +++ b/cat-consumer/src/test/java/com/dianping/cat/consumer/problem/ProblemAnalyzerTest.java @@ -13,6 +13,10 @@ import org.unidal.lookup.ComponentTestCase; import com.dianping.cat.Constants; import com.dianping.cat.analysis.MessageAnalyzer; import com.dianping.cat.consumer.problem.model.entity.ProblemReport; +import com.dianping.cat.message.Event; +import com.dianping.cat.message.Heartbeat; +import com.dianping.cat.message.internal.DefaultEvent; +import com.dianping.cat.message.internal.DefaultHeartbeat; import com.dianping.cat.message.internal.DefaultTransaction; import com.dianping.cat.message.spi.MessageTree; import com.dianping.cat.message.spi.internal.DefaultMessageTree; @@ -93,6 +97,13 @@ public class ProblemAnalyzerTest extends ComponentTestCase { break; } + Event error = new DefaultEvent("Error", "Error", null); + Event exception = new DefaultEvent("Exception", "Exception", null); + Heartbeat heartbeat = new DefaultHeartbeat("heartbeat", "heartbeat"); + + t.addChild(error); + t.addChild(exception); + t.addChild(heartbeat); tree.setMessage(t); return tree; diff --git a/cat-consumer/src/test/java/com/dianping/cat/consumer/state/StateAnalyzerTest.java b/cat-consumer/src/test/java/com/dianping/cat/consumer/state/StateAnalyzerTest.java index 0d87d39f7..8b48753ce 100644 --- a/cat-consumer/src/test/java/com/dianping/cat/consumer/state/StateAnalyzerTest.java +++ b/cat-consumer/src/test/java/com/dianping/cat/consumer/state/StateAnalyzerTest.java @@ -36,7 +36,6 @@ public class StateAnalyzerTest extends ComponentTestCase { @Test public void testProcess() throws Exception { - StateReport report = m_analyzer.getReport(m_domain); String expected = Files.forIO().readFrom(getClass().getResourceAsStream("state_analyzer.xml"), "utf-8"); diff --git a/cat-consumer/src/test/resources/com/dianping/cat/consumer/problem/problem_analyzer.xml b/cat-consumer/src/test/resources/com/dianping/cat/consumer/problem/problem_analyzer.xml index 4d5a1c5d7..d843275b4 100644 --- a/cat-consumer/src/test/resources/com/dianping/cat/consumer/problem/problem_analyzer.xml +++ b/cat-consumer/src/test/resources/com/dianping/cat/consumer/problem/problem_analyzer.xml @@ -41,6 +41,390 @@ + + + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + 15 + 16 + 17 + 18 + 19 + 20 + 21 + 22 + 23 + 24 + 25 + 26 + 27 + 28 + 29 + 30 + 31 + 32 + 33 + 34 + 35 + 36 + 37 + 38 + 39 + 40 + 41 + 42 + 43 + 44 + 45 + 46 + 47 + 48 + 49 + 50 + 51 + 52 + 53 + 54 + 55 + 56 + 57 + 58 + 59 + 60 + + + + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + 15 + 16 + 17 + 18 + 19 + 20 + 21 + 22 + 23 + 24 + 25 + 26 + 27 + 28 + 29 + 30 + 31 + 32 + 33 + 34 + 35 + 36 + 37 + 38 + 39 + 40 + 41 + 42 + 43 + 44 + 45 + 46 + 47 + 48 + 49 + 50 + 51 + 52 + 53 + 54 + 55 + 56 + 57 + 58 + 59 + 60 + + + + + + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + 15 + 16 + 17 + 18 + 19 + 20 + 21 + 22 + 23 + 24 + 25 + 26 + 27 + 28 + 29 + 30 + 31 + 32 + 33 + 34 + 35 + 36 + 37 + 38 + 39 + 40 + 41 + 42 + 43 + 44 + 45 + 46 + 47 + 48 + 49 + 50 + 51 + 52 + 53 + 54 + 55 + 56 + 57 + 58 + 59 + 60 + + + + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + 15 + 16 + 17 + 18 + 19 + 20 + 21 + 22 + 23 + 24 + 25 + 26 + 27 + 28 + 29 + 30 + 31 + 32 + 33 + 34 + 35 + 36 + 37 + 38 + 39 + 40 + 41 + 42 + 43 + 44 + 45 + 46 + 47 + 48 + 49 + 50 + 51 + 52 + 53 + 54 + 55 + 56 + 57 + 58 + 59 + 60 + + + + + + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + 15 + 16 + 17 + 18 + 19 + 20 + 21 + 22 + 23 + 24 + 25 + 26 + 27 + 28 + 29 + 30 + 31 + 32 + 33 + 34 + 35 + 36 + 37 + 38 + 39 + 40 + 41 + 42 + 43 + 44 + 45 + 46 + 47 + 48 + 49 + 50 + 51 + 52 + 53 + 54 + 55 + 56 + 57 + 58 + 59 + 60 + + + + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + 15 + 16 + 17 + 18 + 19 + 20 + 21 + 22 + 23 + 24 + 25 + 26 + 27 + 28 + 29 + 30 + 31 + 32 + 33 + 34 + 35 + 36 + 37 + 38 + 39 + 40 + 41 + 42 + 43 + 44 + 45 + 46 + 47 + 48 + 49 + 50 + 51 + 52 + 53 + 54 + 55 + 56 + 57 + 58 + 59 + 60 + + + 2 diff --git a/cat-core/src/test/java/com/dianping/cat/MockLog.java b/cat-core/src/test/java/com/dianping/cat/MockLog.java new file mode 100644 index 000000000..bbd6f7fb9 --- /dev/null +++ b/cat-core/src/test/java/com/dianping/cat/MockLog.java @@ -0,0 +1,110 @@ +package com.dianping.cat; + +import org.codehaus.plexus.logging.Logger; + +public class MockLog implements Logger { + + @Override + public void debug(String message) { + + } + + @Override + public void debug(String message, Throwable throwable) { + + } + + @Override + public boolean isDebugEnabled() { + + return false; + } + + @Override + public void info(String message) { + + } + + @Override + public void info(String message, Throwable throwable) { + + } + + @Override + public boolean isInfoEnabled() { + + return false; + } + + @Override + public void warn(String message) { + + } + + @Override + public void warn(String message, Throwable throwable) { + + } + + @Override + public boolean isWarnEnabled() { + + return false; + } + + @Override + public void error(String message) { + + } + + @Override + public void error(String message, Throwable throwable) { + + } + + @Override + public boolean isErrorEnabled() { + + return false; + } + + @Override + public void fatalError(String message) { + + } + + @Override + public void fatalError(String message, Throwable throwable) { + + } + + @Override + public boolean isFatalErrorEnabled() { + + return false; + } + + @Override + public Logger getChildLogger(String name) { + + return null; + } + + @Override + public int getThreshold() { + + return 0; + } + + @Override + public void setThreshold(int threshold) { + + } + + @Override + public String getName() { + + return null; + } + +} \ No newline at end of file -- GitLab