-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'simulate-compute-intensive-task'
- Loading branch information
Showing
11 changed files
with
236 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
...n/java/de/unistuttgart/t2/modulith/order/calculation/ComputeIntensiveTotalCalculator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package de.unistuttgart.t2.modulith.order.calculation; | ||
|
||
import de.unistuttgart.t2.modulith.cart.CartContent; | ||
import de.unistuttgart.t2.modulith.cart.CartService; | ||
import de.unistuttgart.t2.modulith.inventory.InventoryService; | ||
import de.unistuttgart.t2.modulith.inventory.Product; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.Optional; | ||
|
||
/** | ||
* Calculator to simulate a compute intensive task. | ||
* Calculates the total sum of order. | ||
* | ||
* @author davidkopp | ||
*/ | ||
public class ComputeIntensiveTotalCalculator implements ITotalCalculator { | ||
|
||
private final CartService cartService; | ||
private final InventoryService inventoryService; | ||
private final int iterations; | ||
|
||
private final Logger LOG = LoggerFactory.getLogger(getClass()); | ||
|
||
public ComputeIntensiveTotalCalculator(CartService cartService, InventoryService inventoryService, int iterations) { | ||
this.cartService = cartService; | ||
this.inventoryService = inventoryService; | ||
this.iterations = iterations; | ||
} | ||
|
||
/** | ||
* Calculates the total of a users cart many times to simulate a compute intensive task. | ||
* <p> | ||
* Depends on the cart module to get the cart content and depends on the inventory module to get the price per | ||
* unit. | ||
* | ||
* @param sessionId identifies the session to get total for | ||
* @return the total money to pay for products in the cart | ||
*/ | ||
public double calculate(String sessionId) { | ||
LOG.debug("Compute intensive order calculation started with {} iterations", iterations); | ||
CartContent cart = cartService.getCart(sessionId).orElse(new CartContent()); | ||
|
||
double total = 0; | ||
for (String productId : cart.getProductIds()) { | ||
Optional<Product> product = inventoryService.getSingleProduct(productId); | ||
if (product.isEmpty()) { | ||
return 0; | ||
} | ||
|
||
// simulate compute intensive task | ||
double temp = 0; | ||
for (int i = 0; i < iterations; i++) { | ||
temp += product.get().getPrice() * cart.getUnits(productId); | ||
} | ||
total += temp / iterations; | ||
} | ||
LOG.debug("Compute intensive order calculation finished"); | ||
return total; | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
src/main/java/de/unistuttgart/t2/modulith/order/calculation/ITotalCalculator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package de.unistuttgart.t2.modulith.order.calculation; | ||
|
||
public interface ITotalCalculator { | ||
|
||
double calculate(String sessionId); | ||
} |
48 changes: 48 additions & 0 deletions
48
src/main/java/de/unistuttgart/t2/modulith/order/calculation/SimpleTotalCalculator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package de.unistuttgart.t2.modulith.order.calculation; | ||
|
||
import de.unistuttgart.t2.modulith.cart.CartContent; | ||
import de.unistuttgart.t2.modulith.cart.CartService; | ||
import de.unistuttgart.t2.modulith.inventory.InventoryService; | ||
import de.unistuttgart.t2.modulith.inventory.Product; | ||
|
||
import java.util.Optional; | ||
|
||
/** | ||
* Simple calculator to get total sum of order (default). | ||
* | ||
* @author davidkopp | ||
*/ | ||
public class SimpleTotalCalculator implements ITotalCalculator { | ||
|
||
private final CartService cartService; | ||
private final InventoryService inventoryService; | ||
|
||
public SimpleTotalCalculator(CartService cartService, InventoryService inventoryService) { | ||
this.cartService = cartService; | ||
this.inventoryService = inventoryService; | ||
} | ||
|
||
/** | ||
* Calculates the total of a users cart. | ||
* <p> | ||
* Depends on the cart module to get the cart content and depends on the inventory module to get the price per | ||
* unit. | ||
* | ||
* @param sessionId identifies the session to get total for | ||
* @return the total money to pay for products in the cart | ||
*/ | ||
public double calculate(String sessionId) { | ||
CartContent cart = cartService.getCart(sessionId).orElse(new CartContent()); | ||
|
||
double total = 0; | ||
|
||
for (String productId : cart.getProductIds()) { | ||
Optional<Product> product = inventoryService.getSingleProduct(productId); | ||
if (product.isEmpty()) { | ||
return 0; | ||
} | ||
total += product.get().getPrice() * cart.getUnits(productId); | ||
} | ||
return total; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/test/java/de/unistuttgart/t2/modulith/order/CalculatorTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package de.unistuttgart.t2.modulith.order; | ||
|
||
import de.unistuttgart.t2.modulith.cart.CartService; | ||
import de.unistuttgart.t2.modulith.inventory.InventoryService; | ||
import de.unistuttgart.t2.modulith.order.calculation.ComputeIntensiveTotalCalculator; | ||
import de.unistuttgart.t2.modulith.order.calculation.SimpleTotalCalculator; | ||
import de.unistuttgart.t2.modulith.order.calculation.ITotalCalculator; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.test.context.ActiveProfiles; | ||
|
||
import static de.unistuttgart.t2.modulith.TestData.*; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.Mockito.when; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
@ActiveProfiles("test") | ||
public class CalculatorTests { | ||
|
||
@Mock | ||
CartService cartService; | ||
|
||
@Mock | ||
InventoryService inventoryService; | ||
|
||
@BeforeEach | ||
public void beforeEach() { | ||
when(cartService.getCart(sessionId)).thenReturn(cartResponseMulti()); | ||
when(inventoryService.getSingleProduct(productId)).thenReturn(inventoryResponse()); | ||
when(inventoryService.getSingleProduct(anotherProductId)).thenReturn(anotherInventoryResponse()); | ||
} | ||
|
||
@Test | ||
public void defaultCalculator() { | ||
ITotalCalculator calculator = new SimpleTotalCalculator(cartService, inventoryService); | ||
double result = calculator.calculate(sessionId); | ||
|
||
assertEquals(totalOfCartMulti, result); | ||
} | ||
|
||
@Test | ||
public void computeIntensiveCalculator() { | ||
int iterations = 1_000_0000; // use a higher number like e.g. 1_000_000_000 to see that it is actually compute intensive | ||
ITotalCalculator calculator = new ComputeIntensiveTotalCalculator(cartService, inventoryService, iterations); | ||
double result = calculator.calculate(sessionId); | ||
|
||
assertEquals(totalOfCartMulti, result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,3 +42,7 @@ t2: | |
timeout: 5 | ||
dummy: | ||
url: http://foo.bar/pay | ||
order: | ||
simulateComputeIntensiveTask: | ||
enabled: false | ||
iterations: 1 |