Skip to content

Commit

Permalink
Add tests to check for writer file only if metrics are present
Browse files Browse the repository at this point in the history
Signed-off-by: Sruti Parthiban <partsrut@amazon.com>
  • Loading branch information
sruti1312 committed Jul 10, 2021
1 parent 27585ce commit f804bbb
Showing 1 changed file with 114 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,18 @@

package org.opensearch.performanceanalyzer.reader_writer_shared;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;

import java.io.File;
import java.io.FileNotFoundException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Scanner;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
Expand All @@ -44,18 +52,94 @@ public class EventLogFileHandlerTests {
@Mock private PerformanceAnalyzerController mockController;
@Mock private PerformanceAnalyzerConfigAction configAction;

String pathToTestMetricsDir;
private final String pathToTestMetricsDir = "/tmp/testMetrics/";
private final EventLog eventLog = new EventLog();
private final EventLogFileHandler eventLogFileHandler =
new EventLogFileHandler(eventLog, pathToTestMetricsDir);
private EventLogQueueProcessor queuePurgerAndPersistor;

@Before
public void init() {
initMocks(this);
pathToTestMetricsDir = "/tmp/testMetrics/";
deleteDirectory(new File(pathToTestMetricsDir));
boolean newDir = new File(pathToTestMetricsDir).mkdir();
when(mockController.isPerformanceAnalyzerEnabled()).thenReturn(true);
PerformanceAnalyzerConfigAction.setInstance(configAction);

queuePurgerAndPersistor =
new EventLogQueueProcessor(
eventLogFileHandler,
MetricsConfiguration.SAMPLING_INTERVAL,
MetricsConfiguration.SAMPLING_INTERVAL,
mockController);
System.setProperty("performanceanalyzer.metrics.log.enabled", "False");
}

@After
public void cleanup() {
deleteDirectory(new File(pathToTestMetricsDir));
}

@Test
public void testFileWithMetrics() throws InterruptedException {
generateWriterFile(5);
queuePurgerAndPersistor.purgeQueueAndPersist();
String bucketTime1 = String.valueOf(getTimeBucket());
assertTrue(
"Tmp file should be present if metrics are available",
isFilePresent(getTmpFileName(bucketTime1)));
Assert.assertEquals(5, checkFileForMetrics(getTmpFileName(bucketTime1)));

// Generate the next writer file tmp file
Thread.sleep(5_000);
generateWriterFile(4);
queuePurgerAndPersistor.purgeQueueAndPersist();
String bucketTime2 = String.valueOf(getTimeBucket());
assertTrue(
"Tmp file should be present if metrics are available",
isFilePresent(getTmpFileName(bucketTime2)));
Assert.assertEquals(4, checkFileForMetrics(getTmpFileName(bucketTime2)));

// Waiting and calling purgeQueueAndPersist method
// for tmp file to be renamed to timestamp file
Thread.sleep(5_000);
queuePurgerAndPersistor.purgeQueueAndPersist();
assertTrue(
"Timestamp file should be present if tmp file is present",
isFilePresent(bucketTime1));
Assert.assertEquals(5, checkFileForMetrics(bucketTime1));

// Waiting and calling purgeQueueAndPersist method
// for tmp file to be renamed to timestamp file
Thread.sleep(5_000);
queuePurgerAndPersistor.purgeQueueAndPersist();
assertTrue(
"Timestamp file should be present if tmp file is present",
isFilePresent(bucketTime1));
Assert.assertEquals(4, checkFileForMetrics(bucketTime2));
}

@Test
public void testNoFileWithNoMetrics() throws InterruptedException {
queuePurgerAndPersistor.purgeQueueAndPersist();
String bucketTime1 = String.valueOf(getTimeBucket());
assertFalse(
"Tmp file should not be present if metrics are not available",
isFilePresent(getTmpFileName(bucketTime1)));

// Waiting and calling purgeQueueAndPersist method
// for tmp file to be renamed to timestamp file
Thread.sleep(5_000);
queuePurgerAndPersistor.purgeQueueAndPersist();
Thread.sleep(5_000);
queuePurgerAndPersistor.purgeQueueAndPersist();
assertFalse(
"Timestamp file should not be present if tmp file is not present",
isFilePresent(bucketTime1));
}

// TODO: Write a test for case when one single Event is larger than BUFFER_SIZE

private boolean deleteDirectory(File directoryToBeDeleted) {
File[] allContents = directoryToBeDeleted.listFiles();
if (allContents != null) {
Expand All @@ -66,57 +150,47 @@ private boolean deleteDirectory(File directoryToBeDeleted) {
return directoryToBeDeleted.delete();
}

// @After
public void cleanup() {
deleteDirectory(new File(pathToTestMetricsDir));
}

private long generateWriterFile(int count) {
long currTime = System.currentTimeMillis();
HeapMetricsCollector heapMetricsCollector = new HeapMetricsCollector();

for (int i = 0; i < count; i++) {
heapMetricsCollector.collectMetrics(currTime);
}

EventLog eventLog = new EventLog();
EventLogFileHandler eventLogFileHandler =
new EventLogFileHandler(eventLog, pathToTestMetricsDir);
EventLogQueueProcessor queuePurgerAndPersistor =
new EventLogQueueProcessor(
eventLogFileHandler,
MetricsConfiguration.SAMPLING_INTERVAL,
MetricsConfiguration.SAMPLING_INTERVAL,
mockController);
queuePurgerAndPersistor.purgeQueueAndPersist();
return PerformanceAnalyzerMetrics.getTimeInterval(currTime);
return currTime;
}

@Test
public void write() {
System.setProperty("performanceanalyzer.metrics.log.enabled", "False");
generateWriterFile(4);
private long getTimeBucket() {
return PerformanceAnalyzerMetrics.getTimeInterval(
System.currentTimeMillis(), MetricsConfiguration.SAMPLING_INTERVAL);
}

@Test
public void readSmallFile() {
System.setProperty("performanceanalyzer.metrics.log.enabled", "False");
// String filename = String.valueOf(generateWriterFile(4));
EventLog eventLog = new EventLog();
EventLogFileHandler eventLogFileHandler =
new EventLogFileHandler(eventLog, "test_files/new_format");
// Assert.assertEquals(4, eventLogFileHandler.read("1566152850000").size());
private String getTmpFileName(String filename) {
return filename + ".tmp";
}

@Test
public void readLargeFile() {
System.setProperty("performanceanalyzer.metrics.log.enabled", "False");
String filename = String.valueOf(generateWriterFile(7));
EventLog eventLog = new EventLog();
EventLogFileHandler eventLogFileHandler =
new EventLogFileHandler(eventLog, pathToTestMetricsDir);
// eventLogFileHandler.read(filename);
private boolean isFilePresent(String filename) {
Path pathToFile = Paths.get(pathToTestMetricsDir, filename);
File tempFile = new File(pathToFile.toString());
return tempFile.exists();
}

// TODO: Write a test for case when one single Event is larger than BUFFER_SIZE
private long checkFileForMetrics(String filename) {
Path pathToFile = Paths.get(pathToTestMetricsDir, filename);
File tempFile = new File(pathToFile.toString());
long metrics_count = 0;
try {
Scanner reader = new Scanner(tempFile);
while (reader.hasNextLine()) {
String line = reader.nextLine();
if (line.contains("current_time")) {
metrics_count += 1;
}
}
reader.close();
} catch (FileNotFoundException e) {
Assert.fail("File not found filename:" + filename);
}
return metrics_count;
}
}

0 comments on commit f804bbb

Please sign in to comment.