From 26de463419ea05dc5b14fa35fa6d752c9ee5b0c9 Mon Sep 17 00:00:00 2001 From: Dominik Stadler Date: Thu, 5 May 2016 12:22:16 +0000 Subject: [PATCH] Refactor SSPerformanceTest into a few methods to make it easier to use a profiler git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1742420 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/ss/examples/SSPerformanceTest.java | 120 ++++++++++-------- 1 file changed, 67 insertions(+), 53 deletions(-) diff --git a/src/examples/src/org/apache/poi/ss/examples/SSPerformanceTest.java b/src/examples/src/org/apache/poi/ss/examples/SSPerformanceTest.java index 4d6dee448..eb7532c7b 100644 --- a/src/examples/src/org/apache/poi/ss/examples/SSPerformanceTest.java +++ b/src/examples/src/org/apache/poi/ss/examples/SSPerformanceTest.java @@ -44,6 +44,17 @@ public class SSPerformanceTest { int cols = parseInt(args[2], "Failed to parse cols value as integer"); boolean saveFile = parseInt(args[3], "Failed to parse saveFile value as integer") != 0; + addContent(workBook, isHType, rows, cols); + + if (saveFile) { + String fileName = type + "_" + rows + "_" + cols + "." + getFileSuffix(args[0]); + saveFile(workBook, fileName); + } + long timeFinished = System.currentTimeMillis(); + System.out.println("Elapsed " + (timeFinished-timeStarted)/1000 + " seconds"); + } + + private static void addContent(Workbook workBook, boolean isHType, int rows, int cols) { Map styles = createStyles(workBook); Sheet sheet = workBook.createSheet("Main Sheet"); @@ -68,63 +79,66 @@ public class SSPerformanceTest { Row row = sheet.createRow(rowIndexInSheet); for (int colIndex = 0; colIndex < cols; colIndex++) { - Cell cell = row.createCell(colIndex); - String address = new CellReference(cell).formatAsString(); - switch (colIndex){ - case 0: - // column A: default number format - cell.setCellValue(value++); - break; - case 1: - // column B: #,##0 - cell.setCellValue(value++); - cell.setCellStyle(styles.get("#,##0.00")); - break; - case 2: - // column C: $#,##0.00 - cell.setCellValue(value++); - cell.setCellStyle(styles.get("$#,##0.00")); - break; - case 3: - // column D: red bold text on yellow background - cell.setCellValue(address); - cell.setCellStyle(styles.get("red-bold")); - break; - case 4: - // column E: boolean - // TODO booleans are shown as 1/0 instead of TRUE/FALSE - cell.setCellValue(rowIndex % 2 == 0); - break; - case 5: - // column F: date / time - cell.setCellValue(calendar); - cell.setCellStyle(styles.get("m/d/yyyy")); - calendar.roll(Calendar.DAY_OF_YEAR, -1); - break; - case 6: - // column F: formula - // TODO formulas are not yet supported in SXSSF - //cell.setCellFormula("SUM(A" + (rowIndex+1) + ":E" + (rowIndex+1)+ ")"); - //break; - default: - cell.setCellValue(value++); - break; - } + value = populateCell(styles, value, calendar, rowIndex, row, colIndex); } rowIndexInSheet++; } - if (saveFile) { - String fileName = type + "_" + rows + "_" + cols + "." + getFileSuffix(args[0]); - try { - FileOutputStream out = new FileOutputStream(fileName); - workBook.write(out); - out.close(); - } catch (IOException ioe) { - System.err.println("Error: failed to write to file \"" + fileName + "\", reason=" + ioe.getMessage()); - } + } + + private static double populateCell(Map styles, double value, Calendar calendar, int rowIndex, Row row, int colIndex) { + Cell cell = row.createCell(colIndex); + String address = new CellReference(cell).formatAsString(); + switch (colIndex){ + case 0: + // column A: default number format + cell.setCellValue(value++); + break; + case 1: + // column B: #,##0 + cell.setCellValue(value++); + cell.setCellStyle(styles.get("#,##0.00")); + break; + case 2: + // column C: $#,##0.00 + cell.setCellValue(value++); + cell.setCellStyle(styles.get("$#,##0.00")); + break; + case 3: + // column D: red bold text on yellow background + cell.setCellValue(address); + cell.setCellStyle(styles.get("red-bold")); + break; + case 4: + // column E: boolean + // TODO booleans are shown as 1/0 instead of TRUE/FALSE + cell.setCellValue(rowIndex % 2 == 0); + break; + case 5: + // column F: date / time + cell.setCellValue(calendar); + cell.setCellStyle(styles.get("m/d/yyyy")); + calendar.roll(Calendar.DAY_OF_YEAR, -1); + break; + case 6: + // column F: formula + // TODO formulas are not yet supported in SXSSF + //cell.setCellFormula("SUM(A" + (rowIndex+1) + ":E" + (rowIndex+1)+ ")"); + //break; + default: + cell.setCellValue(value++); + break; + } + return value; + } + + private static void saveFile(Workbook workBook, String fileName) { + try { + FileOutputStream out = new FileOutputStream(fileName); + workBook.write(out); + out.close(); + } catch (IOException ioe) { + System.err.println("Error: failed to write to file \"" + fileName + "\", reason=" + ioe.getMessage()); } - long timeFinished = System.currentTimeMillis(); - System.out.println("Elapsed " + (timeFinished-timeStarted)/1000 + " seconds"); } static Map createStyles(Workbook wb) {