Skip to content

Instantly share code, notes, and snippets.

@ingenthr
Created April 12, 2011 18:58
Show Gist options
  • Select an option

  • Save ingenthr/916143 to your computer and use it in GitHub Desktop.

Select an option

Save ingenthr/916143 to your computer and use it in GitHub Desktop.
Simple game simulator
/*
* This benchmark has been based on the FTP101 driver
*/
package com.couchbase.demo.gamesim;
import com.google.gson.Gson;
import com.sun.faban.driver.*;
import com.sun.faban.driver.util.Random;
import com.sun.faban.harness.EndRun;
import java.net.URISyntaxException;
import java.util.logging.Level;
import sun.net.ftp.FtpClient;
import javax.xml.xpath.XPathExpressionException;
import java.io.*;
import java.net.URI;
import java.util.ArrayList;
import java.util.logging.Logger;
import net.spy.memcached.MemcachedClient;
@BenchmarkDefinition(name = "Game Simulator",
version = "0.1",
configPrecedence = true)
@BenchmarkDriver(name = "GameSimDriver",
threadPerScale = (float) 1)
@MatrixMix (
operations = {"Login", "Logout", "Eat", "AttackRandom"},
mix = { @Row({0, 0, 66, 34 }),
@Row({100, 0, 0, 0 }),
@Row({0, 10, 56, 34 }),
@Row({0, 10, 56, 34})
}
)
@NegativeExponential(cycleType = CycleType.CYCLETIME,
cycleMean = 2000,
cycleDeviation = 2)
public class GameSimDriver {
/** The driver context for this instance. */
private DriverContext ctx;
Logger logger;
Random random;
FtpClient ftpClient;
int fileCount;
String host;
int port = -1;
int threadId;
int putSequence = 1;
String localFileName;
String uploadPrefix;
String user;
Gson gson = new Gson();
String playerName = "Matt Ingenthron";
String password;
Player player;
byte[] buffer = new byte[8192];
MemcachedClient playersStore;
MemcachedClient currentStatsStore;
/**
* Constructs an instance of a user session on the game simulator.
* @throws XPathExpressionException An XPath error occurred
* @throws IOException I/O error creating the driver instance
*/
public GameSimDriver() throws XPathExpressionException, IOException {
ctx = DriverContext.getContext();
logger = ctx.getLogger();
random = ctx.getRandom();
threadId = ctx.getThreadId();
uploadPrefix = "up" + threadId + '_';
localFileName = "/tmp/ftp" + threadId;
host =
ctx.getXPathValue("/ftpBenchmark/serverConfig/host").trim();
String portNum =
ctx.getXPathValue("/ftpBenchmark/serverConfig/port").trim();
fileCount = Integer.parseInt(ctx.getXPathValue(
"/ftpBenchmark/serverConfig/fileCount").trim());
user = ctx.getProperty("user");
password = ctx.getProperty("password");
URI server;
try {
// Create a basic client
server = new URI("http://localhost:8091/pools");
} catch (URISyntaxException ex) {
Logger.getLogger(GameSimDriver.class.getName()).log(Level.SEVERE, null, ex);
throw new IOException("Could not deal with URI.");
}
ArrayList<URI> servers = new ArrayList<URI>();
servers.add(server);
try {
playersStore = new MemcachedClient(servers, "gamesim-users", "gamesim-users", "letmein");
} catch (javax.naming.ConfigurationException ex) {
Logger.getLogger(GameSimDriver.class.getName()).log(Level.SEVERE, null, ex);
throw new IOException("Could not configure the memcached client.");
}
}
/**
* Operation to simulate a login.
* @throws IOException Error connecting to server
*/
@BenchmarkOperation(name = "Login",
max90th = 2,
timing = Timing.MANUAL)
public void doLogin() throws IOException {
if (player != null) {
return;
}
ctx.recordTime();
String playerJsonRepresentation = (String) playersStore.get(stripBlanks(playerName));
logger.info("Player JSON:\n " + playerJsonRepresentation);
if (playerJsonRepresentation == null) {
logger.info("Player JSON:\n " + playerJsonRepresentation);
player = new Player(playerName);
} else {
player = gson.fromJson(playerJsonRepresentation, Player.class);
}
ctx.recordTime();
}
/**
* Operation to simulate a login.
* @throws IOException Error connecting to server
*/
@BenchmarkOperation(name = "Logout",
max90th = 2,
timing = Timing.MANUAL)
public void doLogout() throws IOException {
if (player == null) {
return; // can't log out when logged out
}
ctx.recordTime();
playersStore.set(stripBlanks(player.getName()), 0, gson.toJson(player));
player = null;
ctx.recordTime();
}
/**
* Operation to simulate attacking another player.
* @throws IOException Error connecting to server
*/
@BenchmarkOperation(name = "AttackRandom",
max90th = 2,
timing = Timing.MANUAL)
public void doAttackRandom() throws IOException {
doLogin();
ctx.recordTime();
player.wound(1);
ctx.recordTime();
}
/**
* Operation to simulate eating.
* @throws IOException Error connecting to server
*/
@BenchmarkOperation(name = "Eat",
max90th = 2,
timing = Timing.MANUAL)
public void doEat() throws IOException {
doLogin();
ctx.recordTime();
player.feed(100);
ctx.recordTime();
}
@EndRun
public void cleanup() {
playersStore.flush();
playersStore.shutdown();
}
public static String stripBlanks(String s) {
return s.replaceAll("\\s", "");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment