[Lipog-commit] net.heilancoo.portal.examples.test/src/net/heilancoo/portal/examples/redirect/test
Status: Beta
Brought to you by:
jbu
From: Joerg B. <jb...@us...> - 2009-08-04 21:07:06
|
Update of /cvsroot/lipog/net.heilancoo.portal.examples.test/src/net/heilancoo/portal/examples/redirect/test In directory ddv4jf1.ch3.sourceforge.com:/tmp/cvs-serv28009/src/net/heilancoo/portal/examples/redirect/test Added Files: Statistics.java OneUser.java Stress.java Log Message: stress testing example --- NEW FILE: Stress.java --- /* * Copyright (c) 2009 Heilan' Coo -- Joerg Bullmann * * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which is available at http://www.eclipse.org/legal/epl-v10.html * * Contributors: * Joerg Bullmann <jb...@he...> */ package net.heilancoo.portal.examples.redirect.test; import net.heilancoo.portal.testutils.PortalTest; import org.apache.log4j.Logger; import org.junit.BeforeClass; import org.junit.Test; public class Stress { private static final Logger logger = Logger.getLogger(Stress.class); // First, the web application service is fired up (we // wait 500 ms after kicking it off). @BeforeClass public static void setUpBeforeClass() throws Exception { PortalTest.startPortal(new String [] { "--log=testlog.txt" }, 500); } @Test public void tryAll() throws Exception { doIt(5); doIt(10); doIt(20); doIt(40); doIt(80); doIt(160); doIt(320); } public void doIt(int count) throws Exception { OneUser [] users = new OneUser[count]; for(int i = 0; i < count; i++) users[i] = new OneUser("user_" + i, i); for(OneUser u : users) u.start(); for(OneUser u : users) u.join(); logger.info("==========="); for(OneUser u : users) u.stats(count + " "); logger.info("==========="); } } --- NEW FILE: Statistics.java --- /* * Copyright (c) 2009 Heilan' Coo -- Joerg Bullmann * * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which is available at http://www.eclipse.org/legal/epl-v10.html * * Contributors: * Joerg Bullmann <jb...@he...> */ package net.heilancoo.portal.examples.redirect.test; import com.gargoylesoftware.htmlunit.Page; /** * @author joerg * */ public class Statistics { private long count; private long min; private long max; private long sum; public Statistics() { this.count = 0; this.min = Long.MAX_VALUE; this.max = Long.MIN_VALUE; this.sum = 0; } public void addValue(long val) { count += 1; sum += val; min = Math.min(min, val); max = Math.max(max, val); } public void addStats(Page page) { addValue(page.getWebResponse().getLoadTime()); } public String toString() { return "count = " + count + ", avg = " + Math.round(sum / (1.0 * count)) + ", min = " + min + ", max = " + max; } public long getCount() { return count; } } --- NEW FILE: OneUser.java --- /* * Copyright (c) 2009 Heilan' Coo -- Joerg Bullmann * * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which is available at http://www.eclipse.org/legal/epl-v10.html * * Contributors: * Joerg Bullmann <jb...@he...> */ package net.heilancoo.portal.examples.redirect.test; import java.io.IOException; import java.net.MalformedURLException; import java.util.List; import java.util.Random; import net.heilancoo.portal.application.ApplicationException; import net.heilancoo.portal.testutils.AppUtils; import org.apache.log4j.Logger; import com.gargoylesoftware.htmlunit.ElementNotFoundException; import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException; import com.gargoylesoftware.htmlunit.html.HtmlForm; import com.gargoylesoftware.htmlunit.html.HtmlPage; /** * @author joerg * */ public class OneUser extends Thread { private static final Logger logger = Logger.getLogger(OneUser.class); private String name; private int direction; private int listSize; private HtmlPage loggedIn; private Random randy; private Statistics login; private Statistics logout; private Statistics operation; public OneUser(String name, int seed) { this.name = name; this.direction = 1; this.listSize = 0; this.randy = new Random(seed); this.login = new Statistics(); this.logout = new Statistics(); this.operation = new Statistics(); } public void run() { for(int i = 0; i < 50; i++) { try { useApplication(); } catch (Exception e) { logger.error(e, e); } } } public void stats(String prefix) { logger.info(prefix + "stats -- login(" + name + "): " + login); logger.info(prefix + "stats -- logout(" + name + "): " + logout); logger.info(prefix + "stats -- operation(" + name + "): " + operation); } private void useApplication() throws FailingHttpStatusCodeException, MalformedURLException, IOException, ApplicationException, InterruptedException { loggedIn = AppUtils.doDefaultLogin("http://127.0.0.1:8080", "todo", name, name); login.addStats(loggedIn); if(listSize == 0) listSize = loggedIn.getByXPath("/html/body/table/tbody/tr/td/form").size(); int add; int remove; if(direction == 1) { if(listSize > 20) direction = -1; } else { if(listSize < 5) direction = 1; } if(direction == 1) { add = 4; remove = 2; } else { add = 2; remove = 4; } for(int i = 0; i < add; i++) addItem(); for(int i = 0; i < 3; i++) changeItem(); for(int i = 0; i < remove; i++) removeItem(); logger.info("User: " + name + ", items = " + listSize); logout.addStats(loggedIn.getAnchorByHref("logout").click()); loggedIn = null; } private void changeItem() { // TODO Auto-generated method stub } private void removeItem() throws ElementNotFoundException, IOException { List<?> forms = loggedIn.getByXPath("/html/body/table/tbody/tr/td/form"); int numItems = forms.size(); int item = (int) Math.floor(randy.nextDouble() * numItems); if(numItems != listSize) logger.error("Mismatch: " + numItems + " vs. " + listSize); loggedIn = ((HtmlForm) forms.get(item)).getInputByName("delete").click(); operation.addStats(loggedIn); listSize -= 1; } private void addItem() throws ElementNotFoundException, IOException { HtmlPage addPage = loggedIn.getFormByName("addForm").getInputByName("add").click(); HtmlForm addForm = addPage.getFormByName("addForm"); addForm.getInputByName("text").setValueAttribute(operation.getCount() + ": make a cup of tea the umpteenth time"); loggedIn = addForm.getInputByName("add").click(); operation.addStats(loggedIn); listSize += 1; } } |