|
| 1 | +package org.cloudcoder.app.loadtester; |
| 2 | + |
| 3 | +import java.util.Arrays; |
| 4 | + |
| 5 | +import org.cloudcoder.app.shared.model.Change; |
| 6 | +import org.cloudcoder.app.shared.model.ChangeType; |
| 7 | +import org.cloudcoder.app.shared.model.CompilationOutcome; |
| 8 | +import org.cloudcoder.app.shared.model.ICallback; |
| 9 | +import org.cloudcoder.app.shared.model.SubmissionResult; |
| 10 | + |
| 11 | +public class LoadTesterTask implements Runnable { |
| 12 | + private HostConfig hostConfig; |
| 13 | + private String userName; |
| 14 | + private String password; |
| 15 | + private EditSequence editSequence; |
| 16 | + private int repeatCount; |
| 17 | + |
| 18 | + public void setHostConfig(HostConfig hostConfig) { |
| 19 | + this.hostConfig = hostConfig; |
| 20 | + } |
| 21 | + |
| 22 | + public void setUserName(String userName) { |
| 23 | + this.userName = userName; |
| 24 | + } |
| 25 | + |
| 26 | + public void setPassword(String password) { |
| 27 | + this.password = password; |
| 28 | + } |
| 29 | + |
| 30 | + public void setEditSequence(EditSequence editSequence) { |
| 31 | + this.editSequence = editSequence; |
| 32 | + } |
| 33 | + |
| 34 | + public void setRepeatCount(int repeatCount) { |
| 35 | + this.repeatCount = repeatCount; |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public void run() { |
| 40 | + try { |
| 41 | + doRun(); |
| 42 | + } catch (Exception e) { |
| 43 | + System.err.println("LoadTesterTask caught exception: " + e.toString()); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + private void doRun() throws Exception { |
| 48 | + Client client = new Client(hostConfig); |
| 49 | + |
| 50 | + client.login(userName, password); |
| 51 | + |
| 52 | + PlayEditSequence player = new PlayEditSequence(); |
| 53 | + player.setClient(client); |
| 54 | + player.setEditSequence(editSequence); |
| 55 | + player.setSubmitOnFullTextChange(true); |
| 56 | + |
| 57 | + player.setOnSend(new ICallback<Change[]>() { |
| 58 | + @Override |
| 59 | + public void call(Change[] value) { |
| 60 | + if (value.length == 1 && value[0].getType() == ChangeType.FULL_TEXT) { |
| 61 | + System.out.print("\u2191"); |
| 62 | + } else { |
| 63 | + char[] a = new char[value.length]; |
| 64 | + Arrays.fill(a, '.'); |
| 65 | + System.out.print(new String(a)); |
| 66 | + } |
| 67 | + System.out.flush(); |
| 68 | + } |
| 69 | + }); |
| 70 | + player.setOnSubmissionResult(new ICallback<SubmissionResult>() { |
| 71 | + public void call(SubmissionResult value) { |
| 72 | + char c; |
| 73 | + if (value.getCompilationResult().getOutcome() != CompilationOutcome.SUCCESS |
| 74 | + || value.getNumTestsPassed() < value.getNumTestsAttempted()) { |
| 75 | + c = '\u2639'; |
| 76 | + } else { |
| 77 | + c = '\u263A'; |
| 78 | + } |
| 79 | + System.out.print(String.valueOf(c)); |
| 80 | + System.out.flush(); |
| 81 | + } |
| 82 | + }); |
| 83 | + |
| 84 | + player.setup(); |
| 85 | + |
| 86 | + for (int i = 0; i < repeatCount; i++) { |
| 87 | + player.play(); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | +} |
0 commit comments