Skip to content

Commit 3948f25

Browse files
author
srathod
committed
- Adding multi threading test.
1 parent 2631236 commit 3948f25

1 file changed

Lines changed: 101 additions & 0 deletions

File tree

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package net.authorize.sample;
2+
3+
import java.security.SecureRandom;
4+
import java.text.DecimalFormat;
5+
import java.util.Date;
6+
import java.util.concurrent.ExecutorService;
7+
import java.util.concurrent.Executors;
8+
9+
import net.authorize.api.contract.v1.CreateCustomerPaymentProfileResponse;
10+
import net.authorize.api.contract.v1.CreateCustomerProfileResponse;
11+
import net.authorize.api.contract.v1.CreateTransactionResponse;
12+
import net.authorize.sample.CustomerProfiles.CreateCustomerPaymentProfile;
13+
import net.authorize.sample.CustomerProfiles.CreateCustomerProfile;
14+
import net.authorize.sample.PaymentTransactions.ChargeCustomerProfile;
15+
16+
public class TestMultiThread {
17+
18+
private static final int MYTHREADS = 120;
19+
private static String apiLoginId = "5KP3u95bQpv";
20+
private static String transactionKey = "346HZ32z3fP4hTG2";
21+
22+
static SecureRandom rgenerator = new SecureRandom();
23+
24+
private static Double getAmount()
25+
{
26+
double d = (double)(1.05 + (450.0 * rgenerator.nextDouble()));
27+
DecimalFormat df = new DecimalFormat("#.##");
28+
d = Double.valueOf(df.format(d));
29+
return d;
30+
}
31+
32+
private static String getEmail()
33+
{
34+
return rgenerator.nextInt(10000) + "@test.com";
35+
}
36+
37+
public static void main(String args[]) throws Exception {
38+
ExecutorService executor = Executors.newFixedThreadPool(MYTHREADS);
39+
40+
Date dt1 = new Date();
41+
for (int i = 0; i < MYTHREADS; i++) {
42+
Runnable worker = new MyRunnable();
43+
executor.execute(worker);
44+
}
45+
executor.shutdown();
46+
47+
// Wait until all threads are finish
48+
while (!executor.isTerminated()) {
49+
50+
}
51+
Date dt2 = new Date();
52+
long difference = dt2.getTime() - dt1.getTime();
53+
54+
System.out.println("\nFinished all threads : " + difference/1000 + " sec");
55+
}
56+
57+
public static class MyRunnable implements Runnable {
58+
59+
@Override
60+
public void run() {
61+
CreateCustomerProfileResponse response = null;
62+
CreateCustomerPaymentProfileResponse paymentProfileResponse = null;
63+
64+
try{
65+
response = (CreateCustomerProfileResponse)CreateCustomerProfile.run(apiLoginId, transactionKey, getEmail());
66+
paymentProfileResponse = (CreateCustomerPaymentProfileResponse)CreateCustomerPaymentProfile.run(apiLoginId, transactionKey, response.getCustomerProfileId());
67+
}
68+
catch (Exception e) {
69+
System.out.println("Failure in CIM calls.");
70+
System.out.println(e.getMessage());
71+
return;
72+
}
73+
74+
try {
75+
Date dt1 = new Date();
76+
System.out.println("============================================================================");
77+
System.out.println("chargeResponse Dt1: + " + dt1.toString());
78+
79+
if((response.getCustomerProfileId() != null) && (paymentProfileResponse.getCustomerPaymentProfileId() != null))
80+
{
81+
CreateTransactionResponse chargeResponse = (CreateTransactionResponse) ChargeCustomerProfile.run(apiLoginId, transactionKey,
82+
response.getCustomerProfileId(), paymentProfileResponse.getCustomerPaymentProfileId(), getAmount());
83+
}
84+
else
85+
{
86+
System.out.println("NULL PROFILE IDS.");
87+
}
88+
89+
Date dt2 = new Date();
90+
System.out.println("chargeResponse Dt2: + " + dt2.toString());
91+
92+
long difference = dt2.getTime() - dt1.getTime();
93+
System.out.println("chargeResponse Diff: " + difference/1000);
94+
System.out.println("============================================================================");
95+
} catch (Exception e) {
96+
System.out.println("Failure in charge customer profile call:");
97+
System.out.println(e.getMessage());
98+
}
99+
}
100+
}
101+
}

0 commit comments

Comments
 (0)