Skip to content

Instantly share code, notes, and snippets.

@dfish3r
Created August 15, 2019 17:57
Show Gist options
  • Select an option

  • Save dfish3r/29fc1127f504de5a269441d6b9621b90 to your computer and use it in GitHub Desktop.

Select an option

Save dfish3r/29fc1127f504de5a269441d6b9621b90 to your computer and use it in GitHub Desktop.
Ldaptive v2 Concurrency Test Client
import java.time.Duration;
import java.util.*;
import java.util.concurrent.*;
import org.ldaptive.*;
import org.ldaptive.auth.*;
import org.ldaptive.pool.*;
import org.ldaptive.ssl.*;
public final class LdaptiveConcurrent
{
private static LdapURL LDAP_URL;
private static String FILTER;
private static int ITERATIONS;
private LdaptiveConcurrent() {}
public static void main(final String[] args)
throws Exception
{
if (args.length != 3) {
System.out.println("Usage: <ldapUrl> <default|single|pooled> <iterations>");
return;
}
LDAP_URL = new LdapURL(args[0]);
final String factory = args[1];
ITERATIONS = Integer.parseInt(args[2]);
final ConnectionConfig connectionConfig = ConnectionConfig.builder()
.url(LDAP_URL.getHostnameWithSchemeAndPort())
.useStartTLS(true)
.autoReconnect(false)
.sslConfig(SslConfig.builder().trustManagers(new AllowAnyTrustManager()).build())
.build();
final ConnectionFactory cf;
switch (factory) {
case "default":
cf = new DefaultConnectionFactory(connectionConfig);
break;
case "single":
cf = new SingleConnectionFactory(connectionConfig);
((SingleConnectionFactory) cf).initialize();
break;
case "pooled":
cf = PooledConnectionFactory.builder()
.config(connectionConfig)
.config(PoolConfig.builder().min(10).max(10).build())
.build();
((PooledConnectionFactory) cf).initialize();
break;
default:
throw new IllegalArgumentException("Unknown factory: " + factory);
}
search(cf);
}
public static void search(final ConnectionFactory factory)
throws Exception
{
final ConcurrentLinkedQueue<LdapEntry> entries = new ConcurrentLinkedQueue<>();
final ConcurrentLinkedQueue<Result> results = new ConcurrentLinkedQueue<>();
final Callable<Void> c = () -> {
doSearch(factory, entries, results);
return null;
};
final ExecutorService executor = Executors.newCachedThreadPool();
final List<Callable<Void>> callables = new ArrayList<>();
for (int i = 0; i < ITERATIONS; i++) {
callables.add(c);
}
long t = System.currentTimeMillis();
executor.invokeAll(callables);
executor.shutdown();
executor.awaitTermination(60, TimeUnit.SECONDS);
t = System.currentTimeMillis() - t;
System.out.println(
"SEARCH DONE:: " + t + "ms :" + entries.size() + " entries :" + results.size() + " results");
}
private static void doSearch(
final ConnectionFactory factory,
final ConcurrentLinkedQueue<LdapEntry> entries,
final ConcurrentLinkedQueue<Result> results)
{
try {
final SearchOperation search = new SearchOperation(factory);
search.setEntryHandlers(e -> {
entries.add(e);
return e;
});
search.setResultHandlers(r -> {
results.add(r);
});
search.setExceptionHandler(e -> System.out.println(
"Handler caught exception: " + e.getClass() +
" -- " + (e.getCause() != null ? e.getCause().getClass() : "") +
" -- " + e.getMessage()));
final SearchResponse result = search.execute(
SearchRequest.builder()
.dn(LDAP_URL.getBaseDn())
.scope(LDAP_URL.getScope())
.aliases(DerefAliases.NEVER)
.timeLimit(Duration.ofMinutes(1))
.filter(LDAP_URL.getFilter())
.attributes(LDAP_URL.getAttributes()).build());
} catch (Throwable e) {
System.out.println(
"Execution caught exception: " + e.getClass() +
" -- " + (e.getCause() != null ? e.getCause().getClass() : "") +
" -- " + e.getMessage());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment