From b12cb96eeb2232e86e90625fe8620b9be0c22e1a Mon Sep 17 00:00:00 2001
From: boli <boli@localhost>
Date: Fri, 17 Sep 2010 17:11:12 +0000
Subject: [PATCH] - Improved usage information for searchrate and modrate - maxIteration is now for the entire run  - Better error handling with the --argument option

---
 opendj-sdk/sdk/src/com/sun/opends/sdk/tools/PerformanceRunner.java |   42 +++---
 opendj-sdk/sdk/src/com/sun/opends/sdk/tools/DataSource.java        |  177 +++++++++++++++++++++++-----
 opendj-sdk/sdk/src/com/sun/opends/sdk/tools/ArgumentParser.java    |   56 --------
 opendj-sdk/sdk/src/com/sun/opends/sdk/tools/Utils.java             |    4 
 opendj-sdk/sdk/src/com/sun/opends/sdk/tools/SearchRate.java        |    9 
 opendj-sdk/sdk/src/com/sun/opends/sdk/messages/messages.properties |    8 +
 opendj-sdk/sdk/src/com/sun/opends/sdk/tools/ModRate.java           |   13 +-
 opendj-sdk/sdk/src/com/sun/opends/sdk/tools/ToolConstants.java     |    7 +
 8 files changed, 192 insertions(+), 124 deletions(-)

diff --git a/opendj-sdk/sdk/src/com/sun/opends/sdk/messages/messages.properties b/opendj-sdk/sdk/src/com/sun/opends/sdk/messages/messages.properties
index 5cb6e2b..6b0a3a1 100755
--- a/opendj-sdk/sdk/src/com/sun/opends/sdk/messages/messages.properties
+++ b/opendj-sdk/sdk/src/com/sun/opends/sdk/messages/messages.properties
@@ -5844,5 +5844,13 @@
  client disconnected
 INFO_CANCELED_BY_CLIENT_ERROR=The operation was canceled because the \
  client connection failed
+INFO_SEARCHRATE_TOOL_DESCRIPTION=This utility can be used to measure \
+  search throughput and response time of a directory service using \
+  user-defined searches
+INFO_SEARCHRATE_TOOL_DESCRIPTION_BASEDN=Base DN format string.
+INFO_MODRATE_TOOL_DESCRIPTION=This utility can be used to measure \
+  modify throughput and response time of a directory service using \
+  user-defined modifications
+INFO_MODRATE_TOOL_DESCRIPTION_TARGETDN=Target entry DN format string
 
 
diff --git a/opendj-sdk/sdk/src/com/sun/opends/sdk/tools/ArgumentParser.java b/opendj-sdk/sdk/src/com/sun/opends/sdk/tools/ArgumentParser.java
index 8f60c53..a6488ed 100644
--- a/opendj-sdk/sdk/src/com/sun/opends/sdk/tools/ArgumentParser.java
+++ b/opendj-sdk/sdk/src/com/sun/opends/sdk/tools/ArgumentParser.java
@@ -1884,59 +1884,9 @@
     // indent the description five characters and try our best to wrap
     // at or
     // before column 79 so it will be friendly to 80-column displays.
-    final LocalizableMessage description = a.getDescription();
-    final int descMaxLength = MAX_LENGTH - indentLength - 1;
-    if (description.length() <= descMaxLength)
-    {
-      buffer.append(INDENT);
-      buffer.append(description);
-      buffer.append(EOL);
-    }
-    else
-    {
-      String s = description.toString();
-      while (s.length() > descMaxLength)
-      {
-        int spacePos = s.lastIndexOf(' ', descMaxLength);
-        if (spacePos > 0)
-        {
-          buffer.append(INDENT);
-          buffer.append(s.substring(0, spacePos).trim());
-          s = s.substring(spacePos + 1).trim();
-          buffer.append(EOL);
-        }
-        else
-        {
-          // There are no spaces in the first 74 columns. See if there
-          // is one
-          // after that point. If so, then break there. If not, then
-          // don't
-          // break at all.
-          spacePos = s.indexOf(' ');
-          if (spacePos > 0)
-          {
-            buffer.append(INDENT);
-            buffer.append(s.substring(0, spacePos).trim());
-            s = s.substring(spacePos + 1).trim();
-            buffer.append(EOL);
-          }
-          else
-          {
-            buffer.append(INDENT);
-            buffer.append(s);
-            s = "";
-            buffer.append(EOL);
-          }
-        }
-      }
-
-      if (s.length() > 0)
-      {
-        buffer.append(INDENT);
-        buffer.append(s);
-        buffer.append(EOL);
-      }
-    }
+    buffer.append(
+        Utils.wrapText(a.getDescription(), MAX_LENGTH, indentLength));
+    buffer.append(EOL);
 
     if (a.needsValue() && (a.getDefaultValue() != null)
         && (a.getDefaultValue().length() > 0))
