Skip to content

Commit

Permalink
test-utils-stress: memory usage statistics added
Browse files Browse the repository at this point in the history
Signed-off-by: Ketoth Xupack <ketoth.xupack@gmail.com>
  • Loading branch information
KetothXupack committed Mar 18, 2014
1 parent 408167f commit b893a9c
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.nohope.test.stress;

import static org.apache.commons.io.FileUtils.byteCountToDisplaySize;

/**
* @author <a href="mailto:ketoth.xupack@gmail.com">Ketoth Xupack</a>
* @since 2014-03-18 12:15
*/
class Memory {
private final long maxMemory;
private final long totalMemory;
private final long freeMemory;

private Memory(final long maxMemory, final long totalMemory, final long freeMemory) {
this.maxMemory = maxMemory;
this.totalMemory = totalMemory;
this.freeMemory = freeMemory;
}

public static Memory getCurrent() {
final Runtime runtime = Runtime.getRuntime();
System.gc();
return new Memory(runtime.maxMemory(), runtime.totalMemory(), runtime.freeMemory());
}

public long getMaxMemory() {
return maxMemory;
}

public long getTotalMemory() {
return totalMemory;
}

public long getFreeMemory() {
return freeMemory;
}

@Override
public String toString() {
return "max: " + byteCountToDisplaySize(maxMemory)
+ ", total: " + byteCountToDisplaySize(totalMemory)
+ ", free: " + byteCountToDisplaySize(freeMemory)
+ ", heap: " + byteCountToDisplaySize(totalMemory - freeMemory);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,23 @@ public class StressResult {
private final int fails;
private final int threadsCount;
private final int cycleCount;
private final Memory memoryStart;
private final Memory memoryEnd;

public StressResult(final Map<String, Result> stats,
final int threadsCount,
final int cycleCount,
final int fails,
final double runtime) {
final double runtime,
final Memory memoryStart,
final Memory memoryEnd) {
this.runtime = runtime;
this.fails = fails;
this.threadsCount = threadsCount;
this.cycleCount = cycleCount;
this.results.putAll(stats);
this.memoryStart = memoryStart;
this.memoryEnd = memoryEnd;
}

/**
Expand Down Expand Up @@ -62,6 +68,14 @@ public int getFails() {
return fails;
}

public Memory getMemoryStart() {
return memoryStart;
}

public Memory getMemoryEnd() {
return memoryEnd;
}

@Override
public String toString() {
final StringBuilder builder = new StringBuilder();
Expand Down Expand Up @@ -95,6 +109,11 @@ public String toString() {
.append(String.format("%.3e", throughputTo(getApproxThroughput(), SECONDS)))
.append(" op/sec")
.append('\n')
.append("Memory usage before test: ")
.append(memoryStart)
.append("\nMemory usage after test: ")
.append(memoryEnd)
.append('\n')
.toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public void run() {
}, "stress-worker-" + k));
}

final Memory memoryStart = Memory.getCurrent();
final long overallStart = resolution.currentTime();
for (final Thread thread : threads) {
thread.start();
Expand All @@ -75,7 +76,10 @@ public void run() {
}
final long overallEnd = resolution.currentTime();

final double runtime = overallEnd - overallStart;
final Memory memoryEnd = Memory.getCurrent();


final double runningTime = overallEnd - overallStart;

final Map<String, Result> results = new HashMap<>();
int fails = 0;
Expand All @@ -85,7 +89,8 @@ public void run() {
results.put(r.getName(), r);
}

return new StressResult(results, threadsNumber, cycleCount, fails, runtime);
return new StressResult(results, threadsNumber, cycleCount,
fails, runningTime, memoryStart, memoryEnd);
}

public StressResult measure(final int threadsNumber,
Expand Down Expand Up @@ -120,6 +125,7 @@ public void run() {
}, "stress-worker-" + k));
}

final Memory memoryStart = Memory.getCurrent();
final long overallStart = resolution.currentTime();
for (final Thread thread : threads) {
thread.start();
Expand All @@ -128,6 +134,7 @@ public void run() {
thread.join();
}
final long overallEnd = resolution.currentTime();
final Memory memoryEnd = Memory.getCurrent();

final double runtime = overallEnd - overallStart;

Expand All @@ -139,8 +146,8 @@ public void run() {
results.put(r.getName(), r);
}

return new StressResult(results, threadsNumber, cycleCount, fails,
runtime);
return new StressResult(results, threadsNumber, cycleCount,
fails, runtime, memoryStart, memoryEnd);
}

public StressResult measurePooled(final int threadsNumber,
Expand Down Expand Up @@ -194,6 +201,7 @@ public void run() {
}, "stress-worker-" + k));
}

final Memory memoryStart = Memory.getCurrent();
final long overallStart = resolution.currentTime();
for (final Thread thread : threads) {
thread.start();
Expand All @@ -209,6 +217,7 @@ public void run() {
}
}
final long overallEnd = resolution.currentTime();
final Memory memoryEnd = Memory.getCurrent();

final double runtime = overallEnd - overallStart;

Expand All @@ -220,6 +229,7 @@ public void run() {
results.put(r.getName(), r);
}

return new StressResult(results, threadsNumber, cycleCount, fails, runtime);
return new StressResult(results, threadsNumber, cycleCount,
fails, runtime, memoryStart, memoryEnd);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.nohope.test.stress;

import org.junit.Test;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

/**
* @author <a href="mailto:ketoth.xupack@gmail.com">Ketoth Xupack</a>
* @since 2014-03-18 12:33
*/
public class MemoryTest {
@Test
public void basic() {
final Memory current = Memory.getCurrent();
assertTrue(current.getTotalMemory() > current.getFreeMemory());
assertTrue(current.getMaxMemory() >= current.getTotalMemory());
assertTrue(current.getMaxMemory() > current.getFreeMemory());
assertNotNull(current.toString());
}
}

0 comments on commit b893a9c

Please sign in to comment.