DEV Community

Charlie Fubon
Charlie Fubon

Posted on

Du


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SimplifiedLogAnalyzer {

    // Define patterns to match in log files
    private static final Pattern WEBAPP_INIT_PATTERN = Pattern.compile("WebApplicationContext: initialization completed in (\\d+) ms");
    private static final Pattern METHOD_EXECUTION_PATTERN = Pattern.compile("Time taken for\\*\\|\\*(.*?)\\*\\|\\*is\\*\\|\\*(\\d+)\\*\\|\\*ms");

    // Data structure to store timing information
    static class TimingInfo {
        String operation;
        long timeInMs;

        TimingInfo(String operation, long timeInMs) {
            this.operation = operation;
            this.timeInMs = timeInMs;
        }
    }

    // Data structure to store statistics
    static class Statistics {
        long min = Long.MAX_VALUE;
        long max = Long.MIN_VALUE;
        long sum = 0;
        int count = 0;

        void addValue(long value) {
            min = Math.min(min, value);
            max = Math.max(max, value);
            sum += value;
            count++;
        }

        double getAverage() {
            return count > 0 ? (double) sum / count : 0;
        }

        @Override
        public String toString() {
            return String.format("Count: %d, Min: %d ms, Max: %d ms, Avg: %.2f ms, Total: %d ms", 
                    count, min, max, getAverage(), sum);
        }
    }

    public static void main(String[] args) {
        // BEFORE UPGRADE: Paste your log content here
        String beforeUpgradeLogContent = 
            "INFO 2024-12-29 15:55:19,347 [[STANDBY] ExecuteThread: '12' for queue: 'weblogic.kernel.Default (self-tuning)'] " +
            "(org-springframework.boot.web.servlet.context.ServletWebServerApplicationContext) - Root WebApplicationContext: initialization completed in 15299 ms\n" +
            "INFO 2024-12-29 15:55:21,560 [[STANDBY] ExecuteThread: '27' for queue: 'weblogic.kernel.Default (self-tuning)'] " +
            "(org-springframework-boot.web.servlet.context.ServletWebServerApplicationContext) - Root WebApplicationContext: initialization completed in 15968 ms\n" +
            "INFO 2024-12-29 16:01:08,067 [LoggerThread-0] (TimeTracker) - 2024-12-29 16:01:08,064 Thread[[ACTIVE] ExecuteThread: " +
            "'11' for queue: 'weblogic.kernel.Default (self-tuning)',5,Pooled Threads] [com.cibc-go.myclient.dao.impl." +
            "ReferenceDataDaoImpl.getRefTableValues(ReferenceDataDaoImpl.java:96)] *| *Time taken for*|*ReferenceDataDaoImpl." +
            "getRefTableValues*|*is*| *567*|*ms.\n" +
            "INFO 2024-12-29 16:01:10,679 [LoggerThread-0] (TimeTracker) - 2024-12-29 16:01:10,678 Thread[[ACTIVE] ExecuteThread: " +
            "'11' for queue: 'weblogic.kernel.Default (self-tuning)',5,Pooled Threads] [com.cibc-go.myclient.dao.impl." +
            "ReferenceDataDaoImpl.getEntitySubTypes(ReferenceDataDaoImpl.java:324)] *| *Time taken for*| *ReferenceDataDaoImpl." +
            "getEntitySubTypes*|*is*|*4206*| *ms.";

        // AFTER UPGRADE: Paste your log content here
        String afterUpgradeLogContent = 
            "INFO 2025-01-29 15:55:19,347 [[STANDBY] ExecuteThread: '12' for queue: 'weblogic.kernel.Default (self-tuning)'] " +
            "(org-springframework.boot.web.servlet.context.ServletWebServerApplicationContext) - Root WebApplicationContext: initialization completed in 11299 ms\n" +
            "INFO 2025-01-29 15:55:21,560 [[STANDBY] ExecuteThread: '27' for queue: 'weblogic.kernel.Default (self-tuning)'] " +
            "(org-springframework-boot.web.servlet.context.ServletWebServerApplicationContext) - Root WebApplicationContext: initialization completed in 11968 ms\n" +
            "INFO 2025-01-29 16:01:08,067 [LoggerThread-0] (TimeTracker) - 2025-01-29 16:01:08,064 Thread[[ACTIVE] ExecuteThread: " +
            "'11' for queue: 'weblogic.kernel.Default (self-tuning)',5,Pooled Threads] [com.cibc-go.myclient.dao.impl." +
            "ReferenceDataDaoImpl.getRefTableValues(ReferenceDataDaoImpl.java:96)] *| *Time taken for*|*ReferenceDataDaoImpl." +
            "getRefTableValues*|*is*| *367*|*ms.\n" +
            "INFO 2025-01-29 16:01:10,679 [LoggerThread-0] (TimeTracker) - 2025-01-29 16:01:10,678 Thread[[ACTIVE] ExecuteThread: " +
            "'11' for queue: 'weblogic.kernel.Default (self-tuning)',5,Pooled Threads] [com.cibc-go.myclient.dao.impl." +
            "ReferenceDataDaoImpl.getEntitySubTypes(ReferenceDataDaoImpl.java:324)] *| *Time taken for*| *ReferenceDataDaoImpl." +
            "getEntitySubTypes*|*is*|*2606*| *ms.";

        // Replace the sample data above with your actual log content
        // You can paste 2000 lines if needed - just add them to the strings

        // Process the logs
        Map<String, Statistics> beforeStats = processLogContent(beforeUpgradeLogContent);
        Map<String, Statistics> afterStats = processLogContent(afterUpgradeLogContent);

        // Print comparison
        printComparison(beforeStats, afterStats);
    }

    private static Map<String, Statistics> processLogContent(String logContent) {
        List<TimingInfo> timings = new ArrayList<>();

        // Split the content into lines
        String[] lines = logContent.split("\n");
        System.out.println("Processing " + lines.length + " log lines");

        for (String line : lines) {
            // Try to match WebApp initialization pattern
            Matcher webappMatcher = WEBAPP_INIT_PATTERN.matcher(line);
            if (webappMatcher.find()) {
                long time = Long.parseLong(webappMatcher.group(1));
                timings.add(new TimingInfo("WebApplicationContext Initialization", time));
                continue;
            }

            // Try to match method execution pattern
            Matcher methodMatcher = METHOD_EXECUTION_PATTERN.matcher(line);
            if (methodMatcher.find()) {
                String methodName = methodMatcher.group(1);
                long time = Long.parseLong(methodMatcher.group(2));
                timings.add(new TimingInfo(methodName, time));
            }
        }

        System.out.println("Found " + timings.size() + " timing entries");

        // Calculate statistics
        return calculateStatistics(timings);
    }

    private static Map<String, Statistics> calculateStatistics(List<TimingInfo> timings) {
        Map<String, Statistics> statsByOperation = new HashMap<>();

        for (TimingInfo timing : timings) {
            Statistics stats = statsByOperation.computeIfAbsent(timing.operation, k -> new Statistics());
            stats.addValue(timing.timeInMs);
        }

        return statsByOperation;
    }

    private static void printComparison(Map<String, Statistics> beforeStats, Map<String, Statistics> afterStats) {
        System.out.println("\n===================== PERFORMANCE COMPARISON =====================");
        System.out.println("Operation | Before Upgrade | After Upgrade | Improvement (%)");
        System.out.println("----------------------------------------------------------------");

        // Combine all keys
        java.util.Set<String> allOperations = new java.util.HashSet<>();
        allOperations.addAll(beforeStats.keySet());
        allOperations.addAll(afterStats.keySet());

        for (String operation : allOperations) {
            Statistics before = beforeStats.getOrDefault(operation, new Statistics());
            Statistics after = afterStats.getOrDefault(operation, new Statistics());

            // Skip if no data for comparison
            if (before.count == 0 || after.count == 0) {
                System.out.println(operation + " - Incomplete data, skipping comparison");
                continue;
            }

            double beforeAvg = before.getAverage();
            double afterAvg = after.getAverage();
            double improvementPct = beforeAvg > 0 ? ((beforeAvg - afterAvg) / beforeAvg) * 100 : 0;

            System.out.printf("%-40s | %-15s | %-15s | %s%n", 
                    operation,
                    String.format("%.2f ms", beforeAvg),
                    String.format("%.2f ms", afterAvg),
                    String.format("%+.2f%%", improvementPct));
        }

        System.out.println("================================================================");
    }
}

