package water.fvec;

import org.junit.BeforeClass;
import org.junit.Test;
import water.TestUtil;
import water.util.ArrayUtils;
import water.util.Log;
import water.util.PrettyPrint;

public class ChunkSpeedTest extends TestUtil {
  @BeforeClass() public static void setup() { stall_till_cloudsize(1); }

  @Test
  public void run() {
    final int cols = 100;
    final int rows = 100000;
    final int rep = 10;
    double[][] raw = new double[cols][rows];
    for (int j=0; j<cols; ++j) {
      for (int i = 0; i < rows; ++i) {
//        switch (j%1) { //just do 1 byte chunks
//        switch (j%2) { //just do 1/2 byte chunks
        switch (j%3) { // do all 3 chunk types
          case 0:
            raw[j][i] = i % 200; //C1NChunk - 1 byte integer
            break;
          case 1:
            raw[j][i] = i % 500; //C2Chunk - 2 byte integer
            break;
          case 2:
            raw[j][i] = i == 17 ? 1 : 0; //CX0Chunk - sparse
            break;
        }
      }
    }
    Chunk[] chunks = new Chunk[cols];
    for (int j=0; j<cols; ++j) {
      chunks[j] = new NewChunk(raw[j]).compress();
      Log.info("Column " + j + " compressed into: " + chunks[j].getClass().toString());
    }

    // raw data
    {
      long start = 0;
      double sum = 0;
      for (int r = 0; r < rep; ++r) {
        if (r==rep/10)
          start = System.currentTimeMillis();
        for (int j=0; j<cols; ++j) {
          for (int i = 0; i < rows; ++i) {
            sum += raw[j][i];
          }
        }
      }
      long done = System.currentTimeMillis();
      Log.info("Sum: " + sum);
      Log.info("Data size: " + PrettyPrint.bytes(rows * cols * 8));
      Log.info("Time to access raw double[]: " + PrettyPrint.msecs(done - start, true));
      Log.info("");
    }

    // chunks
    {
      long start = 0;
      double sum = 0;
      for (int r = 0; r < rep; ++r) {
        if (r==rep/10)
          start = System.currentTimeMillis();
        for (int j=0; j<cols; ++j) {
          for (int i = 0; i < rows; ++i) {
            sum += chunks[j].atd(i);
          }
        }
      }
      long done = System.currentTimeMillis();
      Log.info("Sum: " + sum);
      long siz = 0;
      for (int j=0; j<cols; ++j) {
        siz += chunks[j].byteSize();
      }
      Log.info("Data size: " + PrettyPrint.bytes(siz));
      Log.info("Time to access via atd(): " + PrettyPrint.msecs(done - start, true));
      Log.info("");
    }

  }

}


@@ -571,18 +571,16 @@
  chunks%0A
-//
         
@@ -612,32 +612,34 @@
 1/2 byte chunks%0A
+//
         switch (
@@ -2441,16 +2441,783 @@
 %0A    %7D%0A%0A
+    // bulk chunks%0A    %7B%0A      long start = 0;%0A      double sum = 0;%0A      double%5B%5D bulk = new double%5Brows%5D; //allocate only once%0A      for (int r = 0; r %3C rep; ++r) %7B%0A        if (r==rep/10)%0A          start = System.currentTimeMillis();%0A        for (int j=0; j%3Ccols; ++j) %7B%0A          chunks%5Bj%5D.toDoubleArray(bulk);%0A          for (int i = 0; i %3C rows; ++i) %7B%0A            sum += bulk%5Bi%5D;%0A          %7D%0A        %7D%0A      %7D%0A      long done = System.currentTimeMillis();%0A      Log.info(%22Sum: %22 + sum);%0A      long siz = 0;%0A      for (int j=0; j%3Ccols; ++j) %7B%0A        siz += chunks%5Bj%5D.byteSize();%0A      %7D%0A      Log.info(%22Data size: %22 + PrettyPrint.bytes(siz));%0A      Log.info(%22Time to access via bulk reader: %22 + PrettyPrint.msecs(done - start, true));%0A      Log.info(%22%22);%0A    %7D%0A
   %7D%0A%0A%7D%0A%0A

Add benchmark of new bulk reader.