L2

import java.io.*; import java.util.*; public class LogAnalyzerDebug { public static void main(String[] args) { try { // Define directories File beforeDir = new File("before"); File afterDir = new File("after"); File tempDir = new File("temp"); File reportsDir = new File("reports"); // Create temp and reports directories if they don't exist tempDir.mkdirs(); reportsDir.mkdirs(); System.out.println("=== DEBUG INFO ==="); // Check if directories exist System.out.println("Directory existence check:"); System.out.println("- before directory exists: " + beforeDir.exists()); System.out.println("- after directory exists: " + afterDir.exists()); // List files in directories System.out.println("\nFiles in 'before' directory:"); listFilesInDirectory(beforeDir); System.out.println("\nFiles in 'after' directory:"); listFilesInDirectory(afterDir); // Sample data from files System.out.println("\nSampling data from files:"); sampleDataFromDirectory(beforeDir, "before"); sampleDataFromDirectory(afterDir, "after"); // Process one sample file processSampleFile(beforeDir, tempDir); } catch (Exception e) { System.err.println("Error during analysis: " + e.getMessage()); e.printStackTrace(); } } private static void listFilesInDirectory(File dir) { if (!dir.exists() || !dir.isDirectory()) { System.out.println(" Directory does not exist or is not a directory!"); return; } File[] files = dir.listFiles(); if (files == null || files.length == 0) { System.out.println(" No files found in directory!"); return; } for (File file : files) { if (file.isFile()) { System.out.println(" - " + file.getName() + " (" + file.length() + " bytes)"); } } } private static void sampleDataFromDirectory(File dir, String label) { if (!dir.exists() || !dir.isDirectory()) { System.out.println(" Directory " + label + " does not exist or is not a directory!"); return; } File[] files = dir.listFiles(); if (files == null || files.length == 0) { System.out.println(" No files found in " + label + " directory!"); return; } // Sample first file File sampleFile = files[0]; System.out.println(" Sampling from " + label + " file: " + sampleFile.getName()); try (BufferedReader reader = new BufferedReader(new FileReader(sampleFile))) { String line; int lineCount = 0; int msLineCount = 0; // Sample first 10 lines while ((line = reader.readLine()) != null && lineCount < 10) { System.out.println(" Line " + (lineCount + 1) + ": " + line); lineCount++; if (line.contains("ms")) { msLineCount++; } } // Count total lines and ms lines while ((line = reader.readLine()) != null) { lineCount++; if (line.contains("ms")) { msLineCount++; } } System.out.println(" Total lines in file: " + lineCount); System.out.println(" Lines containing 'ms': " + msLineCount); } catch (IOException e) { System.out.println(" Error reading file: " + e.getMessage()); } } private static void processSampleFile(File inputDir, File tempDir) { File[] files = inputDir.listFiles(); if (files == null || files.length == 0) { System.out.println("\nNo files to process for sample!"); return; } File sampleFile = files[0]; System.out.println("\nProcessing sample file: " + sampleFile.getName()); File beforeTempDir = new File(tempDir, "before"); beforeTempDir.mkdirs(); File controllerTimesFile = new File(beforeTempDir, "controller-times.txt"); File initTimesFile = new File(beforeTempDir, "init-times.txt"); File tokenTimesFile = new File(beforeTempDir, "token-times.txt"); int controllerCount = 0; int initCount = 0; int tokenCount = 0; try (BufferedReader reader = new BufferedReader(new FileReader(sampleFile)); PrintWriter controllerWriter = new PrintWriter(new FileWriter(controllerTimesFile)); PrintWriter initWriter = new PrintWriter(new FileWriter(initTimesFile)); PrintWriter tokenWriter = new PrintWriter(new FileWriter(tokenTimesFile))) { String line; while ((line =

Feb 28, 2025 - 05:21
 0
L2

import java.io.*;
import java.util.*;

public class LogAnalyzerDebug {
    public static void main(String[] args) {
        try {
            // Define directories
            File beforeDir = new File("before");
            File afterDir = new File("after");
            File tempDir = new File("temp");
            File reportsDir = new File("reports");

            // Create temp and reports directories if they don't exist
            tempDir.mkdirs();
            reportsDir.mkdirs();

            System.out.println("=== DEBUG INFO ===");

            // Check if directories exist
            System.out.println("Directory existence check:");
            System.out.println("- before directory exists: " + beforeDir.exists());
            System.out.println("- after directory exists: " + afterDir.exists());

            // List files in directories
            System.out.println("\nFiles in 'before' directory:");
            listFilesInDirectory(beforeDir);

            System.out.println("\nFiles in 'after' directory:");
            listFilesInDirectory(afterDir);

            // Sample data from files
            System.out.println("\nSampling data from files:");
            sampleDataFromDirectory(beforeDir, "before");
            sampleDataFromDirectory(afterDir, "after");

            // Process one sample file
            processSampleFile(beforeDir, tempDir);

        } catch (Exception e) {
            System.err.println("Error during analysis: " + e.getMessage());
            e.printStackTrace();
        }
    }

    private static void listFilesInDirectory(File dir) {
        if (!dir.exists() || !dir.isDirectory()) {
            System.out.println("  Directory does not exist or is not a directory!");
            return;
        }

        File[] files = dir.listFiles();
        if (files == null || files.length == 0) {
            System.out.println("  No files found in directory!");
            return;
        }

        for (File file : files) {
            if (file.isFile()) {
                System.out.println("  - " + file.getName() + " (" + file.length() + " bytes)");
            }
        }
    }

    private static void sampleDataFromDirectory(File dir, String label) {
        if (!dir.exists() || !dir.isDirectory()) {
            System.out.println("  Directory " + label + " does not exist or is not a directory!");
            return;
        }

        File[] files = dir.listFiles();
        if (files == null || files.length == 0) {
            System.out.println("  No files found in " + label + " directory!");
            return;
        }

        // Sample first file
        File sampleFile = files[0];
        System.out.println("  Sampling from " + label + " file: " + sampleFile.getName());

        try (BufferedReader reader = new BufferedReader(new FileReader(sampleFile))) {
            String line;
            int lineCount = 0;
            int msLineCount = 0;

            // Sample first 10 lines
            while ((line = reader.readLine()) != null && lineCount < 10) {
                System.out.println("  Line " + (lineCount + 1) + ": " + line);
                lineCount++;

                if (line.contains("ms")) {
                    msLineCount++;
                }
            }

            // Count total lines and ms lines
            while ((line = reader.readLine()) != null) {
                lineCount++;
                if (line.contains("ms")) {
                    msLineCount++;
                }
            }

            System.out.println("  Total lines in file: " + lineCount);
            System.out.println("  Lines containing 'ms': " + msLineCount);

        } catch (IOException e) {
            System.out.println("  Error reading file: " + e.getMessage());
        }
    }

    private static void processSampleFile(File inputDir, File tempDir) {
        File[] files = inputDir.listFiles();
        if (files == null || files.length == 0) {
            System.out.println("\nNo files to process for sample!");
            return;
        }

        File sampleFile = files[0];
        System.out.println("\nProcessing sample file: " + sampleFile.getName());

        File beforeTempDir = new File(tempDir, "before");
        beforeTempDir.mkdirs();

        File controllerTimesFile = new File(beforeTempDir, "controller-times.txt");
        File initTimesFile = new File(beforeTempDir, "init-times.txt");
        File tokenTimesFile = new File(beforeTempDir, "token-times.txt");

        int controllerCount = 0;
        int initCount = 0;
        int tokenCount = 0;

        try (BufferedReader reader = new BufferedReader(new FileReader(sampleFile));
             PrintWriter controllerWriter = new PrintWriter(new FileWriter(controllerTimesFile));
             PrintWriter initWriter = new PrintWriter(new FileWriter(initTimesFile));
             PrintWriter tokenWriter = new PrintWriter(new FileWriter(tokenTimesFile))) {

            String line;
            while ((line = reader.readLine()) != null) {
                // Look for controller method timings - multiple possible formats
                if ((line.contains("Time taken for") && line.contains("is") && line.contains("ms")) ||
                    (line.contains("|*Time taken for*|") && line.contains("|*is*|") && line.contains("|*ms"))) {
                    controllerWriter.println(line);
                    controllerCount++;
                    if (controllerCount <= 3) {
                        System.out.println("  Controller timing found: " + line);
                    }
                }

                // Look for initialization timings
                else if (line.contains("initialization completed in") && line.contains("ms")) {
                    initWriter.println(line);
                    initCount++;
                    if (initCount <= 3) {
                        System.out.println("  Initialization timing found: " + line);
                    }
                }

                // Look for token timings
                else if (line.contains("Token execution time in(ms)")) {
                    tokenWriter.println(line);
                    tokenCount++;
                    if (tokenCount <= 3) {
                        System.out.println("  Token timing found: " + line);
                    }
                }
            }

            System.out.println("\nProcessing results:");
            System.out.println("  - Controller timings found: " + controllerCount);
            System.out.println("  - Initialization timings found: " + initCount);
            System.out.println("  - Token timings found: " + tokenCount);

        } catch (IOException e) {
            System.out.println("  Error processing sample file: " + e.getMessage());
        }
    }
}