From 110780d7fdbfa52e35e997b0b8518a7b128c65bd Mon Sep 17 00:00:00 2001 From: coderfengyun Date: Tue, 15 Apr 2014 21:24:55 +0800 Subject: [PATCH] refactoring and need to test refactoring and need to test --- .../impl/Para_IteratorNumber.java | 39 --- .../parameterization/impl/Para_List.java | 57 ---- .../impl/Para_RandomNumber.java | 40 --- .../parameterization/impl/Para_Table.java | 257 ------------------ .../bench4q/agent/plugin/basic/http/ui.xml | 88 ++++++ .../plugin/basic/iterator/IteratorNumber.java | 38 +++ .../agent/plugin/basic/iterator/ui.xml | 17 ++ .../agent/plugin/basic/random/Random.java | 40 +++ .../bench4q/agent/plugin/basic/random/ui.xml | 20 ++ 9 files changed, 203 insertions(+), 393 deletions(-) delete mode 100644 Bench4Q-Agent/src/main/java/org/bench4q/agent/parameterization/impl/Para_IteratorNumber.java delete mode 100644 Bench4Q-Agent/src/main/java/org/bench4q/agent/parameterization/impl/Para_List.java delete mode 100644 Bench4Q-Agent/src/main/java/org/bench4q/agent/parameterization/impl/Para_RandomNumber.java delete mode 100644 Bench4Q-Agent/src/main/java/org/bench4q/agent/parameterization/impl/Para_Table.java create mode 100644 Bench4Q-Agent/src/main/java/org/bench4q/agent/plugin/basic/http/ui.xml create mode 100644 Bench4Q-Agent/src/main/java/org/bench4q/agent/plugin/basic/iterator/IteratorNumber.java create mode 100644 Bench4Q-Agent/src/main/java/org/bench4q/agent/plugin/basic/iterator/ui.xml create mode 100644 Bench4Q-Agent/src/main/java/org/bench4q/agent/plugin/basic/random/Random.java create mode 100644 Bench4Q-Agent/src/main/java/org/bench4q/agent/plugin/basic/random/ui.xml diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/parameterization/impl/Para_IteratorNumber.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/parameterization/impl/Para_IteratorNumber.java deleted file mode 100644 index 7bca58f5..00000000 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/parameterization/impl/Para_IteratorNumber.java +++ /dev/null @@ -1,39 +0,0 @@ -package org.bench4q.agent.parameterization.impl; - -import java.util.Map; -import java.util.UUID; - -import org.bench4q.agent.parameterization.ParameterizedClass; -import org.bench4q.agent.parameterization.PickOrder; -import org.bench4q.agent.parameterization.UpdateStrategy; -import org.bench4q.agent.plugin.Constructor; - -@ParameterizedClass(name = "Para_IteratorNumber", permittedPickOrders = { PickOrder.SEQUENTIAL }, permittedUpdateStrategies = { UpdateStrategy.EACH_ITERATION }) -public class Para_IteratorNumber { - public Long iteratorNum = new Long(0); - - @Constructor - public Para_IteratorNumber() { - } - - /** - * For all methods, they will has these two params - * - * @param id - * @param a - * @return - */ - public String getIteratorNumber(UUID id, Map a) { - long result = 0; - synchronized (iteratorNum) { - iteratorNum++; - result = iteratorNum; - } - String ret = String.valueOf(result); - return ret; - } - - public void unreg(UUID id) { - - } -} diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/parameterization/impl/Para_List.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/parameterization/impl/Para_List.java deleted file mode 100644 index be1d10f2..00000000 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/parameterization/impl/Para_List.java +++ /dev/null @@ -1,57 +0,0 @@ -package org.bench4q.agent.parameterization.impl; - -import java.io.File; -import java.util.List; -import java.util.UUID; - -import org.apache.commons.io.FileUtils; -import org.bench4q.agent.parameterization.ParameterizedClass; -import org.bench4q.agent.parameterization.PickOrder; -import org.bench4q.agent.parameterization.UpdateStrategy; -import org.bench4q.agent.plugin.Constructor; -import org.bench4q.agent.utils.ParameterParser; - -@ParameterizedClass(name = "Para_List", permittedPickOrders = { - PickOrder.RANDOM, PickOrder.SEQUENTIAL, PickOrder.UNIQUE }, permittedUpdateStrategies = { - UpdateStrategy.EACH_ITERATION, UpdateStrategy.EACH_OCCURRENCE, - UpdateStrategy.ONCE }) -public class Para_List extends BasePara { - private String currentValue; - - public String getCurrentValue() { - if (this.getUpdateStrategy() == UpdateStrategy.EACH_OCCURRENCE) { - initialValue(); - } - return currentValue; - } - - private void setCurrentValue(String currentValue) { - this.currentValue = currentValue; - } - - @Constructor - public Para_List(UUID testId, PickOrder pickOrder, - UpdateStrategy updateStrategy, String paramName, int currentIndex) { - super(testId, paramName, updateStrategy, pickOrder, currentIndex); - initialValue(); - } - - private void initialValue() { - File paramFile = new File(getParamFileFullPath(this.getParamName())); - List nField = null; - try { - String content = FileUtils.readFileToString(paramFile); - nField = ParameterParser.buildNField(content); - } catch (Exception e) { - e.printStackTrace(); - } - - if (nField.size() == 0) { - throw new NullPointerException( - "The parameter file has no valid content"); - } - this.setCurrentValue(nField.get(getNextIndex(nField.size()) - % nField.size())); - } - -} diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/parameterization/impl/Para_RandomNumber.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/parameterization/impl/Para_RandomNumber.java deleted file mode 100644 index c2c3832b..00000000 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/parameterization/impl/Para_RandomNumber.java +++ /dev/null @@ -1,40 +0,0 @@ -package org.bench4q.agent.parameterization.impl; - -import java.util.Random; -import java.util.UUID; - -import org.bench4q.agent.parameterization.ParameterizedClass; -import org.bench4q.agent.parameterization.PickOrder; -import org.bench4q.agent.parameterization.UpdateStrategy; -import org.bench4q.agent.plugin.Constructor; - -@ParameterizedClass(name = "Para_RandomNumber", permittedPickOrders = { PickOrder.RANDOM }, permittedUpdateStrategies = { - UpdateStrategy.EACH_ITERATION, UpdateStrategy.EACH_OCCURRENCE, - UpdateStrategy.ONCE }) -public class Para_RandomNumber { - - @Constructor - public Para_RandomNumber() { - } - - public String getRandomIntegerNumber(UUID id, String begin, String end, - String stringformat) { - - Random r = new Random(); - int beginNum = Integer.parseInt(begin); - int endNum = Integer.parseInt(end); - - int result = r.nextInt(endNum - beginNum) + beginNum; - return String.format(stringformat, result); - } - - public String getRandomDouble(UUID id, String begin, String end, - String stringformat) { - Random r = new Random(); - double beginNum = Integer.parseInt(begin); - double endNum = Integer.parseInt(end); - - double result = r.nextDouble() * (endNum - beginNum) + beginNum; - return String.format(stringformat, result); - } -} diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/parameterization/impl/Para_Table.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/parameterization/impl/Para_Table.java deleted file mode 100644 index 79b420e0..00000000 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/parameterization/impl/Para_Table.java +++ /dev/null @@ -1,257 +0,0 @@ -package org.bench4q.agent.parameterization.impl; - -import java.io.BufferedReader; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStreamReader; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Random; -import java.util.UUID; -import java.util.concurrent.ArrayBlockingQueue; -import java.util.concurrent.ConcurrentHashMap; - -import org.bench4q.agent.parameterization.ParameterizedClass; -import org.bench4q.agent.parameterization.PickOrder; -import org.bench4q.agent.parameterization.UpdateStrategy; -import org.bench4q.agent.plugin.Constructor; - -@ParameterizedClass(name = "Para_Table", permittedPickOrders = { - PickOrder.RANDOM, PickOrder.SEQUENTIAL, PickOrder.UNIQUE }, permittedUpdateStrategies = { - UpdateStrategy.EACH_ITERATION, UpdateStrategy.EACH_OCCURRENCE }) -public class Para_Table extends BasePara { - public final int cacheCap = 5000;// 1 sequence 2 random - public final int cacheSize = 1000;// 1 sequence 2 random - public ConcurrentHashMap readerMap = new ConcurrentHashMap(); - - @Constructor - public Para_Table(UUID testId, PickOrder pickOrder, - UpdateStrategy updateStrategy, String parameterName, int lastIndex) { - super(testId, parameterName, updateStrategy, pickOrder, lastIndex); - } - - public abstract class Reader { - public final Table table; - - public Reader(Table table) { - this.table = table; - } - - public abstract TableRow nextRow(); - - } - - public class RandomReader extends Reader { - public ArrayBlockingQueue queue; - - public RandomReader(ArrayBlockingQueue queue, Table t) { - super(t); - this.queue = queue; - } - - @Override - public TableRow nextRow() { - TableRow result = null; - do { - if (queue.size() == 0) { - synchronized (queue) { - if (queue.size() == 0) { - List rows = table.readRows(); - int n = rows.size(); - Random r = new Random(); - for (int i = 0; i < n; i++) { - int next = r.nextInt(n); - TableRow tempRow = rows.get(i); - rows.set(i, rows.get(next)); - rows.set(next, tempRow); - } - queue.addAll(rows); - } - } - } - result = queue.poll(); - } while (result == null); - return result; - } - - } - - public class SequenceReader extends Reader { - public ArrayBlockingQueue queue; - - public SequenceReader(ArrayBlockingQueue queue, Table t) { - super(t); - this.queue = queue; - } - - @Override - public TableRow nextRow() { - TableRow result = null; - do { - if (queue.size() == 0) { - synchronized (queue) { - if (queue.size() == 0) { - List rows = table.readRows(); - queue.addAll(rows); - } - } - } - result = queue.poll(); - } while (result == null); - return result; - } - - } - - public abstract class Table { - String sourceValue; - int firstRow; - - public Table(String sourceValue, int firstRow) { - this.sourceValue = sourceValue; - } - - public abstract List readRows(); - - public List rows = new ArrayList(); - - public int getTime = 0; - - public boolean add(TableRow r) { - return rows.add(r); - } - } - - public class FileTable extends Table { - private BufferedReader bfr; - char splitChar; - char lineChar; - - public FileTable(String paramFilePath, int firstRow, char splitChar, - char lineChar) { - super(paramFilePath, firstRow); - this.splitChar = splitChar; - this.lineChar = lineChar; - try { - createBFR(); - } catch (IOException e) { - e.printStackTrace(); - } - } - - private void createBFR() throws IOException { - FileInputStream fis = new FileInputStream(sourceValue); - InputStreamReader ir = new InputStreamReader(fis); - bfr = new BufferedReader(ir); - for (int i = 0; i < firstRow;) { - char readChar = (char) bfr.read(); - if (readChar == lineChar) - i++; - } - - } - - @Override - public List readRows() { - StringBuilder target = new StringBuilder(); - List result = new ArrayList(); - TableRow tempTableRow = new TableRow(); - try { - for (int i = 0; i < cacheSize;) { - int tt = bfr.read(); - char readBuff = (char) tt; - if (tt == -1) { - createBFR(); - break; - } else if (readBuff == this.splitChar) { - String cellString = target.toString(); - tempTableRow.AddCell(cellString); - target = new StringBuilder(); - continue; - } else if (readBuff == this.lineChar) { - tempTableRow.AddCell(target.toString()); - result.add(tempTableRow); - tempTableRow = new TableRow(); - target = new StringBuilder(); - i++; - continue; - } - target.append(readBuff); - } - } catch (IOException ioex) { - return result; - } - if (target.length() > 0) { - tempTableRow.AddCell(target.toString()); - result.add(tempTableRow); - target = new StringBuilder(); - } - return result; - } - - } - - public class TableRow { - public List cells = new ArrayList(); - - public void AddCell(String s) { - cells.add(s); - } - } - - // org.bench4q.agent.parameterization.impl.Para_Table.getTableColumnValue(java.util.UUID, - // java.util.HashMap, java.lang.String, java.lang.String, java.lang.String, - // java.lang.String, java.lang.String, java.lang.String, java.lang.String) - - public String getTableColumnValue(UUID id, - HashMap objCache, String paramName, - String firstRow, String nextRow, String columnSplitor, - String rowSplitor, String column) { - int fRow = Integer.parseInt(firstRow); - char sChar = columnSplitor.charAt(0); - char lChar = rowSplitor.charAt(0); - int col = Integer.parseInt(column); - TableRow resultRow = null; - Table table = null; - Reader reader = null; - if (objCache.containsKey(paramName) == true) { - resultRow = (TableRow) objCache.get(paramName); - return resultRow.cells.get(col); - } - - if (this.readerMap.containsKey(paramName)) { - reader = readerMap.get(paramName); - } else { - table = new FileTable(getParamFileFullPath(paramName), fRow, sChar, - lChar); - if (nextRow.equals("random")) { - ArrayBlockingQueue queue = new ArrayBlockingQueue( - cacheCap); - reader = new RandomReader(queue, table); - } else if (nextRow.equals("sequence")) { - ArrayBlockingQueue queue = new ArrayBlockingQueue( - cacheCap); - reader = new SequenceReader(queue, table); - } - readerMap.put(paramName, reader); - } - resultRow = reader.nextRow(); - objCache.put(paramName, resultRow); - return resultRow.cells.get(col); - } - - public void unreg(UUID id) { - - } - - @Override - public String getCurrentValue() { - return null; - } - - @Override - protected int getNextIndex(int size) { - return 0; - } -} diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/plugin/basic/http/ui.xml b/Bench4Q-Agent/src/main/java/org/bench4q/agent/plugin/basic/http/ui.xml new file mode 100644 index 00000000..cd50dd8e --- /dev/null +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/plugin/basic/http/ui.xml @@ -0,0 +1,88 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + \ No newline at end of file diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/plugin/basic/iterator/IteratorNumber.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/plugin/basic/iterator/IteratorNumber.java new file mode 100644 index 00000000..cca9d301 --- /dev/null +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/plugin/basic/iterator/IteratorNumber.java @@ -0,0 +1,38 @@ +package org.bench4q.agent.plugin.basic.iterator; + +import org.bench4q.agent.plugin.BaseParameterization; +import org.bench4q.agent.plugin.Behavior; +import org.bench4q.agent.plugin.Constructor; +import org.bench4q.agent.plugin.Parameter; +import org.bench4q.agent.plugin.Plugin; +import org.bench4q.agent.utils.Type.SupportTypes; + +@Plugin(value = "IteratorNumber") +public class IteratorNumber extends BaseParameterization { + private Long iteratorNum; + private String format; + + @Constructor + public IteratorNumber( + @Parameter(type = SupportTypes.Field, value = "testId") String testId, + @Parameter(type = SupportTypes.Field, value = "begin") String begin, + @Parameter(type = SupportTypes.Field, value = "format") String format) { + super(testId); + this.iteratorNum = Long.parseLong(begin); + this.format = format; + } + + @Behavior(value = "next") + public void next() { + synchronized (iteratorNum) { + iteratorNum++; + } + } + + @Override + public String getValue(String var) { + return (this.format == null || this.format.isEmpty()) ? String + .valueOf(this.iteratorNum) : String.format(this.format, + this.iteratorNum); + } +} diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/plugin/basic/iterator/ui.xml b/Bench4Q-Agent/src/main/java/org/bench4q/agent/plugin/basic/iterator/ui.xml new file mode 100644 index 00000000..494c891f --- /dev/null +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/plugin/basic/iterator/ui.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/plugin/basic/random/Random.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/plugin/basic/random/Random.java new file mode 100644 index 00000000..38c958a0 --- /dev/null +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/plugin/basic/random/Random.java @@ -0,0 +1,40 @@ +package org.bench4q.agent.plugin.basic.random; + +import org.bench4q.agent.plugin.BaseParameterization; +import org.bench4q.agent.plugin.Behavior; +import org.bench4q.agent.plugin.Constructor; +import org.bench4q.agent.plugin.Parameter; +import org.bench4q.agent.plugin.Plugin; +import org.bench4q.agent.utils.Type.SupportTypes; + +@Plugin(value = "Random") +public class Random extends BaseParameterization { + private String format; + private int begin; + private int end; + private String currentValue; + + @Constructor + public Random( + @Parameter(type = SupportTypes.Field, value = "testId") String testId, + @Parameter(type = SupportTypes.Field, value = "begin") String begin, + @Parameter(type = SupportTypes.Field, value = "end") String end, + @Parameter(type = SupportTypes.Field, value = "format") String format) { + super(testId); + this.format = format; + this.begin = Integer.parseInt(begin); + this.end = Integer.parseInt(end); + } + + @Behavior(value = "next") + public void next() { + java.util.Random r = new java.util.Random(); + int result = r.nextInt(this.end - this.begin) + this.begin; + this.currentValue = String.valueOf(result); + } + + public String getValue(String var) { + return (this.format == null || this.format.isEmpty()) ? this.currentValue + : String.format(this.format, this.currentValue); + } +} diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/plugin/basic/random/ui.xml b/Bench4Q-Agent/src/main/java/org/bench4q/agent/plugin/basic/random/ui.xml new file mode 100644 index 00000000..c044bc72 --- /dev/null +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/plugin/basic/random/ui.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file