Search in sources :

Example 21 with RuntimeMetrics

use of co.cask.cdap.api.metrics.RuntimeMetrics in project cdap by caskdata.

the class SparkKMeansAppTest method test.

@Test
public void test() throws Exception {
    // Deploy the Application
    ApplicationManager appManager = deployApplication(SparkKMeansApp.class);
    // Start the Flow
    FlowManager flowManager = appManager.getFlowManager("PointsFlow").start();
    // Send a few points to the stream
    StreamManager streamManager = getStreamManager("pointsStream");
    // one cluster around (0, 500, 0) and another around (100, 0, 0)
    for (int i = 0; i < 100; i++) {
        double diff = Math.random() / 100;
        streamManager.send(String.format("%f %f %f", diff, 500 + diff, diff));
        streamManager.send(String.format("%f %f %f", 100 + diff, diff, diff));
    }
    //  Wait for the events to be processed, or at most 5 seconds
    RuntimeMetrics metrics = flowManager.getFlowletMetrics("reader");
    metrics.waitForProcessed(200, 10, TimeUnit.SECONDS);
    // Start a Spark Program
    SparkManager sparkManager = appManager.getSparkManager("SparkKMeansProgram").start();
    sparkManager.waitForRun(ProgramRunStatus.COMPLETED, 60, TimeUnit.SECONDS);
    flowManager.stop();
    // Start CentersService
    ServiceManager serviceManager = appManager.getServiceManager(SparkKMeansApp.CentersService.SERVICE_NAME).start();
    // Wait service startup
    serviceManager.waitForStatus(true);
    // Request data and verify it
    String response = requestService(new URL(serviceManager.getServiceURL(15, TimeUnit.SECONDS), "centers/0"));
    String[] coordinates = response.split(",");
    int x0 = Double.valueOf(coordinates[0]).intValue();
    int y0 = Double.valueOf(coordinates[1]).intValue();
    int z0 = Double.valueOf(coordinates[2]).intValue();
    response = requestService(new URL(serviceManager.getServiceURL(15, TimeUnit.SECONDS), "centers/1"));
    coordinates = response.split(",");
    int x1 = Double.valueOf(coordinates[0]).intValue();
    int y1 = Double.valueOf(coordinates[1]).intValue();
    int z1 = Double.valueOf(coordinates[2]).intValue();
    // one cluster should be around (0, 500, 0) and the other around (100, 0, 0)
    if (x0 == 100) {
        Assert.assertEquals(0, y0);
        Assert.assertEquals(0, z0);
        Assert.assertEquals(0, x1);
        Assert.assertEquals(500, y1);
        Assert.assertEquals(0, z1);
    } else {
        Assert.assertEquals(0, x0);
        Assert.assertEquals(500, y0);
        Assert.assertEquals(0, z0);
        Assert.assertEquals(100, x1);
        Assert.assertEquals(0, y1);
        Assert.assertEquals(0, z1);
    }
    // Request data by incorrect index and verify response
    URL url = new URL(serviceManager.getServiceURL(15, TimeUnit.SECONDS), "centers/10");
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    try {
        Assert.assertEquals(HttpURLConnection.HTTP_NO_CONTENT, conn.getResponseCode());
    } finally {
        conn.disconnect();
    }
}
Also used : FlowManager(co.cask.cdap.test.FlowManager) ApplicationManager(co.cask.cdap.test.ApplicationManager) SparkManager(co.cask.cdap.test.SparkManager) HttpURLConnection(java.net.HttpURLConnection) StreamManager(co.cask.cdap.test.StreamManager) RuntimeMetrics(co.cask.cdap.api.metrics.RuntimeMetrics) ServiceManager(co.cask.cdap.test.ServiceManager) URL(java.net.URL) Test(org.junit.Test)

Aggregations

RuntimeMetrics (co.cask.cdap.api.metrics.RuntimeMetrics)21 ApplicationManager (co.cask.cdap.test.ApplicationManager)17 FlowManager (co.cask.cdap.test.FlowManager)17 Test (org.junit.Test)16 ServiceManager (co.cask.cdap.test.ServiceManager)12 StreamManager (co.cask.cdap.test.StreamManager)12 URL (java.net.URL)10 KeyValueTable (co.cask.cdap.api.dataset.lib.KeyValueTable)6 Gson (com.google.gson.Gson)6 HttpURLConnection (java.net.HttpURLConnection)6 Category (org.junit.experimental.categories.Category)5 Get (co.cask.cdap.api.dataset.table.Get)2 Table (co.cask.cdap.api.dataset.table.Table)2 MapReduceManager (co.cask.cdap.test.MapReduceManager)2 SparkManager (co.cask.cdap.test.SparkManager)2 HttpRequest (co.cask.common.http.HttpRequest)2 HttpResponse (co.cask.common.http.HttpResponse)2 Map (java.util.Map)2 KeyValue (co.cask.cdap.api.dataset.lib.KeyValue)1 Row (co.cask.cdap.api.dataset.table.Row)1