diff --git a/src/main/java/com/amazonaws/App.java b/src/main/java/com/amazonaws/App.java new file mode 100644 index 0000000..54bd72a --- /dev/null +++ b/src/main/java/com/amazonaws/App.java @@ -0,0 +1,80 @@ +/* + * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: MIT-0 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this + * software and associated documentation files (the "Software"), to deal in the Software + * without restriction, including without limitation the rights to use, copy, modify, + * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.company.sample.application; + +import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider; +import software.amazon.codeguruprofilerjavaagent.Profiler; + +public class App { + + public static void main(String[] args) { + // start the profiler + Profiler.builder().profilingGroupName("") + .awsCredentialsProvider(DefaultCredentialsProvider.create()) + .build() + .start(); + + App app = new App(); + + while(true){ + app.load(); + app.load1(); + app.load2(); + } + } + + private void load() { + for (int i =0; i< 1 << 20; ++i){ + computeShort(); + } + } + + private void load1(){ + for (int i =0; i< 1 << 20; ++i){ + computeMedium(); + } + } + + private void load2(){ + for (int i =0; i< 1 << 20; ++i){ + computeLong(); + } + } + + private void computeShort(){ + long x = 0; + for (int i =0; i< 1 << 15; ++i){ + x += i; + } + } + + private void computeMedium(){ + long x = 0; + for (int i =0; i< 1 << 17; ++i){ + x += i; + } + } + + private void computeLong(){ + long x = 0; + for (int i =0; i< 1 << 18; ++i){ + x += i; + } + } +} + diff --git a/src/main/java/com/amazonaws/CreateIllegalOrderThread.java b/src/main/java/com/amazonaws/CreateIllegalOrderThread.java new file mode 100644 index 0000000..fe5099d --- /dev/null +++ b/src/main/java/com/amazonaws/CreateIllegalOrderThread.java @@ -0,0 +1,36 @@ +/* + * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: MIT-0 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this + * software and associated documentation files (the "Software"), to deal in the Software + * without restriction, including without limitation the rights to use, copy, modify, + * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.company.sample.application; + +public class CreateIllegalOrderThread extends CreateOrderThread{ + + private volatile boolean exit = false; + + public void run() { + while (!exit) { + //some illegal product names + createOrder("MANGO"); + createOrder("STRAWBERRY"); + createOrder("BANANA"); + createOrder("GRAPE"); + createOrder("CHERRY"); + } + + } +} + diff --git a/src/main/java/com/amazonaws/CreateOrderThread.java b/src/main/java/com/amazonaws/CreateOrderThread.java new file mode 100644 index 0000000..655a981 --- /dev/null +++ b/src/main/java/com/amazonaws/CreateOrderThread.java @@ -0,0 +1,72 @@ +/* + * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: MIT-0 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this + * software and associated documentation files (the "Software"), to deal in the Software + * without restriction, including without limitation the rights to use, copy, modify, + * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.company.sample.application; + +import com.google.common.base.Optional; +import java.util.Date; +import java.util.Random; + +public class CreateOrderThread extends Thread{ + + static int id = 0; + + private volatile boolean exit = false; + + private static Random random = new Random(); + + public void run() { + while (!exit) { + createOrder("APPLE"); + createOrder("ORANGE"); + createOrder("PINEAPPLE"); + } + + } + + /** + * Create random orders + * @param productName + */ + public void createOrder(String productName){ + try { + Date orderDate = Util.getRandomDate(); + Optional optional = ProductName.getProductName(productName); + + if (!optional.isPresent()) { + return; + } + + ProductName enumProductName = optional.get(); + + Order order = new Order(enumProductName, orderDate, random.nextDouble() * 10000, id); + + if (SalesSystem.orders.size() > 10000) { + SalesSystem.orders.clear(); + id = 0; + } + + SalesSystem.orders.put(orderDate, order); + id++; + } catch (IllegalArgumentException e){ + //e.printStackTrace(); + //not showing exception stack trace here because it will wash away Profiler's running log + } + } + +} + diff --git a/src/main/java/com/amazonaws/ListOrderThread.java b/src/main/java/com/amazonaws/ListOrderThread.java new file mode 100644 index 0000000..588f487 --- /dev/null +++ b/src/main/java/com/amazonaws/ListOrderThread.java @@ -0,0 +1,81 @@ +/* + * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: MIT-0 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this + * software and associated documentation files (the "Software"), to deal in the Software + * without restriction, including without limitation the rights to use, copy, modify, + * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.company.sample.application; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import java.text.DateFormat; +import java.text.DateFormatSymbols; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; + +public class ListOrderThread extends Thread { + private volatile boolean exit = false; + + /** + * Resolution: uncomment the following line to see how this improves the profile. + */ +// private static DateFormatSymbols dateFormatSymbols = DateFormatSymbols.getInstance(); +// private static DateFormat myFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", dateFormatSymbols); +// private static DateFormat todayFormat = new SimpleDateFormat("dd MMM yyyy", dateFormatSymbols); + /** + * Here DateFormatSymbols are not provided to the SimpleDateFormat + * constructor and it will look up on every call, comment the below two lines + */ + private static DateFormat myFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z"); + private static DateFormat todayFormat = new SimpleDateFormat("dd MMM yyyy"); + + private static String today = null; + + @Override + public void run() { + while (!exit) { + listOrders(); + + } + } + + /** + * List the same day orders + */ + private void listOrders(){ + ObjectMapper objectMapper = new ObjectMapper(); + + synchronized (SalesSystem.orders) { + for(Date orderDate: SalesSystem.orders.keySet()){ + try { + objectMapper.setDateFormat(myFormat); + Date todayDate = todayFormat.parse(this.today); + if(Util.isSameDay(orderDate, todayDate)) { + String orderAsString = objectMapper.writeValueAsString(SalesSystem.orders.get(orderDate)); + } + } catch (JsonProcessingException e) { + e.printStackTrace(); + } catch (ParseException e) { + e.printStackTrace(); + } + } + } + } + + public void setDate(String today){ + this.today = today; + } +} + diff --git a/src/main/java/com/amazonaws/Order.java b/src/main/java/com/amazonaws/Order.java new file mode 100644 index 0000000..507b3a0 --- /dev/null +++ b/src/main/java/com/amazonaws/Order.java @@ -0,0 +1,67 @@ +/* + * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: MIT-0 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this + * software and associated documentation files (the "Software"), to deal in the Software + * without restriction, including without limitation the rights to use, copy, modify, + * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.company.sample.application; + +import java.util.Date; + +public class Order { + private ProductName productName; + private Date salesDate; + private double amount; + private int orderId; + + public Order(ProductName productName, Date salesDate, double amount, int orderId) { + this.productName = productName; + this.salesDate = salesDate; + this.amount = amount; + this.orderId = orderId; + } + + public ProductName getProductName() { + return productName; + } + + public void setProductName(ProductName productName) { + this.productName = productName; + } + + public Date getSalesDate() { + return salesDate; + } + + public void setSalesDate(Date salesDate) { + this.salesDate = salesDate; + } + + public double getAmount() { + return amount; + } + + public void setAmount(double amount) { + this.amount = amount; + } + + public int getOrderId() { + return orderId; + } + + public void setOrderId(int orderId) { + this.orderId = orderId; + } +} + diff --git a/src/main/java/com/amazonaws/ProductName.java b/src/main/java/com/amazonaws/ProductName.java new file mode 100644 index 0000000..9eb2695 --- /dev/null +++ b/src/main/java/com/amazonaws/ProductName.java @@ -0,0 +1,46 @@ +/* + * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: MIT-0 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this + * software and associated documentation files (the "Software"), to deal in the Software + * without restriction, including without limitation the rights to use, copy, modify, + * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.company.sample.application; + +import com.google.common.base.Enums; + +import com.google.common.base.Optional; + +public enum ProductName{ + + APPLE, ORANGE, PINEAPPLE; + + /** + * Pick a random value of the ProductName enum. + * @return a random ProductName. + */ + public static Optional getProductName(String name) { + /** + * Here is attempting to parse a value in the enum, if the value is not found in the enum, + * it results in an exception being thrown, comment the below two lines to fix it and uncomment line 43 + */ + ProductName productName = ProductName.valueOf(name); + return Optional.of(productName); + + /** + * Resolution: uncomment the following line to see how this improves the profile. + */ +// return Enums.getIfPresent(ProductName.class, name); + } +} + diff --git a/src/main/java/com/amazonaws/SalesSystem.java b/src/main/java/com/amazonaws/SalesSystem.java new file mode 100644 index 0000000..a404606 --- /dev/null +++ b/src/main/java/com/amazonaws/SalesSystem.java @@ -0,0 +1,59 @@ +/* + * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: MIT-0 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this + * software and associated documentation files (the "Software"), to deal in the Software + * without restriction, including without limitation the rights to use, copy, modify, + * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.company.sample.application; + +import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider; +import software.amazon.codeguruprofilerjavaagent.Profiler; + +import java.text.DateFormat; +import java.text.SimpleDateFormat; + +import java.util.Date; +import java.util.concurrent.ConcurrentHashMap; + +public class SalesSystem { + + public static ConcurrentHashMap orders = new ConcurrentHashMap(); + + public static void main(String[] args) { + //Start the profiler + Profiler systemProfiler = + Profiler.builder().profilingGroupName("") + .awsCredentialsProvider(DefaultCredentialsProvider.create()) + .build(); + + systemProfiler.start(); + + //Start create order thread + CreateOrderThread createOrderThread = new CreateOrderThread(); + createOrderThread.start(); + + //Start create Illegal order thread + CreateIllegalOrderThread createIllegalOrderThread = new CreateIllegalOrderThread(); + createIllegalOrderThread.start(); + + //Start list order thread + ListOrderThread listOrderThread = new ListOrderThread(); + + DateFormat currentDateFormat = new SimpleDateFormat("dd MMM yyyy"); + listOrderThread.setDate(currentDateFormat.format(new Date())); + + listOrderThread.start(); + } +} + diff --git a/src/main/java/com/amazonaws/Util.java b/src/main/java/com/amazonaws/Util.java new file mode 100644 index 0000000..c223eef --- /dev/null +++ b/src/main/java/com/amazonaws/Util.java @@ -0,0 +1,68 @@ +/* + * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: MIT-0 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this + * software and associated documentation files (the "Software"), to deal in the Software + * without restriction, including without limitation the rights to use, copy, modify, + * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.company.sample.application; + +import java.util.Calendar; +import java.util.Date; +import java.util.GregorianCalendar; + +public class Util { + /** + * generate a random date in this year + * @return + */ + public static Date getRandomDate(){ + + GregorianCalendar gc = new GregorianCalendar(); + + int dayOfYear = randBetween(1, gc.getActualMaximum(gc.DAY_OF_YEAR)); + + gc.set(gc.DAY_OF_YEAR, dayOfYear); + + return gc.getTime(); + } + + /** + * get a random number between start and end + * @param start + * @param end + * @return + */ + public static int randBetween(int start, int end) { + return start + (int)Math.round(Math.random() * (end - start)); + } + + /** + * Judge two dates are in the same day + * @param date1 + * @param date2 + * @return + */ + public static boolean isSameDay(Date date1, Date date2) { + if(date1 == null && date2 == null) { + throw new IllegalArgumentException("The date must not be null"); + } + //logic to compare the dates and return boolean. + Calendar cal1 = Calendar.getInstance(); + Calendar cal2 = Calendar.getInstance(); + cal1.setTime(date1); + cal2.setTime(date2); + return cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR) && cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR); + } +} +