use of com.unboundid.util.OutputFormat in project ldapsdk by pingidentity.
the class LDAPResultCode method doToolProcessing.
/**
* {@inheritDoc}
*/
@Override()
@NotNull()
public ResultCode doToolProcessing() {
// Get all result codes that should be included in the output.
final Map<Integer, ResultCode> resultCodesByIntValue = new TreeMap<>();
final Map<String, ResultCode> resultCodesByName = new TreeMap<>();
if ((intValueArg != null) && intValueArg.isPresent()) {
final int intValue = intValueArg.getValue();
final ResultCode rc = ResultCode.valueOf(intValue, null, false);
if (rc != null) {
resultCodesByIntValue.put(intValue, rc);
resultCodesByName.put(StaticUtils.toLowerCase(rc.getName()), rc);
}
} else {
final String searchString;
if ((searchArg != null) && searchArg.isPresent()) {
searchString = StaticUtils.toLowerCase(searchArg.getValue());
} else {
searchString = null;
}
for (final ResultCode rc : ResultCode.values()) {
final String name = rc.getName();
final String lowerName = StaticUtils.toLowerCase(name);
if (searchString != null) {
if (!lowerName.contains(searchString)) {
continue;
}
}
resultCodesByIntValue.put(rc.intValue(), rc);
resultCodesByName.put(lowerName, rc);
}
}
// exit with an error.
if (resultCodesByIntValue.isEmpty()) {
wrapErr(0, WRAP_COLUMN, ERR_LDAP_RC_NO_RESULTS.get());
return ResultCode.NO_RESULTS_RETURNED;
}
// Iterate through the matching result codes and figure out how many
// characters are in the longest name and
final String nameLabel = INFO_LDAP_RC_NAME_LABEL.get();
final String intValueLabel = INFO_LDAP_RC_INT_VALUE_LABEL.get();
int numCharsInLongestName = nameLabel.length();
int numCharsInLongestIntValue = intValueLabel.length();
for (final Map.Entry<Integer, ResultCode> e : resultCodesByIntValue.entrySet()) {
final String intValueString = String.valueOf(e.getKey());
numCharsInLongestIntValue = Math.max(numCharsInLongestIntValue, intValueString.length());
final String name = e.getValue().getName();
numCharsInLongestName = Math.max(numCharsInLongestName, name.length());
}
// Construct the column formatter that will be used to generate the output.
final boolean json;
final OutputFormat outputFormat;
final boolean scriptFriendly = ((scriptFriendlyArg != null) && scriptFriendlyArg.isPresent());
if (scriptFriendly) {
json = false;
outputFormat = OutputFormat.TAB_DELIMITED_TEXT;
} else if ((outputFormatArg != null) && outputFormatArg.isPresent()) {
final String outputFormatValue = StaticUtils.toLowerCase(outputFormatArg.getValue());
if (outputFormatValue.equals(OUTPUT_FORMAT_CSV)) {
json = false;
outputFormat = OutputFormat.CSV;
} else if (outputFormatValue.equals(OUTPUT_FORMAT_JSON)) {
json = true;
outputFormat = null;
} else if (outputFormatValue.equals(OUTPUT_FORMAT_TAB_DELIMITED)) {
json = false;
outputFormat = OutputFormat.TAB_DELIMITED_TEXT;
} else {
json = false;
outputFormat = OutputFormat.COLUMNS;
}
} else {
json = false;
outputFormat = OutputFormat.COLUMNS;
}
final ColumnFormatter formatter;
if (json) {
formatter = null;
} else {
formatter = new ColumnFormatter(false, null, outputFormat, " | ", new FormattableColumn(numCharsInLongestName, HorizontalAlignment.LEFT, nameLabel), new FormattableColumn(numCharsInLongestIntValue, HorizontalAlignment.LEFT, intValueLabel));
}
// Display the table header, if appropriate.
if ((formatter != null) && (outputFormat == OutputFormat.COLUMNS)) {
for (final String line : formatter.getHeaderLines(true)) {
out(line);
}
}
// Display the main output.
final Collection<ResultCode> resultCodes;
if ((alphabeticOrderArg != null) && alphabeticOrderArg.isPresent()) {
resultCodes = resultCodesByName.values();
} else {
resultCodes = resultCodesByIntValue.values();
}
for (final ResultCode rc : resultCodes) {
if (formatter == null) {
final JSONObject jsonObject = new JSONObject(new JSONField(JSON_FIELD_NAME, rc.getName()), new JSONField(JSON_FIELD_INT_VALUE, rc.intValue()));
out(jsonObject.toSingleLineString());
} else {
out(formatter.formatRow(rc.getName(), rc.intValue()));
}
}
return ResultCode.SUCCESS;
}
use of com.unboundid.util.OutputFormat in project ldapsdk by pingidentity.
the class SearchRate method doToolProcessing.
/**
* Performs the actual processing for this tool. In this case, it gets a
* connection to the directory server and uses it to perform the requested
* searches.
*
* @return The result code for the processing that was performed.
*/
@Override()
@NotNull()
public ResultCode doToolProcessing() {
// variable rate data file and return.
if (sampleRateFile.isPresent()) {
try {
RateAdjustor.writeSampleVariableRateFile(sampleRateFile.getValue());
return ResultCode.SUCCESS;
} catch (final Exception e) {
Debug.debugException(e);
err("An error occurred while trying to write sample variable data " + "rate file '", sampleRateFile.getValue().getAbsolutePath(), "': ", StaticUtils.getExceptionMessage(e));
return ResultCode.LOCAL_ERROR;
}
}
// Determine the random seed to use.
final Long seed;
if (randomSeed.isPresent()) {
seed = Long.valueOf(randomSeed.getValue());
} else {
seed = null;
}
// Create value patterns for the base DN, filter, LDAP URL, and proxied
// authorization DN.
final ValuePattern dnPattern;
try {
if (baseDN.getNumOccurrences() > 0) {
dnPattern = new ValuePattern(baseDN.getValue(), seed);
} else if (ldapURL.isPresent()) {
dnPattern = null;
} else {
dnPattern = new ValuePattern("", seed);
}
} catch (final ParseException pe) {
Debug.debugException(pe);
err("Unable to parse the base DN value pattern: ", pe.getMessage());
return ResultCode.PARAM_ERROR;
}
final ValuePattern filterPattern;
try {
if (filter.isPresent()) {
filterPattern = new ValuePattern(filter.getValue(), seed);
} else {
filterPattern = null;
}
} catch (final ParseException pe) {
Debug.debugException(pe);
err("Unable to parse the filter pattern: ", pe.getMessage());
return ResultCode.PARAM_ERROR;
}
final ValuePattern ldapURLPattern;
try {
if (ldapURL.isPresent()) {
ldapURLPattern = new ValuePattern(ldapURL.getValue(), seed);
} else {
ldapURLPattern = null;
}
} catch (final ParseException pe) {
Debug.debugException(pe);
err("Unable to parse the LDAP URL pattern: ", pe.getMessage());
return ResultCode.PARAM_ERROR;
}
final ValuePattern authzIDPattern;
if (proxyAs.isPresent()) {
try {
authzIDPattern = new ValuePattern(proxyAs.getValue(), seed);
} catch (final ParseException pe) {
Debug.debugException(pe);
err("Unable to parse the proxied authorization pattern: ", pe.getMessage());
return ResultCode.PARAM_ERROR;
}
} else {
authzIDPattern = null;
}
// Get the alias dereference policy to use.
final DereferencePolicy derefPolicy;
final String derefValue = StaticUtils.toLowerCase(dereferencePolicy.getValue());
if (derefValue.equals("always")) {
derefPolicy = DereferencePolicy.ALWAYS;
} else if (derefValue.equals("search")) {
derefPolicy = DereferencePolicy.SEARCHING;
} else if (derefValue.equals("find")) {
derefPolicy = DereferencePolicy.FINDING;
} else {
derefPolicy = DereferencePolicy.NEVER;
}
// Get the set of controls to include in search requests.
final ArrayList<Control> controlList = new ArrayList<>(5);
if (assertionFilter.isPresent()) {
controlList.add(new AssertionRequestControl(assertionFilter.getValue()));
}
if (sortOrder.isPresent()) {
final ArrayList<SortKey> sortKeys = new ArrayList<>(5);
final StringTokenizer tokenizer = new StringTokenizer(sortOrder.getValue(), ",");
while (tokenizer.hasMoreTokens()) {
String token = tokenizer.nextToken().trim();
final boolean ascending;
if (token.startsWith("+")) {
ascending = true;
token = token.substring(1);
} else if (token.startsWith("-")) {
ascending = false;
token = token.substring(1);
} else {
ascending = true;
}
final String attributeName;
final String matchingRuleID;
final int colonPos = token.indexOf(':');
if (colonPos < 0) {
attributeName = token;
matchingRuleID = null;
} else {
attributeName = token.substring(0, colonPos);
matchingRuleID = token.substring(colonPos + 1);
}
sortKeys.add(new SortKey(attributeName, matchingRuleID, (!ascending)));
}
controlList.add(new ServerSideSortRequestControl(sortKeys));
}
if (control.isPresent()) {
controlList.addAll(control.getValues());
}
// Get the attributes to return.
final String[] attrs;
if (attributes.isPresent()) {
final List<String> attrList = attributes.getValues();
attrs = new String[attrList.size()];
attrList.toArray(attrs);
} else {
attrs = StaticUtils.NO_STRINGS;
}
// If the --ratePerSecond option was specified, then limit the rate
// accordingly.
FixedRateBarrier fixedRateBarrier = null;
if (ratePerSecond.isPresent() || variableRateData.isPresent()) {
// We might not have a rate per second if --variableRateData is specified.
// The rate typically doesn't matter except when we have warm-up
// intervals. In this case, we'll run at the max rate.
final int intervalSeconds = collectionInterval.getValue();
final int ratePerInterval = (ratePerSecond.getValue() == null) ? Integer.MAX_VALUE : ratePerSecond.getValue() * intervalSeconds;
fixedRateBarrier = new FixedRateBarrier(1000L * intervalSeconds, ratePerInterval);
}
// If --variableRateData was specified, then initialize a RateAdjustor.
RateAdjustor rateAdjustor = null;
if (variableRateData.isPresent()) {
try {
rateAdjustor = RateAdjustor.newInstance(fixedRateBarrier, ratePerSecond.getValue(), variableRateData.getValue());
} catch (final IOException | IllegalArgumentException e) {
Debug.debugException(e);
err("Initializing the variable rates failed: " + e.getMessage());
return ResultCode.PARAM_ERROR;
}
}
// If the --maxOutstandingRequests option was specified, then create the
// semaphore used to enforce that limit.
final Semaphore asyncSemaphore;
if (maxOutstandingRequests.isPresent()) {
asyncSemaphore = new Semaphore(maxOutstandingRequests.getValue());
} else {
asyncSemaphore = null;
}
// Determine whether to include timestamps in the output and if so what
// format should be used for them.
final boolean includeTimestamp;
final String timeFormat;
if (timestampFormat.getValue().equalsIgnoreCase("with-date")) {
includeTimestamp = true;
timeFormat = "dd/MM/yyyy HH:mm:ss";
} else if (timestampFormat.getValue().equalsIgnoreCase("without-date")) {
includeTimestamp = true;
timeFormat = "HH:mm:ss";
} else {
includeTimestamp = false;
timeFormat = null;
}
// Determine whether any warm-up intervals should be run.
final long totalIntervals;
final boolean warmUp;
int remainingWarmUpIntervals = warmUpIntervals.getValue();
if (remainingWarmUpIntervals > 0) {
warmUp = true;
totalIntervals = 0L + numIntervals.getValue() + remainingWarmUpIntervals;
} else {
warmUp = true;
totalIntervals = 0L + numIntervals.getValue();
}
// Create the table that will be used to format the output.
final OutputFormat outputFormat;
if (csvFormat.isPresent()) {
outputFormat = OutputFormat.CSV;
} else {
outputFormat = OutputFormat.COLUMNS;
}
final ColumnFormatter formatter = new ColumnFormatter(includeTimestamp, timeFormat, outputFormat, " ", new FormattableColumn(12, HorizontalAlignment.RIGHT, "Recent", "Searches/Sec"), new FormattableColumn(12, HorizontalAlignment.RIGHT, "Recent", "Avg Dur ms"), new FormattableColumn(12, HorizontalAlignment.RIGHT, "Recent", "Entries/Srch"), new FormattableColumn(12, HorizontalAlignment.RIGHT, "Recent", "Errors/Sec"), new FormattableColumn(12, HorizontalAlignment.RIGHT, "Overall", "Searches/Sec"), new FormattableColumn(12, HorizontalAlignment.RIGHT, "Overall", "Avg Dur ms"));
// Create values to use for statistics collection.
final AtomicLong searchCounter = new AtomicLong(0L);
final AtomicLong entryCounter = new AtomicLong(0L);
final AtomicLong errorCounter = new AtomicLong(0L);
final AtomicLong searchDurations = new AtomicLong(0L);
final ResultCodeCounter rcCounter = new ResultCodeCounter();
// Determine the length of each interval in milliseconds.
final long intervalMillis = 1000L * collectionInterval.getValue();
// Create the threads to use for the searches.
final CyclicBarrier barrier = new CyclicBarrier(numThreads.getValue() + 1);
final SearchRateThread[] threads = new SearchRateThread[numThreads.getValue()];
for (int i = 0; i < threads.length; i++) {
final LDAPConnection connection;
try {
connection = getConnection();
} catch (final LDAPException le) {
Debug.debugException(le);
err("Unable to connect to the directory server: ", StaticUtils.getExceptionMessage(le));
return le.getResultCode();
}
threads[i] = new SearchRateThread(this, i, connection, asynchronousMode.isPresent(), dnPattern, scope.getValue(), derefPolicy, sizeLimit.getValue(), timeLimitSeconds.getValue(), typesOnly.isPresent(), filterPattern, attrs, ldapURLPattern, authzIDPattern, simplePageSize.getValue(), controlList, iterationsBeforeReconnect.getValue(), runningThreads, barrier, searchCounter, entryCounter, searchDurations, errorCounter, rcCounter, fixedRateBarrier, asyncSemaphore);
threads[i].start();
}
// Display the table header.
for (final String headerLine : formatter.getHeaderLines(true)) {
out(headerLine);
}
// which case, we'll start it after the warm-up is complete.
if ((rateAdjustor != null) && (remainingWarmUpIntervals <= 0)) {
rateAdjustor.start();
}
// Indicate that the threads can start running.
try {
barrier.await();
} catch (final Exception e) {
Debug.debugException(e);
}
long overallStartTime = System.nanoTime();
long nextIntervalStartTime = System.currentTimeMillis() + intervalMillis;
boolean setOverallStartTime = false;
long lastDuration = 0L;
long lastNumEntries = 0L;
long lastNumErrors = 0L;
long lastNumSearches = 0L;
long lastEndTime = System.nanoTime();
for (long i = 0; i < totalIntervals; i++) {
if (rateAdjustor != null) {
if (!rateAdjustor.isAlive()) {
out("All of the rates in " + variableRateData.getValue().getName() + " have been completed.");
break;
}
}
final long startTimeMillis = System.currentTimeMillis();
final long sleepTimeMillis = nextIntervalStartTime - startTimeMillis;
nextIntervalStartTime += intervalMillis;
if (sleepTimeMillis > 0) {
sleeper.sleep(sleepTimeMillis);
}
if (stopRequested.get()) {
break;
}
final long endTime = System.nanoTime();
final long intervalDuration = endTime - lastEndTime;
final long numSearches;
final long numEntries;
final long numErrors;
final long totalDuration;
if (warmUp && (remainingWarmUpIntervals > 0)) {
numSearches = searchCounter.getAndSet(0L);
numEntries = entryCounter.getAndSet(0L);
numErrors = errorCounter.getAndSet(0L);
totalDuration = searchDurations.getAndSet(0L);
} else {
numSearches = searchCounter.get();
numEntries = entryCounter.get();
numErrors = errorCounter.get();
totalDuration = searchDurations.get();
}
final long recentNumSearches = numSearches - lastNumSearches;
final long recentNumEntries = numEntries - lastNumEntries;
final long recentNumErrors = numErrors - lastNumErrors;
final long recentDuration = totalDuration - lastDuration;
final double numSeconds = intervalDuration / 1_000_000_000.0d;
final double recentSearchRate = recentNumSearches / numSeconds;
final double recentErrorRate = recentNumErrors / numSeconds;
final double recentAvgDuration;
final double recentEntriesPerSearch;
if (recentNumSearches > 0L) {
recentEntriesPerSearch = 1.0d * recentNumEntries / recentNumSearches;
recentAvgDuration = 1.0d * recentDuration / recentNumSearches / 1_000_000;
} else {
recentEntriesPerSearch = 0.0d;
recentAvgDuration = 0.0d;
}
if (warmUp && (remainingWarmUpIntervals > 0)) {
out(formatter.formatRow(recentSearchRate, recentAvgDuration, recentEntriesPerSearch, recentErrorRate, "warming up", "warming up"));
remainingWarmUpIntervals--;
if (remainingWarmUpIntervals == 0) {
out("Warm-up completed. Beginning overall statistics collection.");
setOverallStartTime = true;
if (rateAdjustor != null) {
rateAdjustor.start();
}
}
} else {
if (setOverallStartTime) {
overallStartTime = lastEndTime;
setOverallStartTime = false;
}
final double numOverallSeconds = (endTime - overallStartTime) / 1_000_000_000.0d;
final double overallSearchRate = numSearches / numOverallSeconds;
final double overallAvgDuration;
if (numSearches > 0L) {
overallAvgDuration = 1.0d * totalDuration / numSearches / 1_000_000;
} else {
overallAvgDuration = 0.0d;
}
out(formatter.formatRow(recentSearchRate, recentAvgDuration, recentEntriesPerSearch, recentErrorRate, overallSearchRate, overallAvgDuration));
lastNumSearches = numSearches;
lastNumEntries = numEntries;
lastNumErrors = numErrors;
lastDuration = totalDuration;
}
final List<ObjectPair<ResultCode, Long>> rcCounts = rcCounter.getCounts(true);
if ((!suppressErrors.isPresent()) && (!rcCounts.isEmpty())) {
err("\tError Results:");
for (final ObjectPair<ResultCode, Long> p : rcCounts) {
err("\t", p.getFirst().getName(), ": ", p.getSecond());
}
}
lastEndTime = endTime;
}
// Shut down the RateAdjustor if we have one.
if (rateAdjustor != null) {
rateAdjustor.shutDown();
}
// Stop all of the threads.
ResultCode resultCode = ResultCode.SUCCESS;
for (final SearchRateThread t : threads) {
t.signalShutdown();
}
for (final SearchRateThread t : threads) {
final ResultCode r = t.waitForShutdown();
if (resultCode == ResultCode.SUCCESS) {
resultCode = r;
}
}
return resultCode;
}
use of com.unboundid.util.OutputFormat in project ldapsdk by pingidentity.
the class AuthRate method doToolProcessing.
/**
* Performs the actual processing for this tool. In this case, it gets a
* connection to the directory server and uses it to perform the requested
* searches.
*
* @return The result code for the processing that was performed.
*/
@Override()
@NotNull()
public ResultCode doToolProcessing() {
// variable rate data file and return.
if (sampleRateFile.isPresent()) {
try {
RateAdjustor.writeSampleVariableRateFile(sampleRateFile.getValue());
return ResultCode.SUCCESS;
} catch (final Exception e) {
Debug.debugException(e);
err("An error occurred while trying to write sample variable data " + "rate file '", sampleRateFile.getValue().getAbsolutePath(), "': ", StaticUtils.getExceptionMessage(e));
return ResultCode.LOCAL_ERROR;
}
}
// Determine the random seed to use.
final Long seed;
if (randomSeed.isPresent()) {
seed = Long.valueOf(randomSeed.getValue());
} else {
seed = null;
}
// Create value patterns for the base DN and filter.
final ValuePattern dnPattern;
try {
dnPattern = new ValuePattern(baseDN.getValue(), seed);
} catch (final ParseException pe) {
Debug.debugException(pe);
err("Unable to parse the base DN value pattern: ", pe.getMessage());
return ResultCode.PARAM_ERROR;
}
final ValuePattern filterPattern;
if (filter.isPresent()) {
try {
filterPattern = new ValuePattern(filter.getValue(), seed);
} catch (final ParseException pe) {
Debug.debugException(pe);
err("Unable to parse the filter pattern: ", pe.getMessage());
return ResultCode.PARAM_ERROR;
}
} else {
filterPattern = null;
}
// Get the attributes to return.
final String[] attrs;
if (attributes.isPresent()) {
final List<String> attrList = attributes.getValues();
attrs = new String[attrList.size()];
attrList.toArray(attrs);
} else {
attrs = StaticUtils.NO_STRINGS;
}
// If the --ratePerSecond option was specified, then limit the rate
// accordingly.
FixedRateBarrier fixedRateBarrier = null;
if (ratePerSecond.isPresent() || variableRateData.isPresent()) {
// We might not have a rate per second if --variableRateData is specified.
// The rate typically doesn't matter except when we have warm-up
// intervals. In this case, we'll run at the max rate.
final int intervalSeconds = collectionInterval.getValue();
final int ratePerInterval = (ratePerSecond.getValue() == null) ? Integer.MAX_VALUE : ratePerSecond.getValue() * intervalSeconds;
fixedRateBarrier = new FixedRateBarrier(1000L * intervalSeconds, ratePerInterval);
}
// If --variableRateData was specified, then initialize a RateAdjustor.
RateAdjustor rateAdjustor = null;
if (variableRateData.isPresent()) {
try {
rateAdjustor = RateAdjustor.newInstance(fixedRateBarrier, ratePerSecond.getValue(), variableRateData.getValue());
} catch (final IOException | IllegalArgumentException e) {
Debug.debugException(e);
err("Initializing the variable rates failed: " + e.getMessage());
return ResultCode.PARAM_ERROR;
}
}
// Determine whether to include timestamps in the output and if so what
// format should be used for them.
final boolean includeTimestamp;
final String timeFormat;
if (timestampFormat.getValue().equalsIgnoreCase("with-date")) {
includeTimestamp = true;
timeFormat = "dd/MM/yyyy HH:mm:ss";
} else if (timestampFormat.getValue().equalsIgnoreCase("without-date")) {
includeTimestamp = true;
timeFormat = "HH:mm:ss";
} else {
includeTimestamp = false;
timeFormat = null;
}
// Get the controls to include in bind requests.
final ArrayList<Control> bindControls = new ArrayList<>(5);
if (authorizationIdentityRequestControl.isPresent()) {
bindControls.add(new AuthorizationIdentityRequestControl());
}
if (passwordPolicyRequestControl.isPresent()) {
bindControls.add(new DraftBeheraLDAPPasswordPolicy10RequestControl());
}
bindControls.addAll(bindControl.getValues());
// Determine whether any warm-up intervals should be run.
final long totalIntervals;
final boolean warmUp;
int remainingWarmUpIntervals = warmUpIntervals.getValue();
if (remainingWarmUpIntervals > 0) {
warmUp = true;
totalIntervals = 0L + numIntervals.getValue() + remainingWarmUpIntervals;
} else {
warmUp = true;
totalIntervals = 0L + numIntervals.getValue();
}
// Create the table that will be used to format the output.
final OutputFormat outputFormat;
if (csvFormat.isPresent()) {
outputFormat = OutputFormat.CSV;
} else {
outputFormat = OutputFormat.COLUMNS;
}
final ColumnFormatter formatter = new ColumnFormatter(includeTimestamp, timeFormat, outputFormat, " ", new FormattableColumn(12, HorizontalAlignment.RIGHT, "Recent", "Auths/Sec"), new FormattableColumn(12, HorizontalAlignment.RIGHT, "Recent", "Avg Dur ms"), new FormattableColumn(12, HorizontalAlignment.RIGHT, "Recent", "Errors/Sec"), new FormattableColumn(12, HorizontalAlignment.RIGHT, "Overall", "Auths/Sec"), new FormattableColumn(12, HorizontalAlignment.RIGHT, "Overall", "Avg Dur ms"));
// Create values to use for statistics collection.
final AtomicLong authCounter = new AtomicLong(0L);
final AtomicLong errorCounter = new AtomicLong(0L);
final AtomicLong authDurations = new AtomicLong(0L);
final ResultCodeCounter rcCounter = new ResultCodeCounter();
// Determine the length of each interval in milliseconds.
final long intervalMillis = 1000L * collectionInterval.getValue();
// Create the threads to use for the searches.
final CyclicBarrier barrier = new CyclicBarrier(numThreads.getValue() + 1);
final AuthRateThread[] threads = new AuthRateThread[numThreads.getValue()];
for (int i = 0; i < threads.length; i++) {
final LDAPConnection searchConnection;
final LDAPConnection bindConnection;
try {
searchConnection = getConnection();
bindConnection = getConnection();
} catch (final LDAPException le) {
Debug.debugException(le);
err("Unable to connect to the directory server: ", StaticUtils.getExceptionMessage(le));
return le.getResultCode();
}
threads[i] = new AuthRateThread(this, i, searchConnection, bindConnection, dnPattern, scopeArg.getValue(), filterPattern, attrs, userPassword.getValue(), bindOnly.isPresent(), authType.getValue(), searchControl.getValues(), bindControls, runningThreads, barrier, authCounter, authDurations, errorCounter, rcCounter, fixedRateBarrier);
threads[i].start();
}
// Display the table header.
for (final String headerLine : formatter.getHeaderLines(true)) {
out(headerLine);
}
// which case, we'll start it after the warm-up is complete.
if ((rateAdjustor != null) && (remainingWarmUpIntervals <= 0)) {
rateAdjustor.start();
}
// Indicate that the threads can start running.
try {
barrier.await();
} catch (final Exception e) {
Debug.debugException(e);
}
long overallStartTime = System.nanoTime();
long nextIntervalStartTime = System.currentTimeMillis() + intervalMillis;
boolean setOverallStartTime = false;
long lastDuration = 0L;
long lastNumErrors = 0L;
long lastNumAuths = 0L;
long lastEndTime = System.nanoTime();
for (long i = 0; i < totalIntervals; i++) {
if (rateAdjustor != null) {
if (!rateAdjustor.isAlive()) {
out("All of the rates in " + variableRateData.getValue().getName() + " have been completed.");
break;
}
}
final long startTimeMillis = System.currentTimeMillis();
final long sleepTimeMillis = nextIntervalStartTime - startTimeMillis;
nextIntervalStartTime += intervalMillis;
if (sleepTimeMillis > 0) {
sleeper.sleep(sleepTimeMillis);
}
if (stopRequested.get()) {
break;
}
final long endTime = System.nanoTime();
final long intervalDuration = endTime - lastEndTime;
final long numAuths;
final long numErrors;
final long totalDuration;
if (warmUp && (remainingWarmUpIntervals > 0)) {
numAuths = authCounter.getAndSet(0L);
numErrors = errorCounter.getAndSet(0L);
totalDuration = authDurations.getAndSet(0L);
} else {
numAuths = authCounter.get();
numErrors = errorCounter.get();
totalDuration = authDurations.get();
}
final long recentNumAuths = numAuths - lastNumAuths;
final long recentNumErrors = numErrors - lastNumErrors;
final long recentDuration = totalDuration - lastDuration;
final double numSeconds = intervalDuration / 1_000_000_000.0d;
final double recentAuthRate = recentNumAuths / numSeconds;
final double recentErrorRate = recentNumErrors / numSeconds;
final double recentAvgDuration;
if (recentNumAuths > 0L) {
recentAvgDuration = 1.0d * recentDuration / recentNumAuths / 1_000_000;
} else {
recentAvgDuration = 0.0d;
}
if (warmUp && (remainingWarmUpIntervals > 0)) {
out(formatter.formatRow(recentAuthRate, recentAvgDuration, recentErrorRate, "warming up", "warming up"));
remainingWarmUpIntervals--;
if (remainingWarmUpIntervals == 0) {
out("Warm-up completed. Beginning overall statistics collection.");
setOverallStartTime = true;
if (rateAdjustor != null) {
rateAdjustor.start();
}
}
} else {
if (setOverallStartTime) {
overallStartTime = lastEndTime;
setOverallStartTime = false;
}
final double numOverallSeconds = (endTime - overallStartTime) / 1_000_000_000.0d;
final double overallAuthRate = numAuths / numOverallSeconds;
final double overallAvgDuration;
if (numAuths > 0L) {
overallAvgDuration = 1.0d * totalDuration / numAuths / 1_000_000;
} else {
overallAvgDuration = 0.0d;
}
out(formatter.formatRow(recentAuthRate, recentAvgDuration, recentErrorRate, overallAuthRate, overallAvgDuration));
lastNumAuths = numAuths;
lastNumErrors = numErrors;
lastDuration = totalDuration;
}
final List<ObjectPair<ResultCode, Long>> rcCounts = rcCounter.getCounts(true);
if ((!suppressErrorsArgument.isPresent()) && (!rcCounts.isEmpty())) {
err("\tError Results:");
for (final ObjectPair<ResultCode, Long> p : rcCounts) {
err("\t", p.getFirst().getName(), ": ", p.getSecond());
}
}
lastEndTime = endTime;
}
// Shut down the RateAdjustor if we have one.
if (rateAdjustor != null) {
rateAdjustor.shutDown();
}
// Stop all of the threads.
ResultCode resultCode = ResultCode.SUCCESS;
for (final AuthRateThread t : threads) {
final ResultCode r = t.stopRunning();
if (resultCode == ResultCode.SUCCESS) {
resultCode = r;
}
}
return resultCode;
}
use of com.unboundid.util.OutputFormat in project ldapsdk by pingidentity.
the class LDIFSearch method doExtendedArgumentValidation.
/**
* {@inheritDoc}
*/
@Override()
public void doExtendedArgumentValidation() throws ArgumentException {
// If the output file exists and either compressOutput or encryptOutput is
// present, then the overwrite argument must also be present.
final File outFile = outputFile.getValue();
if ((outFile != null) && outFile.exists() && (compressOutput.isPresent() || encryptOutput.isPresent()) && (!overwriteExistingOutputFile.isPresent())) {
throw new ArgumentException(ERR_LDIFSEARCH_APPEND_WITH_COMPRESSION_OR_ENCRYPTION.get(compressOutput.getIdentifierString(), encryptOutput.getIdentifierString(), overwriteExistingOutputFile.getIdentifierString()));
}
// Create the set of LDAP URLs to use when issuing the searches.
final List<String> trailingArgs = parser.getTrailingArguments();
final List<String> requestedAttributes = new ArrayList<>();
if (filterFile.isPresent()) {
// valid filter.
if (!trailingArgs.isEmpty()) {
try {
Filter.create(trailingArgs.get(0));
throw new ArgumentException(ERR_LDIFSEARCH_FILTER_FILE_WITH_TRAILING_FILTER.get());
} catch (final LDAPException e) {
// This was expected.
}
}
requestedAttributes.addAll(trailingArgs);
readFilterFile();
} else if (ldapURLFile.isPresent()) {
// Make sure there aren't any trailing arguments.
if (!trailingArgs.isEmpty()) {
throw new ArgumentException(ERR_LDIFSEARCH_LDAP_URL_FILE_WITH_TRAILING_ARGS.get());
}
readLDAPURLFile();
// requested attributes.
if ((searchURLs.size() > 1) && (!separateOutputFilePerSearch.isPresent())) {
final Iterator<LDAPURL> iterator = searchURLs.iterator();
final Set<String> requestedAttrs = new HashSet<>(Arrays.asList(iterator.next().getAttributes()));
while (iterator.hasNext()) {
final Set<String> attrSet = new HashSet<>(Arrays.asList(iterator.next().getAttributes()));
if (!requestedAttrs.equals(attrSet)) {
throw new ArgumentException(ERR_LDIFSEARCH_DIFFERENT_URL_ATTRS_IN_SAME_FILE.get(ldapURLFile.getIdentifierString(), separateOutputFilePerSearch.getIdentifierString()));
}
}
}
} else {
// requested arguments.
if (trailingArgs.isEmpty()) {
throw new ArgumentException(ERR_LDIFSEARCH_NO_FILTER.get());
}
final Filter filter;
try {
final List<String> trailingArgList = new ArrayList<>(trailingArgs);
final Iterator<String> trailingArgIterator = trailingArgList.iterator();
filter = Filter.create(trailingArgIterator.next());
while (trailingArgIterator.hasNext()) {
requestedAttributes.add(trailingArgIterator.next());
}
} catch (final LDAPException e) {
Debug.debugException(e);
throw new ArgumentException(ERR_LDIFSEARCH_FIRST_TRAILING_ARG_NOT_FILTER.get(), e);
}
DN dn = baseDN.getValue();
if (dn == null) {
dn = DN.NULL_DN;
}
SearchScope searchScope = scope.getValue();
if (searchScope == null) {
searchScope = SearchScope.SUB;
}
try {
searchURLs.add(new LDAPURL("ldap", null, null, dn, requestedAttributes.toArray(StaticUtils.NO_STRINGS), searchScope, filter));
} catch (final LDAPException e) {
Debug.debugException(e);
// This should never happen.
throw new ArgumentException(StaticUtils.getExceptionMessage(e), e);
}
}
// Create the result writer.
final String outputFormatStr = StaticUtils.toLowerCase(outputFormat.getValue());
if (outputFormatStr.equals("json")) {
resultWriter = new JSONLDAPResultWriter(getOut());
} else if (outputFormatStr.equals("csv") || outputFormatStr.equals("multi-valued-csv") || outputFormatStr.equals("tab-delimited") || outputFormatStr.equals("multi-valued-tab-delimited")) {
// These output formats cannot be used with the --ldapURLFile argument.
if (ldapURLFile.isPresent()) {
throw new ArgumentException(ERR_LDIFSEARCH_OUTPUT_FORMAT_NOT_SUPPORTED_WITH_URLS.get(outputFormat.getValue(), ldapURLFile.getIdentifierString()));
}
// These output formats require a set of requested attributes.
if (requestedAttributes.isEmpty()) {
throw new ArgumentException(ERR_LDIFSEARCH_OUTPUT_FORMAT_REQUIRES_REQUESTED_ATTRS.get(outputFormat.getValue()));
}
final OutputFormat format;
final boolean includeAllValues;
switch(outputFormatStr) {
case "multi-valued-csv":
format = OutputFormat.CSV;
includeAllValues = true;
break;
case "tab-delimited":
format = OutputFormat.TAB_DELIMITED_TEXT;
includeAllValues = false;
break;
case "multi-valued-tab-delimited":
format = OutputFormat.TAB_DELIMITED_TEXT;
includeAllValues = true;
break;
case "csv":
default:
format = OutputFormat.CSV;
includeAllValues = false;
break;
}
resultWriter = new ColumnBasedLDAPResultWriter(getOut(), format, requestedAttributes, WRAP_COLUMN, includeAllValues);
} else if (outputFormatStr.equals("dns-only")) {
resultWriter = new DNsOnlyLDAPResultWriter(getOut());
} else if (outputFormatStr.equals("values-only")) {
resultWriter = new ValuesOnlyLDAPResultWriter(getOut());
} else {
final int wc;
if (doNotWrap.isPresent()) {
wc = Integer.MAX_VALUE;
} else if (wrapColumn.isPresent()) {
wc = wrapColumn.getValue();
} else {
wc = WRAP_COLUMN;
}
resultWriter = new LDIFLDAPResultWriter(getOut(), wc);
}
}
use of com.unboundid.util.OutputFormat in project ldapsdk by pingidentity.
the class ModRate method doToolProcessing.
/**
* Performs the actual processing for this tool. In this case, it gets a
* connection to the directory server and uses it to perform the requested
* modifications.
*
* @return The result code for the processing that was performed.
*/
@Override()
@NotNull()
public ResultCode doToolProcessing() {
// variable rate data file and return.
if (sampleRateFile.isPresent()) {
try {
RateAdjustor.writeSampleVariableRateFile(sampleRateFile.getValue());
return ResultCode.SUCCESS;
} catch (final Exception e) {
Debug.debugException(e);
err("An error occurred while trying to write sample variable data " + "rate file '", sampleRateFile.getValue().getAbsolutePath(), "': ", StaticUtils.getExceptionMessage(e));
return ResultCode.LOCAL_ERROR;
}
}
// Determine the random seed to use.
final Long seed;
if (randomSeed.isPresent()) {
seed = Long.valueOf(randomSeed.getValue());
} else {
seed = null;
}
// Create the value patterns for the target entry DN and proxied
// authorization identities.
final ValuePattern dnPattern;
try {
dnPattern = new ValuePattern(entryDN.getValue(), seed);
} catch (final ParseException pe) {
Debug.debugException(pe);
err("Unable to parse the entry DN value pattern: ", pe.getMessage());
return ResultCode.PARAM_ERROR;
}
final ValuePattern authzIDPattern;
if (proxyAs.isPresent()) {
try {
authzIDPattern = new ValuePattern(proxyAs.getValue(), seed);
} catch (final ParseException pe) {
Debug.debugException(pe);
err("Unable to parse the proxied authorization pattern: ", pe.getMessage());
return ResultCode.PARAM_ERROR;
}
} else {
authzIDPattern = null;
}
// Get the set of controls to include in modify requests.
final ArrayList<Control> controlList = new ArrayList<>(5);
if (assertionFilter.isPresent()) {
controlList.add(new AssertionRequestControl(assertionFilter.getValue()));
}
if (permissiveModify.isPresent()) {
controlList.add(new PermissiveModifyRequestControl());
}
if (preReadAttribute.isPresent()) {
final List<String> attrList = preReadAttribute.getValues();
final String[] attrArray = new String[attrList.size()];
attrList.toArray(attrArray);
controlList.add(new PreReadRequestControl(attrArray));
}
if (postReadAttribute.isPresent()) {
final List<String> attrList = postReadAttribute.getValues();
final String[] attrArray = new String[attrList.size()];
attrList.toArray(attrArray);
controlList.add(new PostReadRequestControl(attrArray));
}
if (control.isPresent()) {
controlList.addAll(control.getValues());
}
final Control[] controlArray = new Control[controlList.size()];
controlList.toArray(controlArray);
// Get the names of the attributes to modify.
final String[] attrs = new String[attribute.getValues().size()];
attribute.getValues().toArray(attrs);
// If the --ratePerSecond option was specified, then limit the rate
// accordingly.
FixedRateBarrier fixedRateBarrier = null;
if (ratePerSecond.isPresent() || variableRateData.isPresent()) {
// We might not have a rate per second if --variableRateData is specified.
// The rate typically doesn't matter except when we have warm-up
// intervals. In this case, we'll run at the max rate.
final int intervalSeconds = collectionInterval.getValue();
final int ratePerInterval = (ratePerSecond.getValue() == null) ? Integer.MAX_VALUE : ratePerSecond.getValue() * intervalSeconds;
fixedRateBarrier = new FixedRateBarrier(1000L * intervalSeconds, ratePerInterval);
}
// If --variableRateData was specified, then initialize a RateAdjustor.
RateAdjustor rateAdjustor = null;
if (variableRateData.isPresent()) {
try {
rateAdjustor = RateAdjustor.newInstance(fixedRateBarrier, ratePerSecond.getValue(), variableRateData.getValue());
} catch (final IOException | IllegalArgumentException e) {
Debug.debugException(e);
err("Initializing the variable rates failed: " + e.getMessage());
return ResultCode.PARAM_ERROR;
}
}
// Determine whether to include timestamps in the output and if so what
// format should be used for them.
final boolean includeTimestamp;
final String timeFormat;
if (timestampFormat.getValue().equalsIgnoreCase("with-date")) {
includeTimestamp = true;
timeFormat = "dd/MM/yyyy HH:mm:ss";
} else if (timestampFormat.getValue().equalsIgnoreCase("without-date")) {
includeTimestamp = true;
timeFormat = "HH:mm:ss";
} else {
includeTimestamp = false;
timeFormat = null;
}
// Determine whether any warm-up intervals should be run.
final long totalIntervals;
final boolean warmUp;
int remainingWarmUpIntervals = warmUpIntervals.getValue();
if (remainingWarmUpIntervals > 0) {
warmUp = true;
totalIntervals = 0L + numIntervals.getValue() + remainingWarmUpIntervals;
} else {
warmUp = true;
totalIntervals = 0L + numIntervals.getValue();
}
// Create the table that will be used to format the output.
final OutputFormat outputFormat;
if (csvFormat.isPresent()) {
outputFormat = OutputFormat.CSV;
} else {
outputFormat = OutputFormat.COLUMNS;
}
final ColumnFormatter formatter = new ColumnFormatter(includeTimestamp, timeFormat, outputFormat, " ", new FormattableColumn(12, HorizontalAlignment.RIGHT, "Recent", "Mods/Sec"), new FormattableColumn(12, HorizontalAlignment.RIGHT, "Recent", "Avg Dur ms"), new FormattableColumn(12, HorizontalAlignment.RIGHT, "Recent", "Errors/Sec"), new FormattableColumn(12, HorizontalAlignment.RIGHT, "Overall", "Mods/Sec"), new FormattableColumn(12, HorizontalAlignment.RIGHT, "Overall", "Avg Dur ms"));
// Create values to use for statistics collection.
final AtomicLong modCounter = new AtomicLong(0L);
final AtomicLong errorCounter = new AtomicLong(0L);
final AtomicLong modDurations = new AtomicLong(0L);
final ResultCodeCounter rcCounter = new ResultCodeCounter();
// Determine the length of each interval in milliseconds.
final long intervalMillis = 1000L * collectionInterval.getValue();
// Create the threads to use for the modifications.
final CyclicBarrier barrier = new CyclicBarrier(numThreads.getValue() + 1);
final ModRateThread[] threads = new ModRateThread[numThreads.getValue()];
for (int i = 0; i < threads.length; i++) {
final LDAPConnection connection;
try {
connection = getConnection();
} catch (final LDAPException le) {
Debug.debugException(le);
err("Unable to connect to the directory server: ", StaticUtils.getExceptionMessage(le));
return le.getResultCode();
}
final String valuePatternString;
if (valuePattern.isPresent()) {
valuePatternString = valuePattern.getValue();
} else {
final int length;
if (valueLength.isPresent()) {
length = valueLength.getValue();
} else {
length = 10;
}
final String charSet;
if (characterSet.isPresent()) {
charSet = characterSet.getValue().replace("]", "]]").replace("[", "[[");
} else {
charSet = "abcdefghijklmnopqrstuvwxyz";
}
valuePatternString = "[random:" + length + ':' + charSet + ']';
}
final ValuePattern parsedValuePattern;
try {
parsedValuePattern = new ValuePattern(valuePatternString);
} catch (final ParseException e) {
Debug.debugException(e);
err(e.getMessage());
return ResultCode.PARAM_ERROR;
}
threads[i] = new ModRateThread(this, i, connection, dnPattern, attrs, parsedValuePattern, valueCount.getValue(), increment.isPresent(), incrementAmount.getValue(), controlArray, authzIDPattern, iterationsBeforeReconnect.getValue(), runningThreads, barrier, modCounter, modDurations, errorCounter, rcCounter, fixedRateBarrier);
threads[i].start();
}
// Display the table header.
for (final String headerLine : formatter.getHeaderLines(true)) {
out(headerLine);
}
// which case, we'll start it after the warm-up is complete.
if ((rateAdjustor != null) && (remainingWarmUpIntervals <= 0)) {
rateAdjustor.start();
}
// Indicate that the threads can start running.
try {
barrier.await();
} catch (final Exception e) {
Debug.debugException(e);
}
long overallStartTime = System.nanoTime();
long nextIntervalStartTime = System.currentTimeMillis() + intervalMillis;
boolean setOverallStartTime = false;
long lastDuration = 0L;
long lastNumErrors = 0L;
long lastNumMods = 0L;
long lastEndTime = System.nanoTime();
for (long i = 0; i < totalIntervals; i++) {
if (rateAdjustor != null) {
if (!rateAdjustor.isAlive()) {
out("All of the rates in " + variableRateData.getValue().getName() + " have been completed.");
break;
}
}
final long startTimeMillis = System.currentTimeMillis();
final long sleepTimeMillis = nextIntervalStartTime - startTimeMillis;
nextIntervalStartTime += intervalMillis;
if (sleepTimeMillis > 0) {
sleeper.sleep(sleepTimeMillis);
}
if (stopRequested.get()) {
break;
}
final long endTime = System.nanoTime();
final long intervalDuration = endTime - lastEndTime;
final long numMods;
final long numErrors;
final long totalDuration;
if (warmUp && (remainingWarmUpIntervals > 0)) {
numMods = modCounter.getAndSet(0L);
numErrors = errorCounter.getAndSet(0L);
totalDuration = modDurations.getAndSet(0L);
} else {
numMods = modCounter.get();
numErrors = errorCounter.get();
totalDuration = modDurations.get();
}
final long recentNumMods = numMods - lastNumMods;
final long recentNumErrors = numErrors - lastNumErrors;
final long recentDuration = totalDuration - lastDuration;
final double numSeconds = intervalDuration / 1_000_000_000.0d;
final double recentModRate = recentNumMods / numSeconds;
final double recentErrorRate = recentNumErrors / numSeconds;
final double recentAvgDuration;
if (recentNumMods > 0L) {
recentAvgDuration = 1.0d * recentDuration / recentNumMods / 1_000_000;
} else {
recentAvgDuration = 0.0d;
}
if (warmUp && (remainingWarmUpIntervals > 0)) {
out(formatter.formatRow(recentModRate, recentAvgDuration, recentErrorRate, "warming up", "warming up"));
remainingWarmUpIntervals--;
if (remainingWarmUpIntervals == 0) {
out("Warm-up completed. Beginning overall statistics collection.");
setOverallStartTime = true;
if (rateAdjustor != null) {
rateAdjustor.start();
}
}
} else {
if (setOverallStartTime) {
overallStartTime = lastEndTime;
setOverallStartTime = false;
}
final double numOverallSeconds = (endTime - overallStartTime) / 1_000_000_000.0d;
final double overallAuthRate = numMods / numOverallSeconds;
final double overallAvgDuration;
if (numMods > 0L) {
overallAvgDuration = 1.0d * totalDuration / numMods / 1_000_000;
} else {
overallAvgDuration = 0.0d;
}
out(formatter.formatRow(recentModRate, recentAvgDuration, recentErrorRate, overallAuthRate, overallAvgDuration));
lastNumMods = numMods;
lastNumErrors = numErrors;
lastDuration = totalDuration;
}
final List<ObjectPair<ResultCode, Long>> rcCounts = rcCounter.getCounts(true);
if ((!suppressErrorsArgument.isPresent()) && (!rcCounts.isEmpty())) {
err("\tError Results:");
for (final ObjectPair<ResultCode, Long> p : rcCounts) {
err("\t", p.getFirst().getName(), ": ", p.getSecond());
}
}
lastEndTime = endTime;
}
// Shut down the RateAdjustor if we have one.
if (rateAdjustor != null) {
rateAdjustor.shutDown();
}
// Stop all of the threads.
ResultCode resultCode = ResultCode.SUCCESS;
for (final ModRateThread t : threads) {
final ResultCode r = t.stopRunning();
if (resultCode == ResultCode.SUCCESS) {
resultCode = r;
}
}
return resultCode;
}
Aggregations