Skip to content

Commit

Permalink
Merge pull request #12 from hohwille/master
Browse files Browse the repository at this point in the history
improve maven-profier
  • Loading branch information
cstamas authored Jul 1, 2024
2 parents 16c90a3 + 066d001 commit 2068c73
Show file tree
Hide file tree
Showing 19 changed files with 583 additions and 113 deletions.
15 changes: 11 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
.*
!.github
!.gitignore
!.mvn
*.bak
*.log
*.diff
*.patch
*.iml
target
.project
.classpath
.settings
release.properties
release.properties

8 changes: 4 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugin>
<plugin>
<groupId>io.tesla.maven.plugins</groupId>
<artifactId>tesla-license-plugin</artifactId>
</plugin>
</plugin>
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>sisu-maven-plugin</artifactId>
Expand Down
38 changes: 38 additions & 0 deletions src/main/java/io/tesla/lifecycle/profiler/AbstractProfile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* Copyright (c) 2012 to original author or authors
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package io.tesla.lifecycle.profiler;

public abstract class AbstractProfile implements Profile {

protected long elapsedTime;

protected AbstractProfile() {
super();
}

/**
* @param elapsedTime the new value of {@link #getElapsedTime()}.
*/
public void setElapsedTime(long elapsedTime) {
this.elapsedTime = elapsedTime;
}

/**
* @param millis the milliseconds to add to {@link #getElapsedTime() elapsed time}.
*/
public void addElapsedTime(long millis) {

this.elapsedTime += millis;
}

@Override
public long getElapsedTime() {
return elapsedTime;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Copyright (c) 2012 to original author or authors
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package io.tesla.lifecycle.profiler;

import io.tesla.lifecycle.profiler.internal.DefaultTimer;

public abstract class AbstractTimerProfile extends AbstractProfile {

protected final Timer timer;

protected AbstractTimerProfile() {
this.timer = new DefaultTimer();
}

public void stop() {
timer.stop();
}

@Override
public long getElapsedTime() {
if(elapsedTime != 0) {
return elapsedTime;
}
return timer.getElapsedTime();
}

}
75 changes: 75 additions & 0 deletions src/main/java/io/tesla/lifecycle/profiler/AggregationProfile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* Copyright (c) 2012 to original author or authors
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package io.tesla.lifecycle.profiler;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AggregationProfile extends AbstractProfile {

private final String name;

private final Map<String, AggregationProfile> childMap;

private final List<AggregationProfile> children;

public AggregationProfile() {
this("all");
}

public AggregationProfile(String name) {
super();
this.name = name;
this.childMap = new HashMap<>();
this.children = new ArrayList<>();
}

@Override
public String getName() {

return this.name;
}

@Override
public long getElapsedTime() {

long millis = this.elapsedTime;
if (millis == 0) {
for (AggregationProfile child : this.children) {
millis += child.getElapsedTime();
}
}
return millis;
}

public AggregationProfile getChild(String name) {

return this.childMap.get(name);
}

public AggregationProfile getOrCreateChild(String name) {

return this.childMap.computeIfAbsent(name, this::newAggregationProfile);
}

private AggregationProfile newAggregationProfile(String name) {

AggregationProfile profile = new AggregationProfile(name);
this.children.add(profile);
return profile;
}

@Override
public Collection<? extends Profile> getChildren() {

return this.children;
}
}
35 changes: 22 additions & 13 deletions src/main/java/io/tesla/lifecycle/profiler/LifecycleProfiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,11 @@
@Singleton
public class LifecycleProfiler extends AbstractEventSpy {

private final static String TESLA_PROFILE = "maven.profile";

//
// Components
//
private SessionProfileRenderer renderer;
private final static String MAVEN_PROFILE = "maven.profile";

private final SessionProfileRenderer renderer;

private final boolean disabled;

//
// Profile data
Expand All @@ -43,7 +42,9 @@ public class LifecycleProfiler extends AbstractEventSpy {

@Inject
public LifecycleProfiler(SessionProfileRenderer sessionProfileRenderer) {
super();
this.renderer = sessionProfileRenderer;
this.disabled = (System.getProperty(MAVEN_PROFILE) == null);
}

@Override
Expand All @@ -52,30 +53,38 @@ public void init(Context context) throws Exception {

@Override
public void onEvent(Object event) throws Exception {
if (this.disabled) {
return;
}
if (event instanceof ExecutionEvent) {
ExecutionEvent executionEvent = (ExecutionEvent) event;
if (executionEvent.getType() == ExecutionEvent.Type.SessionStarted) {
//
//
//
sessionProfile = new SessionProfile();
StringBuilder command = new StringBuilder("mvn");
for (String goal : executionEvent.getSession().getGoals()) {
command.append(' ');
command.append(goal);
}
sessionProfile = new SessionProfile(command.toString());
} else if (executionEvent.getType() == ExecutionEvent.Type.SessionEnded) {
//
//
//
sessionProfile.stop();
if (System.getProperty(TESLA_PROFILE) != null) {
renderer.render(sessionProfile);
}
renderer.render(sessionProfile);
} else if (executionEvent.getType() == ExecutionEvent.Type.ProjectStarted) {
//
// We need to collect the mojoExecutions within each project
//
projectProfile = new ProjectProfile(executionEvent.getProject());
} else if (executionEvent.getType() == ExecutionEvent.Type.ProjectSucceeded || executionEvent.getType() == ExecutionEvent.Type.ProjectFailed) {
//
//
//
if (phaseProfile != null) {
phaseProfile.stop();
projectProfile.addPhaseProfile(phaseProfile);
phaseProfile = null;
}
projectProfile.stop();
sessionProfile.addProjectProfile(projectProfile);
} else if (executionEvent.getType() == ExecutionEvent.Type.MojoStarted) {
Expand Down
25 changes: 19 additions & 6 deletions src/main/java/io/tesla/lifecycle/profiler/MojoProfile.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,34 @@
*/
package io.tesla.lifecycle.profiler;

import io.tesla.lifecycle.profiler.internal.DefaultTimer;
import java.util.Collections;
import java.util.List;

import org.apache.maven.plugin.MojoExecution;

public class MojoProfile extends Profile {
public class MojoProfile extends AbstractTimerProfile {

private MojoExecution mojoExecution;

protected MojoProfile(MojoExecution mojoExecution) {
super(new DefaultTimer());
super();
this.mojoExecution = mojoExecution;
}

public String getId() {
return mojoExecution.getGroupId() + ":" + mojoExecution.getArtifactId() + ":" + mojoExecution.getVersion() + " (" + mojoExecution.getExecutionId() + ") ";
return mojoExecution.getGroupId() + ":" + mojoExecution.getArtifactId() + ":" + mojoExecution.getVersion() + " (" + mojoExecution.getExecutionId() + ")";
}

@Override
public String getName() {

return getId();
}

@Override
public List<? extends Profile> getChildren() {

return Collections.emptyList();
}

}
22 changes: 16 additions & 6 deletions src/main/java/io/tesla/lifecycle/profiler/PhaseProfile.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,20 @@
*/
package io.tesla.lifecycle.profiler;

import io.tesla.lifecycle.profiler.internal.DefaultTimer;

import java.util.ArrayList;
import java.util.List;

public class PhaseProfile extends Profile {
public class PhaseProfile extends AbstractTimerProfile {

private String phase;
private List<MojoProfile> mojoProfiles;

public PhaseProfile(String phase) {
super(new DefaultTimer());
super();
this.phase = phase;
this.mojoProfiles = new ArrayList<MojoProfile>();
}

public void addMojoProfile(MojoProfile mojoProfile) {
mojoProfiles.add(mojoProfile);
}
Expand All @@ -34,4 +32,16 @@ public String getPhase() {
public List<MojoProfile> getMojoProfiles() {
return mojoProfiles;
}

@Override
public String getName() {

return phase;
}

@Override
public List<? extends Profile> getChildren() {

return getMojoProfiles();
}
}
44 changes: 20 additions & 24 deletions src/main/java/io/tesla/lifecycle/profiler/Profile.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,25 @@
*/
package io.tesla.lifecycle.profiler;

import io.tesla.lifecycle.profiler.internal.DefaultTimer;
import java.util.Collection;
import java.util.List;

/**
* Interface for a profile of the profiling. The root profile is {@link SessionProfile} representing the recording of the
* entire session. Each {@link Profile} can have {@link #getChildren() children} representing a partition of the whole
* into smaller steps to give more details and granularity to trace down leaks and find spots worth to optimize.
*/
public interface Profile extends Timing {

/**
* @return the name of this profile (name of e.g. maven project, phase, plugin).
*/
String getName();

/**
* @return the {@link List} of child {@link Profile}s contained in this {@link Profile}. Will be an empty list if this
* profile has not children.
*/
Collection<? extends Profile> getChildren();

public class Profile {

protected long elapsedTime;
protected Timer timer;

protected Profile(DefaultTimer timer) {
this.timer = timer;
}

public void stop() {
timer.stop();
}

void setElapsedTime(long elapsedTime) {
this.elapsedTime = elapsedTime;
}

public long getElapsedTime() {
if(elapsedTime != 0) {
return elapsedTime;
}
return timer.getTime();
}
}
Loading

0 comments on commit 2068c73

Please sign in to comment.