Profiler code

This commit is contained in:
Disasm 2013-07-11 07:51:57 +04:00
parent fc1ab2d8e7
commit 1a419489d1
2 changed files with 51 additions and 0 deletions

View file

@ -261,6 +261,7 @@ public class EntityJump extends Entity {
}
public void prepareToJump() {
LocalProfiler.start("EntityJump.prepareToJump");
boolean betweenWorlds;
betweenWorlds = fromSpace || toSpace;
@ -303,6 +304,7 @@ public class EntityJump extends Entity {
if (distance <= this.shipLength && !betweenWorlds && !isCoordJump) {
killEntity("Not enough space for jump.");
messageToAllPlayersOnShip("Not enough space for jump!");
LocalProfiler.stop();
return;
}
@ -312,6 +314,7 @@ public class EntityJump extends Entity {
if (bedrockOnShip) {
killEntity("Bedrock is on the ship. Aborting.");
messageToAllPlayersOnShip("Bedrock is on the ship. Aborting.");
LocalProfiler.stop();
return;
}
@ -321,6 +324,7 @@ public class EntityJump extends Entity {
this.currentIndexInShip = 0;
msCounter = System.currentTimeMillis();
LocalProfiler.stop();
}
/**
@ -359,6 +363,7 @@ public class EntityJump extends Entity {
* @param shipSize
*/
public void saveShip(int shipSize) {
LocalProfiler.start("EntityJump.saveShip");
ship = new JumpBlock[shipSize];
int index = 0;
@ -367,6 +372,7 @@ public class EntityJump extends Entity {
for (int y = minY; y <= maxY; y++) {
if (ship == null) {
killEntity("ship is null!");
LocalProfiler.stop();
return;
}
@ -391,12 +397,14 @@ public class EntityJump extends Entity {
}
System.out.println((new StringBuilder()).append("[JUMP] Ship saved: ").append((new StringBuilder()).append(ship.length).append(" blocks")).toString());
LocalProfiler.stop();
}
/**
*Ship moving
*/
public void moveShip() {
LocalProfiler.start("EntityJump.moveShip");
int blocksToMove = Math.min(BLOCKS_PER_TICK, ship.length - currentIndexInShip);
System.out.println("[JE] Moving ship part: " + currentIndexInShip + "/" + ship.length + " [btm: " + blocksToMove + "]");
@ -405,6 +413,7 @@ public class EntityJump extends Entity {
moveBlockSimple(currentIndexInShip, distance, dir, toSpace, fromSpace);
currentIndexInShip++;
}
LocalProfiler.stop();
}
/**
@ -444,6 +453,7 @@ public class EntityJump extends Entity {
* Получить реальное количество блоков, из которых состоит корабль
*/
public int getRealShipSize() {
LocalProfiler.start("EntityJump.getRealShipSize");
int shipSize = 0;
for (int x = minX; x <= maxX; x++) {
@ -457,6 +467,7 @@ public class EntityJump extends Entity {
if (blockID == Block.bedrock.blockID) {
bedrockOnShip = true;
LocalProfiler.stop();
return shipSize;
}
}
@ -464,6 +475,7 @@ public class EntityJump extends Entity {
}
}
LocalProfiler.stop();
return shipSize;
}

View file

@ -0,0 +1,39 @@
package cr0s.WarpDrive;
import java.util.Stack;
public class LocalProfiler {
private static class StackElement {
public long start;
public long internal;
public String name;
}
private static Stack<StackElement> stack = new Stack<StackElement>();
public static void start(String name) {
StackElement e = new StackElement();
e.start = System.nanoTime();
e.internal = 0;
e.name = name;
stack.push(e);
}
public static void stop() {
if (stack.isEmpty()) return;
StackElement e = stack.pop();
long end = System.nanoTime();
long dt = end - e.start;
if (!stack.isEmpty()) {
StackElement e2 = stack.peek();
e2.internal += dt;
}
long self = (dt - e.internal) / 1000; // in microseconds
long total = dt / 1000;
System.out.println("[PROF] {" + e.name + "} self: " + (self/1000F) + "ms, total: " + (total/1000F) + "ms");
}
}