From 6cf1b105e3df4ae0c4515ae608e15dcc07244141 Mon Sep 17 00:00:00 2001 From: MZC02-KIMCY Date: Mon, 16 Dec 2019 11:00:49 +0900 Subject: [PATCH] update code --- .DS_Store | Bin 0 -> 6148 bytes CODE_OF_CONDUCT.md | 4 + CONTRIBUTING.md | 61 +++++ LICENSE | 15 ++ pom.xml | 112 +++++++++ .../com/company/sample/application/App.java | 80 ++++++ .../application/CreateIllegalOrderThread.java | 36 +++ .../sample/application/CreateOrderThread.java | 72 ++++++ .../sample/application/ListOrderThread.java | 90 +++++++ .../com/company/sample/application/Order.java | 67 +++++ .../sample/application/ProductName.java | 46 ++++ .../sample/application/SalesSystem.java | 60 +++++ .../com/company/sample/application/Util.java | 233 ++++++++++++++++++ 13 files changed, 876 insertions(+) create mode 100644 .DS_Store create mode 100644 CODE_OF_CONDUCT.md create mode 100644 CONTRIBUTING.md create mode 100644 LICENSE create mode 100644 pom.xml create mode 100644 src/main/java/com/company/sample/application/App.java create mode 100644 src/main/java/com/company/sample/application/CreateIllegalOrderThread.java create mode 100644 src/main/java/com/company/sample/application/CreateOrderThread.java create mode 100644 src/main/java/com/company/sample/application/ListOrderThread.java create mode 100644 src/main/java/com/company/sample/application/Order.java create mode 100644 src/main/java/com/company/sample/application/ProductName.java create mode 100644 src/main/java/com/company/sample/application/SalesSystem.java create mode 100644 src/main/java/com/company/sample/application/Util.java diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 GIT binary patch literal 6148 zcmeH~Jr2S!425mzP>H1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0 + + + 4.0.0 + + com.amazon.SampleApplication + example + 1.0-SNAPSHOT + + example + + + UTF-8 + 1.8 + 1.8 + + + + + + codeguru-profiler + codeguru-profiler + https://d1osg35nybn3tt.cloudfront.net + + + + + + + software.amazon.awssdk + bom + 2.9.17 + pom + import + + + + + + + + com.amazonaws + codeguru-profiler-java-agent + 0.1.0 + + + + com.amazonaws + aws-java-sdk-s3 + 1.11.543 + + + + software.amazon.awssdk + auth + 2.9.17 + + + + com.google.guava + guava + 28.1-jre + + + + + + + + + maven-clean-plugin + 3.1.0 + + + + maven-resources-plugin + 3.0.2 + + + maven-compiler-plugin + 3.8.0 + + + maven-surefire-plugin + 2.22.1 + + + maven-jar-plugin + 3.0.2 + + + maven-install-plugin + 2.5.2 + + + maven-deploy-plugin + 2.8.2 + + + + maven-site-plugin + 3.7.1 + + + maven-project-info-reports-plugin + 3.0.0 + + + + + diff --git a/src/main/java/com/company/sample/application/App.java b/src/main/java/com/company/sample/application/App.java new file mode 100644 index 0000000..e59fb6e --- /dev/null +++ b/src/main/java/com/company/sample/application/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("CodeGuru-Profiler-Sample") + .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/company/sample/application/CreateIllegalOrderThread.java b/src/main/java/com/company/sample/application/CreateIllegalOrderThread.java new file mode 100644 index 0000000..fe5099d --- /dev/null +++ b/src/main/java/com/company/sample/application/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/company/sample/application/CreateOrderThread.java b/src/main/java/com/company/sample/application/CreateOrderThread.java new file mode 100644 index 0000000..655a981 --- /dev/null +++ b/src/main/java/com/company/sample/application/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/company/sample/application/ListOrderThread.java b/src/main/java/com/company/sample/application/ListOrderThread.java new file mode 100644 index 0000000..ac4dd91 --- /dev/null +++ b/src/main/java/com/company/sample/application/ListOrderThread.java @@ -0,0 +1,90 @@ +/* + * 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(); + + String productNames = ""; + + 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)); + System.out.println(orderAsString); + + productNames += "," + SalesSystem.orders.get(orderDate).getProductName(); + } + } catch (JsonProcessingException e) { + e.printStackTrace(); + } catch (ParseException e) { + e.printStackTrace(); + } + } + + productNames = productNames.substring(2); + System.out.println("Product Names : " + productNames); + + } + } + + public void setDate(String today){ + this.today = today; + } +} + diff --git a/src/main/java/com/company/sample/application/Order.java b/src/main/java/com/company/sample/application/Order.java new file mode 100644 index 0000000..507b3a0 --- /dev/null +++ b/src/main/java/com/company/sample/application/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/company/sample/application/ProductName.java b/src/main/java/com/company/sample/application/ProductName.java new file mode 100644 index 0000000..9eb2695 --- /dev/null +++ b/src/main/java/com/company/sample/application/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/company/sample/application/SalesSystem.java b/src/main/java/com/company/sample/application/SalesSystem.java new file mode 100644 index 0000000..a520f7c --- /dev/null +++ b/src/main/java/com/company/sample/application/SalesSystem.java @@ -0,0 +1,60 @@ +/* + * 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.awssdk.auth.credentials.ProfileCredentialsProvider; +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("CodeGuru-Profiler-Sample") + .awsCredentialsProvider(ProfileCredentialsProvider.create("megazone")) + .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/company/sample/application/Util.java b/src/main/java/com/company/sample/application/Util.java new file mode 100644 index 0000000..c513a33 --- /dev/null +++ b/src/main/java/com/company/sample/application/Util.java @@ -0,0 +1,233 @@ +/* + * 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.*; + +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); + } + + public static int test(int len) { + int test = 0; + for(int i=0; i repo = new HashMap(); + if(repo.containsKey(ip)) { + return repo.get(ip); + } + return ""; + } +} +