Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LPS benchmark test 3 #2562

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
4 changes: 4 additions & 0 deletions modules/apps/portal/portal-benchmark-test-util/bnd.bnd
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Bundle-Name: Liferay Portal Benchmark Test Utilities
Bundle-SymbolicName: com.liferay.portal.benchmark.test.util
Bundle-Version: 1.0.0
Export-Package: com.liferay.portal.benchmark.test.util
10 changes: 10 additions & 0 deletions modules/apps/portal/portal-benchmark-test-util/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
dependencies {
compileOnly group: "com.liferay.portal", name: "com.liferay.portal.kernel", version: "default"
compileOnly group: "junit", name: "junit", version: "4.13.1"
compileOnly project(":core:petra:petra-lang")
compileOnly project(":core:petra:petra-string")
}

liferay {
deployDir = file("${liferayHome}/osgi/test")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/**
* SPDX-FileCopyrightText: (c) 2024 Liferay, Inc. https://liferay.com
* SPDX-License-Identifier: LGPL-2.1-or-later OR LicenseRef-Liferay-DXP-EULA-2.0.0-2023-06
*/

package com.liferay.portal.benchmark.test.util;

import com.liferay.petra.string.StringBundler;
import com.liferay.petra.string.StringPool;
import com.liferay.portal.benchmark.test.util.http.HttpResponse;
import com.liferay.portal.benchmark.test.util.http.HttpUtil;

import java.net.URL;

import org.junit.Assert;

/**
* @author Tina Tian
*/
public abstract class BaseBenchmark {

public BaseBenchmark(String hostName, int port) {
_hostName = hostName;
_port = port;
}

public String homePage() throws Exception {
HttpResponse httpResponse = HttpUtil.doGet(_newURL("/"), null);

_assertResult(httpResponse, _KEY_HOME_PAGE);

return httpResponse.getCSRFToken();
}

public void login(String userEmail, String password, String csrfToken)
throws Exception {

_assertRedirect(
HttpUtil.doPost(
_newURL(_URL_LOGIN_POST),
new String[][] {
{
_P_P_ID_PREFIX + "_formDate",
String.valueOf(System.currentTimeMillis())
},
{_P_P_ID_PREFIX + "_saveLastPath", "false"},
{_P_P_ID_PREFIX + "_redirect", ""},
{_P_P_ID_PREFIX + "_doActionAfterLogin", "false"},
{_P_P_ID_PREFIX + "_login", userEmail},
{_P_P_ID_PREFIX + "_password", password},
{_P_P_ID_PREFIX + "_checkboxNames", "rememberMe"}
},
csrfToken, null),
_URL_REDIRECT);

_assertRedirect(
HttpUtil.doGet(_newURL(_URL_REDIRECT), csrfToken),
_URL_LOGIN_REDIRECT);

_assertResult(
HttpUtil.doGet(_newURL(_URL_LOGIN_REDIRECT), csrfToken),
_KEY_LOGIN);
}

public void logout() throws Exception {
HttpUtil.doGet(_newURL(_URL_LOGOUT), null);
}

public void viewLoginPage(String csrfToken) throws Exception {
_assertRedirect(
HttpUtil.doGet(_newURL(_URL_LOGIN_POPUP), csrfToken),
_URL_LOGIN_POPUP_REDIRECT);

_assertResult(
HttpUtil.doGet(_newURL(_URL_LOGIN_POPUP_REDIRECT), csrfToken),
_KEY_LOGIN_POPUP);
}

private void _assertRedirect(
HttpResponse httpResponse, String expectedRedirect)
throws Exception {

Assert.assertEquals(302, httpResponse.getStatusCode());

URL url = _newURL(expectedRedirect);

Assert.assertEquals(url.toString(), httpResponse.getRedirect());
}

private void _assertResult(HttpResponse httpResponse, String key) {
Assert.assertEquals(200, httpResponse.getStatusCode());

if (key != null) {
String httpResponseString = httpResponse.toString();

Assert.assertTrue(httpResponseString.contains(key));
}
}

private URL _newURL(String path) throws Exception {
return new URL("http", _hostName, _port, path);
}

private static final String _KEY_HOME_PAGE = "Liferay.currentURL";

private static final String _KEY_LOGIN =
"ProductNavigationUserPersonalBarPortlet";

private static final String _KEY_LOGIN_POPUP = "Remember Me";

private static final String _P_P_ID =
"com_liferay_login_web_portlet_LoginPortlet";

private static final String _P_P_ID_PREFIX = "_" + _P_P_ID;

private static final String _URL_LOGIN_POPUP =
"/c/portal/login?windowState=exclusive";

private static final String _URL_LOGIN_POPUP_REDIRECT =
StringBundler.concat(
"/home?p_p_id=", _P_P_ID, "&p_p_lifecycle=0&",
"p_p_state=exclusive&p_p_mode=view&", _P_P_ID_PREFIX,
"_mvcRenderCommandName=/login/login&saveLastPath=false");

private static final String _URL_LOGIN_POST = StringBundler.concat(
"/home?p_p_id=", _P_P_ID, "&p_p_lifecycle=1&",
"p_p_state=normal&p_p_mode=view&", _P_P_ID_PREFIX,
"_javax.portlet.action=/login/login&", _P_P_ID_PREFIX,
"_mvcRenderCommandName=/login/login");

private static final String _URL_LOGIN_REDIRECT = StringPool.SLASH;

private static final String _URL_LOGOUT = "/c/portal/logout";

private static final String _URL_REDIRECT = "/c";

private final String _hostName;
private final int _port;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* SPDX-FileCopyrightText: (c) 2024 Liferay, Inc. https://liferay.com
* SPDX-License-Identifier: LGPL-2.1-or-later OR LicenseRef-Liferay-DXP-EULA-2.0.0-2023-06
*/

package com.liferay.portal.benchmark.test.util;

import com.liferay.portal.benchmark.test.util.http.SimpleCookieStore;
import com.liferay.portal.benchmark.test.util.http.ThreadLocalCookieStore;

/**
* @author Tina Tian
*/
public class Login extends BaseBenchmark {

public Login(String hostName, int port) {
super(hostName, port);
}

public void execute(String userEmail, String password) throws Exception {
ThreadLocalCookieStore.setCookieStore(new SimpleCookieStore());

String csrfToken = homePage();

viewLoginPage(csrfToken);

login(userEmail, password, csrfToken);

logout();

ThreadLocalCookieStore.removeCookieStore();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/**
* SPDX-FileCopyrightText: (c) 2024 Liferay, Inc. https://liferay.com
* SPDX-License-Identifier: LGPL-2.1-or-later OR LicenseRef-Liferay-DXP-EULA-2.0.0-2023-06
*/

package com.liferay.portal.benchmark.test.util.http;

import com.liferay.petra.string.CharPool;
import com.liferay.petra.string.StringBundler;
import com.liferay.petra.string.StringPool;
import com.liferay.portal.kernel.util.ListUtil;

import java.net.URLDecoder;

import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* @author Tina Tian
*/
public class HttpResponse {

public HttpResponse(
int statusCode, String statusLine, Map<String, List<String>> headers,
String text, long duration) {

_statusCode = statusCode;
_statusLine = statusLine;
_headers = headers;
_text = text;
_duration = duration;
}

public String getCSRFToken() {
Matcher matcher = _csrfTokenPattern.matcher(getText());

if (matcher.find()) {
return matcher.group(1);
}

throw new IllegalStateException("Unable to parse CSRF token");
}

public long getDuration() {
return _duration;
}

public String getHeader(String name) {
List<String> headers = _headers.get(name);

if (ListUtil.isEmpty(headers)) {
return StringPool.BLANK;
}

StringBundler sb = new StringBundler(headers.size() * 2);

for (String header : headers) {
sb.append(header);
sb.append(StringPool.COMMA);
}

sb.setIndex(sb.index() - 1);

return sb.toString();
}

public Map<String, List<String>> getHeaders() {
return _headers;
}

public String getRedirect() throws Exception {
String location = getHeader("Location");

if (location.contains("%")) {
location = URLDecoder.decode(location, "UTF-8");
}

return location;
}

public int getStatusCode() {
return _statusCode;
}

public String getStatusLine() {
return _statusLine;
}

public String getText() {
return _text;
}

@Override
public String toString() {
StringBundler sb = new StringBundler();

sb.append(_statusCode);
sb.append(CharPool.SPACE);
sb.append(_statusLine);
sb.append(StringPool.NEW_LINE);

for (Map.Entry<String, List<String>> entry : _headers.entrySet()) {
String key = entry.getKey();

if (key != null) {
sb.append(key);
sb.append(StringPool.COLON);
}

for (String value : entry.getValue()) {
sb.append(value);
sb.append(StringPool.COMMA);
}

sb.setStringAt(StringPool.NEW_LINE, sb.index() - 1);
}

sb.append(_text);

return sb.toString();
}

private static final Pattern _csrfTokenPattern = Pattern.compile(
"Liferay\\.authToken = '(.*)';");

private final long _duration;
private final Map<String, List<String>> _headers;
private final int _statusCode;
private final String _statusLine;
private final String _text;

}
Loading