1 4 package com.tctest.performance.servlets; 5 6 import com.tctest.performance.sampledata.OrganicObjectGraph; 7 import com.tctest.performance.sampledata.OrganicObjectGraphManager; 8 9 import java.io.File ; 10 import java.io.IOException ; 11 import java.io.PrintWriter ; 12 import java.util.Enumeration ; 13 import java.util.Random ; 14 15 import javax.servlet.http.HttpServlet ; 16 import javax.servlet.http.HttpServletRequest ; 17 import javax.servlet.http.HttpServletResponse ; 18 import javax.servlet.http.HttpSession ; 19 20 public final class OrganicObjectGraphServlet extends HttpServlet { 21 22 private static final String CHANGES = "changes"; private static final String CREATE_GRAPH = "create"; private static final String FINISHED = "finished"; private static final String ERROR = "error"; 26 private static final String HOST_SYSPROP = "__TEST_HOSTNAME__"; private static final int DEFAULT_GRAPH_SIZE = 10; 28 private final Random random = new Random (0); 29 30 public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { 31 HttpSession session = request.getSession(true); 32 response.setContentType("text/html"); 33 PrintWriter out = response.getWriter(); 34 35 String envKey = System.getProperty(HOST_SYSPROP); 36 if (envKey == null) throw new IOException ("System Property \"" + HOST_SYSPROP + "\" Not Set"); 37 38 if (request.getParameter(FINISHED) != null) { 39 try { 40 writeData(session); 41 } catch (Exception e) { 42 throw new IOException ("Unable to write graph data to filesystem."); 43 } 44 out.println("Wrote Data"); 45 return; 46 } 47 48 int graphCount = graphCount(session); 49 int changes = 0; 50 if (request.getParameter(CHANGES) == null) { 51 out.println(ERROR); 52 return; 53 } else { 54 changes = Integer.parseInt(request.getParameter(CHANGES)); 55 } 56 int createNewGraph = 0; 57 if (request.getParameter(CREATE_GRAPH) == null) { 58 out.println(ERROR); 59 return; 60 } else { 61 createNewGraph = Integer.parseInt(request.getParameter(CREATE_GRAPH)); 62 } 63 64 if (createNewGraph > 0 || session.isNew()) { 65 if (session.isNew()) createNewGraph = DEFAULT_GRAPH_SIZE; 66 try { 67 OrganicObjectGraph graph = OrganicObjectGraphManager.createOrganicGraph(createNewGraph, envKey); 68 session.setAttribute("graph_" + graphCount, graph); 69 } catch (Exception e) { 70 e.printStackTrace(); 71 throw new IOException ("Unable to create data graph"); 72 } 73 } 74 75 if (changes > 0) { 76 int rand = getRandom(graphCount); 77 OrganicObjectGraph graph = (OrganicObjectGraph) session.getAttribute("graph_" + rand); 78 graph.mutateRandom(changes); 79 session.setAttribute("graph_" + rand, graph); } 81 82 out.println("Session Updated"); 83 out.flush(); 84 return; 85 } 86 87 private void writeData(HttpSession session) throws Exception { 88 Enumeration names = session.getAttributeNames(); 89 String name; 90 while (names.hasMoreElements()) { 91 name = (String ) names.nextElement(); 92 File file = new File ("results" + File.separator + session.getId() + "_" + name + ".obj"); 93 System.out.println("Writing File: " + file.getAbsolutePath()); 94 OrganicObjectGraphManager.serializeGraph(((OrganicObjectGraph) session.getAttribute(name)), file); 95 } 96 } 97 98 private int graphCount(HttpSession session) { 99 Enumeration names = session.getAttributeNames(); 100 int count = 0; 101 while (names.hasMoreElements()) { 102 if (((String ) names.nextElement()).startsWith("graph_")) count++; 103 } 104 return count; 105 } 106 107 private int getRandom(int bound) { 108 return new Long (Math.round(Math.floor(bound * random.nextDouble()))).intValue(); 109 } 110 } 111 | Popular Tags |