diff --git a/opendj-sdk/sdk/src/com/sun/opends/sdk/tools/DataSource.java b/opendj-sdk/sdk/src/com/sun/opends/sdk/tools/DataSource.java
index bb529f7..49caa30 100644
--- a/opendj-sdk/sdk/src/com/sun/opends/sdk/tools/DataSource.java
+++ b/opendj-sdk/sdk/src/com/sun/opends/sdk/tools/DataSource.java
@@ -34,8 +34,9 @@
 import java.io.IOException;
 import java.util.*;
 
+import com.sun.opends.sdk.util.StaticUtils;
 import com.sun.opends.sdk.util.Validator;
-
+import org.opends.sdk.LocalizableMessage;
 
 
 /**
@@ -104,6 +105,12 @@
 
       return lines.get(next++);
     }
+
+    public static LocalizableMessage getUsage()
+    {
+      return LocalizableMessage.raw(
+        "\"inc({filename})\" Consecutive, incremental line from file");
+    }
   }
 
 
@@ -141,6 +148,12 @@
 
       return next++;
     }
+
+    public static LocalizableMessage getUsage()
+    {
+      return LocalizableMessage.raw(
+        "\"inc({min},{max})\" Consecutive, incremental number");
+    }
   }
 
 
@@ -185,6 +198,12 @@
     {
       return lines.get(random.nextInt(lines.size()));
     }
+
+    public static LocalizableMessage getUsage()
+    {
+      return LocalizableMessage.raw(
+        "\"rand({filename})\" Random line from file");
+    }
   }
 
 
@@ -218,6 +237,12 @@
     {
       return random.nextInt(range) + offset;
     }
+
+    public static LocalizableMessage getUsage()
+    {
+      return LocalizableMessage.raw(
+          "\"rand({min},{max})\" Random number");
+    }
   }
 
 
@@ -277,6 +302,17 @@
       }
       return new String(str);
     }
+
+    public static LocalizableMessage getUsage()
+    {
+      return LocalizableMessage.raw(
+        "\"randStr({length},<charSet>)\" Random string of specified " +
+            "length and optionally from characters in " +
+            "the charSet string. A range of character " +
+            "can be specified with [start-end] charSet notation. " +
+            "If no charSet is specified, the default charSet of " +
+            "[A-Z][a-z][0-9] will be used");
+    }
   }
 
 
@@ -351,11 +387,11 @@
    * @param sources
    *          The list of source definitions to parse.
    * @return The array of parsed data sources.
-   * @throws IOException
-   *           If an exception occurs while reading a file.
+   * @throws ArgumentException
+   *           If an exception occurs while parsing.
    */
   public static DataSource[] parse(final List<String> sources)
