From e808689db4531db482538c4251dbb729b724ce12 Mon Sep 17 00:00:00 2001 From: Dennis Schafroth Date: Wed, 22 Feb 2012 13:59:18 +0100 Subject: [PATCH] Updated tests. Measure performace --- src/org/marc4j/test/WriterTest.java | 72 ++++++++++++++++++++++------------- 1 file changed, 46 insertions(+), 26 deletions(-) diff --git a/src/org/marc4j/test/WriterTest.java b/src/org/marc4j/test/WriterTest.java index 7707fc2..af5e1a0 100644 --- a/src/org/marc4j/test/WriterTest.java +++ b/src/org/marc4j/test/WriterTest.java @@ -5,6 +5,8 @@ import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.PrintStream; import java.io.StringReader; import javax.xml.transform.stream.StreamResult; @@ -38,6 +40,7 @@ public class WriterTest extends TestCase { } input.close(); writer.close(); + System.out.println(""); } public void testMarcXmlWriter() throws Exception { @@ -51,6 +54,7 @@ public class WriterTest extends TestCase { } input.close(); writer.close(); + System.out.println(""); } public void testTurboMarcXmlWriter() throws Exception { @@ -64,59 +68,75 @@ public class WriterTest extends TestCase { } input.close(); writer.close(); + System.out.println(""); } public void testTurboMarcXmlWriterAutoGraphics() throws Exception { - InputStream input = getClass().getResourceAsStream( - "resources/auto-graphics.mrc"); + InputStream input = getClass().getResourceAsStream("resources/auto-graphics.mrc"); MarcWriter writer = new TurboMarcXmlWriter(new FileOutputStream( new File("auto-graphics.txml")), true); MarcStreamReader reader = new MarcStreamReader(input); - int index = 0; - while (reader.hasNext()) { - Record record = reader.next(); - writer.write(record); - index++; - } - System.out.println("Number of records: " + index); + doReadWrite(writer, reader); input.close(); - writer.close(); } public void testMarcXmlWriterAutoGraphics() throws Exception { - InputStream input = getClass().getResourceAsStream( - "resources/auto-graphics.mrc"); + InputStream input = getClass().getResourceAsStream("resources/auto-graphics.mrc"); MarcWriter writer = new MarcXmlWriter(new FileOutputStream( new File("auto-graphics.txml")), true); MarcStreamReader reader = new MarcStreamReader(input); + doReadWrite(writer, reader); + input.close(); + } + + private void doReadWrite(MarcWriter writer, MarcStreamReader reader) { int index = 0; + long start = System.currentTimeMillis(); while (reader.hasNext()) { Record record = reader.next(); writer.write(record); index++; } - System.out.println("Number of records: " + index); - input.close(); + long time = System.currentTimeMillis() - start; writer.close(); + if (time > 0) + System.out.println("\nNumber of records: " + index + ". Records per second: " + (1000L * index / time)); + else + System.out.println("\nNumber of records: " + index + ". Records per second: (zero millisecs)" ); } - public void testTurboMarcXmlWriter2pz() throws Exception { - InputStream input = getClass().getResourceAsStream( - "resources/summerland.mrc"); + public void testMarcXmlWriter2pz() throws Exception { + InputStream input = getClass().getResourceAsStream("resources/summerland.mrc"); StreamResult result = new StreamResult(System.out); - MarcWriter writer = new MarcXmlWriter(result, new StreamSource(new StringReader(tmarc2pz))); + MarcWriter writer = new MarcXmlWriter(result, new StreamSource(new StringReader(marc2pz))); + MarcStreamReader reader = new MarcStreamReader(input); + doReadWrite(writer, reader); + input.close(); + } + + + public void testMarcXmlWriterAutoGraphics2pz() throws Exception { + InputStream input = getClass().getResourceAsStream("resources/auto-graphics.mrc"); + PrintStream devNull = new java.io.PrintStream ( new java.io.OutputStream ( ) { public void write ( int b ) { } }); + StreamResult result = new StreamResult(new FileOutputStream("autographics.xml")); + MarcWriter writer = new MarcXmlWriter(result, new StreamSource(new InputStreamReader(getClass().getResourceAsStream("resources/marc21.xsl")))); MarcStreamReader reader = new MarcStreamReader(input); - while (reader.hasNext()) { - Record record = reader.next(); - writer.write(record); - } + doReadWrite(writer, reader); + input.close(); + } + + public void testTurboMarcXmlWriterAutoGraphics2pz() throws Exception { + InputStream input = getClass().getResourceAsStream("resources/auto-graphics.mrc"); + PrintStream devNull = new java.io.PrintStream ( new java.io.OutputStream ( ) { public void write ( int b ) { } }); + StreamResult result = new StreamResult(new FileOutputStream("autographics.txml")); + MarcWriter writer = new TurboMarcXmlWriter(result, new StreamSource(new InputStreamReader(getClass().getResourceAsStream("resources/tmarc.xsl")))); + MarcStreamReader reader = new MarcStreamReader(input); + doReadWrite(writer, reader); input.close(); - writer.close(); } public void testWriteAndRead() throws Exception { - InputStream input = getClass().getResourceAsStream( - "resources/summerland.xml"); + InputStream input = getClass().getResourceAsStream("resources/summerland.xml"); ByteArrayOutputStream out = new ByteArrayOutputStream(); MarcStreamWriter writer = new MarcStreamWriter(out); MarcXmlReader reader = new MarcXmlReader(input); @@ -149,7 +169,7 @@ public class WriterTest extends TestCase { } - String tmarc2pz + String marc2pz = "\r\n" + "