60325: Add commented out test that reproduces the performance issue

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1793251 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dominik Stadler 2017-04-30 09:12:36 +00:00
parent 8ee7eb8e74
commit 19cf1d5814
2 changed files with 146 additions and 37 deletions

View File

@ -17,23 +17,6 @@
package org.apache.poi.poifs.filesystem; package org.apache.poi.poifs.filesystem;
import static org.hamcrest.core.IsCollectionContaining.hasItem;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.util.Iterator;
import org.apache.poi.POIDataSamples; import org.apache.poi.POIDataSamples;
import org.apache.poi.hpsf.DocumentSummaryInformation; import org.apache.poi.hpsf.DocumentSummaryInformation;
import org.apache.poi.hpsf.PropertySet; import org.apache.poi.hpsf.PropertySet;
@ -51,6 +34,14 @@ import org.junit.Assume;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import java.io.*;
import java.nio.ByteBuffer;
import java.util.Iterator;
import static org.hamcrest.core.IsCollectionContaining.hasItem;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.*;
/** /**
* Tests for the new NIO POIFSFileSystem implementation * Tests for the new NIO POIFSFileSystem implementation
*/ */
@ -99,8 +90,7 @@ public final class TestNPOIFSFileSystem {
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
fs.writeFilesystem(baos); fs.writeFilesystem(baos);
HeaderBlock header = new HeaderBlock(new ByteArrayInputStream(baos.toByteArray())); return new HeaderBlock(new ByteArrayInputStream(baos.toByteArray()));
return header;
} }
protected static NPOIFSFileSystem writeOutAndReadBack(NPOIFSFileSystem original) throws IOException { protected static NPOIFSFileSystem writeOutAndReadBack(NPOIFSFileSystem original) throws IOException {
@ -158,7 +148,9 @@ public final class TestNPOIFSFileSystem {
try { try {
fs.getBATBlockAndIndex(140); fs.getBATBlockAndIndex(140);
fail("Should only be one BAT, but a 2nd was found"); fail("Should only be one BAT, but a 2nd was found");
} catch(IndexOutOfBoundsException e) {} } catch(IndexOutOfBoundsException e) {
// expected here
}
// Verify a few next offsets // Verify a few next offsets
// 97 -> 98 -> END // 97 -> 98 -> END
@ -202,7 +194,9 @@ public final class TestNPOIFSFileSystem {
try { try {
ministore.getBATBlockAndIndex(256); ministore.getBATBlockAndIndex(256);
fail("Should only be two SBATs, but a 3rd was found"); fail("Should only be two SBATs, but a 3rd was found");
} catch(IndexOutOfBoundsException e) {} } catch(IndexOutOfBoundsException e) {
// expected here
}
// Verify a few offsets: 0->50 is a stream // Verify a few offsets: 0->50 is a stream
for(int i=0; i<50; i++) { for(int i=0; i<50; i++) {
@ -224,7 +218,9 @@ public final class TestNPOIFSFileSystem {
try { try {
fs.getBATBlockAndIndex(1040); fs.getBATBlockAndIndex(1040);
fail("Should only be one BAT, but a 2nd was found"); fail("Should only be one BAT, but a 2nd was found");
} catch(IndexOutOfBoundsException e) {} } catch(IndexOutOfBoundsException e) {
// expected here
}
// Verify a few next offsets // Verify a few next offsets
// 0 -> 1 -> 2 -> END // 0 -> 1 -> 2 -> END
@ -270,7 +266,9 @@ public final class TestNPOIFSFileSystem {
try { try {
ministore.getBATBlockAndIndex(1024); ministore.getBATBlockAndIndex(1024);
fail("Should only be one SBAT, but a 2nd was found"); fail("Should only be one SBAT, but a 2nd was found");
} catch(IndexOutOfBoundsException e) {} } catch(IndexOutOfBoundsException e) {
// expected here
}
// Verify a few offsets: 0->50 is a stream // Verify a few offsets: 0->50 is a stream
for(int i=0; i<50; i++) { for(int i=0; i<50; i++) {
@ -480,7 +478,9 @@ public final class TestNPOIFSFileSystem {
try { try {
assertEquals(false, fs1.getBATBlockAndIndex(128).getBlock().hasFreeSectors()); assertEquals(false, fs1.getBATBlockAndIndex(128).getBlock().hasFreeSectors());
fail("Should only be one BAT"); fail("Should only be one BAT");
} catch(IndexOutOfBoundsException e) {} } catch(IndexOutOfBoundsException e) {
// expected here
}
assertBATCount(fs1, 1, 0); assertBATCount(fs1, 1, 0);
@ -510,7 +510,9 @@ public final class TestNPOIFSFileSystem {
try { try {
assertEquals(false, fs1.getBATBlockAndIndex(109*128).getBlock().hasFreeSectors()); assertEquals(false, fs1.getBATBlockAndIndex(109*128).getBlock().hasFreeSectors());
fail("Should only be 109 BATs"); fail("Should only be 109 BATs");
} catch(IndexOutOfBoundsException e) {} } catch(IndexOutOfBoundsException e) {
// expected here
}
// We now have 109 BATs, but no XBATs // We now have 109 BATs, but no XBATs
assertBATCount(fs1, 109, 0); assertBATCount(fs1, 109, 0);
@ -524,12 +526,16 @@ public final class TestNPOIFSFileSystem {
// Ask for another, will get our first XBAT // Ask for another, will get our first XBAT
free = fs1.getFreeBlock(); free = fs1.getFreeBlock();
assertTrue("Had: " + free, free > 0);
assertEquals(false, fs1.getBATBlockAndIndex(109*128-1).getBlock().hasFreeSectors()); assertEquals(false, fs1.getBATBlockAndIndex(109*128-1).getBlock().hasFreeSectors());
assertEquals(true, fs1.getBATBlockAndIndex(110*128-1).getBlock().hasFreeSectors()); assertEquals(true, fs1.getBATBlockAndIndex(110*128-1).getBlock().hasFreeSectors());
try { try {
assertEquals(false, fs1.getBATBlockAndIndex(110*128).getBlock().hasFreeSectors()); assertEquals(false, fs1.getBATBlockAndIndex(110*128).getBlock().hasFreeSectors());
fail("Should only be 110 BATs"); fail("Should only be 110 BATs");
} catch(IndexOutOfBoundsException e) {} } catch(IndexOutOfBoundsException e) {
// expected here
}
assertBATCount(fs1, 110, 1); assertBATCount(fs1, 110, 1);
header = writeOutAndReadHeader(fs1); header = writeOutAndReadHeader(fs1);
@ -553,18 +559,24 @@ public final class TestNPOIFSFileSystem {
try { try {
assertEquals(false, fs1.getBATBlockAndIndex(236*128).getBlock().hasFreeSectors()); assertEquals(false, fs1.getBATBlockAndIndex(236*128).getBlock().hasFreeSectors());
fail("Should only be 236 BATs"); fail("Should only be 236 BATs");
} catch(IndexOutOfBoundsException e) {} } catch(IndexOutOfBoundsException e) {
// expected here
}
assertBATCount(fs1, 236, 1); assertBATCount(fs1, 236, 1);
// Ask for another, will get our 2nd XBAT // Ask for another, will get our 2nd XBAT
free = fs1.getFreeBlock(); free = fs1.getFreeBlock();
assertTrue("Had: " + free, free > 0);
assertEquals(false, fs1.getBATBlockAndIndex(236*128-1).getBlock().hasFreeSectors()); assertEquals(false, fs1.getBATBlockAndIndex(236*128-1).getBlock().hasFreeSectors());
assertEquals(true, fs1.getBATBlockAndIndex(237*128-1).getBlock().hasFreeSectors()); assertEquals(true, fs1.getBATBlockAndIndex(237*128-1).getBlock().hasFreeSectors());
try { try {
assertEquals(false, fs1.getBATBlockAndIndex(237*128).getBlock().hasFreeSectors()); assertEquals(false, fs1.getBATBlockAndIndex(237*128).getBlock().hasFreeSectors());
fail("Should only be 237 BATs"); fail("Should only be 237 BATs");
} catch(IndexOutOfBoundsException e) {} } catch(IndexOutOfBoundsException e) {
// expected here
}
// Check the counts now // Check the counts now
@ -572,7 +584,7 @@ public final class TestNPOIFSFileSystem {
// Check the header // Check the header
header = writeOutAndReadHeader(fs1); header = writeOutAndReadHeader(fs1);
assertNotNull(header);
// Now, write it out, and read it back in again fully // Now, write it out, and read it back in again fully
NPOIFSFileSystem fs2 = writeOutAndReadBack(fs1); NPOIFSFileSystem fs2 = writeOutAndReadBack(fs1);
@ -586,7 +598,9 @@ public final class TestNPOIFSFileSystem {
try { try {
assertEquals(false, fs2.getBATBlockAndIndex(237*128).getBlock().hasFreeSectors()); assertEquals(false, fs2.getBATBlockAndIndex(237*128).getBlock().hasFreeSectors());
fail("Should only be 237 BATs"); fail("Should only be 237 BATs");
} catch(IndexOutOfBoundsException e) {} } catch(IndexOutOfBoundsException e) {
// expected here
}
// All done // All done
@ -684,9 +698,9 @@ public final class TestNPOIFSFileSystem {
*/ */
@Test @Test
public void readWriteRead() throws Exception { public void readWriteRead() throws Exception {
SummaryInformation sinf = null; SummaryInformation sinf;
DocumentSummaryInformation dinf = null; DocumentSummaryInformation dinf;
DirectoryEntry root = null, testDir = null; DirectoryEntry root, testDir;
for(NPOIFSFileSystem fs1 : get512and4kFileAndInput()) { for(NPOIFSFileSystem fs1 : get512and4kFileAndInput()) {
// Check we can find the entries we expect // Check we can find the entries we expect
@ -1586,7 +1600,7 @@ public final class TestNPOIFSFileSystem {
int s100mb = 100*1024*1024; int s100mb = 100*1024*1024;
int s512mb = 512*1024*1024; int s512mb = 512*1024*1024;
long s2gb = 2l*1024*1024*1024; long s2gb = 2L *1024*1024*1024;
DocumentEntry entry; DocumentEntry entry;
NPOIFSFileSystem fs; NPOIFSFileSystem fs;
@ -1623,7 +1637,7 @@ public final class TestNPOIFSFileSystem {
// Tidy // Tidy
fs.close(); fs.close();
big.delete(); assertTrue(big.delete());
// Create a >2gb file // Create a >2gb file
@ -1657,7 +1671,7 @@ public final class TestNPOIFSFileSystem {
// Tidy // Tidy
fs.close(); fs.close();
big.delete(); assertTrue(big.delete());
// Create a file with a 2gb entry // Create a file with a 2gb entry
fs = POIFSFileSystem.create(big); fs = POIFSFileSystem.create(big);
@ -1693,4 +1707,99 @@ public final class TestNPOIFSFileSystem {
return sz; return sz;
} }
} }
@Ignore("Takes a long time to run")
@Test
public void testPerformance() throws Exception {
int iterations = 200;//1_000;
System.out.println("OPOI:");
long start = System.currentTimeMillis();
for (int i = 0; i < iterations; i++) {
InputStream inputStream = POIDataSamples.getHSMFInstance().openResourceAsStream("lots-of-recipients.msg");
try {
OPOIFSFileSystem srcFileSystem = new OPOIFSFileSystem(inputStream);
OPOIFSFileSystem destFileSystem = new OPOIFSFileSystem();
copyAllEntries(srcFileSystem.getRoot(), destFileSystem.getRoot());
File file = File.createTempFile("opoi", ".dat");
OutputStream outputStream = new FileOutputStream(file);
try {
destFileSystem.writeFilesystem(outputStream);
} finally {
outputStream.close();
}
assertTrue(file.delete());
if (i % 10 == 0) System.out.print(".");
if (i % 800 == 0 && i > 0) System.out.println();
} finally {
inputStream.close();
}
}
System.out.println();
System.out.println("OPOI took: " + (System.currentTimeMillis() - start));
System.out.println();
System.out.println("NPOI:");
start = System.currentTimeMillis();
for (int i = 0; i < iterations; i++) {
InputStream inputStream = POIDataSamples.getHSMFInstance().openResourceAsStream("lots-of-recipients.msg");
try {
NPOIFSFileSystem srcFileSystem = new NPOIFSFileSystem(inputStream);
NPOIFSFileSystem destFileSystem = new NPOIFSFileSystem();
copyAllEntries(srcFileSystem.getRoot(), destFileSystem.getRoot());
File file = File.createTempFile("npoi", ".dat");
OutputStream outputStream = new FileOutputStream(file);
try {
destFileSystem.writeFilesystem(outputStream);
} finally {
outputStream.close();
}
assertTrue(file.delete());
if (i % 10 == 0) System.out.print(".");
if (i % 800 == 0 && i > 0) System.out.println();
} finally {
inputStream.close();
}
}
System.out.println();
System.out.println("NPOI took: " + (System.currentTimeMillis() - start));
System.out.println();
System.out.println();
}
private static void copyAllEntries(DirectoryEntry srcDirectory, DirectoryEntry destDirectory) throws IOException {
Iterator<Entry> iterator = srcDirectory.getEntries();
while (iterator.hasNext()) {
Entry entry = iterator.next();
if (entry.isDirectoryEntry()) {
DirectoryEntry childDest = destDirectory.createDirectory(entry.getName());
copyAllEntries((DirectoryEntry) entry, childDest);
} else {
DocumentEntry srcEntry = (DocumentEntry) entry;
InputStream inputStream = new DocumentInputStream(srcEntry);
try {
destDirectory.createDocument(entry.getName(), inputStream);
} finally {
inputStream.close();
}
}
}
}
} }

Binary file not shown.