Revision: 8387
          
http://languagetool.svn.sourceforge.net/languagetool/?rev=8387&view=rev
Author:   dnaber
Date:     2012-11-12 22:15:26 +0000 (Mon, 12 Nov 2012)
Log Message:
-----------
adding a load test for the HTTPS service - will not run with unit tests but 
needs to be started manually

Modified Paths:
--------------
    
trunk/JLanguageTool/src/test/java/org/languagetool/server/HTTPSServerTest.java
    
trunk/JLanguageTool/src/test/java/org/languagetool/server/HTTPServerLoadTest.java
    
trunk/JLanguageTool/src/test/java/org/languagetool/server/HTTPServerTest.java

Added Paths:
-----------
    
trunk/JLanguageTool/src/test/java/org/languagetool/server/ExampleSentence.java
    
trunk/JLanguageTool/src/test/java/org/languagetool/server/ExampleSentenceProvider.java
    
trunk/JLanguageTool/src/test/java/org/languagetool/server/HTTPSServerTesting.java
    trunk/JLanguageTool/src/test/java/org/languagetool/server/HTTPTools.java

Added: 
trunk/JLanguageTool/src/test/java/org/languagetool/server/ExampleSentence.java
===================================================================
--- 
trunk/JLanguageTool/src/test/java/org/languagetool/server/ExampleSentence.java  
                            (rev 0)
+++ 
trunk/JLanguageTool/src/test/java/org/languagetool/server/ExampleSentence.java  
    2012-11-12 22:15:26 UTC (rev 8387)
@@ -0,0 +1,41 @@
+/* LanguageTool, a natural language style checker
+ * Copyright (C) 2012 Daniel Naber (http://www.danielnaber.de)
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
+ * USA
+ */
+package org.languagetool.server;
+
+/**
+ * A sentence with an error detected by a rule and that rule's id.
+ */
+class ExampleSentence {
+
+  private final String sentence;
+  private final String ruleId;
+
+  ExampleSentence(String sentence, String ruleId) {
+    this.sentence = sentence;
+    this.ruleId = ruleId;
+  }
+
+  public String getSentence() {
+    return sentence;
+  }
+
+  public String getRuleId() {
+    return ruleId;
+  }
+}

Added: 
trunk/JLanguageTool/src/test/java/org/languagetool/server/ExampleSentenceProvider.java
===================================================================
--- 
trunk/JLanguageTool/src/test/java/org/languagetool/server/ExampleSentenceProvider.java
                              (rev 0)
+++ 
trunk/JLanguageTool/src/test/java/org/languagetool/server/ExampleSentenceProvider.java
      2012-11-12 22:15:26 UTC (rev 8387)
@@ -0,0 +1,79 @@
+/* LanguageTool, a natural language style checker
+ * Copyright (C) 2012 Daniel Naber (http://www.danielnaber.de)
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
+ * USA
+ */
+package org.languagetool.server;
+
+import org.languagetool.JLanguageTool;
+import org.languagetool.Language;
+import org.languagetool.rules.IncorrectExample;
+import org.languagetool.rules.Rule;
+import org.languagetool.rules.patterns.PatternRule;
+
+import java.io.IOException;
+import java.util.*;
+
+/**
+ * Provides example sentences from the XML rules.
+ */
+class ExampleSentenceProvider {
+
+  private final int minSentences;
+  private final int maxSentences;
+  private final Random rnd = new Random(12345);
+  private final Map<Language, List<ExampleSentence>> languageToExamples = new 
HashMap<Language, List<ExampleSentence>>();
+
+  ExampleSentenceProvider(int minSentences, int maxSentences) {
+    if (minSentences > maxSentences) {
+      throw new IllegalArgumentException("min > max: " + minSentences + " > " 
+ maxSentences);
+    }
+    this.minSentences = minSentences;
+    this.maxSentences = maxSentences;
+    for (Language language : Language.REAL_LANGUAGES) {
+      try {
+        initExampleSentences(language);
+      } catch (IOException e) {
+        throw new RuntimeException(e);
+      }
+    }
+  }
+
+  private void initExampleSentences(Language language) throws IOException {
+    final JLanguageTool lt = new JLanguageTool(language);
+    lt.activateDefaultPatternRules();
+    final List<Rule> rules = lt.getAllActiveRules();
+    final List<ExampleSentence> sentences = new ArrayList<ExampleSentence>();
+    for (Rule rule : rules) {
+      if (rule instanceof PatternRule && !rule.isDefaultOff()) {
+        final List<IncorrectExample> incorrectExamples = 
rule.getIncorrectExamples();
+        for (IncorrectExample incorrectExample : incorrectExamples) {
+          final ExampleSentence sentence = new 
ExampleSentence(incorrectExample.getExample(), rule.getId());
+          sentences.add(sentence);
+        }
+      }
+    }
+    languageToExamples.put(language, sentences);
+  }
+
+  List<ExampleSentence> getRandomSentences(Language lang) {
+    final List<ExampleSentence> sentences = languageToExamples.get(lang);
+    final int sentenceCount = rnd.nextInt(Math.max(1, maxSentences - 
minSentences)) + minSentences;
+    Collections.shuffle(sentences);
+    return sentences.subList(0, Math.min(sentences.size(), sentenceCount));
+  }
+
+}

