Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testing for concurrency #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions src/main/java/com/amazonaws/App.java
Original file line number Diff line number Diff line change
@@ -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("<insert the profiling group name here>")
.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;
}
}
}

36 changes: 36 additions & 0 deletions src/main/java/com/amazonaws/CreateIllegalOrderThread.java
Original file line number Diff line number Diff line change
@@ -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");
}

}
}

72 changes: 72 additions & 0 deletions src/main/java/com/amazonaws/CreateOrderThread.java
Original file line number Diff line number Diff line change
@@ -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<ProductName> 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
}
}

}

81 changes: 81 additions & 0 deletions src/main/java/com/amazonaws/ListOrderThread.java
Original file line number Diff line number Diff line change
@@ -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;
}
}

67 changes: 67 additions & 0 deletions src/main/java/com/amazonaws/Order.java
Original file line number Diff line number Diff line change
@@ -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;
}
}

46 changes: 46 additions & 0 deletions src/main/java/com/amazonaws/ProductName.java
Original file line number Diff line number Diff line change
@@ -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<ProductName> 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);
}
}

Loading