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.core.VolumeCalculator;
import org.junit.BeforeClass;
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 static TestItemUtil.TestItem[] items;
    private static VolumeCalculator volumeCalculator;

    @BeforeClass
    public static void init() {
        Random random = new Random();
        int length = 1000000;
        int[] hills = new int[length];
        for (int i = 0; i < length; 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);

        volumeCalculator = new VolumeCalculator();
    }

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

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

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

@@ -250,21 +250,16 @@
 t.Before
-Class
 ;%0Aimport
@@ -454,23 +454,16 @@
 private 
-static 
 TestItem
@@ -497,23 +497,16 @@
 private 
-static 
 VolumeCa
@@ -531,16 +531,50 @@
 culator;
+%0A    private int itemCount = 1000;
 %0A%0A    @B
@@ -582,13 +582,8 @@
 fore
-Class
 %0A   
@@ -594,15 +594,8 @@
 lic 
-static 
 void
@@ -655,27 +655,32 @@
    i
-nt length = 1000000
+temCount = itemCount * 2
 ;%0A  
@@ -707,22 +707,25 @@
 new int%5B
-length
+itemCount
 %5D;%0A     
@@ -747,22 +747,25 @@
  0; i %3C 
-length
+itemCount
 ; i++) %7B
@@ -1544,39 +1544,74 @@
 am for %22 + items
-.length
+%5B0%5D.getEmptyHills().length%0A               
  + %22 item(s) -%3E 
@@ -2030,23 +2030,58 @@
  + items
-.length
+%5B0%5D.getEmptyHills().length%0A               
  + %22 ite
@@ -2275,32 +2275,52 @@
  i %3C 10; i++) %7B%0A
+            init();%0A
             loop

upgrade performance test