-      throws IOException
+      throws ArgumentException
   {
     Validator.ensureNotNull(sources);
     final DataSource[] dataSources = new DataSource[sources.size()];
@@ -367,22 +403,47 @@
         final int lparenPos = dataSourceDef.indexOf("(");
         final int commaPos = dataSourceDef.indexOf(",");
         final int rparenPos = dataSourceDef.indexOf(")");
+
         if (commaPos < 0)
         {
-          // This is a file name
-          dataSources[i] = new DataSource(new RandomLineFileDataSource(0,
-              dataSourceDef.substring(lparenPos + 1, rparenPos)));
+          try
+          {
+            // This is a file name
+            dataSources[i] = new DataSource(new RandomLineFileDataSource(0,
+                dataSourceDef.substring(lparenPos + 1, rparenPos)));
+          }
+          catch(IOException ioe)
+          {
+            throw new ArgumentException(LocalizableMessage.raw(
+                "Error opening file %s: %s",
+                dataSourceDef.substring(lparenPos + 1, rparenPos),
+                ioe.getMessage()), ioe);
+          }
+          catch(Exception e)
+          {
+            throw new ArgumentException(LocalizableMessage.raw(
+                "Error parsing value generator: %s", e.getMessage()), e);
+          }
         }
         else
         {
-          // This range of integers
-          final int low = Integer.parseInt(dataSourceDef.substring(
-              lparenPos + 1, commaPos));
-          final int high = Integer.parseInt(dataSourceDef.substring(
-              commaPos + 1, rparenPos));
-          dataSources[i] = new DataSource(new RandomNumberDataSource(Thread
-              .currentThread().getId(), low, high));
+          try
+          {
+            // This range of integers
+            final int low = Integer.parseInt(dataSourceDef.substring(
+                lparenPos + 1, commaPos));
+            final int high = Integer.parseInt(dataSourceDef.substring(
+                commaPos + 1, rparenPos));
+            dataSources[i] = new DataSource(new RandomNumberDataSource(Thread
+                .currentThread().getId(), low, high));
+          }
+          catch(Exception e)
+          {
+            throw new ArgumentException(LocalizableMessage.raw(
+                "Error parsing value generator: %s", e.getMessage()), e);
+          }
         }
+
       }
       else if (dataSourceDef.startsWith("randstr(")
           && dataSourceDef.endsWith(")"))
@@ -392,22 +453,29 @@
         final int rparenPos = dataSourceDef.indexOf(")");
         int length;
         String charSet;
-        if (commaPos < 0)
+        try
         {
-          length = Integer.parseInt(dataSourceDef.substring(lparenPos + 1,
-              rparenPos));
-          charSet = "[A-Z][a-z][0-9]";
+          if (commaPos < 0)
+          {
+            length = Integer.parseInt(dataSourceDef.substring(lparenPos + 1,
+                rparenPos));
+            charSet = "[A-Z][a-z][0-9]";
+          }
+          else
+          {
+            // length and charSet
+            length = Integer.parseInt(dataSourceDef.substring(lparenPos + 1,
+                commaPos));
+            charSet = dataSourceDef.substring(commaPos + 1, rparenPos);
+          }
+          dataSources[i] = new DataSource(new RandomStringDataSource(0, length,
+              charSet));
         }
-        else
+        catch(Exception e)
         {
-          // length and charSet
-          length = Integer.parseInt(dataSourceDef.substring(lparenPos + 1,
-              commaPos));
-          charSet = dataSourceDef.substring(commaPos + 1, rparenPos);
+          throw new ArgumentException(LocalizableMessage.raw(
+              "Error parsing value generator: %s", e.getMessage()), e);
         }
-        dataSources[i] = new DataSource(new RandomStringDataSource(0, length,
-            charSet));
-
       }
       else if (dataSourceDef.startsWith("inc(") && dataSourceDef.endsWith(")"))
       {
@@ -416,18 +484,41 @@
         final int rparenPos = dataSourceDef.indexOf(")");
         if (commaPos < 0)
         {
-          // This is a file name
-          dataSources[i] = new DataSource(new IncrementLineFileDataSource(
-              dataSourceDef.substring(lparenPos + 1, rparenPos)));
+          try
+          {
+            // This is a file name
+            dataSources[i] = new DataSource(new IncrementLineFileDataSource(
+                dataSourceDef.substring(lparenPos + 1, rparenPos)));
+          }
+          catch(IOException ioe)
+          {
+            throw new ArgumentException(LocalizableMessage.raw(
+                "Error opening file %s: %s",
+                dataSourceDef.substring(lparenPos + 1, rparenPos),
+                ioe.getMessage()), ioe);
+          }
+          catch(Exception e)
+          {
+            throw new ArgumentException(LocalizableMessage.raw(
+                "Error parsing value generator: %s", e.getMessage()), e);
+          }
         }
         else
         {
-          final int low = Integer.parseInt(dataSourceDef.substring(
-              lparenPos + 1, commaPos));
-          final int high = Integer.parseInt(dataSourceDef.substring(
-              commaPos + 1, rparenPos));
-          dataSources[i] = new DataSource(new IncrementNumberDataSource(low,
-              high));
+          try
+          {
+            final int low = Integer.parseInt(dataSourceDef.substring(
+                lparenPos + 1, commaPos));
+            final int high = Integer.parseInt(dataSourceDef.substring(
+                commaPos + 1, rparenPos));
+            dataSources[i] = new DataSource(new IncrementNumberDataSource(low,
+                high));
+          }
+          catch(Exception e)
+          {
+            throw new ArgumentException(LocalizableMessage.raw(
+                "Error parsing value generator: %s", e.getMessage()), e);
+          }
         }
       }
       else
@@ -448,6 +539,22 @@
   }
 
 
+  public static LocalizableMessage getUsage()
+  {
+    StringBuilder builder = new StringBuilder();
+    builder.append(IncrementLineFileDataSource.getUsage());
+    builder.append(StaticUtils.EOL);
+    builder.append(IncrementNumberDataSource.getUsage());
+    builder.append(StaticUtils.EOL);
+    builder.append(RandomLineFileDataSource.getUsage());
+    builder.append(StaticUtils.EOL);
+    builder.append(RandomNumberDataSource.getUsage());
+    builder.append(StaticUtils.EOL);
+    builder.append(RandomStringDataSource.getUsage());
+    return LocalizableMessage.raw(builder.toString());
+  }
+
+
 
   private final IDataSource impl;
 
diff --git a/opendj-sdk/sdk/src/com/sun/opends/sdk/tools/ModRate.java b/opendj-sdk/sdk/src/com/sun/opends/sdk/tools/ModRate.java
index 548180c..488fedb 100644
--- a/opendj-sdk/sdk/src/com/sun/opends/sdk/tools/ModRate.java
+++ b/opendj-sdk/sdk/src/com/sun/opends/sdk/tools/ModRate.java
@@ -318,12 +318,11 @@
   {
     // Create the command-line argument parser for use with this
     // program.
-    final LocalizableMessage toolDescription = LocalizableMessage
-        .raw("This utility can be used to " + "measure modify performance");
-    // TODO: correct usage
+    final LocalizableMessage toolDescription =
+        INFO_MODRATE_TOOL_DESCRIPTION.get();
     final ArgumentParser argParser = new ArgumentParser(
         ModRate.class.getName(), toolDescription, false, true, 1, 0,
-        "[modifyString ...]");
+        "[(attribute:value format string) ...]");
     ArgumentParserConnectionFactory connectionFactory;
     ModifyPerformanceRunner runner;
 
@@ -353,9 +352,9 @@
       argParser.addArgument(noPropertiesFileArgument);
       argParser.setNoPropertiesFileArgument(noPropertiesFileArgument);
 
-      baseDN = new StringArgument("baseDN", OPTION_SHORT_BASEDN,
-          OPTION_LONG_BASEDN, true, false, true, INFO_BASEDN_PLACEHOLDER.get(),
-          null, null, INFO_SEARCH_DESCRIPTION_BASEDN.get());
+      baseDN = new StringArgument("targetDN", OPTION_SHORT_BASEDN,
+          OPTION_LONG_TARGETDN, true, false, true, INFO_TARGETDN_PLACEHOLDER.get(),
+          null, null, INFO_MODRATE_TOOL_DESCRIPTION_TARGETDN.get());
       baseDN.setPropertyName(OPTION_LONG_BASEDN);
       argParser.addArgument(baseDN);
 
diff --git a/opendj-sdk/sdk/src/com/sun/opends/sdk/tools/PerformanceRunner.java b/opendj-sdk/sdk/src/com/sun/opends/sdk/tools/PerformanceRunner.java
index 1b7d3e7..e03b631 100644
--- a/opendj-sdk/sdk/src/com/sun/opends/sdk/tools/PerformanceRunner.java
+++ b/opendj-sdk/sdk/src/com/sun/opends/sdk/tools/PerformanceRunner.java
@@ -37,6 +37,7 @@
 import java.util.concurrent.atomic.AtomicLong;
 import java.util.concurrent.atomic.AtomicReference;
 
