You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SSPerformanceTest.java 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * ====================================================================
  3. * Licensed to the Apache Software Foundation (ASF) under one or more
  4. * contributor license agreements. See the NOTICE file distributed with
  5. * this work for additional information regarding copyright ownership.
  6. * The ASF licenses this file to You under the Apache License, Version 2.0
  7. * (the "License"); you may not use this file except in compliance with
  8. * the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. * ====================================================================
  18. */
  19. package org.apache.poi.examples.ss;
  20. import java.io.FileOutputStream;
  21. import java.io.IOException;
  22. import java.util.Arrays;
  23. import java.util.Calendar;
  24. import java.util.HashMap;
  25. import java.util.Locale;
  26. import java.util.Map;
  27. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  28. import org.apache.poi.ooxml.POIXMLTypeLoader;
  29. import org.apache.poi.ss.usermodel.Cell;
  30. import org.apache.poi.ss.usermodel.CellStyle;
  31. import org.apache.poi.ss.usermodel.FillPatternType;
  32. import org.apache.poi.ss.usermodel.Font;
  33. import org.apache.poi.ss.usermodel.HorizontalAlignment;
  34. import org.apache.poi.ss.usermodel.IndexedColors;
  35. import org.apache.poi.ss.usermodel.Row;
  36. import org.apache.poi.ss.usermodel.Sheet;
  37. import org.apache.poi.ss.usermodel.VerticalAlignment;
  38. import org.apache.poi.ss.usermodel.Workbook;
  39. import org.apache.poi.ss.util.CellRangeAddress;
  40. import org.apache.poi.ss.util.CellReference;
  41. import org.apache.poi.util.LocaleUtil;
  42. import org.apache.poi.xssf.streaming.SXSSFWorkbook;
  43. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  44. @SuppressWarnings({"java:S106","java:S4823","java:S1192"})
  45. public final class SSPerformanceTest {
  46. private SSPerformanceTest() {}
  47. public static void main(String[] args) throws IOException {
  48. if (args.length < 4) {
  49. usage("need at least four command arguments");
  50. }
  51. String type = args[0];
  52. int rows = parseInt(args[1], "Failed to parse rows value as integer");
  53. int cols = parseInt(args[2], "Failed to parse cols value as integer");
  54. boolean saveFile = parseInt(args[3], "Failed to parse saveFile value as integer") != 0;
  55. boolean warmup = false;
  56. for(int arg = 4; arg < args.length;arg++) {
  57. if(args[arg].equals("--unsynchronized-xmlbeans")) {
  58. POIXMLTypeLoader.DEFAULT_XML_OPTIONS.setUnsynchronized();
  59. }
  60. if(args[arg].equals("--with-warmup-run")) {
  61. warmup = true;
  62. }
  63. }
  64. if(warmup) {
  65. System.out.println("Performing a warmup run first");
  66. runWithArgs(type, rows, cols, saveFile);
  67. }
  68. long timeStarted = System.currentTimeMillis();
  69. runWithArgs(type, rows, cols, saveFile);
  70. long timeFinished = System.currentTimeMillis();
  71. System.out.printf(Locale.ROOT, "Elapsed %.2f seconds for arguments %s\n", ((double)timeFinished - timeStarted) / 1000, Arrays.toString(args));
  72. }
  73. private static void runWithArgs(String type, int rows, int cols, boolean saveFile) throws IOException {
  74. try (Workbook workBook = createWorkbook(type)) {
  75. boolean isHType = workBook instanceof HSSFWorkbook;
  76. addContent(workBook, isHType, rows, cols);
  77. if (saveFile) {
  78. String fileName = type + "_" + rows + "_" + cols + "." + getFileSuffix(type);
  79. saveFile(workBook, fileName);
  80. }
  81. }
  82. }
  83. private static void addContent(Workbook workBook, boolean isHType, int rows, int cols) {
  84. Map<String, CellStyle> styles = createStyles(workBook);
  85. Sheet sheet = workBook.createSheet("Main Sheet");
  86. Cell headerCell = sheet.createRow(0).createCell(0);
  87. headerCell.setCellValue("Header text is spanned across multiple cells");
  88. headerCell.setCellStyle(styles.get("header"));
  89. sheet.addMergedRegion(CellRangeAddress.valueOf("$A$1:$F$1"));
  90. int sheetNo = 0;
  91. int rowIndexInSheet = 1;
  92. double value = 0;
  93. Calendar calendar = LocaleUtil.getLocaleCalendar();
  94. for (int rowIndex = 0; rowIndex < rows; rowIndex++) {
  95. if (isHType && sheetNo != rowIndex / 0x10000) {
  96. sheet = workBook.createSheet("Spillover from sheet " + (++sheetNo));
  97. headerCell.setCellValue("Header text is spanned across multiple cells");
  98. headerCell.setCellStyle(styles.get("header"));
  99. sheet.addMergedRegion(CellRangeAddress.valueOf("$A$1:$F$1"));
  100. rowIndexInSheet = 1;
  101. }
  102. Row row = sheet.createRow(rowIndexInSheet);
  103. for (int colIndex = 0; colIndex < cols; colIndex++) {
  104. value = populateCell(styles, value, calendar, rowIndex, row, colIndex);
  105. }
  106. rowIndexInSheet++;
  107. }
  108. }
  109. private static double populateCell(Map<String, CellStyle> styles, double value, Calendar calendar, int rowIndex, Row row, int colIndex) {
  110. Cell cell = row.createCell(colIndex);
  111. String address = new CellReference(cell).formatAsString();
  112. switch (colIndex){
  113. case 0:
  114. // column A: default number format
  115. cell.setCellValue(value++);
  116. break;
  117. case 1:
  118. // column B: #,##0
  119. cell.setCellValue(value++);
  120. cell.setCellStyle(styles.get("#,##0.00"));
  121. break;
  122. case 2:
  123. // column C: $#,##0.00
  124. cell.setCellValue(value++);
  125. cell.setCellStyle(styles.get("$#,##0.00"));
  126. break;
  127. case 3:
  128. // column D: red bold text on yellow background
  129. cell.setCellValue(address);
  130. cell.setCellStyle(styles.get("red-bold"));
  131. break;
  132. case 4:
  133. // column E: boolean
  134. // TODO booleans are shown as 1/0 instead of TRUE/FALSE
  135. cell.setCellValue(rowIndex % 2 == 0);
  136. break;
  137. case 5:
  138. // column F: date / time
  139. cell.setCellValue(calendar);
  140. cell.setCellStyle(styles.get("m/d/yyyy"));
  141. calendar.roll(Calendar.DAY_OF_YEAR, -1);
  142. break;
  143. case 6:
  144. // column F: formula
  145. // TODO formulas are not yet supported in SXSSF
  146. //cell.setCellFormula("SUM(A" + (rowIndex+1) + ":E" + (rowIndex+1)+ ")");
  147. //break;
  148. default:
  149. cell.setCellValue(value++);
  150. break;
  151. }
  152. return value;
  153. }
  154. private static void saveFile(Workbook workBook, String fileName) {
  155. try {
  156. FileOutputStream out = new FileOutputStream(fileName);
  157. workBook.write(out);
  158. out.close();
  159. } catch (IOException ioe) {
  160. System.err.println("Error: failed to write to file \"" + fileName + "\", reason=" + ioe.getMessage());
  161. }
  162. }
  163. static Map<String, CellStyle> createStyles(Workbook wb) {
  164. Map<String, CellStyle> styles = new HashMap<>();
  165. CellStyle style;
  166. Font headerFont = wb.createFont();
  167. headerFont.setFontHeightInPoints((short) 14);
  168. headerFont.setBold(true);
  169. style = wb.createCellStyle();
  170. style.setAlignment(HorizontalAlignment.CENTER);
  171. style.setVerticalAlignment(VerticalAlignment.CENTER);
  172. style.setFont(headerFont);
  173. style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex());
  174. style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  175. styles.put("header", style);
  176. Font monthFont = wb.createFont();
  177. monthFont.setFontHeightInPoints((short)12);
  178. monthFont.setColor(IndexedColors.RED.getIndex());
  179. monthFont.setBold(true);
  180. style = wb.createCellStyle();
  181. style.setAlignment(HorizontalAlignment.CENTER);
  182. style.setVerticalAlignment(VerticalAlignment.CENTER);
  183. style.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
  184. style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  185. style.setFont(monthFont);
  186. styles.put("red-bold", style);
  187. String[] nfmt = {"#,##0.00", "$#,##0.00", "m/d/yyyy"};
  188. for(String fmt : nfmt){
  189. style = wb.createCellStyle();
  190. style.setDataFormat(wb.createDataFormat().getFormat(fmt));
  191. styles.put(fmt, style);
  192. }
  193. return styles;
  194. }
  195. static void usage(String message) {
  196. System.err.println(message);
  197. System.err.println("usage: java SSPerformanceTest HSSF|XSSF|SXSSF rows cols saveFile (0|1)? [--unsynchronized-xmlbeans] [--with-warmup-run]");
  198. System.exit(1);
  199. }
  200. static Workbook createWorkbook(String type) {
  201. if ("HSSF".equals(type))
  202. return new HSSFWorkbook();
  203. else if ("XSSF".equals(type))
  204. return new XSSFWorkbook();
  205. else if ("SXSSF".equals(type))
  206. return new SXSSFWorkbook();
  207. usage("Unknown type \"" + type + "\"");
  208. throw new IllegalArgumentException("Should not reach this point");
  209. }
  210. static String getFileSuffix(String type) {
  211. if ("HSSF".equals(type))
  212. return "xls";
  213. else if ("XSSF".equals(type))
  214. return "xlsx";
  215. else if ("SXSSF".equals(type))
  216. return "xlsx";
  217. return null;
  218. }
  219. static int parseInt(String value, String msg) {
  220. try {
  221. return Integer.parseInt(value);
  222. } catch (NumberFormatException e) {
  223. usage(msg);
  224. }
  225. return 0;
  226. }
  227. }