refactor
This commit is contained in:
coderfengyun 2014-05-16 10:25:06 +08:00
parent b4a3c9f18b
commit ab06059674
9 changed files with 126 additions and 91 deletions

View File

@ -1,4 +1,4 @@
package org.bench4q.agent.plugin.basic.hbase;
package org.bench4q.agent.plugin.basic.electric;
import org.bench4q.agent.plugin.basic.PluginReturn;

View File

@ -1,4 +1,4 @@
package org.bench4q.agent.plugin.basic.hbase;
package org.bench4q.agent.plugin.basic.electric;
import java.text.ParseException;
import java.text.SimpleDateFormat;
@ -7,10 +7,12 @@ import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.bench4q.agent.plugin.Constructor;
import org.bench4q.agent.plugin.Parameter;
import org.bench4q.agent.plugin.Plugin;
import org.bench4q.agent.plugin.basic.http.HttpPlugin;
import org.bench4q.agent.plugin.behavior.Behavior;
import org.bench4q.agent.plugin.behavior.BehaviorType;
import org.bench4q.agent.utils.Type.SupportTypes;
@ -18,11 +20,11 @@ import org.bench4q.agent.utils.types.Table;
import org.bench4q.agent.utils.types.Table.Row;
@Plugin("Hbase")
public class HBasePlugin {
public class ElectricPlugin {
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;
private HttpPlugin httpPlugin = new HttpPlugin(UUID.randomUUID().toString());
public static List<String> LABEL_LIST;
static {
LABEL_LIST = new ArrayList<String>();
@ -45,10 +47,6 @@ public class HBasePlugin {
DATAGENERATOR_PROTOCAL_LIST.add("'_' split");
DATAGENERATOR_PROTOCAL_LIST.add("'+' split");
}
private RecordGenerator voltageGenerator;
private RecordGenerator electricCurrentGenerator;
private RecordGenerator electricityGenerator;
private HBaseMesseger hBaseMesseger = new HBaseMesseger();
private SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyyMMddhhmmssS");
@ -69,7 +67,7 @@ public class HBasePlugin {
* @param electricity
*/
@Constructor
public HBasePlugin(
public ElectricPlugin(
@Parameter(value = "sampleRate", type = SupportTypes.Field) String sampleRate,
@Parameter(value = "sendRate", type = SupportTypes.Field) String sendRate,
@Parameter(value = "generatorParams", type = SupportTypes.Table) String generatorParams,
@ -85,13 +83,8 @@ public class HBasePlugin {
for (Row row : generatorTable.getRows()) {
RecordGenerator generator = RecordGenerator.buildGeneratorWithRow(
row, sampleCountForSendOnce);
// this.generators.add(generator);
this.generatorMap.put(row.getCell(0), generator);
}
this.voltageGenerator = this.generatorMap.get("voltage");
this.electricCurrentGenerator = this.generatorMap
.get("electricCurrent");
this.electricityGenerator = this.generatorMap.get("electricity");
}
@Behavior(value = "Send", type = BehaviorType.USER_BEHAVIOR)
@ -138,30 +131,46 @@ public class HBasePlugin {
private TwoTupple sendForOneUserOneDay(int beginUserInt,
double[] sumOfEletricity, Date dayIndex, int userIndex) {
TwoTupple result = new TwoTupple(0, 0);
double electricity = 0, voltage = 0, electricCurrent = 0;
double electricity = 0;
int indexInSum = userIndex - beginUserInt;
Date sendTime = dayIndex;
resetGenerators();
for (int sampleIndex = 0; sampleIndex < ONE_DAY_TO_MINUTE / 10; sampleIndex++) {
electricity = this.electricityGenerator.getDouble();
voltage = this.voltageGenerator.getDouble();
electricCurrent = this.electricCurrentGenerator.getDouble();
electricity = this.generatorMap.get("electricity").getDouble();
Map<String, String> properties = new LinkedHashMap<String, String>();
properties.put("DevId", userIndex + "");
properties.put("Usage", this.generatorMap.get("electricity")
.getDouble() + "");
properties.put("U", this.generatorMap.get("voltage").getDouble()
+ "");
properties.put("I", this.generatorMap.get("electricCurrent")
.getDouble() + "");
properties.put("Time", this.dateFormat.format(sendTime));
sumOfEletricity[indexInSum] += electricity;
properties.put("Sum", sumOfEletricity[indexInSum] + "");
boolean ret = this.hBaseMesseger.send(
sumOfEletricity[indexInSum],
electricity,
voltage,
electricCurrent,
properties,
this.dateFormat.format(sendTime) + "_"
+ String.valueOf(userIndex));
result.add(ret);
// result.add(this.httpPlugin
// .post("http://133.133.133.99:8080/getMeterLog",
// "eleData=DevId:10001_Time:2014051514561_U:220_I:210_Usage:65.3_Sum:11.1;",
// null, null, null, null).getStatusCode() == 200);
sendTime = new Date(sendTime.getTime() + this.sampleRate
* ONE_MINUTE_TO_MS);
}
return result;
}
// private String buildForAdd(Map<String, String> properties) {
// StringBuilder stringBuilder = new StringBuilder();
//
// }
private void resetGenerators() {
for (RecordGenerator generator : this.generatorMap.values()) {
generator.reset();

View File

@ -0,0 +1,57 @@
package org.bench4q.agent.plugin.basic.electric;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
public class HBaseMesseger {
public String userId = null;
private Configuration conf = HBaseConfiguration.create();
private HTable table;
public HBaseMesseger() {
try {
@SuppressWarnings({ "unused", "resource" })
HBaseAdmin admin = new HBaseAdmin(conf);
this.table = new HTable(conf, "MeterLogData");
} catch (Exception e) {
e.printStackTrace();
}
}
public boolean send(double sumOfEletricity, Map<String, String> properties,
String time_userId) {
try {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("usage :" + sumOfEletricity + "_");
for (Entry<String, String> entry : properties.entrySet()) {
stringBuilder.append(entry.getKey() + " :" + entry.getValue()
+ "_");
}
stringBuilder.append("timeUserId :");
stringBuilder.append(time_userId);
System.out.println(stringBuilder.toString());
Put put = new Put(time_userId.getBytes());
put.add("AI".getBytes(), "Usage".getBytes(),
String.valueOf(sumOfEletricity).getBytes());
put.add("EU".getBytes(), "Sum".getBytes(),
String.valueOf(properties.get("electricity")).getBytes());
put.add("SI".getBytes(), "U".getBytes(),
String.valueOf(properties.get("voltage")).getBytes());
put.add("SI".getBytes(), "I".getBytes(),
String.valueOf(properties.get("electricCurrent"))
.getBytes());
this.table.put(put);
return true;
} catch (Exception ex) {
System.out.println(ex.getMessage());
return false;
}
}
}

View File

@ -1,4 +1,4 @@
package org.bench4q.agent.plugin.basic.hbase;
package org.bench4q.agent.plugin.basic.electric;
import java.util.LinkedHashSet;
import java.util.Random;
@ -120,7 +120,7 @@ public abstract class RecordGenerator {
static abstract class TrigonometricGenerator extends RecordGenerator {
protected static Double Trigonometric_Cycle = 2 * Math.PI;
protected static int OneDay_SplitBy_TenMinute = HBasePlugin.ONE_DAY_TO_MINUTE / 10;
protected static int OneDay_SplitBy_TenMinute = ElectricPlugin.ONE_DAY_TO_MINUTE / 10;
protected static Double Accumulate_Once = Trigonometric_Cycle
/ OneDay_SplitBy_TenMinute;

View File

@ -1,45 +0,0 @@
package org.bench4q.agent.plugin.basic.hbase;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
public class HBaseMesseger {
public String userId = null;
private Configuration conf = HBaseConfiguration.create();
public HBaseMesseger() {
try {
@SuppressWarnings({ "unused", "resource" })
HBaseAdmin admin = new HBaseAdmin(conf);
} catch (Exception e) {
e.printStackTrace();
}
}
public boolean send(double sum, double usage, double U, double I,
String time_userId) {
try {
System.out.println("sum :" + sum + "_usage :" + usage + "_U :" + U
+ "_I :" + I + "_time_userId" + time_userId);
@SuppressWarnings("resource")
HTable table = new HTable(conf, "MeterLogData");
Put put = new Put(time_userId.getBytes());
put.add("AI".getBytes(), "Usage".getBytes(), String.valueOf(usage)
.getBytes());
put.add("EU".getBytes(), "Sum".getBytes(), String.valueOf(sum)
.getBytes());
put.add("SI".getBytes(), "U".getBytes(), String.valueOf(U)
.getBytes());
put.add("SI".getBytes(), "I".getBytes(), String.valueOf(I)
.getBytes());
table.put(put);
} catch (Exception ex) {
System.out.println(ex.getMessage());
return false;
}
return true;
}
}

View File

@ -1,5 +1,6 @@
package org.bench4q.agent.test.plugin;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
@ -114,4 +115,17 @@ public class Test_HttpPlugin {
assertEquals("e55de793fc934001b156e97f94ff3eb6", this.getHttpPlugin()
.getValue("ticket"));
}
@Test
public void testForElectric() {
Map<String, String> queryParams = new HashMap<String, String>();
queryParams
.put("eleData",
"DevId:10001_Time:2014051514561_U:220_I:210_Usage:65.3_Sum:11.1");
HttpReturn result = this.httpPlugin
.post("http://133.133.133.99:8080/getMeterLog",
"eleData=DevId:10001_Time:2014051514561_U:220_I:210_Usage:65.3_Sum:11.1;",
null, null, null, null);
assertEquals(200, result.getStatusCode());
}
}

View File

@ -11,8 +11,8 @@ import org.junit.Test;
import static org.junit.Assert.*;
import org.apache.commons.io.FileUtils;
import org.bench4q.agent.plugin.basic.hbase.ElectReturn;
import org.bench4q.agent.plugin.basic.hbase.HBasePlugin;
import org.bench4q.agent.plugin.basic.electric.ElectReturn;
import org.bench4q.agent.plugin.basic.electric.ElectricPlugin;
import org.bench4q.share.helper.MarshalHelper;
import org.bench4q.share.helper.TestHelper;
import org.bench4q.share.models.agent.ParameterModel;
@ -142,7 +142,7 @@ public class Test_HBasePlugin {
@Test
public void testSend() {
HBasePlugin plugin = new HBasePlugin(
ElectricPlugin plugin = new ElectricPlugin(
"10",
"1",
"paramName=voltage|dataType=Int|generator=Random|min=10|max=50|;"
@ -150,7 +150,7 @@ public class Test_HBasePlugin {
+ "paramName=electricity|dataType=long|generator=Cos|min=1056|max=1068|;",
"http", "'_' split");
ElectReturn result = plugin.send("201403270000000", "201403280000000",
"1", "4");
"5", "8");
System.out.println("The End : ****************************");
System.out.println(result.getSuccessCount());
assertEquals(144 * 4, result.getSuccessCount());

View File

@ -2,9 +2,9 @@ package org.bench4q.agent.test.plugin.hbase;
import static org.junit.Assert.*;
import org.bench4q.agent.plugin.basic.hbase.HBasePlugin;
import org.bench4q.agent.plugin.basic.hbase.RecordGenerator;
import org.bench4q.agent.plugin.basic.hbase.RecordGenerator.RandomGenerator;
import org.bench4q.agent.plugin.basic.electric.ElectricPlugin;
import org.bench4q.agent.plugin.basic.electric.RecordGenerator;
import org.bench4q.agent.plugin.basic.electric.RecordGenerator.RandomGenerator;
import org.bench4q.agent.utils.types.Table;
import org.bench4q.agent.utils.types.Table.Row;
import org.bench4q.share.exception.Bench4QRunTimeException;
@ -13,8 +13,8 @@ import org.junit.Test;
public class Test_RecordGenerator {
@Test
public void test_buildGeneratorWithRandomRightlyInput() {
String input = "dataType=Int|generator=Random|min=10|max=50|;";
Row inputRow = Table.buildTable(input, HBasePlugin.LABEL_LIST)
String input = "paramName=voltage|dataType=Int|generator=Random|min=10|max=50|;";
Row inputRow = Table.buildTable(input, ElectricPlugin.LABEL_LIST)
.getRow(0);
RecordGenerator resultGenerator = RecordGenerator
.buildGeneratorWithRow(inputRow, 144);
@ -29,16 +29,16 @@ public class Test_RecordGenerator {
@Test(expected = Bench4QRunTimeException.class)
public void test_buildGeneratorWithIllegalGenerator() {
String input = "dataType=Int|generator=hah|min=10|max=50|;";
Row inputRow = Table.buildTable(input, HBasePlugin.LABEL_LIST)
String input = "paramName=voltage|dataType=Int|generator=hah|min=10|max=50|;";
Row inputRow = Table.buildTable(input, ElectricPlugin.LABEL_LIST)
.getRow(0);
RecordGenerator.buildGeneratorWithRow(inputRow, 144);
}
@Test
public void test_buildGeneratorWithRandomWithMinGTmax() {
String input = "dataType=Int|generator=Random|min=50|max=10|;";
Row inputRow = Table.buildTable(input, HBasePlugin.LABEL_LIST)
String input = "paramName=voltage|dataType=Int|generator=Random|min=50|max=10|;";
Row inputRow = Table.buildTable(input, ElectricPlugin.LABEL_LIST)
.getRow(0);
try {
RecordGenerator.buildGeneratorWithRow(inputRow, 144);
@ -51,8 +51,8 @@ public class Test_RecordGenerator {
@Test
public void test_buildGeneratorWithRandomWithIllegalDataType() {
String input = "dataType=String|generator=Random|min=10|max=50|;";
Row inputRow = Table.buildTable(input, HBasePlugin.LABEL_LIST)
String input = "paramName=voltage|dataType=String|generator=Random|min=10|max=50|;";
Row inputRow = Table.buildTable(input, ElectricPlugin.LABEL_LIST)
.getRow(0);
try {
RecordGenerator.buildGeneratorWithRow(inputRow, 144);
@ -64,8 +64,8 @@ public class Test_RecordGenerator {
@Test
public void test_getValueOfSin() {
String input = "dataType=double|generator=Sin|min=10|max=50|;";
Row inputRow = Table.buildTable(input, HBasePlugin.LABEL_LIST)
String input = "paramName=voltage|dataType=double|generator=Sin|min=10|max=50|;";
Row inputRow = Table.buildTable(input, ElectricPlugin.LABEL_LIST)
.getRow(0);
RecordGenerator recordGenerator = RecordGenerator
.buildGeneratorWithRow(inputRow, 144);
@ -80,8 +80,8 @@ public class Test_RecordGenerator {
@Test
public void test_getCosValue() {
String input = "dataType=double|generator=Cos|min=10|max=50|;";
Row inputRow = Table.buildTable(input, HBasePlugin.LABEL_LIST)
String input = "paramName=voltage|dataType=double|generator=Cos|min=10|max=50|;";
Row inputRow = Table.buildTable(input, ElectricPlugin.LABEL_LIST)
.getRow(0);
RecordGenerator recordGenerator = RecordGenerator
.buildGeneratorWithRow(inputRow, 144);
@ -95,8 +95,8 @@ public class Test_RecordGenerator {
@Test
public void test_cosReset() {
String input = "dataType=double|generator=Cos|min=10|max=50|;";
Row inputRow = Table.buildTable(input, HBasePlugin.LABEL_LIST)
String input = "paramName=voltage|dataType=double|generator=Cos|min=10|max=50|;";
Row inputRow = Table.buildTable(input, ElectricPlugin.LABEL_LIST)
.getRow(0);
RecordGenerator recordGenerator = RecordGenerator
.buildGeneratorWithRow(inputRow, 144);
@ -109,8 +109,8 @@ public class Test_RecordGenerator {
@Test
public void test_sinReset() {
String input = "dataType=double|generator=sin|min=10|max=50|;";
Row inputRow = Table.buildTable(input, HBasePlugin.LABEL_LIST)
String input = "paramName=voltage|dataType=double|generator=sin|min=10|max=50|;";
Row inputRow = Table.buildTable(input, ElectricPlugin.LABEL_LIST)
.getRow(0);
RecordGenerator recordGenerator = RecordGenerator
.buildGeneratorWithRow(inputRow, 144);