+import com.sun.opends.sdk.util.StaticUtils;
 import org.opends.sdk.*;
 import org.opends.sdk.responses.ExtendedResult;
 import org.opends.sdk.responses.Result;
@@ -810,21 +811,21 @@
     numThreadsArgument = new IntegerArgument("numThreads", 't', "numThreads",
         false, false, true, LocalizableMessage.raw("{numThreads}"), 1, null,
         true, 1, false, 0, LocalizableMessage
-            .raw("number of search threads per connection"));
+            .raw("Number of worker threads per connection"));
     numThreadsArgument.setPropertyName("numThreads");
     argParser.addArgument(numThreadsArgument);
 
     numConnectionsArgument = new IntegerArgument("numConnections", 'c',
         "numConnections", false, false, true, LocalizableMessage
             .raw("{numConnections}"), 1, null, true, 1, false, 0,
-        LocalizableMessage.raw("number of connections"));
+        LocalizableMessage.raw("Number of connections"));
     numThreadsArgument.setPropertyName("numConnections");
     argParser.addArgument(numConnectionsArgument);
 
     maxIterationsArgument = new IntegerArgument("maxIterations", 'm',
         "maxIterations", false, false, true, LocalizableMessage
             .raw("{maxIterations}"), 0, null, LocalizableMessage
-            .raw("max searches per thread, 0 for unlimited"));
+            .raw("Max iterations, 0 for unlimited"));
     numThreadsArgument.setPropertyName("maxIterations");
     argParser.addArgument(maxIterationsArgument);
 
@@ -852,25 +853,31 @@
     argParser.addArgument(percentilesArgument);
 
     keepConnectionsOpen = new BooleanArgument("keepConnectionsOpen", 'f',
-        "keepConnectionsOpen", LocalizableMessage.raw("keep connections open"));
+        "keepConnectionsOpen", LocalizableMessage.raw("Keep connections open"));
     keepConnectionsOpen.setPropertyName("keepConnectionsOpen");
     argParser.addArgument(keepConnectionsOpen);
 
     noRebindArgument = new BooleanArgument("noRebind", 'F', "noRebind",
-        LocalizableMessage.raw("keep connections open and don't rebind"));
+        LocalizableMessage.raw("Keep connections open and don't rebind"));
     keepConnectionsOpen.setPropertyName("noRebind");
     argParser.addArgument(noRebindArgument);
 
     asyncArgument = new BooleanArgument("asynchronous", 'A', "asynchronous",
-        LocalizableMessage.raw("asynch, don't wait for results"));
+        LocalizableMessage.raw("Use asynchronous mode and don't " +
+            "wait for results before sending the next request"));
     keepConnectionsOpen.setPropertyName("asynchronous");
     argParser.addArgument(asyncArgument);
 
-    arguments = new StringArgument("arguments", 'g', "arguments", false, true,
-        true, LocalizableMessage.raw("{arguments}"), null, null,
-        LocalizableMessage
-            .raw("arguments for variables in the filter and/or base DN"));
-    arguments.setPropertyName("arguments");
+    arguments = new StringArgument("argument", 'g', "argument", false, true,
+        true, LocalizableMessage.raw("{generator function or static string}"),
+        null, null,
+        LocalizableMessage.raw("Argument used to evaluate the Java " +
+            "style format strings in program parameters (ie. Base DN, " +
+            "Search Filter). The set of all arguments provided form the " +
+            "the argument list in order. Besides static string " +
+            "arguments, they can be generated per iteration with the " +
+            "following functions: " + StaticUtils.EOL +
+            DataSource.getUsage()));
     argParser.addArgument(arguments);
   }
 
@@ -912,7 +919,8 @@
   {
     numConnections = numConnectionsArgument.getIntValue();
     numThreads = numThreadsArgument.getIntValue();
-    maxIterations = maxIterationsArgument.getIntValue();
+    maxIterations = maxIterationsArgument.getIntValue() /
+        numConnections / numThreads;
     statsInterval = statsIntervalArgument.getIntValue() * 1000;
     targetThroughput = targetThroughputArgument.getIntValue();
 
@@ -933,15 +941,7 @@
           + " must be used when using --" + asyncArgument.getLongIdentifier()));
     }
 
