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

fix #4 Support Report Naming to be multiproject compatible #5

Open
wants to merge 2 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
16 changes: 16 additions & 0 deletions src/main/java/io/morethan/jenkins/jmhreport/Archive.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.morethan.jenkins.jmhreport;

public final class Archive {

static String resultFileName(String archiveName) {
return String.format("%s-%s", archiveName, Constants.ARCHIVED_RESULT_FILE);
}

static String runUrl(String reportName) {
return String.format("%s-%s", RunJmhView.URL_NAME, reportName);
}

static String runDisplayName(String reportName) {
return String.format("%s - %s", "JMH Run Report", reportName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public class JmhPublisherDslExtension extends ContextExtensionPoint
@DslExtensionMethod(context=PublisherContext.class)
public Object jmhPublisher(String resultPath)
{
return new RunPublisher(resultPath);
RunPublisher runPublisher = new RunPublisher();
runPublisher.setResultPath(resultPath);
return runPublisher;
}
}
89 changes: 0 additions & 89 deletions src/main/java/io/morethan/jenkins/jmhreport/ProjectJmhView.java

This file was deleted.

32 changes: 15 additions & 17 deletions src/main/java/io/morethan/jenkins/jmhreport/RunJmhView.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Collection;
import java.util.*;

import javax.servlet.ServletException;

Expand All @@ -25,14 +24,16 @@
*/
public class RunJmhView implements Action, LastBuildAction, Serializable {

private static final String URL_NAME = "jmh-run-report";
static final String URL_NAME = "jmh-run-report";

private static final long serialVersionUID = 1L;

private final Run<?, ?> _run;
private final String _reportName;

public RunJmhView(Run<?, ?> run) {
public RunJmhView(Run<?, ?> run, String reportName) {
_run = run;
_reportName = reportName;
}

/**
Expand All @@ -46,12 +47,12 @@ public String getIconFileName() {

@Override
public String getDisplayName() {
return "JMH Run Report";
return Archive.runDisplayName(_reportName);
}

@Override
public String getUrlName() {
return URL_NAME;
return Archive.runUrl(_reportName);
}

public Run<?, ?> getRun() {
Expand All @@ -72,7 +73,7 @@ public String getContextPath() {

public String getProvidedJsUrl() {
String contextPath = Stapler.getCurrentRequest().getContextPath();
return new StringBuilder(contextPath).append("/").append(getRun().getUrl()).append(URL_NAME)
return new StringBuilder(contextPath).append("/").append(getRun().getUrl()).append(getUrlName())
.append("/provided-").append(getBuildNumber()).append(".js").toString();
}

Expand All @@ -84,30 +85,27 @@ public String getBundleJsUrl() {
public void doDynamic(final StaplerRequest request, final StaplerResponse response)
throws IOException, ServletException {
ProvidedJsBuilder jsBuilder = new ProvidedJsBuilder();
File resultFile = new File(_run.getRootDir(), Constants.ARCHIVED_RESULT_FILE);
File resultFile = new File(_run.getRootDir(), Archive.resultFileName(_reportName));
jsBuilder.addRun(getBuildNumber(), resultFile);

addPossiblePreviousBuild(jsBuilder, _run);
addPossiblePreviousBuild(jsBuilder, _run, _reportName);

response.setContentType("text/javascript;charset=UTF-8");
response.getWriter().println(jsBuilder.buildReverse());
}

private static void addPossiblePreviousBuild(ProvidedJsBuilder jsBuilder, Run<?, ?> run) {
while ((run = run.getPreviousNotFailedBuild()) != null) {
File previousResultFile = new File(run.getRootDir(), Constants.ARCHIVED_RESULT_FILE);
private static void addPossiblePreviousBuild(ProvidedJsBuilder jsBuilder, Run<?, ?> run, String reportName) {
int runCount = 0;
while ((run = run.getPreviousNotFailedBuild()) != null && runCount++ < 10) {
File previousResultFile = new File(run.getRootDir(), Archive.resultFileName(reportName));
if (previousResultFile.exists()) {
jsBuilder.addRun(run.getNumber(), previousResultFile);

// For the run view we only display the latest 2 runs (currently)
return;
}
}
}

@Override
public Collection<? extends Action> getProjectActions() {
return Arrays.asList(new ProjectJmhView(_run.getParent()));
return new ArrayList<>();
}

}
43 changes: 28 additions & 15 deletions src/main/java/io/morethan/jenkins/jmhreport/RunPublisher.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
package io.morethan.jenkins.jmhreport;

import java.io.File;
import java.io.IOException;

import org.jenkinsci.Symbol;
import org.kohsuke.stapler.DataBoundConstructor;

import hudson.AbortException;
import hudson.Extension;
import hudson.FilePath;
Expand All @@ -19,24 +13,43 @@
import hudson.tasks.Publisher;
import hudson.tasks.Recorder;
import jenkins.tasks.SimpleBuildStep;
import org.jenkinsci.Symbol;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;

import java.io.File;
import java.io.IOException;

/**
* A {@link Recorder} executed after each build. It copies the JMH result file
* into the corresponding build dir and registers the {@link ProjectJmhView}
* which renders the build run report and itself registers the
* {@link ProjectJmhView} through {@link LastBuildAction}.
* into the corresponding build dir.
*/
public class RunPublisher extends Recorder implements SimpleBuildStep {

private final String _resultPath;
private String _resultPath;

private String _reportName;

public String getResultPath() {
return _resultPath;
}

@DataBoundConstructor
public RunPublisher(String resultPath) {
public RunPublisher() {
}

@DataBoundSetter
public void setResultPath(String resultPath) {
_resultPath = resultPath;
}

public String getResultPath() {
return _resultPath;
public String getReportName() {
return _reportName;
}

@DataBoundSetter
public void setReportName(String reportName) {
_reportName = reportName;
}

@Override
Expand All @@ -62,11 +75,11 @@ public void perform(Run<?, ?> run, FilePath workspace, Launcher launcher, TaskLi
listener.getLogger().println("Found JMH result: " + _resultPath);

// Copy the result file into the build dir of the Jenkins project
File archivedResult = new File(run.getRootDir(), Constants.ARCHIVED_RESULT_FILE);
File archivedResult = new File(run.getRootDir(), Archive.resultFileName(_reportName));
resultFile.copyTo(new FilePath(archivedResult));
listener.getLogger().println("Archived JMH result to: " + archivedResult);

run.addAction(new RunJmhView(run));
run.addAction(new RunJmhView(run, getReportName()));
// TODO set on major decreases ?
// build.setResult(Result.UNSTABLE);
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@
<f:entry title="JMH result path" field="resultPath">
<f:textbox default="build/reports/jmh/result.json"/>
</f:entry>
<f:entry title="JMH report name" field="reportName">
<f:textbox default="benchmark"/>
</f:entry>
</j:jelly>
2 changes: 1 addition & 1 deletion src/main/webapp/bundle.js

Large diffs are not rendered by default.