Enter fullscreen mode Exit fullscreen mode

Top comments (6)

Collapse
 
gchar profile image
Charlie Fubon

import java.io.*;
import java.nio.charset.Charset;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class LogAnalyzer {

    // The three patterns from the first message
    private static final Pattern PATTERN_1 = Pattern.compile("initialization completed in (\\d+) ms");
    private static final Pattern PATTERN_2 = Pattern.compile("Time taken for\\*\\|\\*(.*?)\\*\\|\\*is\\*\\|\\*(\\d+)\\*\\|\\*ms");
    private static final Pattern PATTERN_3 = Pattern.compile("Time taken for(.*?)is(\\d+)ms");

    static class TimingInfo {
        String category;
        String operation;
        int timeInMs;

        TimingInfo(String category, String operation, int timeInMs) {
            this.category = category;
            this.operation = operation;
            this.timeInMs = timeInMs;
        }

        @Override
        public String toString() {
            return category + " - " + operation + ": " + timeInMs + " ms";
        }
    }

    public static void main(String[] args) {
        // File paths
        String[] beforeFiles = {"Jan29log.txt", "Jan30log.txt", "Jan31log.txt"};
        String[] afterFiles = {"Feb25thlog.txt", "Feb26thlog.txt", "Feb27thlog.txt"};

        try {
            // Process files and collect timing data
            List<TimingInfo> beforeTimings = new ArrayList<>();
            System.out.println("PROCESSING BEFORE UPGRADE FILES:");
            for (String file : beforeFiles) {
                List<TimingInfo> timings = processFile(file);
                beforeTimings.addAll(timings);
            }

            List<TimingInfo> afterTimings = new ArrayList<>();
            System.out.println("\nPROCESSING AFTER UPGRADE FILES:");
            for (String file : afterFiles) {
                List<TimingInfo> timings = processFile(file);
                afterTimings.addAll(timings);
            }

            // Generate the comparison report
            generateReport(beforeTimings, afterTimings);

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

    private static List<TimingInfo> processFile(String filename) {
        List<TimingInfo> timings = new ArrayList<>();
        File file = new File(filename);

        if (!file.exists()) {
            System.out.println("  " + filename + " not found");
            return timings;
        }

        System.out.println("  Processing " + filename);

        // Track counts for each pattern
        int pattern1Count = 0;
        int pattern2Count = 0;
        int pattern3Count = 0;

        try {
            // Use UTF-16LE encoding as we discovered this is critical
            BufferedReader reader = new BufferedReader(
                new InputStreamReader(new FileInputStream(file), Charset.forName("UTF-16LE")));

            String line;
            int lineCount = 0;

            while ((line = reader.readLine()) != null) {
                lineCount++;

                // Try Pattern 1: "initialization completed in X ms"
                Matcher matcher1 = PATTERN_1.matcher(line);
                if (matcher1.find()) {
                    int time = Integer.parseInt(matcher1.group(1));
                    timings.add(new TimingInfo("Initialization", "WebApplicationContext", time));
                    pattern1Count++;
                    continue;
                }

                // Try Pattern 2: "Time taken for*|*Method*|*is*|*X*|*ms"
                Matcher matcher2 = PATTERN_2.matcher(line);
                if (matcher2.find()) {
                    String operation = matcher2.group(1).trim();
                    int time = Integer.parseInt(matcher2.group(2));
                    timings.add(new TimingInfo("Method Execution", operation, time));
                    pattern2Count++;
                    continue;
                }

                // Try Pattern 3: Another format of "Time taken for X is Y ms"
                Matcher matcher3 = PATTERN_3.matcher(line);
                if (matcher3.find()) {
                    String operation = matcher3.group(1).trim();
                    int time = Integer.parseInt(matcher3.group(2));
                    timings.add(new TimingInfo("Method Execution", operation, time));
                    pattern3Count++;
                }
            }
            reader.close();

            System.out.println("    Processed " + lineCount + " lines");
            System.out.println("    Pattern 1 (initialization): " + pattern1Count + " matches");
            System.out.println("    Pattern 2 (formatted method): " + pattern2Count + " matches");
            System.out.println("    Pattern 3 (simple method): " + pattern3Count + " matches");
            System.out.println("    Total timing entries found: " + timings.size());

            // Print a few examples of each category if available
            Map<String, List<TimingInfo>> byCategory = groupByCategory(timings);
            for (Map.Entry<String, List<TimingInfo>> entry : byCategory.entrySet()) {
                System.out.println("    Sample " + entry.getKey() + " timings:");
                List<TimingInfo> categoryTimings = entry.getValue();
                for (int i = 0; i < Math.min(3, categoryTimings.size()); i++) {
                    System.out.println("      " + categoryTimings.get(i));
                }
            }

        } catch (Exception e) {
            System.out.println("    Error processing " + filename + ": " + e.getMessage());
        }

        return timings;
    }

    private static Map<String, List<TimingInfo>> groupByCategory(List<TimingInfo> timings) {
        Map<String, List<TimingInfo>> grouped = new HashMap<>();

        for (TimingInfo timing : timings) {
            if (!grouped.containsKey(timing.category)) {
                grouped.put(timing.category, new ArrayList<>());
            }
            grouped.get(timing.category).add(timing);
        }

        return grouped;
    }

    private static void generateReport(List<TimingInfo> beforeTimings, List<TimingInfo> afterTimings) {
        // Group by category and operation
        Map<String, Map<String, List<Integer>>> beforeByCategory = groupTimings(beforeTimings);
        Map<String, Map<String, List<Integer>>> afterByCategory = groupTimings(afterTimings);

        // Generate the report
        System.out.println("\n=========== PERFORMANCE COMPARISON REPORT ===========");

        // Combine all keys to ensure we don't miss any
        Set<String> allCategories = new HashSet<>();
        allCategories.addAll(beforeByCategory.keySet());
        allCategories.addAll(afterByCategory.keySet());

        for (String category : allCategories) {
            System.out.println("\n" + category.toUpperCase() + " PERFORMANCE:");
            System.out.println("Operation | Before Upgrade | After Upgrade | Improvement (%)");
            System.out.println("----------------------------------------------------------");

            // Get operations for this category
            Map<String, List<Integer>> beforeOps = beforeByCategory.getOrDefault(category, new HashMap<>());
            Map<String, List<Integer>> afterOps = afterByCategory.getOrDefault(category, new HashMap<>());

            Set<String> allOperations = new HashSet<>();
            allOperations.addAll(beforeOps.keySet());
            allOperations.addAll(afterOps.keySet());

            for (String operation : allOperations) {
                List<Integer> beforeTimes = beforeOps.getOrDefault(operation, Collections.emptyList());
                List<Integer> afterTimes = afterOps.getOrDefault(operation, Collections.emptyList());

                if (beforeTimes.isEmpty() || afterTimes.isEmpty()) {
                    System.out.println(operation + " | Insufficient data for comparison");
                    continue;
                }

                double beforeAvg = calculateAverage(beforeTimes);
                double afterAvg = calculateAverage(afterTimes);
                double improvementPct = ((beforeAvg - afterAvg) / beforeAvg) * 100;

                System.out.printf("%-30s | %-14.2f | %-13.2f | %+.2f%%\n", 
                        truncateString(operation, 30), beforeAvg, afterAvg, improvementPct);
            }

            // Print category summary
            List<Integer> allBeforeTimes = flattenTimings(beforeOps);
            List<Integer> allAfterTimes = flattenTimings(afterOps);

            if (!allBeforeTimes.isEmpty() && !allAfterTimes.isEmpty()) {
                double beforeAvg = calculateAverage(allBeforeTimes);
                double afterAvg = calculateAverage(allAfterTimes);
                double improvementPct = ((beforeAvg - afterAvg) / beforeAvg) * 100;

                System.out.println("----------------------------------------------------------");
                System.out.printf("CATEGORY AVERAGE               | %-14.2f | %-13.2f | %+.2f%%\n", 
                        beforeAvg, afterAvg, improvementPct);
            }
        }

        // Overall summary
        List<Integer> allBeforeTimes = new ArrayList<>();
        for (TimingInfo timing : beforeTimings) {
            allBeforeTimes.add(timing.timeInMs);
        }

        List<Integer> allAfterTimes = new ArrayList<>();
        for (TimingInfo timing : afterTimings) {
            allAfterTimes.add(timing.timeInMs);
        }

        if (!allBeforeTimes.isEmpty() && !allAfterTimes.isEmpty()) {
            double beforeAvg = calculateAverage(allBeforeTimes);
            double afterAvg = calculateAverage(allAfterTimes);
            double improvementPct = ((beforeAvg - afterAvg) / beforeAvg) * 100;

            System.out.println("\nOVERALL PERFORMANCE IMPROVEMENT:");
            System.out.println("----------------------------------------------------------");
            System.out.printf("ALL OPERATIONS                  | %-14.2f | %-13.2f | %+.2f%%\n", 
                    beforeAvg, afterAvg, improvementPct);
        }

        System.out.println("\n=======================================================");
    }

    private static Map<String, Map<String, List<Integer>>> groupTimings(List<TimingInfo> timings) {
        Map<String, Map<String, List<Integer>>> grouped = new HashMap<>();

        for (TimingInfo timing : timings) {
            // Create category map if not exists
            if (!grouped.containsKey(timing.category)) {
                grouped.put(timing.category, new HashMap<>());
            }

            // Create operation list if not exists
            Map<String, List<Integer>> categoryMap = grouped.get(timing.category);
            if (!categoryMap.containsKey(timing.operation)) {
                categoryMap.put(timing.operation, new ArrayList<>());
            }

            // Add timing value
            categoryMap.get(timing.operation).add(timing.timeInMs);
        }

        return grouped;
    }

    private static List<Integer> flattenTimings(Map<String, List<Integer>> operationsMap) {
        List<Integer> flattened = new ArrayList<>();

        for (List<Integer> timings : operationsMap.values()) {
            flattened.addAll(timings);
        }

        return flattened;
    }

    private static double calculateAverage(List<Integer> values) {
        if (values.isEmpty()) {
            return 0;
        }

        long sum = 0;
        for (int value : values) {
            sum += value;
        }

        return (double) sum / values.size();
    }

    private static String truncateString(String str, int maxLength) {
        if (str.length() <= maxLength) {
            return str;
        }
        return str.substring(0, maxLength - 3) + "...";
    }
}

Enter fullscreen mode Exit fullscreen mode
Collapse
 
gchar profile image
Charlie Fubon

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class UTF16LogAnalyzer {

    private static final Pattern TIME_PATTERN = Pattern.compile("completed in (\\d+) ms");

    public static void main(String[] args) {
        // Files to process
        String[] beforeFiles = {"Jan29log.txt", "Jan30log.txt", "Jan31log.txt"};
        String[] afterFiles = {"Feb25thlog.txt", "Feb26thlog.txt", "Feb27thlog.txt"};

        try {
            // Process before files
            System.out.println("BEFORE UPGRADE:");
            List<Integer> beforeTimes = new ArrayList<>();
            for (String file : beforeFiles) {
                List<Integer> times = processFile(file);
                System.out.println("  " + file + ": Found " + times.size() + " timing values");
                beforeTimes.addAll(times);
            }

            // Process after files
            System.out.println("\nAFTER UPGRADE:");
            List<Integer> afterTimes = new ArrayList<>();
            for (String file : afterFiles) {
                List<Integer> times = processFile(file);
                System.out.println("  " + file + ": Found " + times.size() + " timing values");
                afterTimes.addAll(times);
            }

            // Calculate statistics and print comparison
            if (!beforeTimes.isEmpty() && !afterTimes.isEmpty()) {
                printComparisonResults(beforeTimes, afterTimes);
            } else {
                System.out.println("\nERROR: Not enough data to make a comparison");
            }

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

    private static List<Integer> processFile(String filename) {
        List<Integer> timings = new ArrayList<>();
        File file = new File(filename);

        if (!file.exists()) {
            System.out.println("File not found: " + filename);
            return timings;
        }

        try {
            // Use UTF-16LE encoding - this is critical based on the screenshot
            BufferedReader reader = new BufferedReader(
                new InputStreamReader(new FileInputStream(file), Charset.forName("UTF-16LE")));

            String line;
            int lineCount = 0;

            // Print the first few bytes for debugging
            System.out.println("  First 20 bytes of " + filename + ":");
            try (FileInputStream fis = new FileInputStream(file)) {
                byte[] bytes = new byte[20];
                int bytesRead = fis.read(bytes);
                for (int i = 0; i < bytesRead; i++) {
                    System.out.printf("%02X ", bytes[i]);
                }
                System.out.println();
            }

            // Read and process each line
            System.out.println("  Processing lines...");
            while ((line = reader.readLine()) != null) {
                lineCount++;

                // Print first few lines for debugging
                if (lineCount <= 3) {
                    System.out.println("    Line " + lineCount + ": " + line);
                }

                // Try the main pattern
                Matcher matcher = TIME_PATTERN.matcher(line);
                if (matcher.find()) {
                    int time = Integer.parseInt(matcher.group(1));
                    timings.add(time);
                    System.out.println("    Found timing: " + time + " ms on line " + lineCount);
                    continue;
                }

                // If that doesn't match, try a more aggressive approach - look for "ms" context
                if (line.contains("ms") && line.contains("init")) {
                    Pattern numPattern = Pattern.compile("(\\d+)\\s+ms");
                    Matcher numMatcher = numPattern.matcher(line);
                    if (numMatcher.find()) {
                        int time = Integer.parseInt(numMatcher.group(1));
                        timings.add(time);
                        System.out.println("    Found timing (method 2): " + time + " ms on line " + lineCount);
                    }
                }
            }
            reader.close();

            System.out.println("  Processed " + lineCount + " lines, found " + timings.size() + " timing values");

            // If we still have no timings, try direct byte scanning as last resort
            if (timings.isEmpty()) {
                System.out.println("  No timings found, trying direct byte scanning...");
                timings = scanFileBytes(file);
            }

        } catch (Exception e) {
            System.out.println("  Error processing " + filename + ": " + e.getMessage());
            e.printStackTrace();
        }

        return timings;
    }

    private static List<Integer> scanFileBytes(File file) throws IOException {
        List<Integer> timings = new ArrayList<>();

        try (FileInputStream fis = new FileInputStream(file)) {
            byte[] buffer = new byte[(int) file.length()];
            fis.read(buffer);

            String content = new String(buffer, Charset.forName("UTF-16LE"));
            String[] lines = content.split("\n");

            System.out.println("  Scanning " + lines.length + " lines in direct byte mode");

            for (int i = 0; i < lines.length; i++) {
                String line = lines[i];

                // Check for initialization context
                if (line.contains("initialization") && line.contains("ms")) {
                    Pattern numPattern = Pattern.compile("\\b(\\d{4,6})\\s+ms\\b");
                    Matcher matcher = numPattern.matcher(line);
                    if (matcher.find()) {
                        int time = Integer.parseInt(matcher.group(1));
                        timings.add(time);
                        System.out.println("    Found timing (byte scan): " + time + " ms on line " + i);
                    }
                }
            }
        }

        return timings;
    }

    private static void printComparisonResults(List<Integer> beforeTimes, List<Integer> afterTimes) {
        // Calculate averages
        double beforeAvg = calculateAverage(beforeTimes);
        double afterAvg = calculateAverage(afterTimes);

        // Calculate min and max
        int beforeMin = Integer.MAX_VALUE;
        int beforeMax = Integer.MIN_VALUE;
        for (int time : beforeTimes) {
            beforeMin = Math.min(beforeMin, time);
            beforeMax = Math.max(beforeMax, time);
        }

        int afterMin = Integer.MAX_VALUE;
        int afterMax = Integer.MIN_VALUE;
        for (int time : afterTimes) {
            afterMin = Math.min(afterMin, time);
            afterMax = Math.max(afterMax, time);
        }

        // Calculate improvement percentage
        double improvementPct = ((beforeAvg - afterAvg) / beforeAvg) * 100;

        // Print summary
        System.out.println("\n===== PERFORMANCE COMPARISON =====");
        System.out.println("Metric       | Before Upgrade | After Upgrade | Improvement");
        System.out.println("-----------------------------------------------------------");
        System.out.printf("Min Time     | %-14d | %-13d | %.2f%%\n", 
                beforeMin, afterMin, ((double)(beforeMin - afterMin) / beforeMin) * 100);
        System.out.printf("Max Time     | %-14d | %-13d | %.2f%%\n", 
                beforeMax, afterMax, ((double)(beforeMax - afterMax) / beforeMax) * 100);
        System.out.printf("Average Time | %-14.2f | %-13.2f | %.2f%%\n", 
                beforeAvg, afterAvg, improvementPct);
        System.out.println("-----------------------------------------------------------");

        // Print all values
        System.out.println("\nBefore upgrade times (ms):");
        for (int time : beforeTimes) {
            System.out.println("  " + time);
        }

        System.out.println("\nAfter upgrade times (ms):");
        for (int time : afterTimes) {
            System.out.println("  " + time);
        }
    }

    private static double calculateAverage(List<Integer> values) {
        long sum = 0;
        for (int value : values) {
            sum += value;
        }
        return (double) sum / values.size();
    }
}

Enter fullscreen mode Exit fullscreen mode
Collapse
 
gchar profile image
Charlie Fubon

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class LogAnalyzer {

    // Pattern that accounts for spaces between characters
    private static final Pattern INIT_PATTERN = Pattern.compile("R\\s*o\\s*o\\s*t\\s*W\\s*e\\s*b\\s*A\\s*p\\s*p\\s*l\\s*i\\s*c\\s*a\\s*t\\s*i\\s*o\\s*n\\s*C\\s*o\\s*n\\s*t\\s*e\\s*x\\s*t\\s*:\\s*i\\s*n\\s*i\\s*t\\s*i\\s*a\\s*l\\s*i\\s*z\\s*a\\s*t\\s*i\\s*o\\s*n\\s*c\\s*o\\s*m\\s*p\\s*l\\s*e\\s*t\\s*e\\s*d\\s*i\\s*n\\s*(\\d+)\\s*m\\s*s");

    // Alternative simpler pattern that looks for "completed in X ms" with potential spaces
    private static final Pattern SIMPLE_PATTERN = Pattern.compile("c\\s*o\\s*m\\s*p\\s*l\\s*e\\s*t\\s*e\\s*d\\s*i\\s*n\\s*(\\d+)\\s*m\\s*s");

    static class TimingInfo {
        String operation;
        long timeInMs;

        TimingInfo(String operation, long timeInMs) {
            this.operation = operation;
            this.timeInMs = timeInMs;
        }
    }

    static class Statistics {
        long min = Long.MAX_VALUE;
        long max = Long.MIN_VALUE;
        long sum = 0;
        int count = 0;

        void addValue(long value) {
            min = Math.min(min, value);
            max = Math.max(max, value);
            sum += value;
            count++;
        }

        double getAverage() {
            return count > 0 ? (double) sum / count : 0;
        }
    }

    public static void main(String[] args) {
        // Files to process
        String[] beforeFiles = {"Jan29log.txt", "Jan30log.txt", "Jan31log.txt"};
        String[] afterFiles = {"Feb25thlog.txt", "Feb26thlog.txt", "Feb27thlog.txt"};

        List<TimingInfo> beforeTimings = new ArrayList<>();
        List<TimingInfo> afterTimings = new ArrayList<>();

        try {
            // Process before files
            System.out.println("Processing BEFORE upgrade files:");
            for (String filename : beforeFiles) {
                File file = new File(filename);
                if (file.exists()) {
                    List<TimingInfo> timings = processFile(file);
                    beforeTimings.addAll(timings);
                    System.out.println("  - " + filename + ": Found " + timings.size() + " timing entries");

                    // Print a few examples if any were found
                    if (!timings.isEmpty()) {
                        for (int i = 0; i < Math.min(3, timings.size()); i++) {
                            TimingInfo info = timings.get(i);
                            System.out.println("      * " + info.operation + ": " + info.timeInMs + " ms");
                        }
                    } else {
                        // Try emergency pattern matching
                        System.out.println("    Trying emergency pattern matching for " + filename);
                        List<TimingInfo> emergencyTimings = emergencyProcessFile(file);
                        beforeTimings.addAll(emergencyTimings);
                        System.out.println("    Emergency found " + emergencyTimings.size() + " timing entries");
                    }
                }
            }

            // Process after files
            System.out.println("\nProcessing AFTER upgrade files:");
            for (String filename : afterFiles) {
                File file = new File(filename);
                if (file.exists()) {
                    List<TimingInfo> timings = processFile(file);
                    afterTimings.addAll(timings);
                    System.out.println("  - " + filename + ": Found " + timings.size() + " timing entries");

                    // Print a few examples if any were found
                    if (!timings.isEmpty()) {
                        for (int i = 0; i < Math.min(3, timings.size()); i++) {
                            TimingInfo info = timings.get(i);
                            System.out.println("      * " + info.operation + ": " + info.timeInMs + " ms");
                        }
                    } else {
                        // Try emergency pattern matching
                        System.out.println("    Trying emergency pattern matching for " + filename);
                        List<TimingInfo> emergencyTimings = emergencyProcessFile(file);
                        afterTimings.addAll(emergencyTimings);
                        System.out.println("    Emergency found " + emergencyTimings.size() + " timing entries");
                    }
                }
            }

            // Calculate summary and print results
            if (!beforeTimings.isEmpty() && !afterTimings.isEmpty()) {
                printResults(beforeTimings, afterTimings);
            } else {
                System.out.println("\nERROR: Not enough timing data found to make a comparison.");
                System.out.println("Before timings found: " + beforeTimings.size());
                System.out.println("After timings found: " + afterTimings.size());
            }

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

    private static List<TimingInfo> processFile(File file) throws IOException {
        List<TimingInfo> timings = new ArrayList<>();

        try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
            String line;
            int lineCount = 0;
            int foundCount = 0;

            while ((line = reader.readLine()) != null) {
                lineCount++;

                // For the first 10 lines, print them to see the actual format
                if (lineCount <= 10) {
                    System.out.println("Line " + lineCount + ": " + line.substring(0, Math.min(60, line.length())) + "...");
                }

                // Try both patterns
                Matcher initMatcher = INIT_PATTERN.matcher(line);
                if (initMatcher.find()) {
                    long time = Long.parseLong(initMatcher.group(1));
                    timings.add(new TimingInfo("WebApplicationContext Initialization", time));
                    foundCount++;

                    if (foundCount <= 3) {
                        System.out.println("  Found match with INIT_PATTERN: " + time + " ms");
                    }
                    continue;
                }

                Matcher simpleMatcher = SIMPLE_PATTERN.matcher(line);
                if (simpleMatcher.find()) {
                    long time = Long.parseLong(simpleMatcher.group(1));
                    timings.add(new TimingInfo("WebApplicationContext Initialization", time));
                    foundCount++;

                    if (foundCount <= 3) {
                        System.out.println("  Found match with SIMPLE_PATTERN: " + time + " ms");
                    }
                }
            }

            System.out.println("Processed " + lineCount + " lines in " + file.getName());
        }

        return timings;
    }

    private static List<TimingInfo> emergencyProcessFile(File file) throws IOException {
        List<TimingInfo> timings = new ArrayList<>();

        try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
            String line;

            while ((line = reader.readLine()) != null) {
                // Remove spaces between letters for better matching
                String noSpaceLine = line.replaceAll("\\s+", "");

                // Look for patterns in the no-space line
                if (noSpaceLine.contains("initializationcompletedIn")) {
                    // Extract the number right after "in"
                    int inIndex = noSpaceLine.indexOf("completedIn");
                    if (inIndex >= 0) {
                        String timeStr = "";
                        for (int i = inIndex + "completedIn".length(); i < noSpaceLine.length(); i++) {
                            char c = noSpaceLine.charAt(i);
                            if (Character.isDigit(c)) {
                                timeStr += c;
                            } else {
                                break;
                            }
                        }

                        if (!timeStr.isEmpty()) {
                            long time = Long.parseLong(timeStr);
                            timings.add(new TimingInfo("WebApplicationContext Initialization", time));
                            System.out.println("  Found emergency match: " + time + " ms");
                        }
                    }
                }

                // Try with original line looking for number + ms patterns
                String[] words = line.split("\\s+");
                for (int i = 0; i < words.length - 1; i++) {
                    if (words[i].matches("\\d+") && words[i+1].equalsIgnoreCase("ms")) {
                        long time = Long.parseLong(words[i]);
                        timings.add(new TimingInfo("WebApplicationContext Initialization", time));
                        System.out.println("  Found direct number match: " + time + " ms");
                    }
                }
            }
        }

        return timings;
    }

    private static void printResults(List<TimingInfo> beforeTimings, List<TimingInfo> afterTimings) {
        // Calculate average time for each category
        double beforeAvg = calculateAverage(beforeTimings);
        double afterAvg = calculateAverage(afterTimings);

        // Calculate improvement percentage
        double improvementPct = beforeAvg > 0 ? ((beforeAvg - afterAvg) / beforeAvg) * 100 : 0;

        // Calculate min and max
        long beforeMin = Long.MAX_VALUE;
        long beforeMax = Long.MIN_VALUE;
        long afterMin = Long.MAX_VALUE;
        long afterMax = Long.MIN_VALUE;

        for (TimingInfo info : beforeTimings) {
            beforeMin = Math.min(beforeMin, info.timeInMs);
            beforeMax = Math.max(beforeMax, info.timeInMs);
        }

        for (TimingInfo info : afterTimings) {
            afterMin = Math.min(afterMin, info.timeInMs);
            afterMax = Math.max(afterMax, info.timeInMs);
        }

        // Print summary
        System.out.println("\n===== PERFORMANCE COMPARISON =====");
        System.out.println("Metric       | Before Upgrade | After Upgrade | Improvement (%)");
        System.out.println("----------------------------------------------------------");
        System.out.printf("Minimum Time | %-14d | %-13d | N/A\n", beforeMin, afterMin);
        System.out.printf("Maximum Time | %-14d | %-13d | N/A\n", beforeMax, afterMax);
        System.out.printf("Average Time | %-14.2f | %-13.2f | %.2f%%\n", beforeAvg, afterAvg, improvementPct);
        System.out.println("----------------------------------------------------------");

        // Print detailed comparison
        System.out.println("\n===== DETAILED TIMING VALUES =====");
        System.out.println("Before Upgrade Times (ms):");
        for (TimingInfo timing : beforeTimings) {
            System.out.println("  - " + timing.timeInMs);
        }

        System.out.println("\nAfter Upgrade Times (ms):");
        for (TimingInfo timing : afterTimings) {
            System.out.println("  - " + timing.timeInMs);
        }
    }

    private static double calculateAverage(List<TimingInfo> timings) {
        if (timings.isEmpty()) {
            return 0;
        }

        long sum = 0;
        for (TimingInfo timing : timings) {
            sum += timing.timeInMs;
        }

        return (double) sum / timings.size();
    }
}

Enter fullscreen mode Exit fullscreen mode
Collapse
 
gchar profile image
Charlie Fubon

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SimpleLogAnalyzer {

    // Simple class to store timing information
    static class TimingInfo {
        String operation;
        long timeInMs;

        TimingInfo(String operation, long timeInMs) {
            this.operation = operation;
            this.timeInMs = timeInMs;
        }
    }

    // Simple class to store statistics
    static class Statistics {
        long min = Long.MAX_VALUE;
        long max = Long.MIN_VALUE;
        long sum = 0;
        int count = 0;

        void addValue(long value) {
            min = Math.min(min, value);
            max = Math.max(max, value);
            sum += value;
            count++;
        }

        double getAverage() {
            return count > 0 ? (double) sum / count : 0;
        }
    }

    public static void main(String[] args) {
        // File paths - use current directory
        String[] beforeFiles = {"Jan29log.txt", "Jan30log.txt", "Jan31log.txt"};
        String[] afterFiles = {"Feb25thlog.txt", "Feb26thlog.txt", "Feb27thlog.txt"};

        // Check if files exist
        System.out.println("Checking files:");
        for (String filename : beforeFiles) {
            File f = new File(filename);
            System.out.println(filename + " exists: " + f.exists());
        }
        for (String filename : afterFiles) {
            File f = new File(filename);
            System.out.println(filename + " exists: " + f.exists());
        }

        try {
            // Process before and after files
            List<TimingInfo> beforeTimings = new ArrayList<>();
            List<TimingInfo> afterTimings = new ArrayList<>();

            // Process before files
            for (String filename : beforeFiles) {
                File file = new File(filename);
                if (file.exists()) {
                    List<TimingInfo> timings = processFile(file);
                    beforeTimings.addAll(timings);
                    System.out.println("Found " + timings.size() + " timing entries in " + filename);
                }
            }

            // Process after files
            for (String filename : afterFiles) {
                File file = new File(filename);
                if (file.exists()) {
                    List<TimingInfo> timings = processFile(file);
                    afterTimings.addAll(timings);
                    System.out.println("Found " + timings.size() + " timing entries in " + filename);
                }
            }

            // Calculate statistics
            Map<String, Statistics> beforeStats = calculateStats(beforeTimings);
            Map<String, Statistics> afterStats = calculateStats(afterTimings);

            // Print comparison
            printComparison(beforeStats, afterStats);

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

    private static List<TimingInfo> processFile(File file) throws IOException {
        List<TimingInfo> timings = new ArrayList<>();

        // Simple pattern to match "completed in X ms" or "is X ms"
        Pattern initPattern = Pattern.compile("initialization completed in (\\d+) ms");
        Pattern genericPattern = Pattern.compile("(\\d+) ms");

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

            while ((line = reader.readLine()) != null) {
                lineCount++;

                // Print sample lines for debugging
                if (lineCount <= 5) {
                    System.out.println("Sample line " + lineCount + ": " + line);
                }

                // Try to match initialization pattern
                Matcher initMatcher = initPattern.matcher(line);
                if (initMatcher.find()) {
                    long time = Long.parseLong(initMatcher.group(1));
                    timings.add(new TimingInfo("Initialization", time));
                    continue;
                }

                // If "Time taken" is in the line, extract operation and time
                if (line.contains("Time taken")) {
                    Matcher genericMatcher = genericPattern.matcher(line);
                    if (genericMatcher.find()) {
                        long time = Long.parseLong(genericMatcher.group(1));
                        timings.add(new TimingInfo("Method Execution", time));
                    }
                }
            }

            System.out.println("Processed " + lineCount + " lines in " + file.getName());
        }

        return timings;
    }

    private static Map<String, Statistics> calculateStats(List<TimingInfo> timings) {
        Map<String, Statistics> stats = new HashMap<>();

        for (TimingInfo timing : timings) {
            Statistics stat = stats.get(timing.operation);
            if (stat == null) {
                stat = new Statistics();
                stats.put(timing.operation, stat);
            }
            stat.addValue(timing.timeInMs);
        }

        return stats;
    }

    private static void printComparison(Map<String, Statistics> beforeStats, Map<String, Statistics> afterStats) {
        System.out.println("\n===== PERFORMANCE COMPARISON =====");
        System.out.println("Operation | Before | After | Improvement (%)");
        System.out.println("----------------------------------------");

        for (String operation : beforeStats.keySet()) {
            Statistics before = beforeStats.get(operation);
            Statistics after = afterStats.get(operation);

            if (after == null) {
                System.out.println(operation + " | " + before.getAverage() + " ms | N/A | N/A");
                continue;
            }

            double beforeAvg = before.getAverage();
            double afterAvg = after.getAverage();
            double improvement = beforeAvg > 0 ? ((beforeAvg - afterAvg) / beforeAvg) * 100 : 0;

            System.out.printf("%s | %.2f ms | %.2f ms | %.2f%%\n", 
                    operation, beforeAvg, afterAvg, improvement);
        }

        // Check for operations only in after
        for (String operation : afterStats.keySet()) {
            if (!beforeStats.containsKey(operation)) {
                Statistics after = afterStats.get(operation);
                System.out.println(operation + " | N/A | " + after.getAverage() + " ms | N/A");
            }
        }

        System.out.println("======================================");
    }
}

Enter fullscreen mode Exit fullscreen mode
Collapse
 
gchar profile image
Charlie Fubon

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class LogAnalyzer {

    // More flexible patterns to match timing information
    private static final Pattern[] PATTERNS = {
        // Original patterns
        Pattern.compile("WebApplicationContext: initialization completed in (\\d+) ms"),
        Pattern.compile("Time taken for\\*\\|\\*(.*?)\\*\\|\\*is\\*\\|\\*(\\d+)\\*\\|\\*ms"),

        // Additional patterns for other potential formats
        Pattern.compile("initialization completed in (\\d+) ms"), 
        Pattern.compile("Time taken for(.*?)is(\\d+)ms"), 
        Pattern.compile("Time taken for(.*?)is(\\d+) ms"),
        Pattern.compile("\\*Time taken for\\*(.*?)\\*is\\*(\\d+)\\*ms"),
        Pattern.compile("Time taken for\\|\\*(.*?)\\*\\|is\\|\\*(\\d+)\\*\\|ms"),

        // More variations of the Time Taken pattern
        Pattern.compile("Time taken for\\|(.*?)\\|is\\|(\\d+)\\|ms"),
        Pattern.compile("Time taken for (.*?) is (\\d+) ms"),
        Pattern.compile("Time taken for (.*?)\\s+is\\s+(\\d+)\\s+ms"),

        // Patterns to match different delimiters
        Pattern.compile("completed in (\\d+)ms"),
        Pattern.compile("took (\\d+) ms"),
        Pattern.compile("duration: (\\d+)ms"),
        Pattern.compile("elapsed: (\\d+)ms"),

        // Generic pattern for any number followed by "ms"
        Pattern.compile("(\\d+)\\s*ms")
    };

    // Data structure to store timing information
    static class TimingInfo {
        String operation;
        long timeInMs;

        TimingInfo(String operation, long timeInMs) {
            this.operation = operation;
            this.timeInMs = timeInMs;
        }
    }

    // Data structure to store statistics
    static class Statistics {
        long min = Long.MAX_VALUE;
        long max = Long.MIN_VALUE;
        long sum = 0;
        int count = 0;

        void addValue(long value) {
            min = Math.min(min, value);
            max = Math.max(max, value);
            sum += value;
            count++;
        }

        double getAverage() {
            return count > 0 ? (double) sum / count : 0;
        }

        @Override
        public String toString() {
            return String.format("Count: %d, Min: %d ms, Max: %d ms, Avg: %.2f ms, Total: %d ms", 
                    count, min, max, getAverage(), sum);
        }
    }

    public static void main(String[] args) {
        // File paths - change these to match your directory structure if needed
        String beforeUpgradeDirPath = "."; // Current directory
        String afterUpgradeDirPath = ".";  // Current directory

        // Files to process - modify these to match your file names
        // These files must be in the current directory
        String[] beforeFiles = {"Jan29log.txt", "Jan30log.txt", "Jan31log.txt"};
        String[] afterFiles = {"Feb25thlog.txt", "Feb26thlog.txt", "Feb27thlog.txt"};

        // Print file existence check for debugging
        System.out.println("FILE EXISTENCE CHECK:");
        for (String file : beforeFiles) {
            File f = new File(file);
            System.out.println(file + " exists: " + f.exists() + ", size: " + (f.exists() ? f.length() + " bytes" : "N/A"));
        }
        for (String file : afterFiles) {
            File f = new File(file);
            System.out.println(file + " exists: " + f.exists() + ", size: " + (f.exists() ? f.length() + " bytes" : "N/A"));
        }

        try {
            // Process the specific files instead of directories
            Map<String, Statistics> beforeStats = processSpecificFiles(beforeFiles);
            Map<String, Statistics> afterStats = processSpecificFiles(afterFiles);

            // Print detailed stats for debugging
            System.out.println("\nBEFORE UPGRADE STATS:");
            printStats(beforeStats);

            System.out.println("\nAFTER UPGRADE STATS:");
            printStats(afterStats);

            // Print comparison
            printComparison(beforeStats, afterStats);

        } catch (IOException e) {
            System.err.println("Error processing log files: " + e.getMessage());
            e.printStackTrace();
        }
    }

    private static Map<String, Statistics> processSpecificFiles(String[] filenames) throws IOException {
        List<TimingInfo> allTimings = new ArrayList<>();

        for (String filename : filenames) {
            File file = new File(filename);
            if (!file.exists()) {
                System.out.println("Warning: File not found: " + filename);
                continue;
            }

            System.out.println("Processing file: " + filename);
            List<TimingInfo> timings = extractTimings(file);
            System.out.println("  Found " + timings.size() + " timing entries");

            // Print first few entries for debugging
            if (!timings.isEmpty()) {
                System.out.println("  Sample entries:");
                for (int i = 0; i < Math.min(3, timings.size()); i++) {
                    TimingInfo info = timings.get(i);
                    System.out.println("    - " + info.operation + ": " + info.timeInMs + " ms");
                }
            }

            allTimings.addAll(timings);
        }

        // If no timings found, add a default entry to prevent empty comparison
        if (allTimings.isEmpty()) {
            System.out.println("WARNING: No timing data found in the files! Adding dummy data.");

            // Find the first line containing "ms" and print some context
            for (String filename : filenames) {
                File file = new File(filename);
                if (!file.exists()) continue;

                try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
                    String line;
                    int lineCount = 0;
                    System.out.println("\nSampling lines containing 'ms' from " + filename + ":");
                    while ((line = reader.readLine()) != null && lineCount < 10) {
                        if (line.contains("ms")) {
                            System.out.println("  " + line);
                            lineCount++;
                        }
                    }
                }
            }

            // Add dummy data to prevent empty output
            allTimings.add(new TimingInfo("Sample Operation", 1000));
        }

        return calculateStatistics(allTimings);
    }

    private static List<TimingInfo> extractTimings(File logFile) throws IOException {
        List<TimingInfo> timings = new ArrayList<>();
        Map<String, Integer> patternMatchCounts = new HashMap<>();
        int totalLines = 0;
        int linesWithMs = 0;

        System.out.println("\n==== DETAILED DEBUG INFO FOR: " + logFile.getName() + " ====");

        // Initialize pattern match counters
        for (int i = 0; i < PATTERNS.length; i++) {
            patternMatchCounts.put("Pattern " + i, 0);
        }

        try (BufferedReader reader = new BufferedReader(new FileReader(logFile))) {
            String line;
            int lineNum = 0;

            // Print first few lines of file to see content format
            System.out.println("  First 5 lines of file for reference:");
            BufferedReader previewReader = new BufferedReader(new FileReader(logFile));
            for (int i = 0; i < 5; i++) {
                String previewLine = previewReader.readLine();
                if (previewLine != null) {
                    System.out.println("    " + (i+1) + ": " + previewLine);
                }
            }
            previewReader.close();

            // Process the file
            System.out.println("\n  Processing lines:");
            while ((line = reader.readLine()) != null) {
                lineNum++;
                totalLines++;

                // Debug every 1000 lines to show progress
                if (lineNum % 1000 == 0) {
                    System.out.println("    Processed " + lineNum + " lines...");
                }

                boolean foundMatch = false;

                // Check if line contains "ms" for debugging
                if (line.contains("ms") || line.contains(" MS ") || line.contains(" Ms ")) {
                    linesWithMs++;

                    // Print every line containing "ms" for debug (limit to first 20)
                    if (linesWithMs <= 20) {
                        System.out.println("    MS Line " + lineNum + ": " + line);
                    }

                    // Try each pattern on this line
                    for (int i = 0; i < PATTERNS.length; i++) {
                        Pattern pattern = PATTERNS[i];
                        Matcher matcher = pattern.matcher(line);

                        if (matcher.find()) {
                            foundMatch = true;
                            patternMatchCounts.put("Pattern " + i, patternMatchCounts.get("Pattern " + i) + 1);

                            // Debug output
                            if (timings.size() < 10) {
                                System.out.println("      MATCH FOUND with pattern " + i + ": " + pattern.pattern());
                                for (int g = 0; g <= matcher.groupCount(); g++) {
                                    System.out.println("        Group " + g + ": " + matcher.group(g));
                                }
                            }

                            try {
                                // Different handling based on pattern
                                if (i <= 1) {
                                    // Original patterns
                                    if (i == 0) {
                                        // WebApp init pattern
                                        long time = Long.parseLong(matcher.group(1));
                                        timings.add(new TimingInfo("WebApplicationContext Initialization", time));
                                    } else {
                                        // Method execution pattern
                                        String methodName = matcher.group(1).trim();
                                        long time = Long.parseLong(matcher.group(2));
                                        timings.add(new TimingInfo(methodName, time));
                                    }
                                } else if (i == 2) {
                                    // Just initialization pattern
                                    long time = Long.parseLong(matcher.group(1));
                                    timings.add(new TimingInfo("Context Initialization", time));
                                } else if (i >= 3 && i <= 5) {
                                    // Alternative method patterns
                                    String methodName = "Unknown Method";
                                    if (matcher.groupCount() >= 1) {
                                        methodName = matcher.group(1).trim();
                                        if (methodName.isEmpty()) methodName = "Unknown Method";
                                    }

                                    long time = Long.parseLong(matcher.group(matcher.groupCount()));
                                    timings.add(new TimingInfo(methodName, time));
                                } else if (i == 6) {
                                    // Generic ms pattern - check if line has context
                                    String operation = "Unknown Operation";
                                    if (line.contains("initialization")) {
                                        operation = "Initialization";
                                    } else if (line.contains("WebApplication")) {
                                        operation = "WebApplication";
                                    } else if (line.contains("execution")) {
                                        operation = "Method Execution";
                                    } else if (line.contains("Time taken")) {
                                        operation = line.substring(0, Math.min(line.length(), 50)).trim();
                                    }

                                    long time = Long.parseLong(matcher.group(1));
                                    timings.add(new TimingInfo(operation, time));
                                }
                            } catch (Exception e) {
                                System.out.println("      ERROR processing match: " + e.getMessage());
                                System.out.println("      Line: " + line);
                            }

                            // Only use first matching pattern
                            break;
                        }
                    }

                    // If line contains "ms" but no pattern matched, print it for debugging
                    if (!foundMatch && linesWithMs <= 30) {
                        System.out.println("      NO MATCH for Line " + lineNum + ": " + line);
                        // Try to show why each pattern failed
                        for (int i = 0; i < PATTERNS.length; i++) {
                            Pattern pattern = PATTERNS[i];
                            System.out.println("        Pattern " + i + ": " + pattern.pattern() + " - Did not match");
                        }
                    }
                }
            }
        }

        // Print summary statistics
        System.out.println("\n  File Summary:");
        System.out.println("    Total lines processed: " + totalLines);
        System.out.println("    Lines containing 'ms': " + linesWithMs);
        System.out.println("    Total timing entries found: " + timings.size());

        // Print which patterns matched and how many times
        System.out.println("\n  Pattern Match Counts:");
        for (Map.Entry<String, Integer> entry : patternMatchCounts.entrySet()) {
            if (entry.getValue() > 0) {
                System.out.println("    " + entry.getKey() + ": " + entry.getValue() + " matches");
            }
        }

        // Show sample of extracted timings
        if (!timings.isEmpty()) {
            System.out.println("\n  Sample timing entries extracted:");
            for (int i = 0; i < Math.min(10, timings.size()); i++) {
                TimingInfo info = timings.get(i);
                System.out.println("    - " + info.operation + ": " + info.timeInMs + " ms");
            }
        } else {
            System.out.println("\n  NO TIMING ENTRIES FOUND IN THIS FILE!");
            System.out.println("\n  Trying new custom pattern extraction...");

            // Second pass with extreme pattern matching for numbers
            try (BufferedReader reader = new BufferedReader(new FileReader(logFile))) {
                String line;
                while ((line = reader.readLine()) != null) {
                    if (line.contains("ms") || line.contains("MS") || line.contains("milliseconds")) {
                        // Look for any number immediately before "ms"
                        Pattern extremePattern = Pattern.compile("(\\d+)[\\s]*ms");
                        Matcher matcher = extremePattern.matcher(line);
                        if (matcher.find()) {
                            String context = line.length() > 50 ? line.substring(0, 50) + "..." : line;
                            long time = Long.parseLong(matcher.group(1));
                            timings.add(new TimingInfo("Context: " + context, time));
                            System.out.println("    FOUND with extreme pattern: " + time + "ms - Context: " + context);
                        }
                    }
                }
            } catch (Exception e) {
                System.out.println("    Error in second pass: " + e.getMessage());
            }
        }

        System.out.println("==== END DEBUG INFO ====\n");
        return timings;
    }

    private static Map<String, Statistics> calculateStatistics(List<TimingInfo> timings) {
        Map<String, Statistics> statsByOperation = new HashMap<>();

        for (TimingInfo timing : timings) {
            statsByOperation.computeIfAbsent(timing.operation, k -> new Statistics())
                    .addValue(timing.timeInMs);
        }

        return statsByOperation;
    }

    private static void printStats(Map<String, Statistics> stats) {
        for (Map.Entry<String, Statistics> entry : stats.entrySet()) {
            System.out.println(entry.getKey() + ": " + entry.getValue());
        }
    }

    private static void printComparison(Map<String, Statistics> beforeStats, Map<String, Statistics> afterStats) {
        System.out.println("\n===================== PERFORMANCE COMPARISON =====================");
        System.out.println("Operation | Before Upgrade | After Upgrade | Improvement (%)");
        System.out.println("----------------------------------------------------------------");

        // Combine all keys
        Set<String> allOperations = new HashSet<>();
        allOperations.addAll(beforeStats.keySet());
        allOperations.addAll(afterStats.keySet());

        for (String operation : allOperations) {
            Statistics before = beforeStats.getOrDefault(operation, new Statistics());
            Statistics after = afterStats.getOrDefault(operation, new Statistics());

            // Skip if no data for comparison
            if (before.count == 0 || after.count == 0) {
                System.out.println(operation + " - Incomplete data, skipping comparison");
                continue;
            }

            double beforeAvg = before.getAverage();
            double afterAvg = after.getAverage();
            double improvementPct = beforeAvg > 0 ? ((beforeAvg - afterAvg) / beforeAvg) * 100 : 0;

            System.out.printf("%-40s | %-15s | %-15s | %s%n", 
                    operation,
                    String.format("%.2f ms", beforeAvg),
                    String.format("%.2f ms", afterAvg),
                    String.format("%+.2f%%", improvementPct));
        }

        System.out.println("================================================================");
    }
}

Enter fullscreen mode Exit fullscreen mode
Collapse
 
gchar profile image
Charlie Fubon

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class LogPerformanceAnalyzer {

    // Define patterns to match in log files
    private static final Pattern WEBAPP_INIT_PATTERN = Pattern.compile("WebApplicationContext: initialization completed in (\\d+) ms");
    private static final Pattern METHOD_EXECUTION_PATTERN = Pattern.compile("Time taken for\\*\\|\\*(.*?)\\*\\|\\*is\\*\\|\\*(\\d+)\\*\\|\\*ms");

    // Data structure to store timing information
    static class TimingInfo {
        String operation;
        long timeInMs;

        TimingInfo(String operation, long timeInMs) {
            this.operation = operation;
            this.timeInMs = timeInMs;
        }
    }

    // Data structure to store statistics
    static class Statistics {
        long min = Long.MAX_VALUE;
        long max = Long.MIN_VALUE;
        long sum = 0;
        int count = 0;

        void addValue(long value) {
            min = Math.min(min, value);
            max = Math.max(max, value);
            sum += value;
            count++;
        }

        double getAverage() {
            return count > 0 ? (double) sum / count : 0;
        }

        @Override
        public String toString() {
            return String.format("Count: %d, Min: %d ms, Max: %d ms, Avg: %.2f ms, Total: %d ms", 
                    count, min, max, getAverage(), sum);
        }
    }

    public static void main(String[] args) {
        // Replace with your actual directory paths
        String beforeUpgradeDirPath = "before_upgrade_logs";
        String afterUpgradeDirPath = "after_upgrade_logs";

        // Analyze logs and print comparison
        try {
            Map<String, Statistics> beforeStats = analyzeLogs(beforeUpgradeDirPath);
            Map<String, Statistics> afterStats = analyzeLogs(afterUpgradeDirPath);

            printComparison(beforeStats, afterStats);
        } catch (IOException e) {
            System.err.println("Error processing log files: " + e.getMessage());
            e.printStackTrace();
        }
    }

    private static Map<String, Statistics> analyzeLogs(String dirPath) throws IOException {
        File dir = new File(dirPath);
        File[] logFiles = dir.listFiles((d, name) -> name.toLowerCase().endsWith(".log"));

        if (logFiles == null || logFiles.length == 0) {
            throw new IOException("No log files found in directory: " + dirPath);
        }

        List<TimingInfo> allTimings = new ArrayList<>();

        for (File logFile : logFiles) {
            System.out.println("Processing file: " + logFile.getName());
            List<TimingInfo> timings = extractTimings(logFile);
            allTimings.addAll(timings);
        }

        return calculateStatistics(allTimings);
    }

    private static List<TimingInfo> extractTimings(File logFile) throws IOException {
        List<TimingInfo> timings = new ArrayList<>();

        try (BufferedReader reader = new BufferedReader(new FileReader(logFile))) {
            String line;
            while ((line = reader.readLine()) != null) {
                // Try to match WebApp initialization pattern
                Matcher webappMatcher = WEBAPP_INIT_PATTERN.matcher(line);
                if (webappMatcher.find()) {
                    long time = Long.parseLong(webappMatcher.group(1));
                    timings.add(new TimingInfo("WebApplicationContext Initialization", time));
                    continue;
                }

                // Try to match method execution pattern
                Matcher methodMatcher = METHOD_EXECUTION_PATTERN.matcher(line);
                if (methodMatcher.find()) {
                    String methodName = methodMatcher.group(1);
                    long time = Long.parseLong(methodMatcher.group(2));
                    timings.add(new TimingInfo(methodName, time));
                }
            }
        }

        return timings;
    }

    private static Map<String, Statistics> calculateStatistics(List<TimingInfo> timings) {
        Map<String, Statistics> statsByOperation = new HashMap<>();

        for (TimingInfo timing : timings) {
            statsByOperation.computeIfAbsent(timing.operation, k -> new Statistics())
                    .addValue(timing.timeInMs);
        }

        return statsByOperation;
    }

    private static void printComparison(Map<String, Statistics> beforeStats, Map<String, Statistics> afterStats) {
        System.out.println("\n===================== PERFORMANCE COMPARISON =====================");
        System.out.println("Operation | Before Upgrade | After Upgrade | Improvement (%)");
        System.out.println("----------------------------------------------------------------");

        // Combine all keys
        Set<String> allOperations = new HashSet<>();
        allOperations.addAll(beforeStats.keySet());
        allOperations.addAll(afterStats.keySet());

        for (String operation : allOperations) {
            Statistics before = beforeStats.getOrDefault(operation, new Statistics());
            Statistics after = afterStats.getOrDefault(operation, new Statistics());

            // Skip if no data for comparison
            if (before.count == 0 || after.count == 0) {
                continue;
            }

            double beforeAvg = before.getAverage();
            double afterAvg = after.getAverage();
            double improvementPct = beforeAvg > 0 ? ((beforeAvg - afterAvg) / beforeAvg) * 100 : 0;

            System.out.printf("%-40s | %-15s | %-15s | %s%n", 
                    operation,
                    String.format("%.2f ms", beforeAvg),
                    String.format("%.2f ms", afterAvg),
                    String.format("%+.2f%%", improvementPct));
        }

        System.out.println("================================================================");
    }

    // Utility method to run the analyzer with custom directories
    public static void analyzeCustomDirectories(String beforeDir, String afterDir) throws IOException {
        Map<String, Statistics> beforeStats = analyzeLogs(beforeDir);
        Map<String, Statistics> afterStats = analyzeLogs(afterDir);

        printComparison(beforeStats, afterStats);
    }
}

Enter fullscreen mode Exit fullscreen mode