📄 userregistryperformancetest.java
字号:
package net.sf.jawp.gf.persistence.test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;
import net.sf.jawp.gf.api.services.LoginService;
import net.sf.jawp.gf.api.services.UserService;
import net.sf.jawp.gf.system.GameController;
import net.sf.jawp.util.PasswordUtil;
import junit.framework.TestCase;
/**
* this test runs multiple threads and performs register and login operations
*
* @author jarek
* @version $Revision: 1.8 $
*
*/
public class UserRegistryPerformanceTest extends TestCase
{
private static final long TEST_TIME = 1000;
private static final long THREADS = 100;
private int finished = 0;
private int loginOperations = 0;
private int registerOperations = 0;
private int failedRegistrations = 0;
private int failedLoginsTotal = 0;
public final void testConcurrency()
{
final Object monitor = new Object();
final GameController ctrl = new GameController(
"prevayler_store/performance_test", false);
final UserService usr = ctrl.getUserService();
for (int i = 0; i < UserRegistryPerformanceTest.THREADS; i++)
{
new WorkingThread(UserRegistryPerformanceTest.TEST_TIME, monitor,
usr).start();
}
while (getFinished() < UserRegistryPerformanceTest.THREADS)
{
try
{
Thread.sleep(200);
synchronized (monitor)
{
monitor.notifyAll();
}
}
catch (final InterruptedException e)
{
//on purpose
}
}
System.out.println("finished tasks:" + this.finished);
System.out.println("reg tasks:" + this.registerOperations);
System.out.println("log tasks:" + this.loginOperations);
System.out.println("failed regs:" + this.failedRegistrations);
System.out.println("failed logins:" + this.failedLoginsTotal);
ctrl.dispose();
}
private synchronized void reportThreadFinished(final int regOperations,
final int loginOperationsArg, final int failReg, final int failedLogins)
{
this.finished++;
this.registerOperations += regOperations;
this.loginOperations += loginOperationsArg;
this.failedRegistrations += failReg;
this.failedLoginsTotal += failedLogins;
}
/**
* performance test thread
* @author jarek
* @version $Revision: 1.8 $
*
*/
public final class WorkingThread extends Thread
{
public static final int LOGINS_PER_LOOP = 5;
private static final int REGISTRATIONS_PER_LOOP = 1;
private final long addTime;
private final Object theOne;
private final UserService userService;
private final HashMap<String, String> logins = new HashMap<String, String>(
100);
private final ArrayList<String> loginsOnly = new ArrayList<String>(100);
private final Random rnd = new Random();
private int failedReg = 0;
private int succesLogins = 0;
private int failedLogins = 0;
public WorkingThread(final long time, final Object obj,
final UserService usr)
{
super();
// TODO Auto-generated constructor stub
addTime = time;
this.theOne = obj;
this.userService = usr;
}
/**
* {@inheritDoc}
*/
@Override
public void run()
{
synchronized (theOne)
{
try
{
theOne.wait();
}
catch (final InterruptedException e)
{
//on purpose
}
}
mainLoop();
reportThreadFinished(this.logins.size(), succesLogins, failedReg,
failedLogins);
}
private void mainLoop()
{
final long endTime = System.currentTimeMillis() + addTime;
while (System.currentTimeMillis() < endTime)
{
// we will register some users
for (int i = 0; i < WorkingThread.REGISTRATIONS_PER_LOOP; i++)
{
registerRandomUser();
}
for (int i = 0; i < WorkingThread.LOGINS_PER_LOOP; i++)
{
loginRandomUser();
}
}
}
private void registerRandomUser()
{
final String login = "test_user_" + rnd.nextInt() + "_"
+ rnd.nextInt() + "_" + rnd.nextInt();
final String pass = "pass_" + rnd.nextInt();
final String encPass = PasswordUtil.encrypt(pass);
try
{
this.userService.registerUser(login, encPass);
this.logins.put(login, encPass);
this.loginsOnly.add(login);
}
catch (final Exception e)
{
e.printStackTrace();
failedReg++;
}
}
private void loginRandomUser()
{
final int n = rnd.nextInt(this.loginsOnly.size());
final String login = this.loginsOnly.get(n);
final String pass = this.logins.get(login);
try
{
final LoginService ls = this.userService.initLogin(login);
final String seed = ls.getServerSeed();
final String mix = PasswordUtil.encryptMix(pass, seed);
if (ls.login(mix) == null)
{
throw new RuntimeException("problem with login for:"
+ login);
}
this.succesLogins++;
}
catch (final Exception e)
{
e.printStackTrace();
failedLogins++;
}
}
}
/**
* Returns the finished.
*
* @return the value of finished
*/
private synchronized int getFinished()
{
return finished;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -