Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: baaa5b6313bbf456414785e8b42842c2f380c555 (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
89
90
/*******************************************************************************
 * Copyright (c) 2009, 2016 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * 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.eclipse.core.runtime.CoreException;
import org.eclipse.help.internal.server.WebappManager;
import org.junit.Assert;

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);
		try (InputStream input = url.openStream()) {
			int firstbyte = input.read();
			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);
		try (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);
		}
	}

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

}

Back to the top