通过不同准备数据进行测试
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@BenchmarkMode(Mode.AverageTime)
@State(Scope.Benchmark)
public class ForTests {
@Param({ "1000", "2000", "3000", "4000" })
public int size;
public final static List<GoodsInfo> GOODS_INFOS_1000 = new ArrayList<>();
public final static List<GoodsInfo> GOODS_INFOS_2000 = new ArrayList<>();
public final static List<GoodsInfo> GOODS_INFOS_3000 = new ArrayList<>();
public final static List<GoodsInfo> GOODS_INFOS_4000 = new ArrayList<>();
static {
for (int i = 0; i < 1000; i++) {
GoodsInfo goodsInfo = new GoodsInfo();
goodsInfo.setCatIds(Arrays.asList(1, 2, 3, 0));
GOODS_INFOS_1000.add(goodsInfo);
}
for (int i = 0; i < 2000; i++) {
GoodsInfo goodsInfo = new GoodsInfo();
goodsInfo.setCatIds(Arrays.asList(1, 2, 3, 0));
GOODS_INFOS_2000.add(goodsInfo);
}
for (int i = 0; i < 3000; i++) {
GoodsInfo goodsInfo = new GoodsInfo();
goodsInfo.setCatIds(Arrays.asList(1, 2, 3, 0));
GOODS_INFOS_3000.add(goodsInfo);
}
for (int i = 0; i < 4000; i++) {
GoodsInfo goodsInfo = new GoodsInfo();
goodsInfo.setCatIds(Arrays.asList(1, 2, 3, 0));
GOODS_INFOS_4000.add(goodsInfo);
}
}
public static void main(String[] args) throws RunnerException {
Options opt = new OptionsBuilder().include(ForTests.class.getSimpleName()).warmupIterations(1).measurementIterations(1).forks(1).build();
new Runner(opt).run();
}
@Benchmark
public void testOrigin() {
test(size, this::origin);
}
@Benchmark
public void testOptimize() {
test(size, this::optimize);
}
private void test(int size, Function<GoodsInfo, AlgoGoods> function) {
if (size == 1000) {
GOODS_INFOS_1000.forEach(goodsInfo -> {
function.apply(goodsInfo);
});
}
if (size == 2000) {
GOODS_INFOS_2000.forEach(goodsInfo -> {
function.apply(goodsInfo);
});
}
if (size == 3000) {
GOODS_INFOS_3000.forEach(goodsInfo -> {
function.apply(goodsInfo);
});
}
if (size == 4000) {
GOODS_INFOS_4000.forEach(goodsInfo -> {
function.apply(goodsInfo);
});
}
}
public AlgoGoods origin(GoodsInfo goodsInfo) {
AlgoGoods algoGoods = new AlgoGoods();
int catId = 0;
for (int i = goodsInfo.getCatIds().size() - 1; i >= 0; --i) {
if (goodsInfo.getCatIds().get(i) > 0) {
catId = goodsInfo.getCatIds().get(i);
break;
}
}
algoGoods.setCatId(catId);
algoGoods.setCatId1(goodsInfo.getCatIds().get(0));
algoGoods.setCatId2(goodsInfo.getCatIds().get(1));
algoGoods.setCatId3(goodsInfo.getCatIds().get(2));
algoGoods.setCatId4(goodsInfo.getCatIds().get(3));
for (int i = 0; i < goodsInfo.getCatIds().size(); i++) {
algoGoods.getCatIds().add(i, goodsInfo.getCatIds().get(i));
}
return algoGoods;
}
private AlgoGoods optimize(GoodsInfo goodsInfo) {
AlgoGoods algoGoods = new AlgoGoods();
int catId = 0;
int catIdSize = goodsInfo.getCatIds().size();
for (int i = 0; i < catIdSize; i++) {
algoGoods.getCatIds().add(i, goodsInfo.getCatIds().get(i));
if (goodsInfo.getCatIds().get(i) > 0) {
catId = goodsInfo.getCatIds().get(i);
}
}
algoGoods.setCatId(catId);
algoGoods.setCatId1(goodsInfo.getCatIds().get(0));
algoGoods.setCatId2(goodsInfo.getCatIds().get(1));
algoGoods.setCatId3(goodsInfo.getCatIds().get(2));
algoGoods.setCatId4(goodsInfo.getCatIds().get(3));
return algoGoods;
}
@Data
private static class GoodsInfo {
List<Integer> catIds;
}
@Data
private static class AlgoGoods {
private int catId;
private int catId1;
private int catId2;
private int catId3;
private int catId4;
List<Integer> catIds = new ArrayList<>();
}
}