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 205346 1 9 1 #2564

Open
wants to merge 7 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
22 changes: 22 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,22 @@
dependencies {
compileOnly group: "com.liferay.portal", name: "com.liferay.portal.impl", version: "default"
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")
}

test {
systemProperty "benchmark.test.excluded.companies", System.getProperty("benchmark.test.excluded.companies", "liferay.com")
systemProperty "benchmark.test.jdbc.driverClassName", System.getProperty("benchmark.test.jdbc.driverClassName", "com.mysql.cj.jdbc.Driver")
systemProperty "benchmark.test.jdbc.url", System.getProperty("benchmark.test.jdbc.url", "jdbc:mysql://localhost/lportal?characterEncoding=UTF-8&dontTrackOpenResources=true&holdResultsOpenOverStatementClose=true&serverTimezone=GMT&useFastDateParsing=false&useUnicode=true")
systemProperty "benchmark.test.jdbc.username", System.getProperty("benchmark.test.jdbc.userName", "")
systemProperty "benchmark.test.jdbc.password", System.getProperty("benchmark.test.jdbc.password", "")
systemProperty "benchmark.test.run.count", System.getProperty("benchmark.test.run.count", "1")
systemProperty "benchmark.test.skip.warm.up", System.getProperty("benchmark.test.skip.warm.up", "false")
systemProperty "benchmark.test.thread.count", System.getProperty("benchmark.test.thread.count", "1")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
/**
* 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 HttpResponse homePage() throws Exception {
HttpResponse httpResponse = HttpUtil.doGet(_newURL("/"), null);

_assertResult(httpResponse, _KEY_HOME_PAGE);

return httpResponse;
}

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

HttpResponse httpResponse1 = 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);

_assertRedirect(httpResponse1, _URL_REDIRECT);

HttpResponse httpResponse2 = HttpUtil.doGet(
_newURL(_URL_REDIRECT), csrfToken);

_assertRedirect(httpResponse2, _URL_LOGIN_REDIRECT);

HttpResponse httpResponse3 = HttpUtil.doGet(
_newURL(_URL_LOGIN_REDIRECT), csrfToken);

_assertResult(httpResponse3, _KEY_LOGIN);

return _getTotalDuration(httpResponse1, httpResponse2, httpResponse3);
}

public long logout() throws Exception {
HttpResponse httpResponse = HttpUtil.doGet(_newURL(_URL_LOGOUT), null);

return httpResponse.getDuration();
}

public long viewLoginPage(String csrfToken) throws Exception {
HttpResponse httpResponse1 = HttpUtil.doGet(
_newURL(_URL_LOGIN_POPUP), csrfToken);

_assertRedirect(httpResponse1, _URL_LOGIN_POPUP_REDIRECT);

HttpResponse httpResponse2 = HttpUtil.doGet(
_newURL(_URL_LOGIN_POPUP_REDIRECT), csrfToken);

_assertResult(httpResponse2, _KEY_LOGIN_POPUP);

return _getTotalDuration(httpResponse1, httpResponse2);
}

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

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

URL url = _newURL(expectedRedirect);

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

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

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

Assert.assertTrue(
httpResponse.toString(), httpResponseString.contains(key));
}
}

private long _getTotalDuration(HttpResponse... httpResponses) {
long duration = 0;

for (HttpResponse httpResponse : httpResponses) {
duration += httpResponse.getDuration();
}

return duration;
}

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,50 @@
/**
* 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.HttpResponse;
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, String userEmail, String password,
Statistics statistics) {

super(hostName, port);

_userEmail = userEmail;
_password = password;
_statistics = statistics;
}

public void execute() throws Exception {
ThreadLocalCookieStore.setCookieStore(new SimpleCookieStore());

HttpResponse httpResponse = homePage();

_statistics.record("homePage", httpResponse.getDuration());

_statistics.record(
"viewLoginPage", viewLoginPage(httpResponse.getCSRFToken()));

_statistics.record(
"login", login(_userEmail, _password, httpResponse.getCSRFToken()));

_statistics.record("logout", logout());

ThreadLocalCookieStore.removeCookieStore();
}

private final String _password;
private final Statistics _statistics;
private final String _userEmail;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/**
* 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 java.util.Map;
import java.util.Queue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Supplier;

import org.junit.Assert;

/**
* @author Dante Wang
*/
public class Statistics {

public Statistics(int runCount) {
_runCount = runCount;
}

public void finish() {
long totalTime = System.currentTimeMillis() - _startTime;

System.out.println(
StringBundler.concat(
"\nTests took ", totalTime, " ms. Each Login session took ",
String.format("%.2f", (double)totalTime / _runCount), " ms."));

for (String testName : _testNames) {
AtomicLong durationSum = _sumsMap.get(testName);

double average = (double)durationSum.get() / _runCount;

Queue<Long> durations = _durationsMap.get(testName);

Assert.assertEquals(
durations.toString(), _runCount, durations.size());

double variance = 0;

for (Long duration : durations) {
variance += (duration - average) * (duration - average);
}

variance = variance / _runCount;

System.out.println(
StringBundler.concat(
"\nTest step ", testName, "\n\tAverage time: ",
String.format("%.2f", average),
" ms\n\tStandard deviation: ",
String.format("%.2f", Math.sqrt(variance)), 2,
" ms\n\tTPS: ",
String.format(
"%.2f", _runCount * 1000 / (double)durationSum.get())));
}
}

public void record(String testName, long duration) {
AtomicLong sum = _getOrCreate(testName, _sumsMap, AtomicLong::new);

sum.addAndGet(duration);

Queue<Long> durations = _getOrCreate(
testName, _durationsMap, ConcurrentLinkedQueue::new);

durations.offer(duration);
}

public void start() {
_startTime = System.currentTimeMillis();
}

private <T> T _getOrCreate(
String key, Map<String, T> map, Supplier<T> supplier) {

T t = map.get(key);

if (t == null) {
t = supplier.get();

T prevousT = map.putIfAbsent(key, t);

if (prevousT != null) {
t = prevousT;
}
else {
if (!_testNames.contains(key)) {
_testNames.offer(key);
}
}
}

return t;
}

private final Map<String, Queue<Long>> _durationsMap =
new ConcurrentHashMap<>();
private final int _runCount;
private long _startTime;
private final Map<String, AtomicLong> _sumsMap = new ConcurrentHashMap<>();
private final Queue<String> _testNames = new ConcurrentLinkedQueue<>();

}
Loading