@Test public void testMetricsOnFileRename() throws Exception { long base = getBaseWebResponses(); Path originalPath = new Path("/metricsTest_RenameStart"); Path destinationPath = new Path("/metricsTest_RenameFinal"); // Create an empty file assertEquals(0, AzureMetricsTestUtil.getLongCounterValue(getInstrumentation(), WASB_FILES_CREATED)); assertTrue(getFileSystem().createNewFile(originalPath)); logOpResponseCount("Creating an empty file", base); base = assertWebResponsesInRange(base, 2, 20); assertEquals(1, AzureMetricsTestUtil.getLongCounterValue(getInstrumentation(), WASB_FILES_CREATED)); // Rename the file assertTrue( ((FileSystem) getFileSystem()).rename(originalPath, destinationPath)); // Varies: at the time of writing this code it takes 7 requests/responses. logOpResponseCount("Renaming a file", base); base = assertWebResponsesInRange(base, 2, 15); assertNoErrors(); }
/** * Gets (and asserts) the value of the wasb_web_responses counter just * after the creation of the file system object. */ private long getBaseWebResponses() { // The number of requests should start at 0 return assertWebResponsesEquals(0, 0); }
/** * Checks that the wasb_web_responses counter is in the given range. * @param base The base value (before the operation of interest). * @param inclusiveLowerLimit The lower limit for what it should increase by. * @param inclusiveUpperLimit The upper limit for what it should increase by. * @return The new base value now. */ private long assertWebResponsesInRange(long base, long inclusiveLowerLimit, long inclusiveUpperLimit) { long currentResponses = getCurrentWebResponses(); long justOperation = currentResponses - base; assertTrue(String.format( "Web responses expected in range [%d, %d], but was %d.", inclusiveLowerLimit, inclusiveUpperLimit, justOperation), justOperation >= inclusiveLowerLimit && justOperation <= inclusiveUpperLimit); return currentResponses; }
private void assertNoErrors() { assertEquals(0, AzureMetricsTestUtil.getLongCounterValue(getInstrumentation(), WASB_CLIENT_ERRORS)); assertEquals(0, AzureMetricsTestUtil.getLongCounterValue(getInstrumentation(), WASB_SERVER_ERRORS)); }
@Test public void testMetricsOnMkdirList() throws Exception { long base = getBaseWebResponses(); // Create a directory assertTrue(fs.mkdirs(new Path("a"))); // At the time of writing // getAncestor uses 2 calls for each folder level /user/<name>/a // plus 1 call made by checkContainer // mkdir checks the hierarchy with 2 calls per level // mkdirs calls storeEmptyDir to create the empty folder, which makes 5 calls // For a total of 7 + 6 + 5 = 18 web responses base = assertWebResponsesInRange(base, 1, 18); assertEquals(1, AzureMetricsTestUtil.getLongCounterValue(getInstrumentation(), WASB_DIRECTORIES_CREATED)); // List the root contents assertEquals(1, getFileSystem().listStatus(new Path("/")).length); base = assertWebResponsesEquals(base, 1); assertNoErrors(); }
@Test public void testMetricsOnDirRename() throws Exception { long base = getBaseWebResponses(); Path originalDirName = new Path("/metricsTestDirectory_RenameStart"); Path innerFileName = new Path(originalDirName, "innerFile"); Path destDirName = new Path("/metricsTestDirectory_RenameFinal"); // Create an empty directory assertTrue(getFileSystem().mkdirs(originalDirName)); base = getCurrentWebResponses(); // Create an inner file assertTrue(getFileSystem().createNewFile(innerFileName)); base = getCurrentWebResponses(); // Rename the directory assertTrue(getFileSystem().rename(originalDirName, destDirName)); // At the time of writing this code it takes 11 requests/responses // to rename the directory with one file. Check for range 1-20 for now. logOpResponseCount("Renaming a directory", base); base = assertWebResponsesInRange(base, 1, 20); assertNoErrors(); }
@Test public void testClientErrorMetrics() throws Exception { String fileName = "metricsTestFile_ClientError"; Path filePath = new Path("/"+fileName); final int FILE_SIZE = 100; OutputStream outputStream = null; String leaseID = null; try { // Create a file outputStream = getFileSystem().create(filePath); leaseID = getTestAccount().acquireShortLease(fileName); try { outputStream.write(new byte[FILE_SIZE]); outputStream.close(); assertTrue("Should've thrown", false); } catch (AzureException ex) { assertTrue("Unexpected exception: " + ex, ex.getMessage().contains("lease")); } assertEquals(1, AzureMetricsTestUtil.getLongCounterValue(getInstrumentation(), WASB_CLIENT_ERRORS)); assertEquals(0, AzureMetricsTestUtil.getLongCounterValue(getInstrumentation(), WASB_SERVER_ERRORS)); } finally { if(leaseID != null){ getTestAccount().releaseLease(leaseID, fileName); } IOUtils.closeStream(outputStream); } }
private void logOpResponseCount(String opName, long base) { LOG.info("{} took {} web responses to complete.", opName, getCurrentWebResponses() - base); }
/** * Recursive discovery of path depth * @param path path to measure. * @return depth, where "/" == 0. */ int depth(Path path) { if (path.isRoot()) { return 0; } else { return 1 + depth(path.getParent()); } }
@Test public void testMetricsOnFileExistsDelete() throws Exception { long base = getBaseWebResponses(); Path filePath = new Path("/metricsTest_delete"); // Check existence assertFalse(getFileSystem().exists(filePath)); // At the time of writing this code it takes 2 requests/responses to // check existence, which seems excessive, plus initial request for // container check, plus 2 ancestor checks only in the secure case. logOpResponseCount("Checking file existence for non-existent file", base); base = assertWebResponsesInRange(base, 1, 5); // Create an empty file assertTrue(getFileSystem().createNewFile(filePath)); base = getCurrentWebResponses(); // Check existence again assertTrue(getFileSystem().exists(filePath)); logOpResponseCount("Checking file existence for existent file", base); base = assertWebResponsesInRange(base, 1, 4); // Delete the file assertEquals(0, AzureMetricsTestUtil.getLongCounterValue(getInstrumentation(), WASB_FILES_DELETED)); assertTrue(getFileSystem().delete(filePath, false)); // At the time of writing this code it takes 4 requests/responses to // delete, which seems excessive. Check for range 1-4 for now. logOpResponseCount("Deleting a file", base); base = assertWebResponsesInRange(base, 1, 4); assertEquals(1, AzureMetricsTestUtil.getLongCounterValue(getInstrumentation(), WASB_FILES_DELETED)); assertNoErrors(); }
@Test public void testMetricsOnBigFileCreateRead() throws Exception { long base = getBaseWebResponses(); assertEquals(0, AzureMetricsTestUtil.getCurrentBytesWritten(getInstrumentation())); getBandwidthGaugeUpdater().suppressAutoUpdate(); OutputStream outputStream = getFileSystem().create(filePath); outputStream.write(new byte[FILE_SIZE]); outputStream.close(); logOpResponseCount("Creating a 100 MB file", base); base = assertWebResponsesInRange(base, 20, 50); getBandwidthGaugeUpdater().triggerUpdate(true); long totalBytesWritten = AzureMetricsTestUtil.getCurrentTotalBytesWritten(getInstrumentation()); assertTrue("The total bytes written " + totalBytesWritten + " is pretty far from the expected range of around " + FILE_SIZE + " bytes plus a little overhead.", totalBytesWritten >= FILE_SIZE && totalBytesWritten < (FILE_SIZE * 2)); long uploadRate = AzureMetricsTestUtil.getLongGaugeValue(getInstrumentation(), WASB_UPLOAD_RATE); LOG.info("Upload rate: " + uploadRate + " bytes/second."); long uploadLatency = AzureMetricsTestUtil.getLongGaugeValue(getInstrumentation(), WASB_UPLOAD_LATENCY); LOG.info("Upload latency: " + uploadLatency); assertTrue("The upload latency " + uploadLatency + " should be greater than zero now that I've just uploaded a file.", uploadLatency > 0); InputStream inputStream = getFileSystem().open(filePath);
@Test public void testMetricsOnFileCreateRead() throws Exception { long base = getBaseWebResponses(); assertEquals(0, AzureMetricsTestUtil.getCurrentBytesWritten(getInstrumentation())); getBandwidthGaugeUpdater().suppressAutoUpdate(); OutputStream outputStream = getFileSystem().create(filePath); outputStream.write(nonZeroByteArray(FILE_SIZE)); outputStream.close(); long uploadDurationMs = new Date().getTime() - start.getTime(); logOpResponseCount("Creating a 1K file", base); base = assertWebResponsesInRange(base, 2, 15); getBandwidthGaugeUpdater().triggerUpdate(true); long bytesWritten = AzureMetricsTestUtil.getCurrentBytesWritten(getInstrumentation()); assertTrue("The bytes written in the last second " + bytesWritten + " is pretty far from the expected range of around " + FILE_SIZE + " bytes plus a little overhead.", bytesWritten > (FILE_SIZE / 2) && bytesWritten < (FILE_SIZE * 2)); long totalBytesWritten = AzureMetricsTestUtil.getCurrentTotalBytesWritten(getInstrumentation()); assertTrue("The total bytes written " + totalBytesWritten + " is pretty far from the expected range of around " + FILE_SIZE + " bytes plus a little overhead.", totalBytesWritten >= FILE_SIZE && totalBytesWritten < (FILE_SIZE * 2)); long uploadRate = AzureMetricsTestUtil.getLongGaugeValue(getInstrumentation(), WASB_UPLOAD_RATE); LOG.info("Upload rate: " + uploadRate + " bytes/second."); long expectedRate = (FILE_SIZE * 1000L) / uploadDurationMs; assertTrue("The upload rate " + uploadRate +