Repository: eagle Updated Branches: refs/heads/master c970bb426 -> 6e919c2e8
http://git-wip-us.apache.org/repos/asf/eagle/blob/6e919c2e/eagle-core/eagle-query/eagle-entity-base/src/test/java/org/apache/eagle/log/entity/meta/TestMapSerDeser.java ---------------------------------------------------------------------- diff --git a/eagle-core/eagle-query/eagle-entity-base/src/test/java/org/apache/eagle/log/entity/meta/TestMapSerDeser.java b/eagle-core/eagle-query/eagle-entity-base/src/test/java/org/apache/eagle/log/entity/meta/TestMapSerDeser.java index 96051f4..39c53cc 100644 --- a/eagle-core/eagle-query/eagle-entity-base/src/test/java/org/apache/eagle/log/entity/meta/TestMapSerDeser.java +++ b/eagle-core/eagle-query/eagle-entity-base/src/test/java/org/apache/eagle/log/entity/meta/TestMapSerDeser.java @@ -25,82 +25,82 @@ import org.junit.Test; public class TestMapSerDeser { - @SuppressWarnings("rawtypes") - @Test - public void testStringToStringMapSerDeser() { - MapSerDeser serDeser = new MapSerDeser(); - Map<String, String> sources = new HashMap<String, String>(); - sources.put("test1", "value1"); - sources.put("test2", null); - sources.put("test3", "value3"); - - byte[] bytes = serDeser.serialize(sources); - Assert.assertEquals(4 + sources.size() * 16 + 27, bytes.length); - Map targets = serDeser.deserialize(bytes); - Assert.assertEquals(sources.size(), targets.size()); - - Assert.assertEquals("value1", targets.get("test1")); - Assert.assertNull(targets.get("test2")); - Assert.assertEquals("value3", targets.get("test3")); - } - - - @SuppressWarnings("rawtypes") - @Test - public void testStringToIntegerMapSerDeser() { - MapSerDeser serDeser = new MapSerDeser(); - Map<String, Integer> sources = new HashMap<String, Integer>(); - sources.put("test1", 1); - sources.put("test2", null); - sources.put("test3", 3); - - byte[] bytes = serDeser.serialize(sources); - Assert.assertEquals(4 + sources.size() * 16 + 23, bytes.length); - Map targets = serDeser.deserialize(bytes); - Assert.assertEquals(sources.size(), targets.size()); - - Assert.assertEquals(1, targets.get("test1")); - Assert.assertNull(targets.get("test2")); - Assert.assertEquals(3, targets.get("test3")); - } - - - @SuppressWarnings({ "rawtypes", "unchecked" }) - @Test - public void testStringToMapMapSerDeser() { - MapSerDeser serDeser = new MapSerDeser(); - Map<String, Map<String, String>> sources = new HashMap<String, Map<String, String>>(); - Map<String, String> map1 = new HashMap<String, String>(); - map1.put("key11", "value11"); - map1.put("key12", null); - map1.put("key13", "value13"); - sources.put("test1", map1); - sources.put("test2", null); - Map<String, String> map3 = new HashMap<String, String>(); - map3.put("key31", "value31"); - map3.put("key32", null); - map3.put("key33", "value33"); - sources.put("test3", map3); - - byte[] bytes = serDeser.serialize(sources); - Map targets = serDeser.deserialize(bytes); - Assert.assertEquals(sources.size(), targets.size()); - - map1 = (Map)targets.get("test1"); - Assert.assertNotNull(map1); - Assert.assertEquals(3, map1.size()); - Assert.assertEquals("value11", map1.get("key11")); - Assert.assertNull(map1.get("key12")); - Assert.assertEquals("value13", map1.get("key13")); - - Assert.assertNull(targets.get("test2")); - - map3 = (Map)targets.get("test3"); - Assert.assertNotNull(map3); - Assert.assertEquals(3, map3.size()); - Assert.assertEquals("value31", map3.get("key31")); - Assert.assertNull(map3.get("key32")); - Assert.assertEquals("value33", map3.get("key33")); - } + @SuppressWarnings("rawtypes") + @Test + public void testStringToStringMapSerDeser() { + MapSerDeser serDeser = new MapSerDeser(); + Map<String, String> sources = new HashMap<String, String>(); + sources.put("test1", "value1"); + sources.put("test2", null); + sources.put("test3", "value3"); + + byte[] bytes = serDeser.serialize(sources); + Assert.assertEquals(4 + sources.size() * 16 + 27, bytes.length); + Map targets = serDeser.deserialize(bytes); + Assert.assertEquals(sources.size(), targets.size()); + + Assert.assertEquals("value1", targets.get("test1")); + Assert.assertNull(targets.get("test2")); + Assert.assertEquals("value3", targets.get("test3")); + } + + @SuppressWarnings("rawtypes") + @Test + public void testStringToIntegerMapSerDeser() { + MapSerDeser serDeser = new MapSerDeser(); + Map<String, Integer> sources = new HashMap<String, Integer>(); + sources.put("test1", 1); + sources.put("test2", null); + sources.put("test3", 3); + + byte[] bytes = serDeser.serialize(sources); + Assert.assertEquals(4 + sources.size() * 16 + 23, bytes.length); + Map targets = serDeser.deserialize(bytes); + Assert.assertEquals(sources.size(), targets.size()); + + Assert.assertEquals(1, targets.get("test1")); + Assert.assertNull(targets.get("test2")); + Assert.assertEquals(3, targets.get("test3")); + } + + @SuppressWarnings({ + "rawtypes", "unchecked" + }) + @Test + public void testStringToMapMapSerDeser() { + MapSerDeser serDeser = new MapSerDeser(); + Map<String, Map<String, String>> sources = new HashMap<String, Map<String, String>>(); + Map<String, String> map1 = new HashMap<String, String>(); + map1.put("key11", "value11"); + map1.put("key12", null); + map1.put("key13", "value13"); + sources.put("test1", map1); + sources.put("test2", null); + Map<String, String> map3 = new HashMap<String, String>(); + map3.put("key31", "value31"); + map3.put("key32", null); + map3.put("key33", "value33"); + sources.put("test3", map3); + + byte[] bytes = serDeser.serialize(sources); + Map targets = serDeser.deserialize(bytes); + Assert.assertEquals(sources.size(), targets.size()); + + map1 = (Map)targets.get("test1"); + Assert.assertNotNull(map1); + Assert.assertEquals(3, map1.size()); + Assert.assertEquals("value11", map1.get("key11")); + Assert.assertNull(map1.get("key12")); + Assert.assertEquals("value13", map1.get("key13")); + + Assert.assertNull(targets.get("test2")); + + map3 = (Map)targets.get("test3"); + Assert.assertNotNull(map3); + Assert.assertEquals(3, map3.size()); + Assert.assertEquals("value31", map3.get("key31")); + Assert.assertNull(map3.get("key32")); + Assert.assertEquals("value33", map3.get("key33")); + } } http://git-wip-us.apache.org/repos/asf/eagle/blob/6e919c2e/eagle-core/eagle-query/eagle-entity-base/src/test/java/org/apache/eagle/log/entity/repo/TestEntityRepositoryScanner.java ---------------------------------------------------------------------- diff --git a/eagle-core/eagle-query/eagle-entity-base/src/test/java/org/apache/eagle/log/entity/repo/TestEntityRepositoryScanner.java b/eagle-core/eagle-query/eagle-entity-base/src/test/java/org/apache/eagle/log/entity/repo/TestEntityRepositoryScanner.java index 2904153..3c89c14 100755 --- a/eagle-core/eagle-query/eagle-entity-base/src/test/java/org/apache/eagle/log/entity/repo/TestEntityRepositoryScanner.java +++ b/eagle-core/eagle-query/eagle-entity-base/src/test/java/org/apache/eagle/log/entity/repo/TestEntityRepositoryScanner.java @@ -23,9 +23,9 @@ import org.junit.Test; public class TestEntityRepositoryScanner { - @Test - public void testScan() throws InstantiationException, IllegalAccessException { - Assert.assertEquals(MetricMetadataEntity.class, - EntityDefinitionManager.getEntityByServiceName("MetricMetadataService").getEntityClass()); - } -} \ No newline at end of file + @Test + public void testScan() throws InstantiationException, IllegalAccessException { + Assert.assertEquals(MetricMetadataEntity.class, EntityDefinitionManager + .getEntityByServiceName("MetricMetadataService").getEntityClass()); + } +} http://git-wip-us.apache.org/repos/asf/eagle/blob/6e919c2e/eagle-core/eagle-query/eagle-entity-base/src/test/java/org/apache/eagle/log/expression/TestExpressionParser.java ---------------------------------------------------------------------- diff --git a/eagle-core/eagle-query/eagle-entity-base/src/test/java/org/apache/eagle/log/expression/TestExpressionParser.java b/eagle-core/eagle-query/eagle-entity-base/src/test/java/org/apache/eagle/log/expression/TestExpressionParser.java index ea548b8..146f5cd 100755 --- a/eagle-core/eagle-query/eagle-entity-base/src/test/java/org/apache/eagle/log/expression/TestExpressionParser.java +++ b/eagle-core/eagle-query/eagle-entity-base/src/test/java/org/apache/eagle/log/expression/TestExpressionParser.java @@ -28,229 +28,163 @@ import java.util.List; * @since Nov 10, 2014 */ public class TestExpressionParser { - - @Test - public void testSingleVariable() throws Exception{ - String exprStr = "mapProgress"; - ExpressionParser parser = new ExpressionParser(exprStr); - Double value = parser.setVariable("mapProgress", 100.0) - .eval(); - Assert.assertEquals(value, 100.0, 0.001); - List<String> dependentFields = parser.getDependentFields(); - Assert.assertEquals(dependentFields.size(), 1); - Assert.assertEquals(dependentFields.get(0), "mapProgress"); - } - - @Test - public void testgetDependency() throws Exception{ - /** NOTICE: expression should be enclosure with "EXP{}" , This is for making antlr easy to parse - * variable name cannot be "pi" OR "E", there are parssi builtin constants */ - String exprStr = "min(mAx, Max) / abs(MAX)"; - ExpressionParser parser = new ExpressionParser(exprStr); - List<String> variables = parser.getDependentFields(); - Assert.assertEquals(variables.size(), 3); - Assert.assertTrue(variables.contains("mAx")); - Assert.assertTrue(variables.contains("Max")); - Assert.assertTrue(variables.contains("MAX")); - } - @Test - public void testFunction() throws Exception{ - String exprStr = "min(mapProgress, reduceProgress) / abs(endTime - startTime)"; - ExpressionParser parser = new ExpressionParser(exprStr); - Double value = parser.setVariable("mapProgress", 100.0) - .setVariable("reduceProgress", 20.0) - .setVariable("endTime", 1415590100000.0) - .setVariable("startTime", 1415590000000.0) - .eval(); - Assert.assertEquals(value, 0.0002, 0.001); - } - - @Test - public void testOperator() throws Exception{ - String exprStr = "(a+b*c) / (2*(d-e))"; - ExpressionParser parser = new ExpressionParser(exprStr); - Double value = parser.setVariable("a", 200.0) - .setVariable("b", 400.0) - .setVariable("c", 3.0) - .setVariable("d", 225.0) - .setVariable("e", -125.0) - .eval(); - Assert.assertEquals(value, 2.0, 0.001); - } - - @Test - public void testOperatorWithFunction() throws Exception{ - String exprStr = "(max(a, b)* min(a, b)) / abs(a-b+c-d)"; - ExpressionParser parser = new ExpressionParser(exprStr); - Double value = parser.setVariable("a", 300.0) - .setVariable("b", 200.0) - .setVariable("c", -300.0) - .setVariable("d", -300.0) - .eval(); - Assert.assertEquals(value, 600.0, 0.001); - } - - @Test - public void testWithAtFieldName() throws Exception{ - String exprStr = "(max(a, b)* min(a, b)) / abs(a-b+c-d)"; - ExpressionParser parser = new ExpressionParser(exprStr); - Double value = parser.setVariable("a", 300.0) - .setVariable("b", 200.0) - .setVariable("c", -300.0) - .setVariable("d", -300.0) - .eval(); - Assert.assertEquals(value, 600.0, 0.001); - } - - @Test - public void testConstant() throws Exception { - String exprStr = "a"; - ExpressionParser parser = new ExpressionParser(exprStr); - Double value = parser.setVariable("a", 300.0) - .setVariable("b", 200.0) - .setVariable("c", -300.0) - .setVariable("d", -300.0) - .eval(); - Assert.assertEquals(value, 300.0, 0.001); - - value = parser.setVariable("a", 200.0) - .setVariable("b", 200.0) - .setVariable("c", -300.0) - .setVariable("d", -300.0) - .eval(); - Assert.assertEquals(value, 200.0, 0.001); - } - - @Test - public void testBooleanExpression() throws Exception { - String exprStr = "a > b"; - ExpressionParser parser = new ExpressionParser(exprStr); - Double value = parser.setVariable("a", 300.0) - .setVariable("b", 200.0) - .setVariable("c", -300.0) - .setVariable("d", -300.0) - .eval(); - Assert.assertEquals(value, 1.0, 0.001); - - value = parser.setVariable("a", 100.0) - .setVariable("b", 200.0) - .setVariable("c", -300.0) - .setVariable("d", -300.0) - .eval(); - Assert.assertEquals(value, 0.0, 0.001); - - exprStr = "a < b"; - parser = new ExpressionParser(exprStr); - value = parser.setVariable("a", 300.0) - .setVariable("b", 300.0) - .setVariable("c", -300.0) - .setVariable("d", -300.0) - .eval(); - Assert.assertTrue(value == 0.0); - - value = parser.setVariable("a", 400.0) - .setVariable("b", 300.0) - .setVariable("c", -300.0) - .setVariable("d", -300.0) - .eval(); - Assert.assertTrue(value == 0.0); - - value = parser.setVariable("a", 100.0) - .setVariable("b", 200.0) - .setVariable("c", -300.0) - .setVariable("d", -300.0) - .eval(); - Assert.assertTrue(value == 1.0); - - // !!! Not support well >= - exprStr = "a >= b"; - parser = new ExpressionParser(exprStr); - value = parser.setVariable("a", 300.0) - .setVariable("b", 300.0) - .setVariable("c", -300.0) - .setVariable("d", -300.0) - .eval(); - Assert.assertTrue(value == 0.0); // expect 1.0 - - value = parser.setVariable("a", 400.0) - .setVariable("b", 300.0) - .setVariable("c", -300.0) - .setVariable("d", -300.0) - .eval(); - Assert.assertTrue(value == 1.0); // expect 1.0 - - value = parser.setVariable("a", 100.0) - .setVariable("b", 200.0) - .setVariable("c", -300.0) - .setVariable("d", -300.0) - .eval(); - Assert.assertTrue(value == 1.0); // expect 0.0 - - exprStr = "a <= b"; - parser = new ExpressionParser(exprStr); - value = parser.setVariable("a", 300.0) - .setVariable("b", 300.0) - .setVariable("c", -300.0) - .setVariable("d", -300.0) - .eval(); - Assert.assertTrue(value == 1.0); - - value = parser.setVariable("a", 400.0) - .setVariable("b", 300.0) - .setVariable("c", -300.0) - .setVariable("d", -300.0) - .eval(); - Assert.assertTrue(value == 0.0); - - value = parser.setVariable("a", 100.0) - .setVariable("b", 200.0) - .setVariable("c", -300.0) - .setVariable("d", -300.0) - .eval(); - Assert.assertTrue(value == 1.0); - - exprStr = "a = b"; - parser = new ExpressionParser(exprStr); - value = parser.setVariable("a", 300.0) - .setVariable("b", 300.0) - .setVariable("c", -300.0) - .setVariable("d", -300.0) - .eval(); - Assert.assertEquals(value, 1.0, 0.001); - - value = parser.setVariable("a", 100.0) - .setVariable("b", 200.0) - .setVariable("c", -300.0) - .setVariable("d", -300.0) - .eval(); - Assert.assertEquals(value, 0.0, 0.001); - } - - @Test - public void testParsiiBug() throws Exception { - // !!! Not support >= - String exprStr = "a >= b"; - ExpressionParser parser = new ExpressionParser(exprStr); - Double value = parser.setVariable("a", 300.0) - .setVariable("b", 300.0) - .setVariable("c", -300.0) - .setVariable("d", -300.0) - .eval(); - Assert.assertTrue(value == 0.0); // expect 1.0 - - value = parser.setVariable("a", 400.0) - .setVariable("b", 300.0) - .setVariable("c", -300.0) - .setVariable("d", -300.0) - .eval(); - Assert.assertTrue(value == 1.0); // expect 1.0 - - value = parser.setVariable("a", 100.0) - .setVariable("b", 200.0) - .setVariable("c", -300.0) - .setVariable("d", -300.0) - .eval(); - Assert.assertTrue(value == 1.0); // expect 0.0 - } + @Test + public void testSingleVariable() throws Exception { + String exprStr = "mapProgress"; + ExpressionParser parser = new ExpressionParser(exprStr); + Double value = parser.setVariable("mapProgress", 100.0).eval(); + Assert.assertEquals(value, 100.0, 0.001); + List<String> dependentFields = parser.getDependentFields(); + Assert.assertEquals(dependentFields.size(), 1); + Assert.assertEquals(dependentFields.get(0), "mapProgress"); + } + + @Test + public void testgetDependency() throws Exception { + /** + * NOTICE: expression should be enclosure with "EXP{}" , This is for making antlr easy to parse + * variable name cannot be "pi" OR "E", there are parssi builtin constants + */ + String exprStr = "min(mAx, Max) / abs(MAX)"; + ExpressionParser parser = new ExpressionParser(exprStr); + List<String> variables = parser.getDependentFields(); + Assert.assertEquals(variables.size(), 3); + Assert.assertTrue(variables.contains("mAx")); + Assert.assertTrue(variables.contains("Max")); + Assert.assertTrue(variables.contains("MAX")); + } + + @Test + public void testFunction() throws Exception { + String exprStr = "min(mapProgress, reduceProgress) / abs(endTime - startTime)"; + ExpressionParser parser = new ExpressionParser(exprStr); + Double value = parser.setVariable("mapProgress", 100.0).setVariable("reduceProgress", 20.0) + .setVariable("endTime", 1415590100000.0).setVariable("startTime", 1415590000000.0).eval(); + Assert.assertEquals(value, 0.0002, 0.001); + } + + @Test + public void testOperator() throws Exception { + String exprStr = "(a+b*c) / (2*(d-e))"; + ExpressionParser parser = new ExpressionParser(exprStr); + Double value = parser.setVariable("a", 200.0).setVariable("b", 400.0).setVariable("c", 3.0) + .setVariable("d", 225.0).setVariable("e", -125.0).eval(); + Assert.assertEquals(value, 2.0, 0.001); + } + + @Test + public void testOperatorWithFunction() throws Exception { + String exprStr = "(max(a, b)* min(a, b)) / abs(a-b+c-d)"; + ExpressionParser parser = new ExpressionParser(exprStr); + Double value = parser.setVariable("a", 300.0).setVariable("b", 200.0).setVariable("c", -300.0) + .setVariable("d", -300.0).eval(); + Assert.assertEquals(value, 600.0, 0.001); + } + + @Test + public void testWithAtFieldName() throws Exception { + String exprStr = "(max(a, b)* min(a, b)) / abs(a-b+c-d)"; + ExpressionParser parser = new ExpressionParser(exprStr); + Double value = parser.setVariable("a", 300.0).setVariable("b", 200.0).setVariable("c", -300.0) + .setVariable("d", -300.0).eval(); + Assert.assertEquals(value, 600.0, 0.001); + } + + @Test + public void testConstant() throws Exception { + String exprStr = "a"; + ExpressionParser parser = new ExpressionParser(exprStr); + Double value = parser.setVariable("a", 300.0).setVariable("b", 200.0).setVariable("c", -300.0) + .setVariable("d", -300.0).eval(); + Assert.assertEquals(value, 300.0, 0.001); + + value = parser.setVariable("a", 200.0).setVariable("b", 200.0).setVariable("c", -300.0) + .setVariable("d", -300.0).eval(); + Assert.assertEquals(value, 200.0, 0.001); + } + + @Test + public void testBooleanExpression() throws Exception { + String exprStr = "a > b"; + ExpressionParser parser = new ExpressionParser(exprStr); + Double value = parser.setVariable("a", 300.0).setVariable("b", 200.0).setVariable("c", -300.0) + .setVariable("d", -300.0).eval(); + Assert.assertEquals(value, 1.0, 0.001); + + value = parser.setVariable("a", 100.0).setVariable("b", 200.0).setVariable("c", -300.0) + .setVariable("d", -300.0).eval(); + Assert.assertEquals(value, 0.0, 0.001); + + exprStr = "a < b"; + parser = new ExpressionParser(exprStr); + value = parser.setVariable("a", 300.0).setVariable("b", 300.0).setVariable("c", -300.0) + .setVariable("d", -300.0).eval(); + Assert.assertTrue(value == 0.0); + + value = parser.setVariable("a", 400.0).setVariable("b", 300.0).setVariable("c", -300.0) + .setVariable("d", -300.0).eval(); + Assert.assertTrue(value == 0.0); + + value = parser.setVariable("a", 100.0).setVariable("b", 200.0).setVariable("c", -300.0) + .setVariable("d", -300.0).eval(); + Assert.assertTrue(value == 1.0); + + // !!! Not support well >= + exprStr = "a >= b"; + parser = new ExpressionParser(exprStr); + value = parser.setVariable("a", 300.0).setVariable("b", 300.0).setVariable("c", -300.0) + .setVariable("d", -300.0).eval(); + Assert.assertTrue(value == 0.0); // expect 1.0 + + value = parser.setVariable("a", 400.0).setVariable("b", 300.0).setVariable("c", -300.0) + .setVariable("d", -300.0).eval(); + Assert.assertTrue(value == 1.0); // expect 1.0 + + value = parser.setVariable("a", 100.0).setVariable("b", 200.0).setVariable("c", -300.0) + .setVariable("d", -300.0).eval(); + Assert.assertTrue(value == 1.0); // expect 0.0 + + exprStr = "a <= b"; + parser = new ExpressionParser(exprStr); + value = parser.setVariable("a", 300.0).setVariable("b", 300.0).setVariable("c", -300.0) + .setVariable("d", -300.0).eval(); + Assert.assertTrue(value == 1.0); + + value = parser.setVariable("a", 400.0).setVariable("b", 300.0).setVariable("c", -300.0) + .setVariable("d", -300.0).eval(); + Assert.assertTrue(value == 0.0); + + value = parser.setVariable("a", 100.0).setVariable("b", 200.0).setVariable("c", -300.0) + .setVariable("d", -300.0).eval(); + Assert.assertTrue(value == 1.0); + + exprStr = "a = b"; + parser = new ExpressionParser(exprStr); + value = parser.setVariable("a", 300.0).setVariable("b", 300.0).setVariable("c", -300.0) + .setVariable("d", -300.0).eval(); + Assert.assertEquals(value, 1.0, 0.001); + + value = parser.setVariable("a", 100.0).setVariable("b", 200.0).setVariable("c", -300.0) + .setVariable("d", -300.0).eval(); + Assert.assertEquals(value, 0.0, 0.001); + } + + @Test + public void testParsiiBug() throws Exception { + // !!! Not support >= + String exprStr = "a >= b"; + ExpressionParser parser = new ExpressionParser(exprStr); + Double value = parser.setVariable("a", 300.0).setVariable("b", 300.0).setVariable("c", -300.0) + .setVariable("d", -300.0).eval(); + Assert.assertTrue(value == 0.0); // expect 1.0 + + value = parser.setVariable("a", 400.0).setVariable("b", 300.0).setVariable("c", -300.0) + .setVariable("d", -300.0).eval(); + Assert.assertTrue(value == 1.0); // expect 1.0 + + value = parser.setVariable("a", 100.0).setVariable("b", 200.0).setVariable("c", -300.0) + .setVariable("d", -300.0).eval(); + Assert.assertTrue(value == 1.0); // expect 0.0 + } } http://git-wip-us.apache.org/repos/asf/eagle/blob/6e919c2e/eagle-core/eagle-query/eagle-entity-base/src/test/java/org/apache/eagle/log/expression/TestExpressionPerformance.java ---------------------------------------------------------------------- diff --git a/eagle-core/eagle-query/eagle-entity-base/src/test/java/org/apache/eagle/log/expression/TestExpressionPerformance.java b/eagle-core/eagle-query/eagle-entity-base/src/test/java/org/apache/eagle/log/expression/TestExpressionPerformance.java index 0a2b729..0c3edc3 100755 --- a/eagle-core/eagle-query/eagle-entity-base/src/test/java/org/apache/eagle/log/expression/TestExpressionPerformance.java +++ b/eagle-core/eagle-query/eagle-entity-base/src/test/java/org/apache/eagle/log/expression/TestExpressionPerformance.java @@ -38,83 +38,84 @@ import java.util.Map.Entry; public class TestExpressionPerformance { - public interface ExpressionParser { - double parse(String exprStr, Map<String, Double> tuple) throws Exception; - } - - public class ParsiiParser implements ExpressionParser{ - public Expression expression; - - public double parse(String exprStr, Map<String, Double> tuple) throws Exception{ - Scope scope = Scope.create(); - if (expression == null) { - expression = Parser.parse(exprStr, scope); - } - for(String valName : tuple.keySet()) { - Object value = tuple.get(valName); - if(value instanceof Number) { - scope.getVariable(valName).setValue(((Number)value).doubleValue()); - } - } - return expression.evaluate(); - } - } - - public long doParse(ExpressionParser parser, String exprStr, List<String> parameters) throws Exception{ - long startTime = System.currentTimeMillis(); - int parNum = parameters.size(); - Map<String, Double> tuple = new HashMap<String, Double>(); - for (int i = 1; i < 100000; i++) { - for (int j = 0; j < parNum; j++) { - tuple.put(parameters.get(j), (double) (i * 3 + j)); - } - parser.parse(exprStr, tuple); - } - long endTime = System.currentTimeMillis(); - return endTime - startTime; - } - - @Test - public void TestPerformance() throws Exception{ - List<ExpressionParser> parsers = new ArrayList<ExpressionParser>(); - parsers.add(new ParsiiParser()); + public interface ExpressionParser { + double parse(String exprStr, Map<String, Double> tuple) throws Exception; + } - String exprStr = "a + b / c * 2"; - List<String> parameters = new ArrayList<String>(); - parameters.add("a"); - parameters.add("b"); - parameters.add("c"); - - Map<String, Long> timeComsued = new HashMap<String, Long>(); - - for (int i = 0; i < 10; i++) { - for (ExpressionParser parser : parsers) { - String name = parser.getClass().getName(); - if (timeComsued.get(name) == null) { - timeComsued.put(name, 0L); - } - timeComsued.put(name, timeComsued.get(name) + doParse(parser, exprStr, parameters)); - } - } - for (Entry<String, Long> time : timeComsued.entrySet()) { - System.out.println("time consumed of " + time.getKey() + ": " + time.getValue() +"ms"); - } - } + public class ParsiiParser implements ExpressionParser { + public Expression expression; - @Test - public void TestEvaluatoinValid() throws Exception{ - List<ExpressionParser> parsers = new ArrayList<ExpressionParser>(); - parsers.add(new ParsiiParser()); + @Override + public double parse(String exprStr, Map<String, Double> tuple) throws Exception { + Scope scope = Scope.create(); + if (expression == null) { + expression = Parser.parse(exprStr, scope); + } + for (String valName : tuple.keySet()) { + Object value = tuple.get(valName); + if (value instanceof Number) { + scope.getVariable(valName).setValue(((Number)value).doubleValue()); + } + } + return expression.evaluate(); + } + } - String exprStr = "max(a, 3 * b) + min(b, 10000) / abs(c * 2)"; - Map<String ,Double> tuples = new HashMap<String, Double>(); - tuples.put("a", 20.5); - tuples.put("b", 123.7); - tuples.put("c", 97.57); - DecimalFormat df = new DecimalFormat("#.00"); - for (ExpressionParser parser : parsers) { - System.out.println(parser.getClass().getName() + " : " + parser.parse(exprStr, tuples)); - Assert.assertEquals(df.format(parser.parse(exprStr, tuples)), "371.73"); - } - } + public long doParse(ExpressionParser parser, String exprStr, List<String> parameters) throws Exception { + long startTime = System.currentTimeMillis(); + int parNum = parameters.size(); + Map<String, Double> tuple = new HashMap<String, Double>(); + for (int i = 1; i < 100000; i++) { + for (int j = 0; j < parNum; j++) { + tuple.put(parameters.get(j), (double)(i * 3 + j)); + } + parser.parse(exprStr, tuple); + } + long endTime = System.currentTimeMillis(); + return endTime - startTime; + } + + @Test + public void TestPerformance() throws Exception { + List<ExpressionParser> parsers = new ArrayList<ExpressionParser>(); + parsers.add(new ParsiiParser()); + + String exprStr = "a + b / c * 2"; + List<String> parameters = new ArrayList<String>(); + parameters.add("a"); + parameters.add("b"); + parameters.add("c"); + + Map<String, Long> timeComsued = new HashMap<String, Long>(); + + for (int i = 0; i < 10; i++) { + for (ExpressionParser parser : parsers) { + String name = parser.getClass().getName(); + if (timeComsued.get(name) == null) { + timeComsued.put(name, 0L); + } + timeComsued.put(name, timeComsued.get(name) + doParse(parser, exprStr, parameters)); + } + } + for (Entry<String, Long> time : timeComsued.entrySet()) { + System.out.println("time consumed of " + time.getKey() + ": " + time.getValue() + "ms"); + } + } + + @Test + public void TestEvaluatoinValid() throws Exception { + List<ExpressionParser> parsers = new ArrayList<ExpressionParser>(); + parsers.add(new ParsiiParser()); + + String exprStr = "max(a, 3 * b) + min(b, 10000) / abs(c * 2)"; + Map<String, Double> tuples = new HashMap<String, Double>(); + tuples.put("a", 20.5); + tuples.put("b", 123.7); + tuples.put("c", 97.57); + DecimalFormat df = new DecimalFormat("#.00"); + for (ExpressionParser parser : parsers) { + System.out.println(parser.getClass().getName() + " : " + parser.parse(exprStr, tuples)); + Assert.assertEquals(df.format(parser.parse(exprStr, tuples)), "371.73"); + } + } }
