目录
1、Eclispe安装插件
在eclipse中安装m2e-apt插件
2、Maven增加依赖
在pom.xml中增加依赖项
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<version>1.20</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>1.20</version>
</dependency>
3、JMH测试代码
package com.autocoding.hutool;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeUtil;
@BenchmarkMode(value = { Mode.All })
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@State(Scope.Thread)
public class TreeUtilTest {
public static void main(String[] args) throws Exception {
String name = TreeUtilTest.class.getName();
Options options = new OptionsBuilder().include(name).forks(1).measurementIterations(10).warmupIterations(3)
.build();
new Runner(options).run();
}
@Benchmark
public void treeTest() {
// 构建node列表
List<TreeNode<String>> nodeList = CollUtil.newArrayList();
nodeList.add(new TreeNode<>("1", "0", "系统管理", 5));
nodeList.add(new TreeNode<>("11", "1", "用户管理", 222222));
nodeList.add(new TreeNode<>("111", "11", "用户添加", 0));
nodeList.add(new TreeNode<>("2", "0", "店铺管理", 1));
nodeList.add(new TreeNode<>("21", "2", "商品管理", 44));
nodeList.add(new TreeNode<>("221", "2", "商品管理2", 2));
List<Tree<String>> treeList = TreeUtil.build(nodeList, "0");
System.err.println("生成一棵树");
}
}
生成的树嵌套结构为:
[
{
"weight": 1,
"parentId": "0",
"children": [
{
"weight": 2,
"parentId": "2",
"name": "商品库存",
"id": "221"
},
{
"weight": 44,
"parentId": "2",
"name": "商品发布",
"id": "21"
}
],
"name": "店铺管理",
"id": "2"
},
{
"weight": 5,
"parentId": "0",
"children": [
{
"weight": 222222,
"parentId": "1",
"children": [
{
"weight": 0,
"parentId": "11",
"name": "用户添加",
"id": "111"
}
],
"name": "用户管理",
"id": "11"
}
],
"name": "系统管理",
"id": "1"
}
]
4、Benchmark测试结果