Skip to content

Commit

Permalink
Support configurable latency.
Browse files Browse the repository at this point in the history
Added the stack frame is now part of addRandomChanceOfFailure() because it is needed for all branching logic.
  • Loading branch information
mrwilson committed Jun 3, 2016
1 parent 8383d5b commit 70dca40
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class ByteMonkeyClassTransformer implements ClassFileTransformer {
private static Double activationRatio = 1.0;
private static Random random = new Random();
private final Pattern filter;
private final long latency;

public ByteMonkeyClassTransformer(String args) {
Map<String, String> configuration = Arrays
Expand All @@ -30,7 +31,8 @@ public ByteMonkeyClassTransformer(String args) {
);

this.mode = OperationMode.fromLowerCase(configuration.getOrDefault("mode", OperationMode.FAULT.name()));
this.filter = Pattern.compile(configuration.getOrDefault("filter",".*"));
this.filter = Pattern.compile(configuration.getOrDefault("filter", ".*"));
this.latency = Long.valueOf(configuration.getOrDefault("latency","100"));
activationRatio = Double.valueOf(configuration.getOrDefault("rate","1"));

}
Expand Down Expand Up @@ -81,9 +83,9 @@ private Optional<InsnList> createNewInstructions(MethodNode method) {
}

private Optional<InsnList> createLatency() {
InsnList list = new InsnList();
final InsnList list = new InsnList();

list.add(new LdcInsnNode(2000L));
list.add(new LdcInsnNode(this.latency));
list.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "java/lang/Thread", "sleep", "(J)V", false));

return Optional.of(addRandomChanceOfFailure(list));
Expand All @@ -108,12 +110,6 @@ private Optional<InsnList> throwException(List<String> exceptionsThrown) {

list.add(new InsnNode(Opcodes.ATHROW));

list.add(new FrameNode(
Opcodes.F_APPEND, // append to the last stack frame
0, new Object[] {}, // no local variables here
0, new Object[] {} // no stack either!
));

return Optional.of(addRandomChanceOfFailure(list));
}

Expand All @@ -131,7 +127,15 @@ private InsnList addRandomChanceOfFailure(final InsnList newInstructions) {
));

list.add(new JumpInsnNode(Opcodes.IFEQ, originalCodeLabel));

list.add(newInstructions);

list.add(new FrameNode(
Opcodes.F_APPEND, // append to the last stack frame
0, new Object[] {}, // no local variables here
0, new Object[] {} // no stack either!
));

list.add(originalCodeLabel);

return list;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ public void printSomething() throws IOException {
public void printSomethingElse() throws IllegalStateException {
System.out.println("Goodbye!");
}

public void safePrint() {
System.out.println("Hi!");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package uk.co.probablyfine.bytemonkey.latency;

import com.ea.agentloader.AgentLoader;
import org.junit.Test;
import uk.co.probablyfine.bytemonkey.ByteMonkeyAgent;
import uk.co.probablyfine.bytemonkey.testfiles.TestObject;

import java.io.IOException;

import static org.junit.Assert.assertTrue;

public class Rate100Test {

@Test
public void shouldThrowExceptionWhenInstrumented_throwPercentageIs100() throws IOException {
AgentLoader.loadAgentClass(
ByteMonkeyAgent.class.getName(),
"mode:latency,rate:1,latency:200,filter:uk/co/probablyfine/bytemonkey/testfiles"
);

long timeTaken = timed(new TestObject()::safePrint);

assertTrue("Actually took "+timeTaken+"ms", timeTaken >= 200 && timeTaken < 300);
}

public static long timed(Runnable runnable) {
long startTime = System.currentTimeMillis();

runnable.run();

return System.currentTimeMillis() - startTime;
}

}

0 comments on commit 70dca40

Please sign in to comment.