diff --git a/src/edu/stanford/nlp/sempre/ILUtils.java b/src/edu/stanford/nlp/sempre/ILUtils.java index cde1926..c8cd476 100644 --- a/src/edu/stanford/nlp/sempre/ILUtils.java +++ b/src/edu/stanford/nlp/sempre/ILUtils.java @@ -6,12 +6,10 @@ import java.nio.file.Files; import java.nio.file.Paths; import java.time.LocalDateTime; import java.util.ArrayList; -import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; @@ -23,13 +21,10 @@ import org.testng.collections.Lists; import com.google.common.collect.ImmutableList; -import edu.stanford.nlp.sempre.Master.Options; -import edu.stanford.nlp.sempre.Master.Response; import edu.stanford.nlp.sempre.interactive.ActionFormula; import edu.stanford.nlp.sempre.interactive.BlockFn; import edu.stanford.nlp.sempre.interactive.DefinitionAligner; import edu.stanford.nlp.sempre.interactive.FlatWorld; -import edu.stanford.nlp.sempre.interactive.DefinitionAligner.Match; import edu.stanford.nlp.sempre.interactive.GrammarInducer; import fig.basic.IOUtils; import fig.basic.LispTree; @@ -191,7 +186,9 @@ public final class ILUtils { LogInfo.logs("exHead: %s", exHead.getTokens()); exHead.predDerivations = Lists.newArrayList(bodyDeriv); - refEx.value = exHead; + if (refEx != null) { + refEx.value = exHead; + } return ILUtils.induceRules(exHead.getTokens(), ILUtils.utterancefromJson(jsonDef), bodyDeriv, state.chartList); } @@ -304,7 +301,9 @@ public final class ILUtils { Map json = Json.readMapHard(l); String command = json.get("log").toString(); Session trainer = master.getSession(json.get("id").toString()); - executor.execute(() -> master.handleCommand(trainer, command, master.new Response())); + executor.execute(() -> { + master.handleCommand(trainer, command, master.new Response()); + }); }); } else if (fileName.endsWith(".lisp")) { stream.forEach(l -> { diff --git a/src/edu/stanford/nlp/sempre/JsonServer.java b/src/edu/stanford/nlp/sempre/JsonServer.java index 2aad5c5..1d2ad68 100644 --- a/src/edu/stanford/nlp/sempre/JsonServer.java +++ b/src/edu/stanford/nlp/sempre/JsonServer.java @@ -250,7 +250,7 @@ public class JsonServer { this.master = master; } - void run() { + public void run() { try { String hostname = fig.basic.SysInfoUtils.getHostName(); HttpServer server = HttpServer.create(new InetSocketAddress(opts.port), 10); @@ -262,7 +262,7 @@ public class JsonServer { server.createContext("/", new Handler()); server.setExecutor(pool); server.start(); - LogInfo.logs("JSON Server started at http://%s:%s/sempre", hostname, opts.port); + LogInfo.logs("JSON Server (%d threads) started at http://%s:%s/sempre", opts.numThreads, hostname, opts.port); LogInfo.log("Press Ctrl-D to terminate."); while (LogInfo.stdin.readLine() != null) { } LogInfo.log("Shutting down server..."); diff --git a/src/edu/stanford/nlp/sempre/Master.java b/src/edu/stanford/nlp/sempre/Master.java index e51a489..8125834 100644 --- a/src/edu/stanford/nlp/sempre/Master.java +++ b/src/edu/stanford/nlp/sempre/Master.java @@ -452,7 +452,7 @@ public class Master { long approxSeq = ex.getLemmaTokens().stream().filter(s -> s.contains(";")).count(); if (approxSeq >= 8) - response.lines.add("You are taking many actions in one step, consider defining some of them."); + response.lines.add("You are taking many actions in one step, consider defining some of steps as one single step."); if (approxSeq >= ILUtils.opts.maxSequence) response.lines.add(String.format("way too many steps in one command (%d), refusing to execute.", approxSeq)); diff --git a/src/edu/stanford/nlp/sempre/interactive/BlockFn.java b/src/edu/stanford/nlp/sempre/interactive/BlockFn.java index c0a43e5..5e98e98 100644 --- a/src/edu/stanford/nlp/sempre/interactive/BlockFn.java +++ b/src/edu/stanford/nlp/sempre/interactive/BlockFn.java @@ -14,7 +14,6 @@ import fig.basic.Option; /** * return a block sequencing all the children - * * @author sidaw */ public class BlockFn extends SemanticFn { diff --git a/src/edu/stanford/nlp/sempre/interactive/StressTest.java b/src/edu/stanford/nlp/sempre/interactive/StressTest.java new file mode 100644 index 0000000..19c8e80 --- /dev/null +++ b/src/edu/stanford/nlp/sempre/interactive/StressTest.java @@ -0,0 +1,155 @@ +package edu.stanford.nlp.sempre.interactive; + +import static org.testng.AssertJUnit.assertEquals; + +import java.io.BufferedReader; +import java.io.DataOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.PrintWriter; +import java.io.UnsupportedEncodingException; +import java.net.HttpURLConnection; +import java.net.URL; +import java.net.URLEncoder; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.*; +import java.util.function.Predicate; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import fig.basic.*; +import edu.stanford.nlp.sempre.*; +import edu.stanford.nlp.sempre.Master.Response; +import edu.stanford.nlp.sempre.Parser.Spec; + +import org.testng.Assert; +import org.testng.asserts.SoftAssert; +import org.testng.asserts.Assertion; +import org.testng.annotations.Test; +import org.testng.collections.Lists; + +import com.google.common.collect.Sets; + +/** + * Test server threading + * @author Sida Wang + */ +public class StressTest { + Assertion hard = new Assertion(); + Assertion soft = new SoftAssert(); + + + @Test public void realQueryTest() { + + //String fileName = "int-output/interactive.log"; + while(true) { + LogInfo.begin_track("setsTest"); + //T.printAllRules(); + //A.assertAll(); + long startTime = System.nanoTime(); + String fileName = "/Users/sidaw/turk_acl17/int-output-0119/json/sidaw.log.json"; + { + try (Stream stream = Files.lines(Paths.get(fileName))) { + LogInfo.logs("Reading %s", fileName); + if (fileName.endsWith(".json") || fileName.endsWith(".log")) { + stream.forEach(l -> { + Map json = Json.readMapHard(l); + String command = json.get("log").toString(); + try { + sempreQuery(command); + //Thread.sleep(10); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + }); + } + } catch (IOException e) { + e.printStackTrace(); + } finally { + + long endTime = System.nanoTime(); + LogInfo.logs("Stresstest time = %d ns or %.4f s", (endTime - startTime), (endTime - startTime)/1.0e9); + + } + } + LogInfo.end_track(); + } + } + + @Test public void stressTest() throws IOException, InterruptedException { + LogInfo.begin_track("stressTest"); + + List queries = Lists.newArrayList("(:q \"add red; add yellow; select left\")", + "(:accept \"add red\" \"(: add red here)\")", + "(:q \"repeat 3 [repeat 3 [select top; add red]]\")" + ); + Random rand = new Random(); + // continue to spam the server + while (LogInfo.stdin.readLine() != null) { + sempreQuery(queries.get(rand.nextInt(queries.size()))); + Thread.sleep(10); + } + LogInfo.end_track(); + } + + private static void sempreQuery(String query) throws UnsupportedEncodingException { + String params = "q=" + URLEncoder.encode(query, "UTF-8"); + // params = URLEncoder.encode(params); + String url = "http://localhost:8410/sempre?"; + LogInfo.log(params); + String response = executePost(url + params,""); + LogInfo.log(response); + } + + private static String executePost(String targetURL, String urlParameters) { + HttpURLConnection connection = null; + + try { + //Create connection + URL url = new URL(targetURL); + connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod("POST"); + connection.setRequestProperty("Content-Type", + "application/x-www-form-urlencoded"); + + connection.setRequestProperty("Content-Length", + Integer.toString(urlParameters.getBytes().length)); + connection.setRequestProperty("Content-Language", "en-US"); + + connection.setUseCaches(false); + connection.setDoOutput(true); + + //Send request + DataOutputStream wr = new DataOutputStream ( + connection.getOutputStream()); + wr.writeBytes(urlParameters); + wr.close(); + + //Get Response + InputStream is = connection.getInputStream(); + BufferedReader rd = new BufferedReader(new InputStreamReader(is)); + StringBuilder response = new StringBuilder(); // or StringBuffer if Java version 5+ + String line; + while ((line = rd.readLine()) != null) { + response.append(line); + response.append('\r'); + } + rd.close(); + return response.toString(); + } catch (Exception e) { + e.printStackTrace(); + return null; + } finally { + if (connection != null) { + connection.disconnect(); + } + } + } + + + + +}