1 10 11 import java.io.*; 12 import java.net.*; 13 import java.util.*; 14 import bplatt.spider.*; 15 16 public class ServerStressTest { 17 private String base; 18 private static final String syntax = "java ServerStressTest <url> [ thread count ]"; 19 20 public static void main(String [] args) { 21 if (args.length == 0 || args.length > 2) { 22 System.out.println(syntax); 23 System.exit(-1); 24 } 25 String baseUrl = args[0]; 26 if (baseUrl == null || baseUrl.length() == 0) { 27 System.out.println("Missing URL"); 28 System.out.println(syntax); 29 System.exit(-1); 30 } 31 int cnt = 1; 32 int maxcnt = 10; 33 if (args.length == 2) { 34 try { cnt = Integer.parseInt(args[1]); } 35 catch(NumberFormatException e) { cnt = -1; } 36 if (cnt < 1 || cnt > maxcnt) { 37 System.out.println("Invalid thread count, use 1 to "+maxcnt); 38 System.exit(-1); 39 } 40 } 41 System.out.println("Stress Test Started"); 42 43 final ServerStressTest test = new ServerStressTest(baseUrl); 44 Thread threadList[] = new Thread [cnt]; 45 for (int i=0; i<cnt; ++i) { 46 Runnable r = new Runnable () { 47 public void run() { test.spiderSite(); } 48 }; 49 threadList[i] = new Thread (r); 50 System.out.println(threadList[i].toString()+" started"); 51 threadList[i].start(); 52 } 53 54 try { 55 for (int i=0; i<cnt; ++i) threadList[i].join(); 56 } 57 catch(InterruptedException e) { } 59 60 System.out.println("Stress Test Finished"); 61 System.out.println("Traversed "+StressTestSpider.getPageCnt()+" pages"); 62 System.out.println("Found "+StressTestSpider.getBadPages()+" bad pages"); 63 System.out.println("I/O errors in "+StressTestSpider.getBadIO()+" pages"); 64 } 65 66 public ServerStressTest(String base) { 67 this.base = base; 68 } 69 70 private void spiderSite() { 71 StressTestSpider spider = null; 72 try { spider = new StressTestSpider(base); } 73 catch(MalformedURLException e) { 74 System.out.println(e); 75 System.out.println("Invalid URL: "+base); 76 System.exit(-1); 77 } 78 spider.traverse(); 79 } 80 } 81 82 class StressTestSpider extends Arachnid { 83 private static long pageCnt; 84 private static long badPages; 85 private static long badIO; 86 87 public StressTestSpider(String base) throws MalformedURLException 88 { 89 super(base); 90 super.setDelay(2); 91 } 92 protected synchronized void handleBadLink(URL url,URL parent,PageInfo p) { 93 System.out.println("Invalid URL: "+url+ 94 ((parent==null)?"":" in "+parent)); 95 badPages++; 96 } 97 protected synchronized void handleLink(PageInfo p) { 98 System.out.println(p.getUrl()); 99 pageCnt++; 100 } 101 protected synchronized void handleNonHTMLlink(URL url, URL parent, PageInfo p) { 102 System.out.println("Non-HTML link: "+url+" type: "+p.getContentType()); 103 } 104 protected synchronized void handleExternalLink(URL url, URL parent) { 105 System.out.println("External link: "+url+ 106 ((parent==null)?"":" in "+parent)); 107 } 108 protected synchronized void handleBadIO(URL url, URL parentURL) 109 { 110 System.out.println("I/O error accessing page: "+url); 111 badIO++; 112 } 113 117 public static synchronized long getPageCnt() { 118 return pageCnt; 119 } 120 124 public static long getBadPages() { 125 return badPages; 126 } 127 128 132 public static long getBadIO() { 133 return badIO; 134 } 135 136 } 137 138 | Popular Tags |