Created
May 10, 2017 08:49
-
-
Save andreybpanfilov/e5299be53200a46b2b46ad425635c18a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package pro.documentum.util; | |
import java.util.ArrayList; | |
import java.util.List; | |
import org.junit.Test; | |
import com.documentum.fc.client.IDfQuery; | |
import com.documentum.fc.client.IDfSession; | |
import com.documentum.fc.common.DfException; | |
import com.documentum.fc.common.DfUtil; | |
import pro.documentum.junit.DfcTestSupport; | |
import pro.documentum.util.objects.DfObjects; | |
import pro.documentum.util.queries.DfIterator; | |
import pro.documentum.util.queries.Queries; | |
import pro.documentum.util.sessions.Sessions; | |
/** | |
* @author Andrey B. Panfilov <andrey@panfilov.tel> | |
*/ | |
public class ConnectionsTest extends DfcTestSupport { | |
private void doOp(IDfSession session, Runnable r) throws DfException { | |
int startDocbase = docbaseConnections(session); | |
int startDB = dbConnections(session); | |
while (true) { | |
r.run(); | |
System.out.println("Docbase: " | |
+ (docbaseConnections(session) - startDocbase) + ", DB: " | |
+ (dbConnections(session) - startDB)); | |
} | |
} | |
@Test | |
public void testIds() throws Exception { | |
final IDfSession main = getSession(); | |
final int batchSize = 1000; | |
doOp(main, new Runnable() { | |
@Override | |
public void run() { | |
try { | |
IDfSession session = Sessions.brandNew( | |
main.getSessionManager(), main.getDocbaseName()); | |
String[] ids = DfObjects.makeIds(session, "dm_document", | |
batchSize); | |
assertEquals(batchSize, ids.length); | |
} catch (DfException ex) { | |
throw new RuntimeException(ex); | |
} | |
} | |
}); | |
} | |
@Test | |
public void testIdsTransaction() throws Exception { | |
final IDfSession main = getSession(); | |
final int batchSize = 1000; | |
doOp(main, new Runnable() { | |
@Override | |
public void run() { | |
try { | |
IDfSession session = Sessions.brandNew( | |
main.getSessionManager(), main.getDocbaseName()); | |
session.beginTrans(); | |
String[] ids = DfObjects.makeIds(session, "dm_document", | |
batchSize); | |
assertEquals(batchSize, ids.length); | |
} catch (DfException ex) { | |
throw new RuntimeException(ex); | |
} | |
} | |
}); | |
} | |
@Test | |
public void testExec() throws Exception { | |
final IDfSession main = getSession(); | |
doOp(main, new Runnable() { | |
@Override | |
public void run() { | |
try { | |
IDfSession session = Sessions.brandNew( | |
main.getSessionManager(), main.getDocbaseName()); | |
Queries.execute(session, "SELECT * FROM dm_user", | |
IDfQuery.DF_EXEC_QUERY).close(); | |
} catch (DfException ex) { | |
throw new RuntimeException(ex); | |
} | |
} | |
}); | |
} | |
@Test | |
public void testExecTransaction() throws Exception { | |
final IDfSession main = getSession(); | |
doOp(main, new Runnable() { | |
@Override | |
public void run() { | |
try { | |
IDfSession session = Sessions.brandNew( | |
main.getSessionManager(), main.getDocbaseName()); | |
session.beginTrans(); | |
Queries.execute(session, "SELECT * FROM dm_user", | |
IDfQuery.DF_EXEC_QUERY).close(); | |
} catch (DfException ex) { | |
throw new RuntimeException(ex); | |
} | |
} | |
}); | |
} | |
@Test | |
public void testExecs() throws Exception { | |
final IDfSession main = getSession(); | |
doOp(main, new Runnable() { | |
@Override | |
public void run() { | |
try { | |
List<DfIterator> iterators = new ArrayList<>(); | |
IDfSession session = Sessions.brandNew( | |
main.getSessionManager(), main.getDocbaseName()); | |
for (int i = 0; i < 10; i++) { | |
iterators.add(Queries | |
.execute(session, "SELECT * FROM dm_user", | |
IDfQuery.DF_EXEC_QUERY)); | |
} | |
for (DfIterator iterator : iterators) { | |
iterator.close(); | |
} | |
} catch (DfException ex) { | |
throw new RuntimeException(ex); | |
} | |
} | |
}); | |
} | |
@Test | |
public void testExecsLeak() throws Exception { | |
final IDfSession main = getSession(); | |
doOp(main, new Runnable() { | |
@Override | |
public void run() { | |
try { | |
List<DfIterator> iterators = new ArrayList<>(); | |
IDfSession session = Sessions.brandNew( | |
main.getSessionManager(), main.getDocbaseName()); | |
for (int i = 0; i < 10; i++) { | |
iterators.add(Queries | |
.execute(session, "SELECT * FROM dm_user", | |
IDfQuery.DF_EXEC_QUERY)); | |
} | |
// collection leak | |
// for (DfIterator iterator : iterators) { | |
// iterator.close(); | |
// } | |
} catch (DfException ex) { | |
throw new RuntimeException(ex); | |
} | |
} | |
}); | |
} | |
@Test | |
public void testExecIds() throws Exception { | |
final IDfSession main = getSession(); | |
final int batchSize = 1000; | |
doOp(main, new Runnable() { | |
@Override | |
public void run() { | |
try { | |
IDfSession session = Sessions.brandNew( | |
main.getSessionManager(), main.getDocbaseName()); | |
DfIterator iterator = Queries.execute(session, | |
"SELECT * FROM dm_user", IDfQuery.DF_EXEC_QUERY); | |
String[] ids = DfObjects.makeIds(session, "dm_document", | |
batchSize); | |
iterator.close(); | |
assertEquals(batchSize, ids.length); | |
} catch (DfException ex) { | |
throw new RuntimeException(ex); | |
} | |
} | |
}); | |
} | |
@Test | |
public void testExecIdsLeak() throws Exception { | |
final IDfSession main = getSession(); | |
final int batchSize = 1000; | |
doOp(main, new Runnable() { | |
@Override | |
public void run() { | |
try { | |
IDfSession session = Sessions.brandNew( | |
main.getSessionManager(), main.getDocbaseName()); | |
DfIterator iterator = Queries.execute(session, | |
"SELECT * FROM dm_user", IDfQuery.DF_EXEC_QUERY); | |
String[] ids = DfObjects.makeIds(session, "dm_document", | |
batchSize); | |
// collection leak | |
// iterator.close(); | |
assertEquals(batchSize, ids.length); | |
} catch (DfException ex) { | |
throw new RuntimeException(ex); | |
} | |
} | |
}); | |
} | |
@Test | |
public void testRead() throws Exception { | |
final IDfSession main = getSession(); | |
doOp(main, new Runnable() { | |
@Override | |
public void run() { | |
try { | |
IDfSession session = Sessions.brandNew( | |
main.getSessionManager(), main.getDocbaseName()); | |
Queries.execute(session, "SELECT * FROM dm_user", | |
IDfQuery.DF_READ_QUERY).close(); | |
} catch (DfException ex) { | |
throw new RuntimeException(ex); | |
} | |
} | |
}); | |
} | |
private int dbConnections(IDfSession session) throws DfException { | |
String query = "SELECT COUNT(*) cnt FROM v$session " | |
+ "WHERE USERNAME=SYS_CONTEXT('USERENV','SESSION_USER')"; | |
query = "execute EXEC_SELECT_SQL with QUERY='" | |
+ DfUtil.escapeQuotedString(query) + "'"; | |
try (DfIterator iterator = Queries.execute(session, query, | |
IDfQuery.DF_READ_QUERY)) { | |
return Integer.valueOf(iterator.next().getString("cnt")); | |
} | |
} | |
private int docbaseConnections(IDfSession session) throws DfException { | |
int result = 0; | |
try (DfIterator iterator = Queries.execute(session, | |
"execute SHOW_SESSIONS", IDfQuery.DF_READ_QUERY)) { | |
while (iterator.hasNext()) { | |
iterator.next(); | |
result++; | |
} | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment