package com.rush.rainyhills.performance;

import com.rush.rainyhills.TestItemUtil;
import com.rush.rainyhills.core.LoopCalculator;
import com.rush.rainyhills.core.RecursionCalculator;
import com.rush.rainyhills.middleware.WaterCalculator;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

import java.util.*;

/**
 * Created by Ruslan Khalikov
 * Date: 17.06.2016
 * Time: 19:48
 */
public class VolumeCalculatorPerformanceTest {
    private TestItemUtil.TestItem[] items;
    private WaterCalculator waterCalculator;
    private int itemCount = 1000;

    @Before
    public void init() {
        Random random = new Random();
        itemCount = itemCount * 2;
        int[] hills = new int[itemCount];
        for (int i = 0; i < itemCount; i++) {
            hills[i] = Math.abs(random.nextInt());
        }
        TestItemUtil.TestItem item = new TestItemUtil.TestItem(hills, hills, 0);

        List<TestItemUtil.TestItem> itemList = new ArrayList<>();
        for (int i = 0; i < 5000; i++) {
            itemList.add(item);
        }
        items = new TestItemUtil.TestItem[itemList.size()];
        itemList.toArray(items);

        waterCalculator = new WaterCalculator();
    }

    @Ignore
    @Test
    public void loopPerformanceTest() {
        waterCalculator.setVolumeCalculator(new LoopCalculator());
        long startTime = System.currentTimeMillis();
        Arrays.stream(items).forEach(item -> waterCalculator.calculate(item.getHills()));
        System.out.println("Loop calculator stream for " + items[0].getHills().length
                + " item(s) -> " + (System.currentTimeMillis() - startTime) + " ms");
    }

    @Ignore
    @Test
    public void recursionPerformanceTest() {
        waterCalculator.setVolumeCalculator(new RecursionCalculator());
        long startTime = System.currentTimeMillis();
        Arrays.stream(items).forEach(item -> waterCalculator.calculate(item.getHills()));
        System.out.println("Recursion calculator stream for " + items[0].getHills().length
                + " item(s) -> " + (System.currentTimeMillis() - startTime) + " ms");
    }

    @Test
    public void fullPerformanceTest() {
        System.out.println("Preparing...");
        for (int i = 0; i < 10; i++) {
            init();
            loopPerformanceTest();
            recursionPerformanceTest();
        }
        System.out.println("Done!");
    }
}

@@ -447,16 +447,61 @@
 eTest %7B%0A
+    private WaterCalculator waterCalculator;%0A
     priv
@@ -529,16 +529,13 @@
 Item
-%5B%5D
  item
-s
 ;%0A  
@@ -548,39 +548,29 @@
 ate 
-WaterCalculator waterCalculator
+int hillCount = 10000
 ;%0A  
@@ -587,23 +587,20 @@
 int 
-itemCou
+maxI
 nt = 100
 0;%0A%0A
@@ -595,17 +595,16 @@
 nt = 100
-0
 ;%0A%0A    @
@@ -673,24 +673,25 @@
 ndom();%0A
+%0A
         
 itemCoun
@@ -686,20 +686,20 @@
     
-item
+hill
 Count = 
 item
@@ -694,20 +694,20 @@
 Count = 
-item
+hill
 Count * 
@@ -739,20 +739,20 @@
 new int%5B
-item
+hill
 Count%5D;%0A
@@ -772,31 +772,34 @@
 int 
-i
+n
  = 0; 
-i %3C itemCount; i
+n %3C hills.length; n
 ++) 
@@ -822,22 +822,13 @@
 lls%5B
-i
+n
 %5D = 
-Math.abs(
 rand
@@ -838,17 +838,22 @@
 nextInt(
-)
+maxInt
 );%0A     
@@ -857,24 +857,25 @@
       %7D%0A
+%0A
         
 TestItem
@@ -866,38 +866,16 @@
         
-TestItemUtil.TestItem 
 item = n
@@ -922,251 +922,8 @@
 );%0A%0A
-        List%3CTestItemUtil.TestItem%3E itemList = new ArrayList%3C%3E();%0A        for (int i = 0; i %3C 5000; i++) %7B%0A            itemList.add(item);%0A        %7D%0A        items = new TestItemUtil.TestItem%5BitemList.size()%5D;%0A        itemList.toArray(items);%0A%0A
     
@@ -1167,45 +1167,8 @@
     
- Arrays.stream(items).forEach(item -%3E
  wat
@@ -1198,33 +1198,32 @@
 item.getHills())
-)
 ;%0A        System
@@ -1263,36 +1263,32 @@
 eam for %22 + item
-s%5B0%5D
 .getHills().leng
@@ -1586,45 +1586,8 @@
     
- Arrays.stream(items).forEach(item -%3E
  wat
@@ -1625,17 +1625,16 @@
 Hills())
-)
 ;%0A      
@@ -1699,12 +1699,8 @@
 item
-s%5B0%5D
 .get

fix performance test