refactored load tests + added chat load test

This commit is contained in:
betasteward 2015-08-26 15:50:58 -04:00
parent 1e60605d84
commit 2c1df69d11
5 changed files with 166 additions and 67 deletions

View file

@ -149,7 +149,7 @@ public class ServerTest {
Assert.assertEquals("Table state incorrect", TableState.WAITING, table.getTableState());
boolean result = client.joinTable(client.getMainRoomId(), table.getTableId(), USERNAME, "Human", 1, DeckImporterUtil.importDeck("TestDeck.dck"), "");
Assert.assertTrue("Unable to join table", result);
Assert.assertTrue("Joined table didn't fire", client.isJointedTableFired());
Assert.assertTrue("Joined table didn't fire", client.isJoinedTableFired());
}
}

View file

@ -26,6 +26,7 @@ import mage.view.RoomView;
import mage.view.TableView;
import mage.view.UserDataView;
import mage.view.UserRequestMessage;
import org.apache.log4j.Logger;
import org.mage.network.Client;
import org.mage.network.interfaces.MageClient;
import org.mage.network.messages.MessageType;
@ -36,6 +37,8 @@ import org.mage.network.messages.MessageType;
*/
public class TestClient implements MageClient {
protected static final Logger logger = Logger.getLogger(TestClient.class);
private Client client;
private ServerState serverState;
private boolean joinedTableFired = false;
@ -77,9 +80,18 @@ public class TestClient implements MageClient {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
public void joinMainChat() {
UUID mainRoomId = client.getServerState().getMainRoomId();
client.joinChat(client.getRoomChatId(mainRoomId));
}
public void sendChatMessage(UUID chatId, String message) {
client.sendChatMessage(chatId, message);
}
@Override
public void receiveChatMessage(UUID chatId, ChatMessage message) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
logger.info("Recieved message: " + message);
}
@Override
@ -102,7 +114,7 @@ public class TestClient implements MageClient {
joinedTableFired = true;
}
public boolean isJointedTableFired() {
public boolean isJoinedTableFired() {
return joinedTableFired;
}

View file

@ -0,0 +1,82 @@
package org.mage.server.test.load;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import mage.server.ServerMain;
import org.apache.log4j.Logger;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.mage.server.test.TestClient;
/**
*
* @author BetaSteward
*/
public abstract class BaseLoadTest {
protected static final Logger logger = Logger.getLogger(BaseLoadTest.class);
protected Map<String, TestClient> users = new HashMap<>();
@BeforeClass
public static void setUpClass() {
new Thread(new Runnable() {
@Override
public void run() {
ServerMain.main(new String[] {"-fastDbMode=true"});
// ServerMain.main(new String[] {""});
}
}).start();
try {
waitForServer("ACTIVE"); //wait for server to startup
} catch (InterruptedException | IOException ex) {
}
}
private static void waitForServer(String message) throws FileNotFoundException, IOException, InterruptedException {
FileReader fr = new FileReader("mageserver.log");
BufferedReader br = new BufferedReader(fr);
do { //read until end-of-file
String line = br.readLine();
if (line == null) {
break;
}
} while (true);
do { //read only new lines
String line = br.readLine();
if (line == null) {
Thread.sleep(1000);
}
else {
if (line.contains(message))
break;
}
} while (true);
}
protected void connect(int numUsers) {
for (int i = 0; i < numUsers; i++) {
String username = "player" + i;
TestClient client = new TestClient();
client.connect(username);
users.put(username, client);
}
}
protected void disconnect() {
for (TestClient client: users.values()) {
client.disconnect(false);
}
for (TestClient client: users.values()) {
Assert.assertFalse("user did not disconnect", client.isConnected());
}
}
}

View file

@ -0,0 +1,62 @@
package org.mage.server.test.load;
import java.util.Random;
import org.junit.Test;
import org.mage.server.test.TestClient;
/**
*
* @author BetaSteward
*/
public class ChatLoadTest extends BaseLoadTest {
private static final Integer USER_COUNT = 200;
private static final int NUM_TESTS = 50;
private static final StringBuilder sb = new StringBuilder();
private static final Random rng = new Random();
private static final char[] symbols;
static {
StringBuilder tmp = new StringBuilder();
for (char ch = '0'; ch <= '9'; ++ch)
tmp.append(ch);
for (char ch = 'a'; ch <= 'z'; ++ch)
tmp.append(ch);
for (char ch = 'A'; ch <= 'Z'; ++ch)
tmp.append(ch);
symbols = tmp.toString().toCharArray();
}
@Test
public void chat() {
connect(USER_COUNT);
for (TestClient client: users.values()) {
client.joinMainChat();
}
Object[] clients = users.values().toArray();
for (int i = 0; i < NUM_TESTS; i++) {
TestClient client = (TestClient)clients[rng.nextInt(clients.length)];
String message = randomString();
logger.info("Sending chat message#:" + i + " message:" + message);
client.sendChatMessage(client.getServerState().getMainRoomId(), message);
try {
Thread.sleep(rng.nextInt(450) + 50); // sleep between 50 and 500 ms
} catch (InterruptedException ex) {
}
}
disconnect();
}
private String randomString() {
sb.setLength(0);
for (int i = 0; i < rng.nextInt(100); i++) {
sb.append(symbols[rng.nextInt(symbols.length)]);
}
return sb.toString();
}
}

View file

@ -1,84 +1,27 @@
package org.mage.server.test.load;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import mage.server.ServerMain;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.mage.server.test.TestClient;
/**
*
* @author BetaSteward
*/
public class ConnectionLoadTest {
public class ConnectionLoadTest extends BaseLoadTest {
@BeforeClass
public static void setUpClass() {
new Thread(new Runnable() {
@Override
public void run() {
ServerMain.main(new String[] {"-fastDbMode=true"});
// ServerMain.main(new String[] {""});
}
}).start();
try {
waitForServer("ACTIVE"); //wait for server to startup
} catch (InterruptedException | IOException ex) {
}
}
public static void waitForServer(String message) throws FileNotFoundException, IOException, InterruptedException {
FileReader fr = new FileReader("mageserver.log");
BufferedReader br = new BufferedReader(fr);
do { //read until end-of-file
String line = br.readLine();
if (line == null) {
break;
}
} while (true);
do { //read only new lines
String line = br.readLine();
if (line == null) {
Thread.sleep(1000);
}
else {
if (line.contains(message))
break;
}
} while (true);
}
private static final Integer USER_CONNECT_COUNT = 200;
private static final Integer USER_COUNT = 400;
@Test
@Ignore
public void connectTest() {
Map<String, TestClient> users = new HashMap<>();
for (int i = 0; i < USER_CONNECT_COUNT; i++) {
String username = "player" + i;
TestClient client = new TestClient();
client.connect(username);
users.put(username, client);
}
connect(USER_COUNT);
try {
Thread.sleep(10000);
Thread.sleep(60000); // wait for 1 minute -- this allows some ping requests to bounce around
} catch (InterruptedException ex) {
}
for (TestClient client: users.values()) {
client.disconnect(false);
}
for (TestClient client: users.values()) {
Assert.assertFalse("user did not disconnect", client.isConnected());
}
disconnect();
}
}