package sunels;
import java.io.Serializable;
import org.apache.jmeter.protocol.java.sampler.*;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;
import java.util.List;
import java.util.Properties;
import java.io.IOException;
import java.util.ArrayList;
import java.io.FileInputStream;
import com.mycompany.myproduct.um.msgstore.MessagestoreFactory;
import com.mycompany.myproduct.um.profile.ProfileFactory;
import java.util.Date;
import com.mycompany.myproduct.um.common.Language;
import com.mycompany.myproduct.um.profile.Subscriber;
import java.util.Random;
public class JmeterTest implements JavaSamplerClient , Serializable {
private static List msisdns = new ArrayList();
private static int totalRecordCount = 0;
private static java.util.Date startDate = new java.util.Date();
private static final String umModuleName = "UM";
private ProfileFactory profilefactory = ProfileFactory.getInstance (umModuleName);
private MessagestoreFactory msgstoreFactory = MessagestoreFactory.getInstance (umModuleName);
private Random randomGenerator = new Random();
private static int testType = 1;
static {
try {
Properties props = new Properties();
props.load (new FileInputStream ("/tmp/phoneNumbers.txt"));
msisdns = new ArrayList (props.keySet());
totalRecordCount = msisdns.size();
System.out.println (" -- Msisdns are loaded ....");
}
catch (IOException e) {
e.printStackTrace ();
System.exit (1);
}
}
public void setupTest(JavaSamplerContext context) {
}
public Arguments getDefaultParameters() {
Arguments params = new Arguments();
return params;
}
public synchronized String getMsisdn (){
String subscriberMsisdn = (String) msisdns.get(randomGenerator.nextInt(totalRecordCount));
return subscriberMsisdn;
}
public Subscriber playWithSubscriber (String subscriberPhoneNumber) throws Exception {
Subscriber subscriber = null;
subscriber = (Subscriber) profilefactory.querySubscriberByPhoneNumber (profilefactory.getCommonFactory ().createPhoneNumber (subscriberPhoneNumber));
if (subscriber != null) {
if(testType != 1){
Language subscriberLanguage = subscriber.getLanguage ();
if (subscriberLanguage == Language.ENGLISH) {
subscriber.setLanguage (Language.UKRAINIAN);
}
else {
subscriber.setLanguage (Language.ENGLISH);
}
Date currDate = new java.util.Date ();
subscriber.getSubscription ().setLastLoginDate (currDate);
subscriber.getSubscription ().setLastMessageActivityDate (currDate);
subscriber.save ();
}
}
else {
System.out.println (" -- Unable to find subscriber with msisdn :" + subscriberPhoneNumber);
}
return subscriber;
}
public SampleResult runTest(JavaSamplerContext context) {
SampleResult results = new SampleResult();
results.sampleStart();
String msisdn = getMsisdn();
try{
playWithSubscriber (msisdn);
results.setSuccessful(true);
results.setResponseCodeOK();
results.setResponseMessage("Subscriber found:" + msisdn);
} catch (Exception ex){
ex.printStackTrace();
results.setSuccessful(true);
results.setResponseCode("Exception*");
results.setResponseMessage("Subscriber not found:" + msisdn);
}
results.sampleEnd();
return results;
}
public void teardownTest(JavaSamplerContext context) {
}
}
No comments:
Post a Comment