forked from takari/maven-profiler
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
o dealing with profiling multi-module projects
- Loading branch information
Jason van Zyl
committed
Jan 26, 2012
1 parent
ea85b8d
commit ec7986c
Showing
11 changed files
with
352 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
src/main/java/io/tesla/lifecycle/profiler/LifecycleProfiler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package io.tesla.lifecycle.profiler; | ||
|
||
import javax.inject.Named; | ||
import javax.inject.Singleton; | ||
|
||
import org.apache.maven.eventspy.AbstractEventSpy; | ||
import org.apache.maven.execution.ExecutionEvent; | ||
|
||
/** | ||
* @author Jason van Zyl | ||
*/ | ||
@Named | ||
@Singleton | ||
public class LifecycleProfiler extends AbstractEventSpy { | ||
|
||
private SessionProfile sessionProfile; | ||
private ProjectProfile projectProfile; | ||
private PhaseProfile phaseProfile; | ||
private MojoProfile mojoProfile; | ||
|
||
@Override | ||
public void init(Context context) throws Exception { | ||
} | ||
|
||
@Override | ||
public void onEvent(Object event) throws Exception { | ||
if (event instanceof ExecutionEvent) { | ||
|
||
|
||
ExecutionEvent executionEvent = (ExecutionEvent) event; | ||
if (executionEvent.getType() == ExecutionEvent.Type.SessionStarted) { | ||
// | ||
// | ||
// | ||
sessionProfile = new SessionProfile(); | ||
} else if (executionEvent.getType() == ExecutionEvent.Type.SessionEnded) { | ||
// | ||
// | ||
// | ||
sessionProfile.stop(); | ||
|
||
SessionProfileRenderer r = new SessionProfileRenderer(); | ||
r.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) { | ||
// | ||
// | ||
// | ||
projectProfile.stop(); | ||
sessionProfile.addProjectProfile(projectProfile); | ||
} else if (executionEvent.getType() == ExecutionEvent.Type.MojoStarted) { | ||
String phase = executionEvent.getMojoExecution().getLifecyclePhase(); | ||
// | ||
// Create a new phase profile if one doesn't exist or the phase has changed. | ||
// | ||
if(phaseProfile == null) { | ||
phaseProfile = new PhaseProfile(phase); | ||
} else if (!phaseProfile.getPhase().equals(phase)) { | ||
phaseProfile.stop(); | ||
System.out.println(">> phase time : " + phaseProfile.getElapsedTime()); | ||
projectProfile.addPhaseProfile(phaseProfile); | ||
phaseProfile = new PhaseProfile(phase); | ||
} | ||
mojoProfile = new MojoProfile(executionEvent.getMojoExecution()); | ||
} else if (executionEvent.getType() == ExecutionEvent.Type.MojoSucceeded || executionEvent.getType() == ExecutionEvent.Type.MojoFailed) { | ||
// | ||
// | ||
// | ||
mojoProfile.stop(); | ||
phaseProfile.addMojoProfile(mojoProfile); | ||
} | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/io/tesla/lifecycle/profiler/MojoProfile.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package io.tesla.lifecycle.profiler; | ||
|
||
import org.apache.maven.plugin.MojoExecution; | ||
|
||
public class MojoProfile extends Profile { | ||
|
||
private MojoExecution mojoExecution; | ||
|
||
protected MojoProfile(MojoExecution mojoExecution) { | ||
super(new Timer()); | ||
this.mojoExecution = mojoExecution; | ||
} | ||
|
||
public String getId() { | ||
return mojoExecution.getGroupId() + ":" + mojoExecution.getArtifactId() + ":" + mojoExecution.getVersion() + " (" + mojoExecution.getExecutionId() + ") "; | ||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/io/tesla/lifecycle/profiler/PhaseProfile.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package io.tesla.lifecycle.profiler; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class PhaseProfile extends Profile { | ||
|
||
private String phase; | ||
private List<MojoProfile> mojoProfiles; | ||
|
||
public PhaseProfile(String phase) { | ||
super(new Timer()); | ||
this.phase = phase; | ||
this.mojoProfiles = new ArrayList<MojoProfile>(); | ||
} | ||
|
||
public void addMojoProfile(MojoProfile mojoProfile) { | ||
mojoProfiles.add(mojoProfile); | ||
} | ||
|
||
public String getPhase() { | ||
return phase; | ||
} | ||
|
||
public List<MojoProfile> getMojoProfiles() { | ||
return mojoProfiles; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package io.tesla.lifecycle.profiler; | ||
|
||
public class Profile { | ||
|
||
protected long elapsedTime; | ||
protected Timer timer; | ||
|
||
protected Profile(Timer 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(); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/io/tesla/lifecycle/profiler/ProjectProfile.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package io.tesla.lifecycle.profiler; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.apache.maven.project.MavenProject; | ||
|
||
public class ProjectProfile extends Profile { | ||
|
||
private MavenProject project; | ||
private List<PhaseProfile> phaseProfiles; | ||
|
||
public ProjectProfile(MavenProject project) { | ||
super(new Timer()); | ||
this.project = project; | ||
this.phaseProfiles = new ArrayList<PhaseProfile>(); | ||
} | ||
|
||
public void addPhaseProfile(PhaseProfile phaseProfile) { | ||
phaseProfiles.add(phaseProfile); | ||
} | ||
|
||
public String getProjectName() { | ||
return project.getGroupId() + ":" + project.getArtifactId() + ":" + project.getVersion(); | ||
} | ||
|
||
public List<PhaseProfile> getPhaseProfile() { | ||
return phaseProfiles; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/io/tesla/lifecycle/profiler/SessionProfile.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package io.tesla.lifecycle.profiler; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class SessionProfile extends Profile { | ||
|
||
private List<ProjectProfile> projectProfiles; | ||
|
||
public SessionProfile() { | ||
super(new Timer()); | ||
this.projectProfiles = new ArrayList<ProjectProfile>(); | ||
} | ||
|
||
public void addProjectProfile(ProjectProfile projectProfile) { | ||
projectProfiles.add(projectProfile); | ||
} | ||
|
||
public List<ProjectProfile> getProjectProfiles() { | ||
return projectProfiles; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/io/tesla/lifecycle/profiler/SessionProfileRenderer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package io.tesla.lifecycle.profiler; | ||
|
||
public class SessionProfileRenderer { | ||
|
||
public void render(SessionProfile sessionProfile) { | ||
|
||
for(ProjectProfile pp : sessionProfile.getProjectProfiles()) { | ||
render(pp.getProjectName()); | ||
for(PhaseProfile phaseProfile : pp.getPhaseProfile()) { | ||
render(" " + phaseProfile.getPhase() + " " + Timer.formatTime(phaseProfile.getElapsedTime())); | ||
for(MojoProfile mp : phaseProfile.getMojoProfiles()) { | ||
render(" " + mp.getId() + Timer.formatTime(mp.getElapsedTime())); | ||
} | ||
} | ||
render(""); | ||
} | ||
} | ||
|
||
private void render(String s) { | ||
System.out.println(s); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package io.tesla.lifecycle.profiler; | ||
|
||
public class Timer { | ||
public static final int MS_PER_SEC = 1000; | ||
public static final int SEC_PER_MIN = 60; | ||
private long start; | ||
private long time; | ||
|
||
public Timer() { | ||
start = System.currentTimeMillis(); | ||
} | ||
|
||
public void stop() { | ||
time = elapsedTime(); | ||
} | ||
|
||
public long getTime() { | ||
return time; | ||
} | ||
|
||
private long elapsedTime() { | ||
return System.currentTimeMillis() - start; | ||
} | ||
|
||
public static String formatTime(long ms) { | ||
long secs = ms / MS_PER_SEC; | ||
long mins = secs / SEC_PER_MIN; | ||
secs = secs % SEC_PER_MIN; | ||
long fractionOfASecond = ms - (secs * 1000); | ||
// System.out.println("mins " + mins); | ||
// System.out.println("secs " + secs); | ||
// System.out.println(fractionOfASecond); | ||
// System.out.println(">> " + fractionOfASecond); | ||
|
||
String msg = mins + "m " + secs + "." + fractionOfASecond; | ||
|
||
if (msg.length() == 3) { | ||
msg += "00s"; | ||
} else if (msg.length() == 4) { | ||
msg += "0s"; | ||
} else { | ||
msg += "s"; | ||
} | ||
|
||
return msg; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<component-set> | ||
<components> | ||
<component> | ||
<role>org.apache.maven.eventspy.EventSpy</role> | ||
<role-hint>default</role-hint> | ||
<implementation>io.tesla.lifecycle.profiler.LifecycleProfiler</implementation> | ||
<description /> | ||
<isolated-realm>false</isolated-realm> | ||
</component> | ||
</components> | ||
</component-set> |
63 changes: 63 additions & 0 deletions
63
src/test/java/io/tesla/lifecycle/profiler/LifecycleProfilerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package io.tesla.lifecycle.profiler; | ||
|
||
import junit.framework.TestCase; | ||
|
||
import org.apache.maven.model.Plugin; | ||
import org.apache.maven.plugin.MojoExecution; | ||
import org.apache.maven.project.MavenProject; | ||
|
||
|
||
public class LifecycleProfilerTest extends TestCase { | ||
|
||
public void testSessionProfile() { | ||
|
||
SessionProfile s = new SessionProfile(); | ||
|
||
ProjectProfile p0 = new ProjectProfile(project("g0", "a0", "v0")); | ||
PhaseProfile ph0 = new PhaseProfile("phase0"); | ||
MojoProfile m0 = new MojoProfile(mojoExecution("goal0","m0")); | ||
m0.setElapsedTime(3000); | ||
ph0.addMojoProfile(m0); | ||
MojoProfile m00 = new MojoProfile(mojoExecution("goal00","m00")); | ||
m00.setElapsedTime(5492009); | ||
ph0.addMojoProfile(m00); | ||
p0.addPhaseProfile(ph0); | ||
s.addProjectProfile(p0); | ||
|
||
ProjectProfile p1 = new ProjectProfile(project("g1", "a1", "v1")); | ||
PhaseProfile ph1 = new PhaseProfile("phase1"); | ||
MojoProfile m1 = new MojoProfile(mojoExecution("goal1", "m1")); | ||
m1.setElapsedTime(2500); | ||
ph1.addMojoProfile(m1); | ||
p1.addPhaseProfile(ph1); | ||
s.addProjectProfile(p1); | ||
|
||
ProjectProfile p2 = new ProjectProfile(project("g2", "a2", "v2")); | ||
PhaseProfile ph2 = new PhaseProfile("phase2"); | ||
MojoProfile m2 = new MojoProfile(mojoExecution("goal2","m2")); | ||
m2.setElapsedTime(5000); | ||
ph2.addMojoProfile(m2); | ||
p2.addPhaseProfile(ph2); | ||
s.addProjectProfile(p2); | ||
|
||
SessionProfileRenderer r = new SessionProfileRenderer(); | ||
r.render(s); | ||
} | ||
|
||
protected MavenProject project(String g, String a, String v) { | ||
MavenProject p = new MavenProject(); | ||
p.setGroupId(g); | ||
p.setArtifactId(a); | ||
p.setVersion(v); | ||
return p; | ||
} | ||
|
||
protected MojoExecution mojoExecution(String goal, String executionId) { | ||
Plugin p = new Plugin(); | ||
p.setGroupId("groupId"); | ||
p.setArtifactId("artifactId"); | ||
p.setVersion("version"); | ||
MojoExecution me = new MojoExecution(p, goal, executionId); | ||
return me; | ||
} | ||
} |