-    try
-    {
-      dataSourcePrototypes = DataSource.parse(arguments.getValues());
-    }
-    catch (final IOException ioe)
-    {
-      throw new ArgumentException(LocalizableMessage
-          .raw("Error occured while parsing arguments: " + ioe.toString()));
-    }
+    dataSourcePrototypes = DataSource.parse(arguments.getValues());
   }
 
 
diff --git a/opendj-sdk/sdk/src/com/sun/opends/sdk/tools/SearchRate.java b/opendj-sdk/sdk/src/com/sun/opends/sdk/tools/SearchRate.java
index c30c921..be6a73a 100644
--- a/opendj-sdk/sdk/src/com/sun/opends/sdk/tools/SearchRate.java
+++ b/opendj-sdk/sdk/src/com/sun/opends/sdk/tools/SearchRate.java
@@ -374,12 +374,11 @@
   {
     // Create the command-line argument parser for use with this
     // program.
-    final LocalizableMessage toolDescription = LocalizableMessage
-        .raw("This utility can be used to " + "measure search performance");
-    // TODO: correct usage
+    final LocalizableMessage toolDescription =
+        INFO_SEARCHRATE_TOOL_DESCRIPTION.get();
     final ArgumentParser argParser = new ArgumentParser(SearchRate.class
         .getName(), toolDescription, false, true, 1, 0,
-        "[filter] [attributes ...]");
+        "[filter format string] [attributes ...]");
 
     ArgumentParserConnectionFactory connectionFactory;
     SearchPerformanceRunner runner;
@@ -420,7 +419,7 @@
 
       baseDN = new StringArgument("baseDN", OPTION_SHORT_BASEDN,
           OPTION_LONG_BASEDN, true, false, true, INFO_BASEDN_PLACEHOLDER.get(),
-          null, null, INFO_SEARCH_DESCRIPTION_BASEDN.get());
+          null, null, INFO_SEARCHRATE_TOOL_DESCRIPTION_BASEDN.get());
       baseDN.setPropertyName(OPTION_LONG_BASEDN);
       argParser.addArgument(baseDN);
 
diff --git a/opendj-sdk/sdk/src/com/sun/opends/sdk/tools/ToolConstants.java b/opendj-sdk/sdk/src/com/sun/opends/sdk/tools/ToolConstants.java
index b62fd4a..9eba321 100755
--- a/opendj-sdk/sdk/src/com/sun/opends/sdk/tools/ToolConstants.java
+++ b/opendj-sdk/sdk/src/com/sun/opends/sdk/tools/ToolConstants.java
@@ -22,7 +22,7 @@
  * CDDL HEADER END
  *
  *
- *      Copyright 2006-2009 Sun Microsystems, Inc.
+ *      Copyright 2006-2010 Sun Microsystems, Inc.
  */
 package com.sun.opends.sdk.tools;
 
@@ -559,6 +559,11 @@
   static final String OPTION_LONG_DONT_WRAP = "dontWrap";
 
   /**
+   * The value for the long option targetDN.
+   */
+  static final String OPTION_LONG_TARGETDN = "targetDN";
+
+  /**
    * Long form of email notification upon completion option.
    */
   static final String OPTION_LONG_COMPLETION_NOTIFICATION_EMAIL = "completionNotify";
diff --git a/opendj-sdk/sdk/src/com/sun/opends/sdk/tools/Utils.java b/opendj-sdk/sdk/src/com/sun/opends/sdk/tools/Utils.java
index d8ba7cc..855e1fe 100644
--- a/opendj-sdk/sdk/src/com/sun/opends/sdk/tools/Utils.java
+++ b/opendj-sdk/sdk/src/com/sun/opends/sdk/tools/Utils.java
@@ -22,7 +22,7 @@
  * CDDL HEADER END
  *
  *
- *      Copyright 2006-2008 Sun Microsystems, Inc.
+ *      Copyright 2006-2010 Sun Microsystems, Inc.
  */
 package com.sun.opends.sdk.tools;
 
@@ -616,7 +616,7 @@
           // It's an end-of-line character, so append it as-is.
           buffer.append(line);
         }
-        else if (line.length() < width)
+        else if (line.length() <= width)
         {
           // The line fits in the specified width, so append it as-is.
           buffer.append(padding);

--
Gitblit v1.10.0