Fix test case

Project: http://git-wip-us.apache.org/repos/asf/chukwa/repo
Commit: http://git-wip-us.apache.org/repos/asf/chukwa/commit/322119d9
Tree: http://git-wip-us.apache.org/repos/asf/chukwa/tree/322119d9
Diff: http://git-wip-us.apache.org/repos/asf/chukwa/diff/322119d9

Branch: refs/heads/master
Commit: 322119d9884ebf49678c0ed307e946c8c05ebc9e
Parents: 4223cbe
Author: fangyiwang <[email protected]>
Authored: Mon May 22 13:25:40 2017 -0700
Committer: fangyiwang <[email protected]>
Committed: Mon May 22 13:25:40 2017 -0700

----------------------------------------------------------------------
 .../hadoop/chukwa/caffe/MetricsCollector.java   | 18 +++++---
 .../chukwa/caffe/TestMemoryUsageDetection.java  | 47 +++++++++++++-------
 2 files changed, 43 insertions(+), 22 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/chukwa/blob/322119d9/src/test/java/org/apache/hadoop/chukwa/caffe/MetricsCollector.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/hadoop/chukwa/caffe/MetricsCollector.java 
b/src/test/java/org/apache/hadoop/chukwa/caffe/MetricsCollector.java
index f264980..bc93055 100644
--- a/src/test/java/org/apache/hadoop/chukwa/caffe/MetricsCollector.java
+++ b/src/test/java/org/apache/hadoop/chukwa/caffe/MetricsCollector.java
@@ -19,18 +19,20 @@ import org.json.simple.JSONObject;
 public class MetricsCollector
 {
   private Timer getMetricSnapshotTimer = null;
-  private long intervalInMin;
+  private long intervalInMilli;
   private String hostname;
+  private String dirName;
   
-  public MetricsCollector (long intervalInMin, String hostname) {
-    this.intervalInMin = intervalInMin;
+  public MetricsCollector (long intervalInMilli, String hostname, String 
dirName) {
+    this.intervalInMilli = intervalInMilli;
     this.hostname = hostname;
+    this.dirName = dirName;
     getMetricSnapshotTimer = new Timer ("GetMetricSnapshot", true);
   }
   
   public void start () {
     if (getMetricSnapshotTimer != null)
-      getMetricSnapshotTimer.schedule (new GetMetricSnapshotTimerTask 
(hostname, intervalInMin), 0, intervalInMin);    
+      getMetricSnapshotTimer.schedule (new GetMetricSnapshotTimerTask 
(hostname, intervalInMilli, dirName), 0, intervalInMilli);    
   }
   
   public void cancel ()
@@ -44,16 +46,18 @@ public class MetricsCollector
     private String hostname = null;
     private BufferedWriter bufferedWriter = null;
     private long intervalInMilli;
+    private String dirName;
 
     /**
      * Normalize the timestamp in time series data to use seconds
      */
     private final static int XSCALE = 1000;
 
-    GetMetricSnapshotTimerTask (String hostname, long intervalInMin)
+    GetMetricSnapshotTimerTask (String hostname, long intervalInMilli, String 
dirName)
     {
       this.hostname = hostname;
-      this.intervalInMilli = intervalInMin * 60 * 1000;
+      this.intervalInMilli = intervalInMilli;
+      this.dirName = dirName;
     }
 
     public void run () 
@@ -102,7 +106,7 @@ public class MetricsCollector
 
     private void generateCsv (List list, String name, long startTime, 
BufferedWriter bufferedWriter) throws Exception
     {
-      String fileName = name + "_" + startTime;
+      String fileName = dirName + "/" + name + "_" + startTime;
       PrintWriter writer = new PrintWriter(fileName + ".csv", "UTF-8");
       int size = list.size ();
       for (int i = 0; i < size; i++) {

http://git-wip-us.apache.org/repos/asf/chukwa/blob/322119d9/src/test/java/org/apache/hadoop/chukwa/caffe/TestMemoryUsageDetection.java
----------------------------------------------------------------------
diff --git 
a/src/test/java/org/apache/hadoop/chukwa/caffe/TestMemoryUsageDetection.java 
b/src/test/java/org/apache/hadoop/chukwa/caffe/TestMemoryUsageDetection.java
index ac4ae59..742878b 100644
--- a/src/test/java/org/apache/hadoop/chukwa/caffe/TestMemoryUsageDetection.java
+++ b/src/test/java/org/apache/hadoop/chukwa/caffe/TestMemoryUsageDetection.java
@@ -21,11 +21,15 @@ import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.net.InetAddress;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
 
 import junit.framework.TestCase;
 
 import org.apache.hadoop.chukwa.util.ExceptionUtil;
 
+
 /**
  * (1) Run non-stop terasort and teragen  
  * (2) Collect memory usage metrics from hbase every 5 minutes for 10 hours 
and write to csv files in /caffe-test/train/data
@@ -40,14 +44,28 @@ public class TestMemoryUsageDetection extends TestCase {
   /**
    * Run non-stop terasort and teragen to force memory leak
    */
-  public void setUp() {
-    new Thread(new Runnable() {
+  public void setUp() {}
+
+  public void tearDown() {}
+
+  public void testMemoryDetection () {
+    String dirName = "/caffe-test/train/data";
+    Thread teraSortThread = createTeraSortThread ();
+    ExecutorService executor = Executors.newFixedThreadPool(1);
+    Future<?> task = executor.submit(teraSortThread);
+    collectNodeManagerMetrics (dirName);
+    task.cancel (true);
+    executor.shutdown ();
+    caffeTrain (dirName);
+  }
+  
+  private Thread createTeraSortThread () {
+    Thread teraSortThread = new Thread(new Runnable() {
       public void run(){
         try {
           String target = new String("/caffe-test/tera/tera.sh");
           Runtime rt = Runtime.getRuntime();
           Process proc = rt.exec(target);
-          proc.waitFor();
           BufferedReader reader = new BufferedReader(new 
InputStreamReader(proc.getInputStream()));
           String line = "";                       
           while ((line = reader.readLine())!= null) {
@@ -57,19 +75,18 @@ public class TestMemoryUsageDetection extends TestCase {
           fail(ExceptionUtil.getStackTrace(e));
         }
       }
-    }).start();
+    });
+    return teraSortThread;
   }
-
-  public void tearDown() {
-  }
-
+  
+  
   /**
    * Collect memory usage data every 15 min.
    * Stop the timer after 10 hours
    */
-  public void testCollectNodeManagerMetrics() {
-    int intervalInMin = 15;
-    long timerElapseTime = 10 * 60 * 60 * 1000;
+  private void collectNodeManagerMetrics(String dirName) {
+    int intervalInMilli = 15 * 60 * 1000;
+    long timerDurationTime = 10 * 60 * 60 * 1000;
     String hostname = "";
     try {
       hostname = InetAddress.getLocalHost().getHostName();
@@ -77,10 +94,10 @@ public class TestMemoryUsageDetection extends TestCase {
     } catch (IOException e) {
       fail(ExceptionUtil.getStackTrace(e));
     }
-    MetricsCollector collector = new MetricsCollector (intervalInMin, 
hostname);
+    MetricsCollector collector = new MetricsCollector (intervalInMilli, 
hostname, dirName);
     collector.start ();
     try {
-      Thread.sleep (timerElapseTime);
+      Thread.sleep (timerDurationTime);
     } catch (InterruptedException e) {
     }
     collector.cancel ();
@@ -97,9 +114,9 @@ public class TestMemoryUsageDetection extends TestCase {
   /**
    * Train the images
    */
-  public void testCaffeTrain () {
+  private void caffeTrain (String dirName) {
     try {
-      String target = new String("/caffe-test/train/train.sh");
+      String target = new String(dirName);
       Runtime rt = Runtime.getRuntime();
       Process proc = rt.exec(target);
       proc.waitFor();

Reply via email to