|
| 1 | +package org.dhatim.fastexcel; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | + |
| 5 | +import java.io.ByteArrayOutputStream; |
| 6 | +import java.io.IOException; |
| 7 | +import java.util.Map.Entry; |
| 8 | +import java.util.concurrent.ConcurrentHashMap; |
| 9 | +import java.util.concurrent.ConcurrentMap; |
| 10 | + |
| 11 | +import static org.assertj.core.api.Assertions.assertThat; |
| 12 | + |
| 13 | +class StyleCacheBeforeAfterTest { |
| 14 | + |
| 15 | + private static class OldStyleCache { |
| 16 | + private final ConcurrentMap<Style, Integer> styles = new ConcurrentHashMap<>(); |
| 17 | + |
| 18 | + int mergeAndCacheStyleOldWay(int currentStyle, String numberingFormat, Font font, |
| 19 | + Fill fill, Border border, Alignment alignment, Protection protection, |
| 20 | + StyleCacheHelper helper) { |
| 21 | + Style original = styles.entrySet().stream() |
| 22 | + .filter(e -> e.getValue().equals(currentStyle)) |
| 23 | + .map(Entry::getKey) |
| 24 | + .findFirst() |
| 25 | + .orElse(null); |
| 26 | + |
| 27 | + Style s = new Style(original, |
| 28 | + helper.cacheValueFormatting(numberingFormat), |
| 29 | + helper.cacheFont(font), |
| 30 | + helper.cacheFill(fill), |
| 31 | + helper.cacheBorder(border), |
| 32 | + alignment, protection); |
| 33 | + |
| 34 | + return styles.computeIfAbsent(s, k -> styles.size()); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + private static class NewStyleCache { |
| 39 | + private final ConcurrentMap<Style, Integer> styles = new ConcurrentHashMap<>(); |
| 40 | + private final ConcurrentMap<Integer, Style> styleIndexToStyle = new ConcurrentHashMap<>(); |
| 41 | + |
| 42 | + int mergeAndCacheStyleNewWay(int currentStyle, String numberingFormat, Font font, |
| 43 | + Fill fill, Border border, Alignment alignment, Protection protection, |
| 44 | + StyleCacheHelper helper) { |
| 45 | + Style original = styleIndexToStyle.get(currentStyle); |
| 46 | + |
| 47 | + Style s = new Style(original, |
| 48 | + helper.cacheValueFormatting(numberingFormat), |
| 49 | + helper.cacheFont(font), |
| 50 | + helper.cacheFill(fill), |
| 51 | + helper.cacheBorder(border), |
| 52 | + alignment, protection); |
| 53 | + |
| 54 | + Integer index = styles.computeIfAbsent(s, k -> styles.size()); |
| 55 | + styleIndexToStyle.putIfAbsent(index, s); |
| 56 | + return index; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + private interface StyleCacheHelper { |
| 61 | + int cacheValueFormatting(String s); |
| 62 | + int cacheFont(Font f); |
| 63 | + int cacheFill(Fill f); |
| 64 | + int cacheBorder(Border b); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + void compareOldVsNewImplementation() { |
| 69 | + int numStyles = 1000; |
| 70 | + int mergesPerStyle = 10; |
| 71 | + |
| 72 | + StyleCacheHelper helper = new StyleCacheHelper() { |
| 73 | + @Override |
| 74 | + public int cacheValueFormatting(String s) { return s != null ? 1 : 0; } |
| 75 | + @Override |
| 76 | + public int cacheFont(Font f) { return 0; } |
| 77 | + @Override |
| 78 | + public int cacheFill(Fill f) { return 0; } |
| 79 | + @Override |
| 80 | + public int cacheBorder(Border b) { return 0; } |
| 81 | + }; |
| 82 | + |
| 83 | + OldStyleCache oldCache = new OldStyleCache(); |
| 84 | + NewStyleCache newCache = new NewStyleCache(); |
| 85 | + |
| 86 | + for (int i = 0; i < 100; i++) { |
| 87 | + oldCache.mergeAndCacheStyleOldWay(0, null, Font.DEFAULT, Fill.NONE, Border.NONE, null, null, helper); |
| 88 | + newCache.mergeAndCacheStyleNewWay(0, null, Font.DEFAULT, Fill.NONE, Border.NONE, null, null, helper); |
| 89 | + } |
| 90 | + |
| 91 | + int currentStyle = 0; |
| 92 | + for (int i = 0; i < numStyles; i++) { |
| 93 | + currentStyle = oldCache.mergeAndCacheStyleOldWay( |
| 94 | + currentStyle, "fmt" + i, Font.DEFAULT, Fill.NONE, Border.NONE, null, null, helper); |
| 95 | + newCache.mergeAndCacheStyleNewWay( |
| 96 | + currentStyle, "fmt" + i, Font.DEFAULT, Fill.NONE, Border.NONE, null, null, helper); |
| 97 | + } |
| 98 | + |
| 99 | + long oldStart = System.nanoTime(); |
| 100 | + currentStyle = 0; |
| 101 | + for (int styleIdx = 0; styleIdx < numStyles; styleIdx++) { |
| 102 | + for (int merge = 0; merge < mergesPerStyle; merge++) { |
| 103 | + currentStyle = oldCache.mergeAndCacheStyleOldWay( |
| 104 | + currentStyle, |
| 105 | + "format" + (styleIdx % 10), |
| 106 | + Font.DEFAULT, |
| 107 | + Fill.NONE, |
| 108 | + Border.NONE, |
| 109 | + null, null, helper); |
| 110 | + } |
| 111 | + } |
| 112 | + long oldDuration = System.nanoTime() - oldStart; |
| 113 | + |
| 114 | + long newStart = System.nanoTime(); |
| 115 | + currentStyle = 0; |
| 116 | + for (int styleIdx = 0; styleIdx < numStyles; styleIdx++) { |
| 117 | + for (int merge = 0; merge < mergesPerStyle; merge++) { |
| 118 | + currentStyle = newCache.mergeAndCacheStyleNewWay( |
| 119 | + currentStyle, |
| 120 | + "format" + (styleIdx % 10), |
| 121 | + Font.DEFAULT, |
| 122 | + Fill.NONE, |
| 123 | + Border.NONE, |
| 124 | + null, null, helper); |
| 125 | + } |
| 126 | + } |
| 127 | + long newDuration = System.nanoTime() - newStart; |
| 128 | + |
| 129 | + System.out.println("\n=== Style Cache Performance Comparison ==="); |
| 130 | + System.out.printf("Styles in cache: %d%n", numStyles); |
| 131 | + System.out.printf("Merges per style: %d%n", mergesPerStyle); |
| 132 | + System.out.printf("Total operations: %d%n", numStyles * mergesPerStyle); |
| 133 | + System.out.println("\nOLD (O(n) linear search):"); |
| 134 | + System.out.printf(" Time: %d ms (%.2f ns per operation)%n", |
| 135 | + oldDuration / 1_000_000, (double) oldDuration / (numStyles * mergesPerStyle)); |
| 136 | + System.out.println("\nNEW (O(1) hash map lookup):"); |
| 137 | + System.out.printf(" Time: %d ms (%.2f ns per operation)%n", |
| 138 | + newDuration / 1_000_000, (double) newDuration / (numStyles * mergesPerStyle)); |
| 139 | + |
| 140 | + double speedup = (double) oldDuration / newDuration; |
| 141 | + System.out.printf("\nSpeedup: %.2fx faster%n", speedup); |
| 142 | + |
| 143 | + assertThat(newDuration).isLessThanOrEqualTo(oldDuration); |
| 144 | + assertThat(speedup).isGreaterThan(1.5); |
| 145 | + } |
| 146 | + |
| 147 | + @Test |
| 148 | + void demonstrateRealWorldScenario() throws IOException { |
| 149 | + int numUniqueStyles = 2000; |
| 150 | + |
| 151 | + System.out.println("\n=== Real-World Scenario: Many Unique Styles ==="); |
| 152 | + System.out.printf("Creating workbook with %d unique styles...%n", numUniqueStyles); |
| 153 | + |
| 154 | + long startTime = System.nanoTime(); |
| 155 | + |
| 156 | + try (ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 157 | + Workbook wb = new Workbook(out, "PerfTest", "1.0")) { |
| 158 | + |
| 159 | + Worksheet ws = wb.newWorksheet("Sheet 1"); |
| 160 | + |
| 161 | + for (int styleIdx = 0; styleIdx < numUniqueStyles; styleIdx++) { |
| 162 | + StyleSetter styleSetter = ws.range(styleIdx % 100, styleIdx % 10, |
| 163 | + (styleIdx % 100) + 1, (styleIdx % 10) + 1) |
| 164 | + .style() |
| 165 | + .fontName("Font" + (styleIdx % 50)) |
| 166 | + .fontSize(10 + (styleIdx % 20)) |
| 167 | + .fillColor(String.format("#%06X", styleIdx % 0xFFFFFF)); |
| 168 | + if (styleIdx % 3 == 0) { |
| 169 | + styleSetter.bold(); |
| 170 | + } |
| 171 | + if (styleIdx % 4 == 0) { |
| 172 | + styleSetter.italic(); |
| 173 | + } |
| 174 | + styleSetter.horizontalAlignment(styleIdx % 2 == 0 ? "center" : "left").set(); |
| 175 | + } |
| 176 | + |
| 177 | + for (int i = 0; i < 100; i++) { |
| 178 | + ws.value(i, 0, "Data " + i); |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + long duration = System.nanoTime() - startTime; |
| 183 | + long durationMs = duration / 1_000_000; |
| 184 | + |
| 185 | + System.out.printf("Completed in %d ms%n", durationMs); |
| 186 | + System.out.printf("Average: %.2f ms per style%n", (double) durationMs / numUniqueStyles); |
| 187 | + |
| 188 | + assertThat(durationMs).isLessThan(5000); |
| 189 | + } |
| 190 | +} |
0 commit comments