├── README.md
├── src
└── main
│ ├── java
│ ├── services
│ │ ├── DataUtils.java
│ │ ├── ExceptionTest.java
│ │ └── ServletUtilities.java
│ ├── forkjoin
│ │ ├── file
│ │ │ ├── WordIndex.java
│ │ │ ├── ParallelStream.java
│ │ │ ├── SerializeThread.java
│ │ │ ├── PureForkJoin.java
│ │ │ ├── example
│ │ │ │ └── Split.java
│ │ │ ├── QueuePool.java
│ │ │ └── TestFile.java
│ │ ├── prime
│ │ │ ├── SerializeThread.java
│ │ │ ├── ParallelStream.java
│ │ │ ├── QueuePool.java
│ │ │ ├── example
│ │ │ │ ├── SequentialStreamPrimes.java
│ │ │ │ └── ForkBlur.java
│ │ │ ├── PureForkJoin.java
│ │ │ └── TestPrime.java
│ │ ├── StartPoint.java
│ │ └── Utils.java
│ └── controllers
│ │ ├── speed
│ │ ├── NormalSpeed.java
│ │ ├── Wait100msSpeed.java
│ │ └── Res1mbSpeed.java
│ │ └── exception
│ │ ├── ExpStackTrace.java
│ │ └── ExpNormal.java
│ ├── webapp
│ ├── 400.json
│ ├── 404.json
│ └── WEB-INF
│ │ └── web.xml
│ └── resources
│ └── log4j.properties
├── .gitignore
├── .settings
└── org.eclipse.jdt.core.prefs
├── .project
├── .classpath
├── pom.xml
└── test.txt
/README.md:
--------------------------------------------------------------------------------
1 | # threads-benchmark
2 | Threads benchmark code for Takipi blog
3 |
--------------------------------------------------------------------------------
/src/main/java/services/DataUtils.java:
--------------------------------------------------------------------------------
1 | package services;
2 |
3 |
4 | public class DataUtils {
5 |
6 |
7 | static {
8 |
9 | }
10 |
11 | }
12 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.class
2 |
3 | # Mobile Tools for Java (J2ME)
4 | .mtj.tmp/
5 |
6 | # Package Files #
7 | *.jar
8 | *.war
9 | *.ear
10 |
11 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
12 | hs_err_pid*
13 |
--------------------------------------------------------------------------------
/.settings/org.eclipse.jdt.core.prefs:
--------------------------------------------------------------------------------
1 | #Mon Jan 19 21:05:26 IST 2015
2 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
3 | eclipse.preferences.version=1
4 | org.eclipse.jdt.core.compiler.source=1.8
5 | org.eclipse.jdt.core.compiler.compliance=1.8
6 |
--------------------------------------------------------------------------------
/src/main/webapp/400.json:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 | {"status":404, "Message":"This reource is not exist"}
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/src/main/webapp/404.json:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 | {"status":404, "Message":"This reource is not exist"}
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/src/main/java/forkjoin/file/WordIndex.java:
--------------------------------------------------------------------------------
1 | package forkjoin.file;
2 |
3 | public class WordIndex {
4 | private long line;
5 | private int localIndex;
6 | public WordIndex(long line, int localIndex){
7 | this.line = line;
8 | this.localIndex = localIndex;
9 | }
10 | @Override
11 | public String toString(){
12 | return String.format("Line = %s, localIndex = %s", line, localIndex);
13 | }
14 |
15 | }
16 |
--------------------------------------------------------------------------------
/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | TakipiAWSTest
4 | NO_M2ECLIPSE_SUPPORT: Project files created with the maven-eclipse-plugin are not supported in M2Eclipse.
5 |
6 |
7 |
8 | org.eclipse.jdt.core.javabuilder
9 |
10 |
11 |
12 | org.eclipse.jdt.core.javanature
13 |
14 |
--------------------------------------------------------------------------------
/src/main/java/forkjoin/prime/SerializeThread.java:
--------------------------------------------------------------------------------
1 | package forkjoin.prime;
2 |
3 | import java.math.BigInteger;
4 |
5 | import forkjoin.Utils;
6 |
7 | public class SerializeThread {
8 | public boolean isprime(BigInteger n) {
9 | boolean isPrime = true;
10 |
11 | for (BigInteger i = Utils.two; i.compareTo(TestPrime.sqrt) <= 0; i = i
12 | .add(BigInteger.ONE)) {
13 | if (n.mod(i).equals(BigInteger.ZERO)) {
14 | isPrime = false;
15 | }
16 | }
17 |
18 | return isPrime;
19 | }
20 |
21 | }
22 |
--------------------------------------------------------------------------------
/src/main/resources/log4j.properties:
--------------------------------------------------------------------------------
1 | # LOG4J configuration
2 | log4j.rootLogger=INFO, Appender1,Appender2
3 |
4 | log4j.appender.Appender1=org.apache.log4j.ConsoleAppender
5 | log4j.appender.Appender1.layout=org.apache.log4j.PatternLayout
6 | log4j.appender.Appender1.layout.ConversionPattern=%-7p %d [%t] %c %x - %m%n
7 |
8 | log4j.appender.Appender2=org.apache.log4j.FileAppender
9 | log4j.appender.Appender2.File=/var/log/billistic.log
10 | log4j.appender.Appender2.layout=org.apache.log4j.PatternLayout
11 | log4j.appender.Appender2.layout.ConversionPattern=%-7p %d [%t] %c %x - %m%n
--------------------------------------------------------------------------------
/src/main/java/forkjoin/prime/ParallelStream.java:
--------------------------------------------------------------------------------
1 | package forkjoin.prime;
2 |
3 | import java.math.BigInteger;
4 | import java.util.Set;
5 | import java.util.stream.Collectors;
6 | import java.util.stream.Stream;
7 |
8 | import forkjoin.Utils;
9 |
10 | public class ParallelStream {
11 | public boolean isPrime() {
12 | Set collect = Stream
13 | .iterate(Utils.two, n -> n.add(TestPrime.lengthForThread))
14 | .limit(TestPrime.numberOfThread.longValue()).parallel()
15 | .filter(i -> Utils.primeProcessPart(i))
16 | .collect(Collectors.toSet());
17 | return collect.size() == 0;
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/main/java/forkjoin/StartPoint.java:
--------------------------------------------------------------------------------
1 | package forkjoin;
2 |
3 | import forkjoin.file.TestFile;
4 | import forkjoin.prime.TestPrime;
5 |
6 | public class StartPoint {
7 | public static void main(String[] args) throws Exception {
8 | int request = Integer.parseInt(args[0]);
9 |
10 | if (request == 0) {
11 | TestFile.createTestFile();
12 | return;
13 | }
14 |
15 | int runCase = Integer.parseInt(args[1]);
16 |
17 | if (request == 1) {
18 | long lineNum = Long.parseLong(args[2]);
19 | TestFile.testFile(runCase, lineNum);
20 | return;
21 | }
22 |
23 | if (request == 2) {
24 | TestPrime.testPrime(runCase, args[2], args[3]);
25 | return;
26 | }
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/src/main/java/controllers/speed/NormalSpeed.java:
--------------------------------------------------------------------------------
1 | package controllers.speed;
2 |
3 | import java.io.IOException;
4 | import java.io.PrintWriter;
5 |
6 | import javax.servlet.ServletException;
7 | import javax.servlet.annotation.WebServlet;
8 | import javax.servlet.http.HttpServlet;
9 | import javax.servlet.http.HttpServletRequest;
10 | import javax.servlet.http.HttpServletResponse;
11 |
12 | @WebServlet("/speed/normal")
13 | public class NormalSpeed extends HttpServlet {
14 | private static final long serialVersionUID = -7166033569721460991L;
15 |
16 | @Override
17 | public void doGet(HttpServletRequest request, HttpServletResponse response)
18 | throws ServletException, IOException {
19 | PrintWriter out = response.getWriter();
20 | out.println("OK");
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/src/main/java/forkjoin/prime/QueuePool.java:
--------------------------------------------------------------------------------
1 | package forkjoin.prime;
2 |
3 | import java.math.BigInteger;
4 | import java.util.concurrent.Callable;
5 |
6 | public class QueuePool implements Callable {
7 |
8 | private BigInteger from;
9 | private BigInteger to;
10 | private BigInteger primeNumber;
11 |
12 | public QueuePool(BigInteger primeNumber, BigInteger from, BigInteger to) {
13 | this.primeNumber = primeNumber;
14 | this.from = from;
15 | this.to = to;
16 | }
17 |
18 | @Override
19 | public Boolean call() {
20 | boolean isPrime = true;
21 | for (BigInteger i = from; i.compareTo(to) <= 0; i = i
22 | .add(BigInteger.ONE)) {
23 | if (this.primeNumber.mod(i).equals(BigInteger.ZERO)) {
24 | isPrime = false;
25 | }
26 | }
27 | return isPrime;
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/src/main/webapp/WEB-INF/web.xml:
--------------------------------------------------------------------------------
1 |
5 |
6 | Web Application
7 |
8 |
9 | 404
10 | /error/404.json
11 |
12 |
13 | 400
14 | /error/400.json
15 |
16 |
17 |
18 | java.lang.Exception
19 | /error/exception.json
20 |
21 |
--------------------------------------------------------------------------------
/src/main/java/controllers/speed/Wait100msSpeed.java:
--------------------------------------------------------------------------------
1 | package controllers.speed;
2 |
3 | import java.io.IOException;
4 | import java.io.PrintWriter;
5 |
6 | import javax.servlet.ServletException;
7 | import javax.servlet.annotation.WebServlet;
8 | import javax.servlet.http.HttpServlet;
9 | import javax.servlet.http.HttpServletRequest;
10 | import javax.servlet.http.HttpServletResponse;
11 |
12 | @WebServlet("/speed/wait100")
13 | public class Wait100msSpeed extends HttpServlet {
14 | private static final long serialVersionUID = 3490470371921661069L;
15 |
16 | @Override
17 | public void doGet(HttpServletRequest request, HttpServletResponse response)
18 | throws ServletException, IOException {
19 | PrintWriter out = response.getWriter();
20 | try {
21 | Thread.sleep(100);
22 | } catch (InterruptedException e) {
23 | e.printStackTrace();
24 | }
25 | out.println("OK");
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/src/main/java/services/ExceptionTest.java:
--------------------------------------------------------------------------------
1 | package services;
2 |
3 |
4 | /** Some simple time savers. Static methods. */
5 |
6 | public class ExceptionTest {
7 | static int k = 0;
8 |
9 | public static void testException() throws Exception {
10 | System.out.println(k++);
11 | test1();
12 | }
13 |
14 | public static void test1() throws Exception {
15 | try {
16 | test2();
17 | } catch (InterruptedException e) {
18 | e.printStackTrace();
19 | throw new Exception();
20 | }
21 | }
22 |
23 | public static void test2() throws InterruptedException {
24 | try {
25 | test3();
26 | } catch (ClassNotFoundException e) {
27 | e.printStackTrace();
28 | throw new InterruptedException();
29 | }
30 | }
31 |
32 | public static void test3() throws ClassNotFoundException {
33 | try {
34 | test4();
35 | } catch (NoSuchFieldException e) {
36 | e.printStackTrace();
37 | throw new ClassNotFoundException();
38 | }
39 | }
40 |
41 | public static void test4() throws NoSuchFieldException {
42 | throw new NoSuchFieldException();
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/src/main/java/forkjoin/prime/example/SequentialStreamPrimes.java:
--------------------------------------------------------------------------------
1 | package forkjoin.prime.example;
2 |
3 | import java.util.Set;
4 | import java.util.stream.Collectors;
5 | import java.util.stream.IntStream;
6 |
7 | public class SequentialStreamPrimes {
8 |
9 | public static boolean isPrime(int prime) {
10 | Set collect = IntStream
11 | .rangeClosed(2, (int) (Math.sqrt(prime))).parallel()
12 | .filter(j -> prime % j == 0).mapToObj(j -> Integer.valueOf(j))
13 | .collect(Collectors.toSet());
14 | System.out.println(collect.size());
15 | return false;
16 | }
17 |
18 | public static Set findPrimes(int maxPrimeTry) {
19 | return IntStream
20 | .rangeClosed(2, maxPrimeTry)
21 | .map(i -> IntStream.rangeClosed(2, (int) (Math.sqrt(i)))
22 | .filter(j -> i % j == 0).map(j -> 0).findAny()
23 | .orElse(i)).filter(i -> i != 0)
24 | .mapToObj(i -> Integer.valueOf(i)).collect(Collectors.toSet());
25 | }
26 |
27 | public static void main(String args[]) {
28 | long startTime = System.currentTimeMillis();
29 | isPrime(3000);
30 |
31 | long timeTaken = System.currentTimeMillis() - startTime;
32 | System.out.println("Time taken: " + timeTaken);
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/src/main/java/controllers/exception/ExpStackTrace.java:
--------------------------------------------------------------------------------
1 | package controllers.exception;
2 |
3 | import java.io.IOException;
4 | import java.io.PrintWriter;
5 |
6 | import javax.servlet.ServletException;
7 | import javax.servlet.annotation.WebServlet;
8 | import javax.servlet.http.HttpServlet;
9 | import javax.servlet.http.HttpServletRequest;
10 | import javax.servlet.http.HttpServletResponse;
11 |
12 | import services.ServletUtilities;
13 |
14 | @WebServlet("/exception/trade")
15 | public class ExpStackTrace extends HttpServlet {
16 | private static final long serialVersionUID = -2076640996856662490L;
17 |
18 | @Override
19 | public void doGet(HttpServletRequest request, HttpServletResponse response)
20 | throws ServletException, IOException {
21 | try {
22 | ServletUtilities.testException();
23 | } catch (Exception e) {
24 | e.printStackTrace();
25 | }
26 |
27 | response.setContentType("text/html");
28 | PrintWriter out = response.getWriter();
29 | out.println("\n" + "\n"
30 | + "A Test Servlet\n"
31 | + "\n" + "Test
\n"
32 | + "Simple servlet for testing.
\n" + "");
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/src/main/java/forkjoin/prime/PureForkJoin.java:
--------------------------------------------------------------------------------
1 | package forkjoin.prime;
2 |
3 | import java.math.BigInteger;
4 | import java.util.concurrent.RecursiveTask;
5 |
6 | import forkjoin.Utils;
7 |
8 | public class PureForkJoin extends RecursiveTask {
9 | private static final long serialVersionUID = -1883115459616562727L;
10 |
11 | private BigInteger from;
12 | private BigInteger to;
13 | boolean isPrime = true;
14 |
15 | public PureForkJoin(BigInteger start, BigInteger to) {
16 | this.from = start;
17 | this.to = to;
18 | }
19 |
20 | private boolean computeDirectly() {
21 | for (BigInteger i = this.from; i.compareTo(this.to) <= 0; i = i
22 | .add(BigInteger.ONE)) {
23 | if (TestPrime.primeNumber.mod(i).equals(BigInteger.ZERO)) {
24 | isPrime = false;
25 | }
26 | }
27 | return isPrime;
28 | }
29 |
30 | @Override
31 | protected Boolean compute() {
32 | BigInteger tmp = to.subtract(from);
33 | if (tmp.compareTo(TestPrime.lengthForThread) <= 0) {
34 | return computeDirectly();
35 | }
36 |
37 | BigInteger middle = to.add(from).divide(Utils.two);
38 | PureForkJoin leftJoin = new PureForkJoin(this.from, middle);
39 | PureForkJoin rightJoin = new PureForkJoin(middle.add(BigInteger.ONE),
40 | this.to);
41 |
42 | invokeAll(leftJoin, rightJoin);
43 |
44 | return leftJoin.isPrime && rightJoin.isPrime;
45 | }
46 | }
--------------------------------------------------------------------------------
/src/main/java/controllers/speed/Res1mbSpeed.java:
--------------------------------------------------------------------------------
1 | package controllers.speed;
2 |
3 | import java.io.BufferedInputStream;
4 | import java.io.IOException;
5 | import java.io.InputStream;
6 | import java.net.URL;
7 |
8 | import javax.servlet.ServletException;
9 | import javax.servlet.ServletOutputStream;
10 | import javax.servlet.annotation.WebServlet;
11 | import javax.servlet.http.HttpServlet;
12 | import javax.servlet.http.HttpServletRequest;
13 | import javax.servlet.http.HttpServletResponse;
14 |
15 | import org.apache.commons.io.IOUtils;
16 |
17 | @WebServlet(urlPatterns = "/speed/res1mb", loadOnStartup = 1)
18 | public class Res1mbSpeed extends HttpServlet {
19 | private static final long serialVersionUID = -5152808640821687260L;
20 |
21 | public byte[] retData;
22 |
23 | public void init() {
24 | InputStream in;
25 | try {
26 | in = new BufferedInputStream(new URL("http",
27 | "download.thinkbroadband.com", "/1MB.zip").openStream());
28 | retData = IOUtils.toByteArray(in);
29 | System.out.println("Get Data OK");
30 |
31 | } catch (Exception e) {
32 | e.printStackTrace();
33 | }
34 | }
35 |
36 | @Override
37 | public void doGet(HttpServletRequest request, HttpServletResponse response)
38 | throws ServletException, IOException {
39 | ServletOutputStream outputStream = response.getOutputStream();
40 | outputStream.write(retData);
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/src/main/java/forkjoin/file/ParallelStream.java:
--------------------------------------------------------------------------------
1 | package forkjoin.file;
2 |
3 | import java.util.Calendar;
4 | import java.util.List;
5 | import java.util.Map;
6 | import java.util.stream.LongStream;
7 |
8 | import forkjoin.Utils;
9 |
10 | public class ParallelStream {
11 | long countLineNumber;
12 |
13 | public ParallelStream(long countLineNumber) {
14 | this.countLineNumber = countLineNumber;
15 | }
16 |
17 | public void process() {
18 |
19 | LongStream.range(0, countLineNumber).parallel()
20 | .filter(i -> i % Utils.numberLinePerThread == 0)
21 | .mapToObj(from -> {
22 | long to = from + Utils.numberLinePerThread - 1;
23 | Map> result = null;
24 | try {
25 | Utils.processPart(from, to);
26 | } catch (Exception e) {
27 | e.printStackTrace();
28 | }
29 | return result;
30 |
31 | }).count();
32 |
33 | }
34 |
35 | public static void main(String args[]) throws InterruptedException {
36 | long countLineNumber = Utils.countLineNumber(Utils.fileLocation);
37 | ParallelStream mc = new ParallelStream(countLineNumber);
38 | System.out.println("line number " + countLineNumber);
39 | for (int i = 0; i < 5; i++) {
40 | long t1 = Calendar.getInstance().getTimeInMillis();
41 | mc.process();
42 | long t2 = Calendar.getInstance().getTimeInMillis();
43 | System.out.println(String.format("Time = %s ms ", (t2 - t1)));
44 | Thread.sleep(5000);
45 | }
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/src/main/java/forkjoin/file/SerializeThread.java:
--------------------------------------------------------------------------------
1 | package forkjoin.file;
2 |
3 | import java.io.FileInputStream;
4 | import java.util.HashMap;
5 | import java.util.List;
6 | import java.util.Map;
7 | import java.util.Scanner;
8 |
9 | import forkjoin.Utils;
10 |
11 | public class SerializeThread {
12 | static Map> result = new HashMap>();
13 |
14 | public static void main(String[] args) {
15 | try {
16 |
17 |
18 | } catch (Exception e) {
19 | e.printStackTrace();
20 | }
21 | }
22 |
23 | public void process() throws Exception {
24 | FileInputStream inputStream = null;
25 | Scanner sc = null;
26 | try {
27 | inputStream = new FileInputStream(Utils.fileLocation);
28 | sc = new Scanner(inputStream, "UTF-8");
29 | int line = 0;
30 | String s = "";
31 | String[] words = null;
32 | while (sc.hasNextLine()) {
33 | s = sc.nextLine();
34 | words = s.trim().split("\\s+");
35 | for (int i = 0; i < words.length; i++) {
36 | if (!Utils.isEmptyString(words[i])) {
37 | //if (result.get(words[i]) == null) {
38 | // result.put(words[i], new LinkedList());
39 | //}
40 | //result.get(words[i]).add(new WordIndex(line, i));
41 | }
42 | }
43 | line++;
44 | }
45 | if (sc.ioException() != null) {
46 | throw sc.ioException();
47 | }
48 | } finally {
49 | if (inputStream != null) {
50 | inputStream.close();
51 | }
52 | if (sc != null) {
53 | sc.close();
54 | }
55 | }
56 | }
57 |
58 | }
59 |
--------------------------------------------------------------------------------
/src/main/java/forkjoin/file/PureForkJoin.java:
--------------------------------------------------------------------------------
1 | package forkjoin.file;
2 |
3 | import java.util.Calendar;
4 | import java.util.concurrent.ForkJoinPool;
5 | import java.util.concurrent.RecursiveTask;
6 |
7 | import forkjoin.Utils;
8 |
9 | public class PureForkJoin extends RecursiveTask {
10 | private static final long serialVersionUID = 7926312633273843668L;
11 |
12 | private long from;
13 | private long to;
14 |
15 | public PureForkJoin(long from, long to) {
16 | this.from = from;
17 | this.to = to;
18 | }
19 |
20 | @Override
21 | protected V compute() {
22 | try {
23 | if (to - from <= Utils.numberLinePerThread) {
24 | Utils.processPart(from, to);
25 | return null;
26 | }
27 | long middle = (to + from) / 2;
28 |
29 | PureForkJoin