Modified: 
trunk/JLanguageTool/src/test/java/org/languagetool/server/HTTPSServerTest.java
===================================================================
--- 
trunk/JLanguageTool/src/test/java/org/languagetool/server/HTTPSServerTest.java  
    2012-11-12 22:05:33 UTC (rev 8386)
+++ 
trunk/JLanguageTool/src/test/java/org/languagetool/server/HTTPSServerTest.java  
    2012-11-12 22:15:26 UTC (rev 8387)
@@ -19,19 +19,11 @@
 package org.languagetool.server;
 
 import org.junit.Test;
-import org.languagetool.tools.StringTools;
 
-import javax.net.ssl.HttpsURLConnection;
-import javax.net.ssl.SSLContext;
-import javax.net.ssl.TrustManager;
-import javax.net.ssl.X509TrustManager;
 import java.io.File;
 import java.io.IOException;
-import java.io.InputStream;
 import java.net.SocketException;
 import java.net.URL;
-import java.security.KeyManagementException;
-import java.security.NoSuchAlgorithmException;
 
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
@@ -43,7 +35,7 @@
 
   @Test
   public void testHTTPSServer() throws Exception {
-    disableCertChecks();
+    HTTPTools.disableCertChecks();
     final URL keystore = HTTPSServerTest.class.getResource(KEYSTORE);
     if (keystore == null) {
       throw new RuntimeException("Not found in classpath : " + KEYSTORE);
@@ -59,47 +51,16 @@
     }
   }
 
-  /**
-   * For testing, we disable all checks because we use a self-signed 
certificate on the server
-   * side and we want this test to run everywhere without importing the 
certificate into the JVM's trust store.
-   *
-   * See 
http://stackoverflow.com/questions/2893819/telling-java-to-accept-self-signed-ssl-certificate
-   */
-  private void disableCertChecks() throws NoSuchAlgorithmException, 
KeyManagementException {
-    final TrustManager[] trustAllCerts = new TrustManager[] {
-            new X509TrustManager() {
-              public java.security.cert.X509Certificate[] getAcceptedIssuers() 
{
-                return null;
-              }
-              public void checkClientTrusted(
-                      java.security.cert.X509Certificate[] certs, String 
authType) {
-              }
-              public void checkServerTrusted(
-                      java.security.cert.X509Certificate[] certs, String 
authType) {
-              }
-            }
-    };
-    final SSLContext sc = SSLContext.getInstance("SSL");
-    sc.init(null, trustAllCerts, new java.security.SecureRandom());
-    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
-  }
-
   private void runTests() throws IOException {
     final String httpsPrefix = "https://localhost:"; + 
HTTPServerConfig.DEFAULT_PORT + "/";
     try {
       final String httpPrefix = "http://localhost:"; + 
HTTPServerConfig.DEFAULT_PORT + "/";
-      checkAtUrl(new URL(httpPrefix + "?text=a+test&language=en"));
+      HTTPTools.checkAtUrl(new URL(httpPrefix + "?text=a+test&language=en"));
       fail("HTTP should not work, only HTTPS");
     } catch (SocketException expected) {}
 
-    final String result = checkAtUrl(new URL(httpsPrefix + 
"?text=a+test&language=en"));
+    final String result = HTTPTools.checkAtUrl(new URL(httpsPrefix + 
"?text=a+test&language=en"));
     assertTrue("Got " + result, result.contains("UPPERCASE_SENTENCE_START"));
   }
 
-  private String checkAtUrl(URL url) throws IOException {
-    final InputStream stream = (InputStream)url.getContent();
-    final String result = StringTools.streamToString(stream, "UTF-8");
-    return result;
-  }
-
 }

Added: 
trunk/JLanguageTool/src/test/java/org/languagetool/server/HTTPSServerTesting.java
===================================================================
--- 
trunk/JLanguageTool/src/test/java/org/languagetool/server/HTTPSServerTesting.java
                           (rev 0)
+++ 
trunk/JLanguageTool/src/test/java/org/languagetool/server/HTTPSServerTesting.java
   2012-11-12 22:15:26 UTC (rev 8387)
@@ -0,0 +1,124 @@
+/* LanguageTool, a natural language style checker
+ * Copyright (C) 2012 Daniel Naber (http://www.danielnaber.de)
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
+ * USA
+ */
+package org.languagetool.server;
+
+import org.junit.Ignore;
+import org.junit.Test;
+import org.languagetool.Language;
+
+import java.io.IOException;
+import java.net.URL;
+import java.net.URLEncoder;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Random;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Load testing the HTTPS server. Start the server yourself.
+ */
+public class HTTPSServerTesting {
+
+  private static final String SERVER_URL = "https://localhost:8081";;
+  //private static final String SERVER_URL = "https://languagetool.org:8081";;
+  private static final int REPEAT_COUNT = 100;
+  private static final int THREAD_COUNT = 1;
+
+  private final ExampleSentenceProvider provider = new 
ExampleSentenceProvider(1, 500);
+  private final Random rnd = new Random(10);
+  private int checkCount = 0;
+
+  @Ignore("For interactive testing, thus ignored for unit tests")
+  @Test
+  public void interactiveHTTPServerTest() throws Exception {
+    HTTPTools.disableCertChecks();
+    final long startTime = System.currentTimeMillis();
+    try {
+      final ExecutorService executorService = 
Executors.newFixedThreadPool(THREAD_COUNT);
+      final List<Future> futures = new ArrayList<Future>();
+      for (int i = 0; i < THREAD_COUNT; i++) {
+        final Future<?> future = executorService.submit(new TestRunnable());
+        futures.add(future);
+      }
+      for (Future future : futures) {
+        future.get();
+      }
+    } finally {
+      final long runtime = System.currentTimeMillis() - startTime;
+      System.out.println("Running with " + THREAD_COUNT + " threads in " + 
runtime + "ms for " + checkCount + " checks");
+      if (checkCount > 0) {
+        final long timePerCheck = runtime / checkCount;
+        System.out.println(" => on average " + timePerCheck + "ms per check");
+      }
+    }
+  }
+
+  private class TestRunnable implements Runnable {
+    @Override
+    public void run() {
+      try {
+        for (int i = 0; i < REPEAT_COUNT; i++) {
+          runTests();
+        }
+      } catch (Exception e) {
+        throw new RuntimeException(e);
+      }
+    }
+  }
+
+  private void runTests() throws IOException {
+    final Language[] languages = Language.REAL_LANGUAGES;
+    final Language lang = languages[rnd.nextInt(languages.length)];
+    final List<ExampleSentence> sentences = provider.getRandomSentences(lang);
+    final String text = getSentencesAsText(sentences);
+    final String data = "language=" + lang.getShortNameWithVariant() + 
"&text=" + URLEncoder.encode(text, "utf-8");
+    final String resultXml = checkAtUrl(new URL(SERVER_URL), data);
+    for (ExampleSentence sentence : sentences) {
+      assertTrue("Expected " + sentence.getRuleId() + " for '" + text + "'", 
resultXml.contains(sentence.getRuleId()));
+    }
+  }
+
+  private String getSentencesAsText(List<ExampleSentence> sentences) {
+    final StringBuilder sb = new StringBuilder();
+    for (ExampleSentence sentence : sentences) {
+      final String sentenceStr = sentence.getSentence().replace("<marker>", 
"").replace("</marker>", "");
+      final String cleanSentenceStr = sentenceStr.replaceAll("[\\n\\t]+", "");
+      sb.append(cleanSentenceStr);
+      sb.append("\n\n");
+    }
+    return sb.toString();
+  }
+
+  private String checkAtUrl(URL url, String data) throws IOException {
+    final long startTime = System.currentTimeMillis();
+    final String startOfData = data.substring(0, Math.min(30, data.length()));
+    synchronized(this) {
+      checkCount++;
+    }
+    final String result = HTTPTools.checkAtUrlByPost(url, data);
+    System.out.println(checkCount + ". Got " + url + " with data (" + 
data.length() + " bytes) " + startOfData
+            + "...: " + (System.currentTimeMillis() - startTime) + "ms");
+    return result;
+  }
+
+}

