diff --git a/gatling/chat/ChatSimulation.scala b/gatling/chat/ChatSimulation.scala index 1e70c3bc0bfc3fabb6b6919c8eeabe6482e58c0a..29b6bdfa3ebc4e6de4d253dfbaff6ea15aeb4e38 100644 --- a/gatling/chat/ChatSimulation.scala +++ b/gatling/chat/ChatSimulation.scala @@ -73,5 +73,5 @@ class ChatSimulation extends Simulation { | "messageContent":"Hei fra gatling" |}""".stripMargin))) - setUp(scn.inject(atOnceUsers(100))).protocols(httpProtocol) + setUp(scn.inject(atOnceUsers(25))).protocols(httpProtocol) } diff --git a/src/main/java/resources/CalculatorResource.java b/src/main/java/resources/CalculatorResource.java index b4a9e81ec8fda3c85919163aba241e15b1e3be54..33335dbeaad75e30a7be8f2768346cebed103575 100644 --- a/src/main/java/resources/CalculatorResource.java +++ b/src/main/java/resources/CalculatorResource.java @@ -21,9 +21,25 @@ public class CalculatorResource { @POST @Consumes(MediaType.TEXT_PLAIN) @Produces(MediaType.TEXT_PLAIN) - public float calculate(String expression){ + +/** + * Calculator resource exposed at '/calculator' path + */ + + + /** + * Method handling HTTP POST requests. The calculated answer to the expression will be sent to the client as + * plain/text. + * + * @param expression the expression to be solved as plain/text. + * @return solution to the expression as plain/text or -1 on error + */ + + + public float calculate(String expression) { // Removes all whitespaces - String expressionTrimmed = expression.replaceAll("\\s+",""); + + String expressionTrimmed = expression.replaceAll("\\s+", ""); float result = -1; @@ -37,80 +53,181 @@ public class CalculatorResource { * 1+2, * 10000+1000 */ - if(expressionTrimmed.matches("[0-9]+[+][0-9]+")) result = sum(expressionTrimmed); - else if(expressionTrimmed.matches("[0-9]+[-][0-9]+")) result = subtraction(expressionTrimmed); - else if(expressionTrimmed.matches("[0-9]+[*][0-9]+")) result = multiplication(expressionTrimmed); - else if(expressionTrimmed.matches("[0-9]+[/][0-9]+")) result = division(expressionTrimmed); + String[] expre = expressionTrimmed.split("[+-/*]"); + String[] oper = expressionTrimmed.split("[0-9]+"); + boolean done = false; + float tot = Float.parseFloat(expre[0]); - return result; - } + int i = 1; - /** - * Method used to calculate a sum expression. - * @param expression the equation to be calculated as a String - * @return the answer as an int - */ - public int sum(String expression){ - String[] split = expression.split("[+]"); + for (i = 1; i < expre.length; i++) { + if (oper.length == 2) + done = true; - int number1 = Integer.parseInt(split[0]); - int number2 = Integer.parseInt(split[1]); - return number1 + number2; - } + if (oper[i].equals("+") && i + 1 < oper.length) { - /** - * Method used to calculate a subtraction expression. - * @param expression the expression to be calculated as a String - * @return the answer as an int - */ - public int subtraction(String expression){ - String[] split = expression.split("[-]"); + if (!oper[i + 1].equals("*") && !oper[i + 1].equals("/")) { - int number1 = Integer.parseInt(split[0]); - int number2 = Integer.parseInt(split[1]); + tot += Float.parseFloat(expre[i]); - return number1 - number2; - } - /** - * Method used to calculate a multiplication expression, - * @param expression the expression to be calculated as a String - * @return the answer as an int. - */ + } else if (oper[i + 1].equals("*") || oper[i + 1].equals("/")) { - public int multiplication(String expression){ - String[] split = expression.split("[*]"); - int number1 = Integer.parseInt(split[0]); - int number2 = Integer.parseInt(split[1]); - - return number1*number2; - - // String[] split = expression.split("[*]"); - // int product = 0; - // for (String s : split){ - // product *= Integer.parseInt(s); - // } - // return product; - //int number1 = Integer.parseInt(split[0]); - // int number2 = Integer.parseInt(split[1]); - - - } + int iter = i + 1; + float subtot = Float.parseFloat(expre[i]); - /** - * Method used to calculate a division expression, - * @param expression the expression to be calculated as a String - * @return the answer as a float. - */ - public float division(String expression){ - String[] split = expression.split("[/]"); - int number1 = Integer.parseInt(split[0]); - int number2 = Integer.parseInt(split[1]); + while (oper[iter].equals("*") || oper[iter].equals("/")) { + + if (oper[iter].equals("*")) { + subtot *= Float.parseFloat(expre[iter]); + + } else if (oper[iter].equals("/")) { + subtot /= (Float.parseFloat(expre[iter])); + + } + + + iter++; + i++; + + if (i + 1 >= oper.length) { + + tot += subtot; + done = true; + break; + } + + } + if (done) + break; + + tot += subtot; + + } + } + if (i + 1 >= oper.length && (oper[i].equals("+"))) { + tot += Float.parseFloat(expre[i]); + break; + + } + + + if (oper[i].equals("-") && i + 1 < oper.length) { + + if (!oper[i + 1].equals("*") && !oper[i + 1].equals("/")) { + + tot -= Float.parseFloat(expre[i]); + } else if (oper[i + 1].equals("*") || oper[i + 1].equals("/")) { + int iter = i + 1; + + float subtot = Float.parseFloat(expre[i]); - return (float) number1/number2; + while (oper[iter].equals("*") || oper[iter].equals("/")) { + + if (oper[iter].equals("*")) { + + subtot *= Float.parseFloat(expre[iter]); + + if (i + 1 == oper.length) { + done = true; + continue; + } + } else if (oper[iter].equals("/")) { + subtot /= (Float.parseFloat(expre[iter])); + if (i + 1 == oper.length) { + done = true; + continue; + } + } + + + iter++; + i++; + + if (i + 1 >= (oper.length)) { + + tot -= subtot; + done = true; + break; + } + + + } + if (done) + break; + + tot -= subtot; + + continue; + + } + } + if (i + 1 >= oper.length && oper[i].equals("-")) { + tot -= Float.parseFloat(expre[i]); + break; + + } +//************************************** IF MULTIPLIERS OG DIVISORS ***************************************************** + + if ((oper[i].equals("*") || oper[i].equals("/")) && i + 1 < oper.length) { + int iter = i + 1; + + + if (oper[i].equals("*")) { + tot *= Float.parseFloat(expre[i]); + + } else if (oper[i].equals("/")) { + tot /= Float.parseFloat(expre[i]); + + } + + + while ((oper[iter].equals("*") || oper[iter].equals("/"))) { + + + if (oper[iter].equals("*")) { + tot *= Float.parseFloat(expre[iter]); + + } else if (oper[iter].equals("/")) { + tot /= (Float.parseFloat(expre[iter])); + + + } + + + iter++; + i++; + if (i + 1 >= oper.length) { + break; + } + + + } + + + } + if (i + 1 >= oper.length && done == true) { + + + if (oper[i].equals("*")) + tot *= Float.parseFloat(expre[i]); + + + if (oper[i].equals("/")) + tot /= Float.parseFloat(expre[i]); + break; + } + + + } + + + + return tot; } - - + } + + diff --git a/src/test/java/CalculatorResourceTest.java b/src/test/java/CalculatorResourceTest.java index acda6f2c6a533c36cb930a2163b2b0cae0789afa..7b7f34937af3bef28f5f9bc548ca1da48b1e92b8 100644 --- a/src/test/java/CalculatorResourceTest.java +++ b/src/test/java/CalculatorResourceTest.java @@ -21,10 +21,10 @@ public class CalculatorResourceTest{ CalculatorResource calculatorResource = new CalculatorResource(); String expression = "100+300"; - assertEquals(400, calculatorResource.sum(expression),1.0f); + assertEquals(400, calculatorResource.calculate(expression),1.0f); expression = "300+99"; - assertEquals(399, calculatorResource.sum(expression),1.0f); + assertEquals(399, calculatorResource.calculate(expression),1.0f); } @Test @@ -32,10 +32,10 @@ public class CalculatorResourceTest{ CalculatorResource calculatorResource = new CalculatorResource(); String expression = "999-100"; - assertEquals(899, calculatorResource.subtraction(expression),1.0f); + assertEquals(899, calculatorResource.calculate(expression),1.0f); expression = "20-2"; - assertEquals(18, calculatorResource.subtraction(expression),1.0f); + assertEquals(18, calculatorResource.calculate(expression),1.0f); } @Test public void testMultiplication(){ @@ -43,10 +43,10 @@ public class CalculatorResourceTest{ CalculatorResource calculatorResource = new CalculatorResource(); String expression = "40*5"; - assertEquals(200, calculatorResource.multiplication(expression),1.0f); + assertEquals(200, calculatorResource.calculate(expression),1.0f); expression = "32*9"; - assertEquals(288, calculatorResource.multiplication(expression),1.0f); + assertEquals(288, calculatorResource.calculate(expression),1.0f); } @Test @@ -55,10 +55,10 @@ public class CalculatorResourceTest{ CalculatorResource calculatorResource = new CalculatorResource(); String expression = "30/5"; - assertEquals(6.0f, calculatorResource.division(expression),1.0f); + assertEquals(6.0f, calculatorResource.calculate(expression),1.0f); expression = "10/5"; - assertEquals(2.0f, calculatorResource.division(expression),1.0f); + assertEquals(2.0f, calculatorResource.calculate(expression),1.0f); } }