import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.Scanner;
public class LogFilePerformance {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Log File Performance Measurement Tool");
String logFilePath = promptForLogFilePath(scanner);
long fileSize = getFileSize(logFilePath);
long startTime = System.currentTimeMillis();
long linesRead = countLines(logFilePath);
long endTime = System.currentTimeMillis();
long duration = endTime - startTime;
double linesPerSecond = (double) linesRead / (duration / 1000);
System.out.println("\n--- Performance Results ---");
System.out.println("Log File: " + logFilePath);
System.out.println("File Size: " + fileSize + " bytes");
System.out.println("Lines Read: " + linesRead);
System.out.println("Time Taken: " + Duration.ofMillis(duration) + " ms");
System.out.println("Lines per Second: " + String.format("%.2f", linesPerSecond) + " lines/s");
scanner.close();
}
private static String promptForLogFilePath(Scanner scanner) {
System.out.print("Enter the path to the log file: ");
return scanner.nextLine();
}
private static long getFileSize(String filePath) {
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
long fileSize = 0;
long bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int bytesRead;
while ((bytesRead = reader.read(buffer)) != -1) {
fileSize += bytesRead;
}
return fileSize;
} catch (IOException e) {
System.err.println("Error getting file size: " + e.getMessage());
return -1; // Indicate an error.
}
}
private static long countLines(String filePath) {
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
long lineCount = 0;
String line;
while ((line = reader.readLine()) != null) {
lineCount++;
}
return lineCount;
} catch (IOException e) {
System.err.println("Error counting lines: " + e.getMessage());
return -1; // Indicate an error.
}
}
}
Add your comment