Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 01c1cb2a61be746d32e322ee153631ec3f289602 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*******************************************************************************
 * Copyright (c) 2009, 2013 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.ua.tests.help.util;

import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import org.junit.Assert;

import org.eclipse.help.internal.server.WebappManager;

import org.eclipse.core.runtime.CoreException;

public class LoadServletUtil {

	private static long uniqueParam = System.currentTimeMillis();

	/**
	 * Start a server for use in the performance tests
	 * @throws Exception
	 */
	public static void startServer() throws Exception {
		WebappManager.start("help");
		int port = WebappManager.getPort();
		URL url = new URL("http", "localhost", port, "/help/index.jsp");
		URLConnection connection = url.openConnection();
		setTimeout(connection, 5000);
		InputStream input  = url.openStream();
		int firstbyte = input.read();
		input.close();
		Assert.assertTrue(firstbyte > 0);
	}

	/**
	 * Stop the server created by startServer()
	 * @throws CoreException
	 */
	public static void stopServer() throws CoreException {
		WebappManager.stop("help");
	}

	/**
	 * Read an html page generated by a servlet
	 * @param paragraphs the number of paragraphs in the page
	 * @throws Exception
	 */
	public static void readLoadServlet(int paragraphs) throws Exception {
		int port = WebappManager.getPort();
		// Use a unique parameter to defeat caching
		++uniqueParam;
		URL url = new URL("http", "localhost", port,
				"/help/loadtest?value=" + uniqueParam + "&repeat=" + paragraphs);
		InputStream input = url.openStream();
		int nextChar;
		long value = 0;
		// The loadtest servlet  returns the uniqueParam in an opening comment such as <!--1234-->
		// Read this to verify that we are not getting a cached page
		boolean inFirstComment = true;
        do {
		    nextChar = input.read();
		    if (inFirstComment) {
		    	if (nextChar == '>') {
		    		inFirstComment = false;
		    	} else if (Character.isDigit((char) nextChar)) {
		    		value = value * 10 + (nextChar - '0');
		    	}
		    }
        } while (nextChar != '$');
        Assert.assertEquals(uniqueParam, value);
        input.close();
	}

	private static void setTimeout(URLConnection conn, int milliseconds) {
		conn.setConnectTimeout(milliseconds);
		conn.setReadTimeout(milliseconds);
	}

}

Back to the top