package org.apache.helix.integration;

import java.util.Date;

import org.apache.helix.ExternalCommand;
import org.apache.helix.ScriptTestHelper;
import org.apache.helix.TestHelper;
import org.testng.Assert;
import org.testng.annotations.Test;


public class TestFailOverPerf1kp
{
  @Test
  public void testFailOverPerf1kp() throws Exception
  {
    // Logger.getRootLogger().setLevel(Level.INFO);

    String className = TestHelper.getTestClassName();
    String methodName = TestHelper.getTestMethodName();
    String testName = className + "_" + methodName;
    
    System.out.println("START " + testName + " at "
        + new Date(System.currentTimeMillis()));

    ExternalCommand cmd = ScriptTestHelper.runCommandLineTest("helix_random_kill_local_startzk.sh");
    String output = cmd.getStringOutput("UTF8");
    int i = getStateTransitionLatency(0, output);
    int j = output.indexOf("ms", i);
    long latency = Long.parseLong(output.substring(i, j));
    System.out.println("startup latency: " + latency);
    
    i = getStateTransitionLatency(i, output);
    j = output.indexOf("ms", i);
    latency = Long.parseLong(output.substring(i, j));
    System.out.println("failover latency: " + latency);
    Assert.assertTrue(latency < 800, "failover latency for 1k partition test should < 800ms");
    
    System.out.println("END " + testName + " at "
        + new Date(System.currentTimeMillis()));

  }
  
  int getStateTransitionLatency(int start, String output)
  {
    final String pattern = "state transition latency: ";
    int i = output.indexOf(pattern, start) + pattern.length();
//    String latencyStr = output.substring(i, j);
//    System.out.println(latencyStr);
    return i;
  }
}

@@ -275,16 +275,121 @@
 f1kp%0A%7B%0A 
+ // TODO: renable this test. disable it because the script is not running properly on apache jenkins%0A  //
  @Test%0A 

disable script test because the kill command is not running properly on apache jenkins