Permalink
Browse files

test: improve CopyLargeFileTest performance by dropping non required …

…index and foreign key
  • Loading branch information...
vlsi committed Dec 7, 2015
1 parent 3b10873 commit 410a662ac7cd197e33909e64172534f92325432a
@@ -5,8 +5,9 @@
import org.postgresql.copy.CopyManager;
import org.postgresql.test.TestUtil;
import org.postgresql.test.util.BufferGenerator;
import org.postgresql.test.util.StrangeFileInputStream;
import org.postgresql.test.util.StrangeInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.File;
import java.sql.CallableStatement;
@@ -33,8 +34,8 @@ protected void setUp() throws Exception {
TestUtil.createTable(con, "pgjdbc_issue366_test_glossary", "id SERIAL, text_id VARCHAR(1000) NOT NULL UNIQUE, name VARCHAR(10) NOT NULL UNIQUE");
TestUtil.createTable(con, "pgjdbc_issue366_test_data", "id SERIAL,\n" +
" data_text_id VARCHAR(1000) NOT NULL UNIQUE,\n" +
" glossary_text_id VARCHAR(1000) NOT NULL REFERENCES pgjdbc_issue366_test_glossary(text_id),\n" +
" data_text_id VARCHAR(1000) NOT NULL /*UNIQUE <-- it slows down inserts due to additional index */,\n" +
" glossary_text_id VARCHAR(1000) NOT NULL /* REFERENCES pgjdbc_issue366_test_glossary(text_id) */,\n" +
" value DOUBLE PRECISION NOT NULL");
feedTable();
@@ -62,7 +63,7 @@ protected void tearDown() throws Exception {
try {
TestUtil.dropTable(con, "pgjdbc_issue366_test_data");
TestUtil.dropTable(con, "pgjdbc_issue366_test_glossary");
new File("buffer.txt").delete();
new File("target/buffer.txt").delete();
} finally {
con.close();
}
@@ -78,7 +79,7 @@ public void testFeedTableSeveralTimesTest() throws Exception {
private void feedTableAndCheckTableFeedIsOk(Connection conn) throws Exception {
InputStream in = null;
try {
in = new StrangeFileInputStream("buffer.txt");
in = new StrangeInputStream(new FileInputStream("target/buffer.txt"));
long size = copyAPI.copyIn("COPY pgjdbc_issue366_test_data(data_text_id, glossary_text_id, value) FROM STDIN", in);
assertEquals(BufferGenerator.ROW_COUNT, size);
} finally {
@@ -1,8 +1,6 @@
package org.postgresql.test.util;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.*;
import java.util.Date;
import java.util.Random;
@@ -17,7 +15,9 @@ public static void main(String[] args) throws Exception {
random.setSeed(new Date().getTime());
OutputStream out = null;
try {
out = new BufferedOutputStream(new FileOutputStream("buffer.txt"));
File outFile = new File("target", "buffer.txt");
outFile.getParentFile().mkdir();
out = new BufferedOutputStream(new FileOutputStream(outFile));
for (long i = 0; i < ROW_COUNT; i++) {
StringBuffer line = new StringBuffer();
line.append("VERY_LONG_LINE_TO_ASSIST_IN_DETECTION_OF_ISSUE_366_#_").append(i).append('\t');

This file was deleted.

Oops, something went wrong.
@@ -0,0 +1,36 @@
package org.postgresql.test.util;
import java.io.FileNotFoundException;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Random;
/**
* {@link InputStream} implementation that reads less data than is provided in the destination array.
* This allows to stress test {@link org.postgresql.copy.CopyManager} or other consumers.
*/
public class StrangeInputStream extends FilterInputStream {
private Random rand; //generator of fun events
public StrangeInputStream(InputStream is) throws FileNotFoundException {
super(is);
rand = new Random();
long seed = Long.getLong("StrangeInputStream.seed", System.currentTimeMillis());
System.out.println("Using seed = " + seed +
" for StrangeInputStream. Set -DStrangeInputStream.seed=" + seed + " to reproduce the test");
rand.setSeed(seed);
}
@Override
public int read(byte[] b) throws IOException {
int maxRead = rand.nextInt(b.length);
return super.read(b, 0, maxRead);
}
@Override
public int read(byte[] b, int off, int len) throws IOException {
int maxRead = rand.nextInt(len);
return super.read(b, off, maxRead);
}
}

0 comments on commit 410a662

Please sign in to comment.