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

CELDEV-1055 - Performance metrics with benchmark #174

Draft
wants to merge 5 commits into
base: dev
Choose a base branch
from
Draft
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
47 changes: 47 additions & 0 deletions celements-performance/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
*
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<parent>
<groupId>com.celements</groupId>
<artifactId>celements</artifactId>
<version>5.5</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>celements-performance</artifactId>
<version>5.0-SNAPSHOT</version>
<description>Celements Performance metrics</description>
<dependencies>
<dependency>
<groupId>com.celements</groupId>
<artifactId>celements-model</artifactId>
<version>5.4</version>
<scope>provided</scope>
</dependency>
</dependencies>
<scm>
<connection>scm:git:[email protected]:celements/celements-xwiki.git</connection>
<developerConnection>scm:git:[email protected]:celements/celements-xwiki.git</developerConnection>
<url>https://github.com/celements/celements-xwiki</url>
<tag>HEAD</tag>
</scm>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.celements.performance;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

import org.xwiki.component.annotation.ComponentRole;

@ComponentRole
public interface BenchmarkRole {

void bench(@NotEmpty String label);

void startBench();

@NotNull
String println(boolean visible);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.celements.performance;

import org.xwiki.component.annotation.Component;
import org.xwiki.component.annotation.Requirement;
import org.xwiki.script.service.ScriptService;

import com.celements.model.context.ModelContext;

@Component("bench")
public class BenchmarkScriptService implements ScriptService {

@Requirement
private ModelContext context;

@Requirement
private BenchmarkRole benchSrv;

public String benchAndPrint(String label) {
return benchAndPrint(label, false);
}

public void bench(String label) {
benchSrv.bench(label);
}

public String benchAndPrint(String label, boolean visible) {
benchSrv.bench(label);
boolean requestVisible = context.getRequest().toJavaUtil()
.map(request -> "true".equals(request.getParameter("showBenchmark")))
.orElse(false);
return benchSrv.println(visible || requestVisible);
}

public void startBench() {
benchSrv.startBench();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package com.celements.performance;

import static com.google.common.base.Preconditions.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.stream.Collectors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xwiki.component.annotation.Component;
import org.xwiki.component.annotation.Requirement;
import org.xwiki.context.Execution;

import com.google.common.base.Strings;

@Component
public class BenchmarkService implements BenchmarkRole {

private static final Logger LOGGER = LoggerFactory.getLogger(BenchmarkService.class);

private static final String BENCH_LAST_TIME = "bench_lastTime";

private static final String BENCH_START_TIME = "bench_startTime";

private static final String CEL_BENCHMARK_VAL_PREFIX = "CEL_BENCHMARK_";

private static final String CEL_BENCHMARK_OUT_STRINGS = CEL_BENCHMARK_VAL_PREFIX
+ "benchOutStrings";

@Requirement
Execution execution;

private boolean isBenchStarted() {
return getContextLongValue(BENCH_START_TIME) != null;
}

private Long getContextLongValue(String name) {
return (Long) execution.getContext().getProperty(CEL_BENCHMARK_VAL_PREFIX + name);
}

private void setContextLongValue(String name, long value) {
execution.getContext().setProperty(CEL_BENCHMARK_VAL_PREFIX + name, value);
}

@SuppressWarnings("unchecked")
private ArrayList<String> getBenchOutStringArray() {
ArrayList<String> benchOutStringArray;
if (execution.getContext().getProperty(CEL_BENCHMARK_OUT_STRINGS) == null) {
benchOutStringArray = new ArrayList<>();
execution.getContext().setProperty(CEL_BENCHMARK_OUT_STRINGS, benchOutStringArray);
} else {
benchOutStringArray = (ArrayList<String>) execution.getContext()
.getProperty(CEL_BENCHMARK_OUT_STRINGS);
}
return benchOutStringArray;
}

@Override
public void bench(String label) {
checkArgument(!Strings.isNullOrEmpty(label));
if (isBenchStarted()) {
long currTime = new Date().getTime();
double totalTime = (currTime - getContextLongValue(BENCH_START_TIME)) / 1000.0;
double time = (currTime - getContextLongValue(BENCH_LAST_TIME)) / 1000.0;
setContextLongValue(BENCH_LAST_TIME, currTime);
getBenchOutStringArray()
.add("bench '" + label + "' &mdash; in " + time + "s &mdash; total " + totalTime
+ "s");
} else {
LOGGER.info("bench called without startBench. Skipping.");
}
}

@Override
public void startBench() {
long benchStartTime = new Date().getTime();
setContextLongValue(BENCH_START_TIME, benchStartTime);
setContextLongValue(BENCH_LAST_TIME, benchStartTime);
}

@Override
public String println(boolean visible) {
String outStr = getBenchOutStringArray().stream()
.collect(Collectors.joining("\n"));
getBenchOutStringArray().clear();
if (!visible) {
outStr = "<!-- " + outStr + " -->\n";
}
return outStr;
}

}
3 changes: 3 additions & 0 deletions celements-performance/src/main/resources/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Manifest-Version: 1.0
Class-Path:

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
com.celements.performance.BenchmarkService
com.celements.performance.BenchmarkScriptService