refactor HBasePlugin

refactor HBasePlugin
This commit is contained in:
coderfengyun 2014-05-04 16:43:38 +08:00
parent b19f6b6b5e
commit a9437a45e0
5 changed files with 70 additions and 38 deletions

View File

@ -22,6 +22,12 @@ public class HBasePlugin {
private HBaseMesseger hBaseMesseger = new HBaseMesseger();
private SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyyMMddhhmmssS");
// Give it a default value
private int sendRate = 1;
private int sampleRate = 10;
private static int ONE_DAY_TO_MS = 24 * 60 * 60 * 1000;
private static int ONE_MINUTE_TO_MS = 60 * 1000;
static int ONE_DAY_TO_MINUTE = 24 * 60;
public static List<String> LABEL_LIST;
static {
@ -32,6 +38,16 @@ public class HBasePlugin {
LABEL_LIST.add("max");
}
/**
*
* @param sampleRate
* this param's unit is minute
* @param sendRate
* this param's unit is day
* @param voltage
* @param electricCurrent
* @param electricity
*/
@Constructor
public HBasePlugin(
@Parameter(value = "sampleRate", type = SupportTypes.Field) String sampleRate,
@ -39,17 +55,21 @@ public class HBasePlugin {
@Parameter(value = "voltage", type = SupportTypes.Table) String voltage,
@Parameter(value = "electricCurrent", type = SupportTypes.Table) String electricCurrent,
@Parameter(value = "electricity", type = SupportTypes.Table) String electricity) {
this.sendRate = Integer.parseInt(sendRate);
this.sampleRate = Integer.parseInt(sampleRate);
int sampleCountForSendOnce = this.sendRate * ONE_DAY_TO_MINUTE
/ this.sampleRate;
Row voltageRow = Table.buildTable(voltage, LABEL_LIST).getRow(0);
this.voltageGenerator = RecordGenerator
.buildGeneratorWithRow(voltageRow);
this.voltageGenerator = RecordGenerator.buildGeneratorWithRow(
voltageRow, sampleCountForSendOnce);
Row electricCurrentRow = Table.buildTable(electricCurrent, LABEL_LIST)
.getRow(0);
this.electricCurrentGenerator = RecordGenerator
.buildGeneratorWithRow(electricCurrentRow);
this.electricCurrentGenerator = RecordGenerator.buildGeneratorWithRow(
electricCurrentRow, sampleCountForSendOnce);
Row electricityRow = Table.buildTable(electricity, LABEL_LIST)
.getRow(0);
this.electricityGenerator = RecordGenerator
.buildGeneratorWithRow(electricityRow);
this.electricityGenerator = RecordGenerator.buildGeneratorWithRow(
electricityRow, sampleCountForSendOnce);
}
@Behavior("Send")
@ -78,7 +98,8 @@ public class HBasePlugin {
twoTupple.add(sendForOneUserOneDay(beginUserInt,
sumOfEletricity, dayIndex, userIndex));
}
dayIndex = new Date(dayIndex.getTime() + 24 * 60 * 60 * 1000);
dayIndex = new Date(dayIndex.getTime() + ONE_DAY_TO_MS
* this.sendRate);
}
return new ElectReturn(twoTupple.successCount, twoTupple.failCount);
}
@ -100,7 +121,7 @@ public class HBasePlugin {
Date sendTime = dayIndex;
resetGenerators();
for (int sampleIndex = 0; sampleIndex < 144; sampleIndex++) {
for (int sampleIndex = 0; sampleIndex < ONE_DAY_TO_MINUTE / 10; sampleIndex++) {
electricity = this.electricityGenerator.getDouble();
voltage = this.voltageGenerator.getDouble();
electricCurrent = this.electricCurrentGenerator.getDouble();
@ -113,7 +134,8 @@ public class HBasePlugin {
this.dateFormat.format(sendTime) + "_"
+ String.valueOf(userIndex));
result.add(ret);
sendTime = new Date(sendTime.getTime() + 10 * 60 * 1000);
sendTime = new Date(sendTime.getTime() + this.sampleRate
* ONE_MINUTE_TO_MS);
}
return result;
}

View File

@ -11,6 +11,7 @@ public abstract class RecordGenerator {
protected String dataType;
protected Double minValue;
protected Double maxValue;
protected int sampleCountForSendOnce;
Set<String> legalDataTypes = new LinkedHashSet<String>();
{
@ -26,7 +27,8 @@ public abstract class RecordGenerator {
legalGenerators.add("cos");
}
private RecordGenerator(String dataType, String minValue, String maxValue) {
private RecordGenerator(String dataType, String minValue, String maxValue,
int sampleCountForSendOnce) {
this.dataType = dataType.toLowerCase();
if (!this.legalDataTypes.contains(this.dataType)) {
throw new Bench4QRunTimeException(
@ -71,21 +73,23 @@ public abstract class RecordGenerator {
* @return
*/
private static RecordGenerator buildGenerator(String dataType,
String generator, String min, String max) {
String generator, String min, String max, int sampleCountForSendOnce) {
if (generator.equalsIgnoreCase("random")) {
return new RandomGenerator(dataType, min, max);
return new RandomGenerator(dataType, min, max,
sampleCountForSendOnce);
} else if (generator.equalsIgnoreCase("sin")) {
return new SinGenerator(dataType, min, max);
return new SinGenerator(dataType, min, max, sampleCountForSendOnce);
} else if (generator.equalsIgnoreCase("cos")) {
return new CosGenerator(dataType, min, max);
return new CosGenerator(dataType, min, max, sampleCountForSendOnce);
} else {
throw new Bench4QRunTimeException("illegal generator request");
}
}
public static RecordGenerator buildGeneratorWithRow(Row row) {
public static RecordGenerator buildGeneratorWithRow(Row row,
int sampleCountForSendOnce) {
RecordGenerator result = buildGenerator(row.getCell(0), row.getCell(1),
row.getCell(2), row.getCell(3));
row.getCell(2), row.getCell(3), sampleCountForSendOnce);
result.reset();
return result;
}
@ -94,8 +98,8 @@ public abstract class RecordGenerator {
private Random random;
private RandomGenerator(String dataType, String minValue,
String maxValue) {
super(dataType, minValue, maxValue);
String maxValue, int sampleCountForSendOnce) {
super(dataType, minValue, maxValue, sampleCountForSendOnce);
this.random = new Random();
}
@ -116,21 +120,22 @@ public abstract class RecordGenerator {
static abstract class TrigonometricGenerator extends RecordGenerator {
protected static Double Trigonometric_Cycle = 2 * Math.PI;
protected static int OneDay_SplitBy_TenMinute = 144;
protected static int OneDay_SplitBy_TenMinute = HBasePlugin.ONE_DAY_TO_MINUTE / 10;
protected static Double Accumulate_Once = Trigonometric_Cycle
/ OneDay_SplitBy_TenMinute;
private TrigonometricGenerator(String dataType, String minValue,
String maxValue) {
super(dataType, minValue, maxValue);
String maxValue, int sampleCountForSendOnce) {
super(dataType, minValue, maxValue, sampleCountForSendOnce);
}
}
static class SinGenerator extends TrigonometricGenerator {
private Double currentAngle = 0D;
private SinGenerator(String dataType, String minValue, String maxValue) {
super(dataType, minValue, maxValue);
private SinGenerator(String dataType, String minValue, String maxValue,
int sampleCountForSendOnce) {
super(dataType, minValue, maxValue, sampleCountForSendOnce);
}
@Override
@ -151,8 +156,9 @@ public abstract class RecordGenerator {
static class CosGenerator extends TrigonometricGenerator {
private Double currentAngle = 0D;
private CosGenerator(String dataType, String minValue, String maxValue) {
super(dataType, minValue, maxValue);
private CosGenerator(String dataType, String minValue, String maxValue,
int sampleCountForSendOnce) {
super(dataType, minValue, maxValue, sampleCountForSendOnce);
}
@Override

View File

@ -3,10 +3,10 @@
<ui>
<plugin name="HBase">
<params>
<param name="sampleRate">
<param name="sampleRate" label="The cycle of sampling, unit is minute">
<field size="5"></field>
</param>
<param name="sendRate">
<param name="sendRate" label="The cycle of sending, unit is day">
<field size="5"></field>
</param>
<param name="voltage">

View File

@ -82,17 +82,21 @@ public class Test_CsvProvider extends TestBase {
}
@Test
public void test_InitialRightly_By_VUser() {
public void test_InitialRightly_By_VUser() throws IOException {
final String fileName = "param_csv";
UUID testId = UUID.randomUUID();
createFileAndWriteContent(testId, fileName, CONTENT);
RunScenarioModel runScenarioModel = buildRunScenarioModel(fileName);
Scenario scenario = Scenario.scenarioBuilderWithCompile(runScenarioModel);
VUser vUser = Test_VUser.createVUser(scenario, UUID.randomUUID());
Scenario scenario = Scenario
.scenarioBuilderWithCompile(runScenarioModel);
VUser vUser = Test_VUser.createVUser(scenario, testId);
try {
HashMap<String, Object> plugins = new HashMap<String, Object>();
TestHelper.invokePrivate(vUser, "preparePlugins", new Class<?>[] {
Scenario.class, Map.class }, new Object[] { scenario,
plugins });
assertTrue(plugins.containsKey("csvProvider0"));
dropFiles(testId);
} catch (Exception e) {
e.printStackTrace();
fail();

View File

@ -17,7 +17,7 @@ public class Test_RecordGenerator {
Row inputRow = Table.buildTable(input, HBasePlugin.LABEL_LIST)
.getRow(0);
RecordGenerator resultGenerator = RecordGenerator
.buildGeneratorWithRow(inputRow);
.buildGeneratorWithRow(inputRow, 144);
assertTrue(resultGenerator instanceof RandomGenerator);
for (int i = 0; i < 100; i++) {
Double resultValue = resultGenerator.getDouble();
@ -32,7 +32,7 @@ public class Test_RecordGenerator {
String input = "dataType=Int|generator=hah|min=10|max=50|;";
Row inputRow = Table.buildTable(input, HBasePlugin.LABEL_LIST)
.getRow(0);
RecordGenerator.buildGeneratorWithRow(inputRow);
RecordGenerator.buildGeneratorWithRow(inputRow, 144);
}
@Test
@ -41,7 +41,7 @@ public class Test_RecordGenerator {
Row inputRow = Table.buildTable(input, HBasePlugin.LABEL_LIST)
.getRow(0);
try {
RecordGenerator.buildGeneratorWithRow(inputRow);
RecordGenerator.buildGeneratorWithRow(inputRow, 144);
fail("should not come here");
} catch (Exception e) {
assertTrue(e instanceof Bench4QRunTimeException);
@ -55,7 +55,7 @@ public class Test_RecordGenerator {
Row inputRow = Table.buildTable(input, HBasePlugin.LABEL_LIST)
.getRow(0);
try {
RecordGenerator.buildGeneratorWithRow(inputRow);
RecordGenerator.buildGeneratorWithRow(inputRow, 144);
fail("should not come here");
} catch (Exception e) {
assertTrue(e instanceof Bench4QRunTimeException);
@ -68,7 +68,7 @@ public class Test_RecordGenerator {
Row inputRow = Table.buildTable(input, HBasePlugin.LABEL_LIST)
.getRow(0);
RecordGenerator recordGenerator = RecordGenerator
.buildGeneratorWithRow(inputRow);
.buildGeneratorWithRow(inputRow, 144);
for (int i = 0; i < 144; i++) {
Double resultValue = recordGenerator.getDouble();
System.out.println(resultValue);
@ -84,7 +84,7 @@ public class Test_RecordGenerator {
Row inputRow = Table.buildTable(input, HBasePlugin.LABEL_LIST)
.getRow(0);
RecordGenerator recordGenerator = RecordGenerator
.buildGeneratorWithRow(inputRow);
.buildGeneratorWithRow(inputRow, 144);
for (int i = 0; i < 144; i++) {
Double resultValue = recordGenerator.getDouble();
System.out.println(resultValue);
@ -99,7 +99,7 @@ public class Test_RecordGenerator {
Row inputRow = Table.buildTable(input, HBasePlugin.LABEL_LIST)
.getRow(0);
RecordGenerator recordGenerator = RecordGenerator
.buildGeneratorWithRow(inputRow);
.buildGeneratorWithRow(inputRow, 144);
Double firstValue = recordGenerator.getDouble();
recordGenerator.reset();
Double secondValue = recordGenerator.getDouble();
@ -113,7 +113,7 @@ public class Test_RecordGenerator {
Row inputRow = Table.buildTable(input, HBasePlugin.LABEL_LIST)
.getRow(0);
RecordGenerator recordGenerator = RecordGenerator
.buildGeneratorWithRow(inputRow);
.buildGeneratorWithRow(inputRow, 144);
Double firstValue = recordGenerator.getDouble();
recordGenerator.reset();
Double secondValue = recordGenerator.getDouble();