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
This commit is contained in:
Dominik Stadler 2016-05-05 12:22:16 +00:00
parent f688ed96c6
commit 26de463419

View File

@ -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<String, CellStyle> 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<String, CellStyle> 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<String, CellStyle> createStyles(Workbook wb) {