Modified: 
trunk/JLanguageTool/src/test/java/org/languagetool/server/HTTPServerLoadTest.java
===================================================================
--- 
trunk/JLanguageTool/src/test/java/org/languagetool/server/HTTPServerLoadTest.java
   2012-11-12 22:05:33 UTC (rev 8386)
+++ 
trunk/JLanguageTool/src/test/java/org/languagetool/server/HTTPServerLoadTest.java
   2012-11-12 22:15:26 UTC (rev 8387)
@@ -39,7 +39,7 @@
       System.out.println("Running with " + THREAD_COUNT + " threads in " + 
runtime + "ms");
     }
   }
-  
+
   @Override
   public void testAccessDenied() throws Exception {
     // no need to test this here, tested in super class

Modified: 
trunk/JLanguageTool/src/test/java/org/languagetool/server/HTTPServerTest.java
===================================================================
--- 
trunk/JLanguageTool/src/test/java/org/languagetool/server/HTTPServerTest.java   
    2012-11-12 22:05:33 UTC (rev 8386)
+++ 
trunk/JLanguageTool/src/test/java/org/languagetool/server/HTTPServerTest.java   
    2012-11-12 22:15:26 UTC (rev 8387)
@@ -18,16 +18,6 @@
  */
 package org.languagetool.server;
 
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStreamWriter;
-import java.net.URL;
-import java.net.URLConnection;
-import java.net.URLEncoder;
-import java.util.HashSet;
-
-import javax.xml.parsers.ParserConfigurationException;
-
 import org.apache.commons.lang.StringUtils;
 import org.junit.Ignore;
 import org.junit.Test;
@@ -37,9 +27,14 @@
 import org.languagetool.tools.StringTools;
 import org.xml.sax.SAXException;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
+import javax.xml.parsers.ParserConfigurationException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.net.URLEncoder;
+import java.util.HashSet;
+
+import static org.junit.Assert.*;
 import static org.languagetool.server.HTTPServerConfig.DEFAULT_PORT;
 
 public class HTTPServerTest {
@@ -180,7 +175,7 @@
       try {
         System.out.println("Testing 'missing language parameter' now, please 
ignore the exception");
         final URL url = new URL("http://localhost:"; + DEFAULT_PORT + 
"/?text=foo");
-        checkAtUrl(url);
+        HTTPTools.checkAtUrl(url);
         fail();
       } catch (IOException expected) {
       }
@@ -197,7 +192,7 @@
       urlOptions += "&motherTongue="+motherTongue.getShortName();
     }
     final URL url = new URL("http://localhost:"; + DEFAULT_PORT + urlOptions);
-    return checkAtUrl(url);
+    return HTTPTools.checkAtUrl(url);
   }
 
   private String check(Language lang, String text) throws IOException {
@@ -211,15 +206,9 @@
        urlOptions += "&motherTongue=" + motherTongue.getShortName();
     }
     final URL url = new URL("http://localhost:"; + DEFAULT_PORT + urlOptions);
-    return checkAtUrl(url);
+    return HTTPTools.checkAtUrl(url);
   }
 
-  private String checkAtUrl(URL url) throws IOException {
-    final InputStream stream = (InputStream)url.getContent();
-    final String result = StringTools.streamToString(stream, "UTF-8");
-    return result;
-  }
-
   private String checkWithOptions(Language lang, Language motherTongue, String 
text,
                                   String[] enabledRules, String[] 
disabledRules) throws IOException {
     String urlOptions = "/?language=" + lang.getShortName();
@@ -236,7 +225,7 @@
     }
 
     final URL url = new URL("http://localhost:"; + DEFAULT_PORT + urlOptions);
-    return checkAtUrl(url);
+    return HTTPTools.checkAtUrl(url);
   }
   
   /**
@@ -245,16 +234,7 @@
   private String checkByPOST(Language lang, String text) throws IOException {
     final String postData = "language=" + lang.getShortName() + "&text=" + 
URLEncoder.encode(text, "UTF-8"); // latin1 is not enough for languages like 
polish, romanian, etc
     final URL url = new URL("http://localhost:"; + DEFAULT_PORT);
-    final URLConnection connection = url.openConnection();
-    connection.setDoOutput(true);
-    final OutputStreamWriter wr = new 
OutputStreamWriter(connection.getOutputStream());
-    try {
-        wr.write(postData);
-        wr.flush();
-        return StringTools.streamToString(connection.getInputStream(), 
"UTF-8");
-    } finally {
-      wr.close();
-    }
+    return HTTPTools.checkAtUrlByPost(url, postData);
   }
 
 }

Added: trunk/JLanguageTool/src/test/java/org/languagetool/server/HTTPTools.java
===================================================================
--- trunk/JLanguageTool/src/test/java/org/languagetool/server/HTTPTools.java    
                        (rev 0)
+++ trunk/JLanguageTool/src/test/java/org/languagetool/server/HTTPTools.java    
2012-11-12 22:15:26 UTC (rev 8387)
@@ -0,0 +1,83 @@
+/* LanguageTool, a natural language style checker
+ * Copyright (C) 2012 Daniel Naber (http://www.danielnaber.de)
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
+ * USA
+ */
+package org.languagetool.server;
+
+import org.languagetool.tools.StringTools;
+
+import javax.net.ssl.HttpsURLConnection;
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.TrustManager;
+import javax.net.ssl.X509TrustManager;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStreamWriter;
+import java.net.URL;
+import java.net.URLConnection;
+import java.security.KeyManagementException;
+import java.security.NoSuchAlgorithmException;
+
+class HTTPTools {
+
+  private HTTPTools() {
+  }
+
+  /**
+   * For testing, we disable all checks because we use a self-signed 
certificate on the server
+   * side and we want this test to run everywhere without importing the 
certificate into the JVM's trust store.
+   *
+   * See 
http://stackoverflow.com/questions/2893819/telling-java-to-accept-self-signed-ssl-certificate
+   */
+  static void disableCertChecks() throws NoSuchAlgorithmException, 
KeyManagementException {
+    final TrustManager[] trustAllCerts = new TrustManager[] {
+            new X509TrustManager() {
+              public java.security.cert.X509Certificate[] getAcceptedIssuers() 
{
+                return null;
+              }
+              public void checkClientTrusted(
+                      java.security.cert.X509Certificate[] certs, String 
authType) {
+              }
+              public void checkServerTrusted(
+                      java.security.cert.X509Certificate[] certs, String 
authType) {
+              }
+            }
+    };
+    final SSLContext sc = SSLContext.getInstance("SSL");
+    sc.init(null, trustAllCerts, new java.security.SecureRandom());
+    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
+  }
+
+  static String checkAtUrl(URL url) throws IOException {
+    final InputStream stream = (InputStream)url.getContent();
+    return StringTools.streamToString(stream, "UTF-8");
+  }
+
+  static String checkAtUrlByPost(URL url, String postData) throws IOException {
+    final URLConnection connection = url.openConnection();
+    connection.setDoOutput(true);
+    final OutputStreamWriter writer = new 
OutputStreamWriter(connection.getOutputStream());
+    try {
+      writer.write(postData);
+      writer.flush();
+      return StringTools.streamToString(connection.getInputStream(), "UTF-8");
+    } finally {
+      writer.close();
+    }
+  }
+
+}

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.


------------------------------------------------------------------------------
Monitor your physical, virtual and cloud infrastructure from a single
web console. Get in-depth insight into apps, servers, databases, vmware,
SAP, cloud infrastructure, etc. Download 30-day Free Trial.
Pricing starts from $795 for 25 servers or applications!
http://p.sf.net/sfu/zoho_dev2dev_nov
_______________________________________________
Languagetool-commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/languagetool-commits

Reply via email to