import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.logging.Logger;
public class FilePerformance {
private static final Logger logger = Logger.getLogger(FilePerformance.class.getName());
public static void measureFilePerformance(String filePath) {
logger.info("Starting file performance measurement for: " + filePath);
long startTime = System.nanoTime();
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = reader.readLine()) != null) {
// Process each line (simulate a short-lived task)
// In a real scenario, perform your desired operation here.
// For this example, we just consume the line.
processLine(line);
}
} catch (IOException e) {
logger.error("Error reading file: " + e.getMessage(), e);
}
long endTime = System.nanoTime();
long duration = (endTime - startTime) / 1_000_000; // Duration in milliseconds
logger.info("File performance measurement complete for: " + filePath);
logger.info("Total duration: " + duration + " ms");
}
private static void processLine(String line) {
// Simulate a short-lived task
// Replace with your actual processing logic.
//logger.info("Processing line: " + line);
}
public static void main(String[] args) {
if (args.length != 1) {
logger.severe("Usage: java FilePerformance <file_path>");
System.exit(1);
}
String filePath = args[0];
measureFilePerformance(filePath);
}
}
Add your comment