springboot使用jmh基准测试评估json反序列化、实体转换的性能差异

1、背景

笔者经常把一些经常使用的数据放到redis缓存,方便程序进行读取。

比如按照不同的键名将数值存储到hash值类型中。示例如下

hash             hashkey   hashValue

snapshot          999          "{\"id\":999,\"distId\":999,\"distName\":\"玄武区\",\"confirm\":7,\"suspect\":0,\"dead\":0,\"heal\":2,\"weight\":6.6,\"level\":\"area\",\"mapId\":\"c320100_2\",\"updateTime\":\"2020-03-03 07:20:39\"}"

snapshot1         999           {"confirm":7,"dead":0,"heal":2,"weight":6.6,"mapId":"c320100_2","updateTime":"2020-03-03 07:20:39","name":"玄武区"}

 

以上数据分别2052条

springboot使用jmh基准测试评估json反序列化、实体转换的性能差异_第1张图片

 

 

 springboot使用jmh基准测试评估json反序列化、实体转换的性能差异_第2张图片

 

 

 

分别将snapshot1,snapshot转换成实体,现在将对他们进行基准测试,以评测两种存储方式的性能差异

2、基准测试

2个独立进程,分别依1次热身/热加载,批量执行20次,总计40次

2.1、snapshot1相关测试

这里用的实体转换框架是ModelMapper,官网:http://modelmapper.org/ 其性能比较高,主要用于实体属性之间的拷贝,

是笔者工作中项目实践中通过对比,选择到的这个框架。

maven依赖

<dependency>
   <groupId>org.modelmappergroupId>
   <artifactId>modelmapperartifactId>
   <version>0.7.7version>
dependency>

 以下modelMapper转换成实体代码要加 

modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);

 

不同的方法会影响基准测试的最终结果

@Benchmark
public void testMapToPojo()
{
    ModelMapper modelMapper = new ModelMapper();
    Map map = cacheService.getCacheMap("snapshot1");
    Map result = new HashMap<>();

    for(Iterator> it = map.entrySet().iterator(); it.hasNext();)
    {
        Map.Entry entry = it.next();

        String key = entry.getKey();
        SnapShot snapshot = modelMapper.map(entry, SnapShot.class);
        result.put(key, snapshot);
    }
}
    /**
     * 获得缓存的Map
     *
     * @param key
     * @return
     */
    @Override
    public  Map getCacheMap(String key) {
        Map map = redisTemplate.opsForHash().entries(key);
        return map;
    }

 

测试日志:

D:\develop\java\jdk1.8.0_181\bin\java.exe "-javaagent:D:\Program Files\JetBrains\IntelliJ IDEA 2018.3.4\lib\idea_rt.jar=52204:D:\Program Files\JetBrains\IntelliJ IDEA 2018.3.4\bin" -Dfile.encoding=UTF-8 -classpath D:\develop\java\jdk1.8.0_181\jre\lib\charsets.jar;D:\develop\java\jdk1.8.0_181\jre\lib\deploy.jar;D:\develop\java\jdk1.8.0_181\jre\lib\ext\access-bridge-64.jar;D:\develop\java\jdk1.8.0_181\jre\lib\ext\cldrdata.jar;D:\develop\java\jdk1.8.0_181\jre\lib\ext\dnsns.jar;D:\develop\java\jdk1.8.0_181\jre\lib\ext\jaccess.jar;D:\develop\java\jdk1.8.0_181\jre\lib\ext\jfxrt.jar;D:\develop\java\jdk1.8.0_181\jre\lib\ext\localedata.jar;D:\develop\java\jdk1.8.0_181\jre\lib\ext\nashorn.jar;D:\develop\java\jdk1.8.0_181\jre\lib\ext\sunec.jar;D:\develop\java\jdk1.8.0_181\jre\lib\ext\sunjce_provider.jar;D:\develop\java\jdk1.8.0_181\jre\lib\ext\sunmscapi.jar;D:\develop\java\jdk1.8.0_181\jre\lib\ext\sunpkcs11.jar;D:\develop\java\jdk1.8.0_181\jre\lib\ext\zipfs.jar;D:\develop\java\jdk1.8.0_181\jre\lib\javaws.jar;D:\develop\java\jdk1.8.0_181\jre\lib\jce.jar;D:\develop\java\jdk1.8.0_181\jre\lib\jfr.jar;D:\develop\java\jdk1.8.0_181\jre\lib\jfxswt.jar;D:\develop\java\jdk1.8.0_181\jre\lib\jsse.jar;D:\develop\java\jdk1.8.0_181\jre\lib\management-agent.jar;D:\develop\java\jdk1.8.0_181\jre\lib\plugin.jar;D:\develop\java\jdk1.8.0_181\jre\lib\resources.jar;D:\develop\java\jdk1.8.0_181\jre\lib\rt.jar;E:\project\java\springBootJmh\target\test-classes;E:\project\java\springBootJmh\target\classes;D:\develop\apache-maven-3.5.4\repo\org\apache\commons\commons-pool2\2.5.0\commons-pool2-2.5.0.jar;D:\develop\apache-maven-3.5.4\repo\org\springframework\boot\spring-boot-starter-cache\2.2.4.RELEASE\spring-boot-starter-cache-2.2.4.RELEASE.jar;D:\develop\apache-maven-3.5.4\repo\org\springframework\boot\spring-boot-starter\2.2.4.RELEASE\spring-boot-starter-2.2.4.RELEASE.jar;D:\develop\apache-maven-3.5.4\repo\org\springframework\boot\spring-boot\2.2.4.RELEASE\spring-boot-2.2.4.RELEASE.jar;D:\develop\apache-maven-3.5.4\repo\org\springframework\boot\spring-boot-autoconfigure\2.2.4.RELEASE\spring-boot-autoconfigure-2.2.4.RELEASE.jar;D:\develop\apache-maven-3.5.4\repo\org\springframework\boot\spring-boot-starter-logging\2.2.4.RELEASE\spring-boot-starter-logging-2.2.4.RELEASE.jar;D:\develop\apache-maven-3.5.4\repo\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;D:\develop\apache-maven-3.5.4\repo\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;D:\develop\apache-maven-3.5.4\repo\org\apache\logging\log4j\log4j-to-slf4j\2.12.1\log4j-to-slf4j-2.12.1.jar;D:\develop\apache-maven-3.5.4\repo\org\apache\logging\log4j\log4j-api\2.12.1\log4j-api-2.12.1.jar;D:\develop\apache-maven-3.5.4\repo\org\slf4j\jul-to-slf4j\1.7.30\jul-to-slf4j-1.7.30.jar;D:\develop\apache-maven-3.5.4\repo\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;D:\develop\apache-maven-3.5.4\repo\org\yaml\snakeyaml\1.25\snakeyaml-1.25.jar;D:\develop\apache-maven-3.5.4\repo\org\springframework\spring-context-support\5.2.3.RELEASE\spring-context-support-5.2.3.RELEASE.jar;D:\develop\apache-maven-3.5.4\repo\org\springframework\spring-beans\5.2.3.RELEASE\spring-beans-5.2.3.RELEASE.jar;D:\develop\apache-maven-3.5.4\repo\org\springframework\spring-context\5.2.3.RELEASE\spring-context-5.2.3.RELEASE.jar;D:\develop\apache-maven-3.5.4\repo\org\springframework\spring-expression\5.2.3.RELEASE\spring-expression-5.2.3.RELEASE.jar;D:\develop\apache-maven-3.5.4\repo\org\springframework\boot\spring-boot-starter-data-redis\2.2.4.RELEASE\spring-boot-starter-data-redis-2.2.4.RELEASE.jar;D:\develop\apache-maven-3.5.4\repo\org\springframework\data\spring-data-redis\2.2.4.RELEASE\spring-data-redis-2.2.4.RELEASE.jar;D:\develop\apache-maven-3.5.4\repo\org\springframework\data\spring-data-keyvalue\2.2.4.RELEASE\spring-data-keyvalue-2.2.4.RELEASE.jar;D:\develop\apache-maven-3.5.4\repo\org\springframework\data\spring-data-commons\2.2.4.RELEASE\spring-data-commons-2.2.4.RELEASE.jar;D:\develop\apache-maven-3.5.4\repo\org\springframework\spring-tx\5.2.3.RELEASE\spring-tx-5.2.3.RELEASE.jar;D:\develop\apache-maven-3.5.4\repo\org\springframework\spring-oxm\5.2.3.RELEASE\spring-oxm-5.2.3.RELEASE.jar;D:\develop\apache-maven-3.5.4\repo\org\springframework\spring-aop\5.2.3.RELEASE\spring-aop-5.2.3.RELEASE.jar;D:\develop\apache-maven-3.5.4\repo\org\slf4j\slf4j-api\1.7.30\slf4j-api-1.7.30.jar;D:\develop\apache-maven-3.5.4\repo\io\lettuce\lettuce-core\5.2.1.RELEASE\lettuce-core-5.2.1.RELEASE.jar;D:\develop\apache-maven-3.5.4\repo\io\netty\netty-common\4.1.45.Final\netty-common-4.1.45.Final.jar;D:\develop\apache-maven-3.5.4\repo\io\netty\netty-handler\4.1.45.Final\netty-handler-4.1.45.Final.jar;D:\develop\apache-maven-3.5.4\repo\io\netty\netty-buffer\4.1.45.Final\netty-buffer-4.1.45.Final.jar;D:\develop\apache-maven-3.5.4\repo\io\netty\netty-codec\4.1.45.Final\netty-codec-4.1.45.Final.jar;D:\develop\apache-maven-3.5.4\repo\io\netty\netty-transport\4.1.45.Final\netty-transport-4.1.45.Final.jar;D:\develop\apache-maven-3.5.4\repo\io\netty\netty-resolver\4.1.45.Final\netty-resolver-4.1.45.Final.jar;D:\develop\apache-maven-3.5.4\repo\io\projectreactor\reactor-core\3.3.2.RELEASE\reactor-core-3.3.2.RELEASE.jar;D:\develop\apache-maven-3.5.4\repo\org\reactivestreams\reactive-streams\1.0.3\reactive-streams-1.0.3.jar;D:\develop\apache-maven-3.5.4\repo\com\dyuproject\protostuff\protostuff-runtime\1.1.3\protostuff-runtime-1.1.3.jar;D:\develop\apache-maven-3.5.4\repo\com\dyuproject\protostuff\protostuff-collectionschema\1.1.3\protostuff-collectionschema-1.1.3.jar;D:\develop\apache-maven-3.5.4\repo\com\dyuproject\protostuff\protostuff-api\1.0.8\protostuff-api-1.0.8.jar;D:\develop\apache-maven-3.5.4\repo\com\dyuproject\protostuff\protostuff-core\1.0.8\protostuff-core-1.0.8.jar;D:\develop\apache-maven-3.5.4\repo\org\projectlombok\lombok\1.18.10\lombok-1.18.10.jar;D:\develop\apache-maven-3.5.4\repo\org\springframework\boot\spring-boot-starter-test\2.2.4.RELEASE\spring-boot-starter-test-2.2.4.RELEASE.jar;D:\develop\apache-maven-3.5.4\repo\org\springframework\boot\spring-boot-test\2.2.4.RELEASE\spring-boot-test-2.2.4.RELEASE.jar;D:\develop\apache-maven-3.5.4\repo\org\springframework\boot\spring-boot-test-autoconfigure\2.2.4.RELEASE\spring-boot-test-autoconfigure-2.2.4.RELEASE.jar;D:\develop\apache-maven-3.5.4\repo\com\jayway\jsonpath\json-path\2.4.0\json-path-2.4.0.jar;D:\develop\apache-maven-3.5.4\repo\net\minidev\json-smart\2.3\json-smart-2.3.jar;D:\develop\apache-maven-3.5.4\repo\net\minidev\accessors-smart\1.2\accessors-smart-1.2.jar;D:\develop\apache-maven-3.5.4\repo\org\ow2\asm\asm\5.0.4\asm-5.0.4.jar;D:\develop\apache-maven-3.5.4\repo\jakarta\xml\bind\jakarta.xml.bind-api\2.3.2\jakarta.xml.bind-api-2.3.2.jar;D:\develop\apache-maven-3.5.4\repo\jakarta\activation\jakarta.activation-api\1.2.1\jakarta.activation-api-1.2.1.jar;D:\develop\apache-maven-3.5.4\repo\org\junit\jupiter\junit-jupiter\5.5.2\junit-jupiter-5.5.2.jar;D:\develop\apache-maven-3.5.4\repo\org\junit\jupiter\junit-jupiter-api\5.5.2\junit-jupiter-api-5.5.2.jar;D:\develop\apache-maven-3.5.4\repo\org\apiguardian\apiguardian-api\1.1.0\apiguardian-api-1.1.0.jar;D:\develop\apache-maven-3.5.4\repo\org\opentest4j\opentest4j\1.2.0\opentest4j-1.2.0.jar;D:\develop\apache-maven-3.5.4\repo\org\junit\platform\junit-platform-commons\1.5.2\junit-platform-commons-1.5.2.jar;D:\develop\apache-maven-3.5.4\repo\org\junit\jupiter\junit-jupiter-params\5.5.2\junit-jupiter-params-5.5.2.jar;D:\develop\apache-maven-3.5.4\repo\org\junit\jupiter\junit-jupiter-engine\5.5.2\junit-jupiter-engine-5.5.2.jar;D:\develop\apache-maven-3.5.4\repo\org\junit\platform\junit-platform-engine\1.5.2\junit-platform-engine-1.5.2.jar;D:\develop\apache-maven-3.5.4\repo\org\mockito\mockito-junit-jupiter\3.1.0\mockito-junit-jupiter-3.1.0.jar;D:\develop\apache-maven-3.5.4\repo\org\assertj\assertj-core\3.13.2\assertj-core-3.13.2.jar;D:\develop\apache-maven-3.5.4\repo\org\hamcrest\hamcrest\2.1\hamcrest-2.1.jar;D:\develop\apache-maven-3.5.4\repo\org\mockito\mockito-core\3.1.0\mockito-core-3.1.0.jar;D:\develop\apache-maven-3.5.4\repo\net\bytebuddy\byte-buddy\1.10.6\byte-buddy-1.10.6.jar;D:\develop\apache-maven-3.5.4\repo\net\bytebuddy\byte-buddy-agent\1.10.6\byte-buddy-agent-1.10.6.jar;D:\develop\apache-maven-3.5.4\repo\org\objenesis\objenesis\2.6\objenesis-2.6.jar;D:\develop\apache-maven-3.5.4\repo\org\skyscreamer\jsonassert\1.5.0\jsonassert-1.5.0.jar;D:\develop\apache-maven-3.5.4\repo\com\vaadin\external\google\android-json\0.0.20131108.vaadin1\android-json-0.0.20131108.vaadin1.jar;D:\develop\apache-maven-3.5.4\repo\org\springframework\spring-core\5.2.3.RELEASE\spring-core-5.2.3.RELEASE.jar;D:\develop\apache-maven-3.5.4\repo\org\springframework\spring-jcl\5.2.3.RELEASE\spring-jcl-5.2.3.RELEASE.jar;D:\develop\apache-maven-3.5.4\repo\org\springframework\spring-test\5.2.3.RELEASE\spring-test-5.2.3.RELEASE.jar;D:\develop\apache-maven-3.5.4\repo\org\xmlunit\xmlunit-core\2.6.3\xmlunit-core-2.6.3.jar;D:\develop\apache-maven-3.5.4\repo\com\google\guava\guava\20.0\guava-20.0.jar;D:\develop\apache-maven-3.5.4\repo\com\google\code\gson\gson\2.8.4\gson-2.8.4.jar;D:\develop\apache-maven-3.5.4\repo\org\modelmapper\modelmapper\0.7.7\modelmapper-0.7.7.jar;D:\develop\apache-maven-3.5.4\repo\org\openjdk\jmh\jmh-core\1.21\jmh-core-1.21.jar;D:\develop\apache-maven-3.5.4\repo\net\sf\jopt-simple\jopt-simple\4.6\jopt-simple-4.6.jar;D:\develop\apache-maven-3.5.4\repo\org\apache\commons\commons-math3\3.2\commons-math3-3.2.jar;D:\develop\apache-maven-3.5.4\repo\org\openjdk\jmh\jmh-generator-annprocess\1.21\jmh-generator-annprocess-1.21.jar;D:\develop\apache-maven-3.5.4\repo\com\fasterxml\jackson\core\jackson-annotations\2.10.2\jackson-annotations-2.10.2.jar;D:\develop\apache-maven-3.5.4\repo\com\fasterxml\jackson\core\jackson-databind\2.10.2\jackson-databind-2.10.2.jar;D:\develop\apache-maven-3.5.4\repo\com\fasterxml\jackson\core\jackson-core\2.10.2\jackson-core-2.10.2.jar;D:\develop\apache-maven-3.5.4\repo\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.10.2\jackson-datatype-jsr310-2.10.2.jar;D:\develop\apache-maven-3.5.4\repo\commons-beanutils\commons-beanutils\1.9.3\commons-beanutils-1.9.3.jar;D:\develop\apache-maven-3.5.4\repo\commons-logging\commons-logging\1.2\commons-logging-1.2.jar;D:\develop\apache-maven-3.5.4\repo\commons-collections\commons-collections\3.2.2\commons-collections-3.2.2.jar;D:\develop\apache-maven-3.5.4\repo\junit\junit\4.12\junit-4.12.jar;D:\develop\apache-maven-3.5.4\repo\org\hamcrest\hamcrest-core\2.1\hamcrest-core-2.1.jar com.springboot.AppTest
# JMH version: 1.21
# VM version: JDK 1.8.0_181, Java HotSpot(TM) 64-Bit Server VM, 25.181-b13
# VM invoker: D:\develop\java\jdk1.8.0_181\jre\bin\java.exe
# VM options: -javaagent:D:\Program Files\JetBrains\IntelliJ IDEA 2018.3.4\lib\idea_rt.jar=52204:D:\Program Files\JetBrains\IntelliJ IDEA 2018.3.4\bin -Dfile.encoding=UTF-8
# Warmup: 1 iterations, 10 s each
# Measurement: 20 iterations, 10 s each
# Timeout: 10 min per iteration
# Threads: 1 thread, will synchronize iterations
# Benchmark mode: Average time, time/op
# Benchmark: com.springboot.AppTest.testMapToPojo

# Run progress: 0.00% complete, ETA 00:07:00
# Fork: 1 of 2
# Warmup Iteration   1: LOGBACK: No context given for c.q.l.core.rolling.SizeAndTimeBasedRollingPolicy@999543437

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.2.4.RELEASE)

2020-03-04 10:39:47.875  INFO 10892 --- [jo-jmh-worker-1] o.s.boot.SpringApplication               : Starting application on DESKTOP-AJTC6JT with PID 10892 (started by Administrator in E:\project\java\springBootJmh)
2020-03-04 10:39:47.880  INFO 10892 --- [jo-jmh-worker-1] o.s.boot.SpringApplication               : No active profile set, falling back to default profiles: default
2020-03-04 10:39:48.380  INFO 10892 --- [jo-jmh-worker-1] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
2020-03-04 10:39:48.384  INFO 10892 --- [jo-jmh-worker-1] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data Redis repositories in DEFAULT mode.
2020-03-04 10:39:48.417  INFO 10892 --- [jo-jmh-worker-1] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 19ms. Found 0 Redis repository interfaces.
2020-03-04 10:39:49.420  INFO 10892 --- [jo-jmh-worker-1] o.s.boot.SpringApplication               : Started application in 2.007 seconds (JVM running for 2.878)
2020-03-04 10:39:49.704  INFO 10892 --- [jo-jmh-worker-1] io.lettuce.core.EpollProvider            : Starting without optional epoll library
2020-03-04 10:39:49.705  INFO 10892 --- [jo-jmh-worker-1] io.lettuce.core.KqueueProvider           : Starting without optional kqueue library
4.977 ms/op
Iteration   1: 4.245 ms/op
Iteration   2: 4.212 ms/op
Iteration   3: 4.223 ms/op
Iteration   4: 4.372 ms/op
Iteration   5: 4.472 ms/op
Iteration   6: 4.559 ms/op
Iteration   7: 4.569 ms/op
Iteration   8: 4.393 ms/op
Iteration   9: 4.357 ms/op
Iteration  10: 4.218 ms/op
Iteration  11: 4.273 ms/op
Iteration  12: 4.372 ms/op
Iteration  13: 4.274 ms/op
Iteration  14: 4.194 ms/op
Iteration  15: 4.198 ms/op
Iteration  16: 4.177 ms/op
Iteration  17: 4.175 ms/op
Iteration  18: 4.180 ms/op
Iteration  19: 4.181 ms/op
Iteration  20: 4.185 ms/op

# Run progress: 50.00% complete, ETA 00:03:33
# Fork: 2 of 2
# Warmup Iteration   1: LOGBACK: No context given for c.q.l.core.rolling.SizeAndTimeBasedRollingPolicy@142500488

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.2.4.RELEASE)

2020-03-04 10:43:21.156  INFO 5872 --- [jo-jmh-worker-1] o.s.boot.SpringApplication               : Starting application on DESKTOP-AJTC6JT with PID 5872 (started by Administrator in E:\project\java\springBootJmh)
2020-03-04 10:43:21.159  INFO 5872 --- [jo-jmh-worker-1] o.s.boot.SpringApplication               : No active profile set, falling back to default profiles: default
2020-03-04 10:43:21.571  INFO 5872 --- [jo-jmh-worker-1] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
2020-03-04 10:43:21.574  INFO 5872 --- [jo-jmh-worker-1] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data Redis repositories in DEFAULT mode.
2020-03-04 10:43:21.600  INFO 5872 --- [jo-jmh-worker-1] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 14ms. Found 0 Redis repository interfaces.
2020-03-04 10:43:22.461  INFO 5872 --- [jo-jmh-worker-1] o.s.boot.SpringApplication               : Started application in 1.646 seconds (JVM running for 2.298)
2020-03-04 10:43:22.720  INFO 5872 --- [jo-jmh-worker-1] io.lettuce.core.EpollProvider            : Starting without optional epoll library
2020-03-04 10:43:22.722  INFO 5872 --- [jo-jmh-worker-1] io.lettuce.core.KqueueProvider           : Starting without optional kqueue library
4.919 ms/op
Iteration   1: 4.240 ms/op
Iteration   2: 4.222 ms/op
Iteration   3: 4.188 ms/op
Iteration   4: 4.183 ms/op
Iteration   5: 4.188 ms/op
Iteration   6: 4.179 ms/op
Iteration   7: 4.210 ms/op
Iteration   8: 4.185 ms/op
Iteration   9: 4.409 ms/op
Iteration  10: 4.203 ms/op
Iteration  11: 4.219 ms/op
Iteration  12: 4.225 ms/op
Iteration  13: 4.333 ms/op
Iteration  14: 4.302 ms/op
Iteration  15: 4.240 ms/op
Iteration  16: 4.222 ms/op
Iteration  17: 4.297 ms/op
Iteration  18: 4.253 ms/op
Iteration  19: 4.243 ms/op
Iteration  20: 4.308 ms/op


Result "com.springboot.AppTest.testMapToPojo":
  4.267 ±(99.9%) 0.057 ms/op [Average]
  (min, avg, max) = (4.175, 4.267, 4.569), stdev = 0.101
  CI (99.9%): [4.210, 4.324] (assumes normal distribution)


# Run complete. Total time: 00:07:06

REMEMBER: The numbers below are just data. To gain reusable insights, you need to follow up on
why the numbers are the way they are. Use profilers (see -prof, -lprof), design factorial
experiments, perform baseline and negative tests that provide experimental control, make sure
the benchmarking environment is safe on JVM/OS/HW level, ask for reviews from the domain experts.
Do not assume the numbers tell you what you want them to tell.

Benchmark              Mode  Cnt  Score   Error  Units
AppTest.testMapToPojo  avgt   40  4.267 ± 0.057  ms/op

Process finished with exit code 0

结果:40次基准测试,最小(最理想)4.175毫秒,均值 4.267毫秒,最大值(最坏情况)4.569毫秒

 

2.2、snapshot测试

@Benchmark
public void testGetPojo()
{
    Map map = cacheService.getCacheMap("snapshot", SnapShot.class);
}
@Override
public  Map getCacheMap(String key, Class clazz) {
    Map raw = redisTemplate.opsForHash().entries(key);
    Map map = Maps.newLinkedHashMapWithExpectedSize(raw.size());

    for (Map.Entry entry : raw.entrySet()) {
        String hashKey = entry.getKey();
        String value = entry.getValue();
        map.put(hashKey, gson.fromJson(value, clazz));
    }
    return map;
}

测试日志:

D:\develop\java\jdk1.8.0_181\bin\java.exe "-javaagent:D:\Program Files\JetBrains\IntelliJ IDEA 2018.3.4\lib\idea_rt.jar=52899:D:\Program Files\JetBrains\IntelliJ IDEA 2018.3.4\bin" -Dfile.encoding=UTF-8 -classpath D:\develop\java\jdk1.8.0_181\jre\lib\charsets.jar;D:\develop\java\jdk1.8.0_181\jre\lib\deploy.jar;D:\develop\java\jdk1.8.0_181\jre\lib\ext\access-bridge-64.jar;D:\develop\java\jdk1.8.0_181\jre\lib\ext\cldrdata.jar;D:\develop\java\jdk1.8.0_181\jre\lib\ext\dnsns.jar;D:\develop\java\jdk1.8.0_181\jre\lib\ext\jaccess.jar;D:\develop\java\jdk1.8.0_181\jre\lib\ext\jfxrt.jar;D:\develop\java\jdk1.8.0_181\jre\lib\ext\localedata.jar;D:\develop\java\jdk1.8.0_181\jre\lib\ext\nashorn.jar;D:\develop\java\jdk1.8.0_181\jre\lib\ext\sunec.jar;D:\develop\java\jdk1.8.0_181\jre\lib\ext\sunjce_provider.jar;D:\develop\java\jdk1.8.0_181\jre\lib\ext\sunmscapi.jar;D:\develop\java\jdk1.8.0_181\jre\lib\ext\sunpkcs11.jar;D:\develop\java\jdk1.8.0_181\jre\lib\ext\zipfs.jar;D:\develop\java\jdk1.8.0_181\jre\lib\javaws.jar;D:\develop\java\jdk1.8.0_181\jre\lib\jce.jar;D:\develop\java\jdk1.8.0_181\jre\lib\jfr.jar;D:\develop\java\jdk1.8.0_181\jre\lib\jfxswt.jar;D:\develop\java\jdk1.8.0_181\jre\lib\jsse.jar;D:\develop\java\jdk1.8.0_181\jre\lib\management-agent.jar;D:\develop\java\jdk1.8.0_181\jre\lib\plugin.jar;D:\develop\java\jdk1.8.0_181\jre\lib\resources.jar;D:\develop\java\jdk1.8.0_181\jre\lib\rt.jar;E:\project\java\springBootJmh\target\test-classes;E:\project\java\springBootJmh\target\classes;D:\develop\apache-maven-3.5.4\repo\org\apache\commons\commons-pool2\2.5.0\commons-pool2-2.5.0.jar;D:\develop\apache-maven-3.5.4\repo\org\springframework\boot\spring-boot-starter-cache\2.2.4.RELEASE\spring-boot-starter-cache-2.2.4.RELEASE.jar;D:\develop\apache-maven-3.5.4\repo\org\springframework\boot\spring-boot-starter\2.2.4.RELEASE\spring-boot-starter-2.2.4.RELEASE.jar;D:\develop\apache-maven-3.5.4\repo\org\springframework\boot\spring-boot\2.2.4.RELEASE\spring-boot-2.2.4.RELEASE.jar;D:\develop\apache-maven-3.5.4\repo\org\springframework\boot\spring-boot-autoconfigure\2.2.4.RELEASE\spring-boot-autoconfigure-2.2.4.RELEASE.jar;D:\develop\apache-maven-3.5.4\repo\org\springframework\boot\spring-boot-starter-logging\2.2.4.RELEASE\spring-boot-starter-logging-2.2.4.RELEASE.jar;D:\develop\apache-maven-3.5.4\repo\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;D:\develop\apache-maven-3.5.4\repo\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;D:\develop\apache-maven-3.5.4\repo\org\apache\logging\log4j\log4j-to-slf4j\2.12.1\log4j-to-slf4j-2.12.1.jar;D:\develop\apache-maven-3.5.4\repo\org\apache\logging\log4j\log4j-api\2.12.1\log4j-api-2.12.1.jar;D:\develop\apache-maven-3.5.4\repo\org\slf4j\jul-to-slf4j\1.7.30\jul-to-slf4j-1.7.30.jar;D:\develop\apache-maven-3.5.4\repo\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;D:\develop\apache-maven-3.5.4\repo\org\yaml\snakeyaml\1.25\snakeyaml-1.25.jar;D:\develop\apache-maven-3.5.4\repo\org\springframework\spring-context-support\5.2.3.RELEASE\spring-context-support-5.2.3.RELEASE.jar;D:\develop\apache-maven-3.5.4\repo\org\springframework\spring-beans\5.2.3.RELEASE\spring-beans-5.2.3.RELEASE.jar;D:\develop\apache-maven-3.5.4\repo\org\springframework\spring-context\5.2.3.RELEASE\spring-context-5.2.3.RELEASE.jar;D:\develop\apache-maven-3.5.4\repo\org\springframework\spring-expression\5.2.3.RELEASE\spring-expression-5.2.3.RELEASE.jar;D:\develop\apache-maven-3.5.4\repo\org\springframework\boot\spring-boot-starter-data-redis\2.2.4.RELEASE\spring-boot-starter-data-redis-2.2.4.RELEASE.jar;D:\develop\apache-maven-3.5.4\repo\org\springframework\data\spring-data-redis\2.2.4.RELEASE\spring-data-redis-2.2.4.RELEASE.jar;D:\develop\apache-maven-3.5.4\repo\org\springframework\data\spring-data-keyvalue\2.2.4.RELEASE\spring-data-keyvalue-2.2.4.RELEASE.jar;D:\develop\apache-maven-3.5.4\repo\org\springframework\data\spring-data-commons\2.2.4.RELEASE\spring-data-commons-2.2.4.RELEASE.jar;D:\develop\apache-maven-3.5.4\repo\org\springframework\spring-tx\5.2.3.RELEASE\spring-tx-5.2.3.RELEASE.jar;D:\develop\apache-maven-3.5.4\repo\org\springframework\spring-oxm\5.2.3.RELEASE\spring-oxm-5.2.3.RELEASE.jar;D:\develop\apache-maven-3.5.4\repo\org\springframework\spring-aop\5.2.3.RELEASE\spring-aop-5.2.3.RELEASE.jar;D:\develop\apache-maven-3.5.4\repo\org\slf4j\slf4j-api\1.7.30\slf4j-api-1.7.30.jar;D:\develop\apache-maven-3.5.4\repo\io\lettuce\lettuce-core\5.2.1.RELEASE\lettuce-core-5.2.1.RELEASE.jar;D:\develop\apache-maven-3.5.4\repo\io\netty\netty-common\4.1.45.Final\netty-common-4.1.45.Final.jar;D:\develop\apache-maven-3.5.4\repo\io\netty\netty-handler\4.1.45.Final\netty-handler-4.1.45.Final.jar;D:\develop\apache-maven-3.5.4\repo\io\netty\netty-buffer\4.1.45.Final\netty-buffer-4.1.45.Final.jar;D:\develop\apache-maven-3.5.4\repo\io\netty\netty-codec\4.1.45.Final\netty-codec-4.1.45.Final.jar;D:\develop\apache-maven-3.5.4\repo\io\netty\netty-transport\4.1.45.Final\netty-transport-4.1.45.Final.jar;D:\develop\apache-maven-3.5.4\repo\io\netty\netty-resolver\4.1.45.Final\netty-resolver-4.1.45.Final.jar;D:\develop\apache-maven-3.5.4\repo\io\projectreactor\reactor-core\3.3.2.RELEASE\reactor-core-3.3.2.RELEASE.jar;D:\develop\apache-maven-3.5.4\repo\org\reactivestreams\reactive-streams\1.0.3\reactive-streams-1.0.3.jar;D:\develop\apache-maven-3.5.4\repo\com\dyuproject\protostuff\protostuff-runtime\1.1.3\protostuff-runtime-1.1.3.jar;D:\develop\apache-maven-3.5.4\repo\com\dyuproject\protostuff\protostuff-collectionschema\1.1.3\protostuff-collectionschema-1.1.3.jar;D:\develop\apache-maven-3.5.4\repo\com\dyuproject\protostuff\protostuff-api\1.0.8\protostuff-api-1.0.8.jar;D:\develop\apache-maven-3.5.4\repo\com\dyuproject\protostuff\protostuff-core\1.0.8\protostuff-core-1.0.8.jar;D:\develop\apache-maven-3.5.4\repo\org\projectlombok\lombok\1.18.10\lombok-1.18.10.jar;D:\develop\apache-maven-3.5.4\repo\org\springframework\boot\spring-boot-starter-test\2.2.4.RELEASE\spring-boot-starter-test-2.2.4.RELEASE.jar;D:\develop\apache-maven-3.5.4\repo\org\springframework\boot\spring-boot-test\2.2.4.RELEASE\spring-boot-test-2.2.4.RELEASE.jar;D:\develop\apache-maven-3.5.4\repo\org\springframework\boot\spring-boot-test-autoconfigure\2.2.4.RELEASE\spring-boot-test-autoconfigure-2.2.4.RELEASE.jar;D:\develop\apache-maven-3.5.4\repo\com\jayway\jsonpath\json-path\2.4.0\json-path-2.4.0.jar;D:\develop\apache-maven-3.5.4\repo\net\minidev\json-smart\2.3\json-smart-2.3.jar;D:\develop\apache-maven-3.5.4\repo\net\minidev\accessors-smart\1.2\accessors-smart-1.2.jar;D:\develop\apache-maven-3.5.4\repo\org\ow2\asm\asm\5.0.4\asm-5.0.4.jar;D:\develop\apache-maven-3.5.4\repo\jakarta\xml\bind\jakarta.xml.bind-api\2.3.2\jakarta.xml.bind-api-2.3.2.jar;D:\develop\apache-maven-3.5.4\repo\jakarta\activation\jakarta.activation-api\1.2.1\jakarta.activation-api-1.2.1.jar;D:\develop\apache-maven-3.5.4\repo\org\junit\jupiter\junit-jupiter\5.5.2\junit-jupiter-5.5.2.jar;D:\develop\apache-maven-3.5.4\repo\org\junit\jupiter\junit-jupiter-api\5.5.2\junit-jupiter-api-5.5.2.jar;D:\develop\apache-maven-3.5.4\repo\org\apiguardian\apiguardian-api\1.1.0\apiguardian-api-1.1.0.jar;D:\develop\apache-maven-3.5.4\repo\org\opentest4j\opentest4j\1.2.0\opentest4j-1.2.0.jar;D:\develop\apache-maven-3.5.4\repo\org\junit\platform\junit-platform-commons\1.5.2\junit-platform-commons-1.5.2.jar;D:\develop\apache-maven-3.5.4\repo\org\junit\jupiter\junit-jupiter-params\5.5.2\junit-jupiter-params-5.5.2.jar;D:\develop\apache-maven-3.5.4\repo\org\junit\jupiter\junit-jupiter-engine\5.5.2\junit-jupiter-engine-5.5.2.jar;D:\develop\apache-maven-3.5.4\repo\org\junit\platform\junit-platform-engine\1.5.2\junit-platform-engine-1.5.2.jar;D:\develop\apache-maven-3.5.4\repo\org\mockito\mockito-junit-jupiter\3.1.0\mockito-junit-jupiter-3.1.0.jar;D:\develop\apache-maven-3.5.4\repo\org\assertj\assertj-core\3.13.2\assertj-core-3.13.2.jar;D:\develop\apache-maven-3.5.4\repo\org\hamcrest\hamcrest\2.1\hamcrest-2.1.jar;D:\develop\apache-maven-3.5.4\repo\org\mockito\mockito-core\3.1.0\mockito-core-3.1.0.jar;D:\develop\apache-maven-3.5.4\repo\net\bytebuddy\byte-buddy\1.10.6\byte-buddy-1.10.6.jar;D:\develop\apache-maven-3.5.4\repo\net\bytebuddy\byte-buddy-agent\1.10.6\byte-buddy-agent-1.10.6.jar;D:\develop\apache-maven-3.5.4\repo\org\objenesis\objenesis\2.6\objenesis-2.6.jar;D:\develop\apache-maven-3.5.4\repo\org\skyscreamer\jsonassert\1.5.0\jsonassert-1.5.0.jar;D:\develop\apache-maven-3.5.4\repo\com\vaadin\external\google\android-json\0.0.20131108.vaadin1\android-json-0.0.20131108.vaadin1.jar;D:\develop\apache-maven-3.5.4\repo\org\springframework\spring-core\5.2.3.RELEASE\spring-core-5.2.3.RELEASE.jar;D:\develop\apache-maven-3.5.4\repo\org\springframework\spring-jcl\5.2.3.RELEASE\spring-jcl-5.2.3.RELEASE.jar;D:\develop\apache-maven-3.5.4\repo\org\springframework\spring-test\5.2.3.RELEASE\spring-test-5.2.3.RELEASE.jar;D:\develop\apache-maven-3.5.4\repo\org\xmlunit\xmlunit-core\2.6.3\xmlunit-core-2.6.3.jar;D:\develop\apache-maven-3.5.4\repo\com\google\guava\guava\20.0\guava-20.0.jar;D:\develop\apache-maven-3.5.4\repo\com\google\code\gson\gson\2.8.4\gson-2.8.4.jar;D:\develop\apache-maven-3.5.4\repo\org\modelmapper\modelmapper\0.7.7\modelmapper-0.7.7.jar;D:\develop\apache-maven-3.5.4\repo\org\openjdk\jmh\jmh-core\1.21\jmh-core-1.21.jar;D:\develop\apache-maven-3.5.4\repo\net\sf\jopt-simple\jopt-simple\4.6\jopt-simple-4.6.jar;D:\develop\apache-maven-3.5.4\repo\org\apache\commons\commons-math3\3.2\commons-math3-3.2.jar;D:\develop\apache-maven-3.5.4\repo\org\openjdk\jmh\jmh-generator-annprocess\1.21\jmh-generator-annprocess-1.21.jar;D:\develop\apache-maven-3.5.4\repo\com\fasterxml\jackson\core\jackson-annotations\2.10.2\jackson-annotations-2.10.2.jar;D:\develop\apache-maven-3.5.4\repo\com\fasterxml\jackson\core\jackson-databind\2.10.2\jackson-databind-2.10.2.jar;D:\develop\apache-maven-3.5.4\repo\com\fasterxml\jackson\core\jackson-core\2.10.2\jackson-core-2.10.2.jar;D:\develop\apache-maven-3.5.4\repo\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.10.2\jackson-datatype-jsr310-2.10.2.jar;D:\develop\apache-maven-3.5.4\repo\commons-beanutils\commons-beanutils\1.9.3\commons-beanutils-1.9.3.jar;D:\develop\apache-maven-3.5.4\repo\commons-logging\commons-logging\1.2\commons-logging-1.2.jar;D:\develop\apache-maven-3.5.4\repo\commons-collections\commons-collections\3.2.2\commons-collections-3.2.2.jar;D:\develop\apache-maven-3.5.4\repo\junit\junit\4.12\junit-4.12.jar;D:\develop\apache-maven-3.5.4\repo\org\hamcrest\hamcrest-core\2.1\hamcrest-core-2.1.jar com.springboot.AppTest
# JMH version: 1.21
# VM version: JDK 1.8.0_181, Java HotSpot(TM) 64-Bit Server VM, 25.181-b13
# VM invoker: D:\develop\java\jdk1.8.0_181\jre\bin\java.exe
# VM options: -javaagent:D:\Program Files\JetBrains\IntelliJ IDEA 2018.3.4\lib\idea_rt.jar=52899:D:\Program Files\JetBrains\IntelliJ IDEA 2018.3.4\bin -Dfile.encoding=UTF-8
# Warmup: 1 iterations, 10 s each
# Measurement: 20 iterations, 10 s each
# Timeout: 10 min per iteration
# Threads: 1 thread, will synchronize iterations
# Benchmark mode: Average time, time/op
# Benchmark: com.springboot.AppTest.testGetPojo

# Run progress: 0.00% complete, ETA 00:07:00
# Fork: 1 of 2
# Warmup Iteration   1: LOGBACK: No context given for c.q.l.core.rolling.SizeAndTimeBasedRollingPolicy@1051164110

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.2.4.RELEASE)

2020-03-04 11:08:57.181  INFO 12868 --- [jo-jmh-worker-1] o.s.boot.SpringApplication               : Starting application on DESKTOP-AJTC6JT with PID 12868 (started by Administrator in E:\project\java\springBootJmh)
2020-03-04 11:08:57.185  INFO 12868 --- [jo-jmh-worker-1] o.s.boot.SpringApplication               : No active profile set, falling back to default profiles: default
2020-03-04 11:08:57.710  INFO 12868 --- [jo-jmh-worker-1] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
2020-03-04 11:08:57.712  INFO 12868 --- [jo-jmh-worker-1] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data Redis repositories in DEFAULT mode.
2020-03-04 11:08:57.746  INFO 12868 --- [jo-jmh-worker-1] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 21ms. Found 0 Redis repository interfaces.
2020-03-04 11:08:58.849  INFO 12868 --- [jo-jmh-worker-1] o.s.boot.SpringApplication               : Started application in 2.147 seconds (JVM running for 3.097)
2020-03-04 11:08:59.086  INFO 12868 --- [jo-jmh-worker-1] io.lettuce.core.EpollProvider            : Starting without optional epoll library
2020-03-04 11:08:59.087  INFO 12868 --- [jo-jmh-worker-1] io.lettuce.core.KqueueProvider           : Starting without optional kqueue library
8.041 ms/op
Iteration   1: 7.019 ms/op
Iteration   2: 7.032 ms/op
Iteration   3: 7.008 ms/op
Iteration   4: 7.081 ms/op
Iteration   5: 6.997 ms/op
Iteration   6: 6.958 ms/op
Iteration   7: 6.925 ms/op
Iteration   8: 6.935 ms/op
Iteration   9: 6.943 ms/op
Iteration  10: 6.940 ms/op
Iteration  11: 6.948 ms/op
Iteration  12: 6.956 ms/op
Iteration  13: 7.033 ms/op
Iteration  14: 6.969 ms/op
Iteration  15: 6.964 ms/op
Iteration  16: 6.962 ms/op
Iteration  17: 6.962 ms/op
Iteration  18: 6.955 ms/op
Iteration  19: 6.959 ms/op
Iteration  20: 6.973 ms/op

# Run progress: 50.00% complete, ETA 00:03:33
# Fork: 2 of 2
# Warmup Iteration   1: LOGBACK: No context given for c.q.l.core.rolling.SizeAndTimeBasedRollingPolicy@1741951395

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.2.4.RELEASE)

2020-03-04 11:12:30.673  INFO 12292 --- [jo-jmh-worker-1] o.s.boot.SpringApplication               : Starting application on DESKTOP-AJTC6JT with PID 12292 (started by Administrator in E:\project\java\springBootJmh)
2020-03-04 11:12:30.676  INFO 12292 --- [jo-jmh-worker-1] o.s.boot.SpringApplication               : No active profile set, falling back to default profiles: default
2020-03-04 11:12:31.092  INFO 12292 --- [jo-jmh-worker-1] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
2020-03-04 11:12:31.094  INFO 12292 --- [jo-jmh-worker-1] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data Redis repositories in DEFAULT mode.
2020-03-04 11:12:31.121  INFO 12292 --- [jo-jmh-worker-1] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 15ms. Found 0 Redis repository interfaces.
2020-03-04 11:12:32.013  INFO 12292 --- [jo-jmh-worker-1] o.s.boot.SpringApplication               : Started application in 1.678 seconds (JVM running for 2.34)
2020-03-04 11:12:32.297  INFO 12292 --- [jo-jmh-worker-1] io.lettuce.core.EpollProvider            : Starting without optional epoll library
2020-03-04 11:12:32.299  INFO 12292 --- [jo-jmh-worker-1] io.lettuce.core.KqueueProvider           : Starting without optional kqueue library
7.919 ms/op
Iteration   1: 7.405 ms/op
Iteration   2: 7.021 ms/op
Iteration   3: 7.133 ms/op
Iteration   4: 6.975 ms/op
Iteration   5: 6.915 ms/op
Iteration   6: 6.898 ms/op
Iteration   7: 6.934 ms/op
Iteration   8: 6.939 ms/op
Iteration   9: 6.909 ms/op
Iteration  10: 6.839 ms/op
Iteration  11: 7.037 ms/op
Iteration  12: 7.005 ms/op
Iteration  13: 7.044 ms/op
Iteration  14: 7.009 ms/op
Iteration  15: 6.941 ms/op
Iteration  16: 6.860 ms/op
Iteration  17: 6.848 ms/op
Iteration  18: 6.869 ms/op
Iteration  19: 6.893 ms/op
Iteration  20: 6.874 ms/op


Result "com.springboot.AppTest.testGetPojo":
  6.972 ±(99.9%) 0.053 ms/op [Average]
  (min, avg, max) = (6.839, 6.972, 7.405), stdev = 0.094
  CI (99.9%): [6.919, 7.025] (assumes normal distribution)


# Run complete. Total time: 00:07:07

REMEMBER: The numbers below are just data. To gain reusable insights, you need to follow up on
why the numbers are the way they are. Use profilers (see -prof, -lprof), design factorial
experiments, perform baseline and negative tests that provide experimental control, make sure
the benchmarking environment is safe on JVM/OS/HW level, ask for reviews from the domain experts.
Do not assume the numbers tell you what you want them to tell.

Benchmark            Mode  Cnt  Score   Error  Units
AppTest.testGetPojo  avgt   40  6.972 ± 0.053  ms/op

Process finished with exit code 0

结果:最小值:6.839(最理想值)毫秒,  均值6.972    最大值(最坏情况)7.405

 

3、测试数据

3.1、数据存储方式

{"confirm":7,"dead":0,"heal":2,"weight":6.6,"mapId":"c320100_2","updateTime":"2020-03-03 07:20:39","name":"玄武区"}

  3.1.1 存到redis缓存中,取出来时候是在java这边是以LinkedHashMap(基于链表方式存储的Hash)

springboot使用jmh基准测试评估json反序列化、实体转换的性能差异_第3张图片

 

 

springboot使用jmh基准测试评估json反序列化、实体转换的性能差异_第4张图片

 

 

 

"{\"id\":999,\"distId\":999,\"distName\":\"玄武区\",\"confirm\":7,\"suspect\":0,\"dead\":0,\"heal\":2,\"weight\":6.6,\"level\":\"area\",\"mapId\":\"c320100_2\",\"updateTime\":\"2020-03-03 07:20:39\"}"

3.1.2、从内存中取出来,在java这里是以String存储的

 

 

 

 

测试代码

RedisTest.java
package com.springboot;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.springboot.entity.SnapShot;
import com.springboot.service.CacheService;
import org.junit.Before;
import org.junit.jupiter.api.Test;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ConfigurableApplicationContext;

import java.util.Iterator;
import java.util.Map;
@SpringBootTest(classes = App.class)
public class RedisTest {
    private ConfigurableApplicationContext context;

    final Gson gson = new Gson();
    ObjectMapper mapper = new ObjectMapper();
ModelMapper modelMapper
= new ModelMapper(); @Autowired CacheService cacheService; /** * setup初始化容器的时候只执行一次 */ @Before public void init(){ context = SpringApplication.run(App.class); cacheService = context.getBean(CacheService.class); } @Test public void testGetPojo() { Map map = cacheService.getCacheMap("snapshot", SnapShot.class); System.out.println(map); cacheService.setCacheMap("snapshot1",map); } @Test public void testMapToPojo() { Map map = cacheService.getCacheMap("snapshot1"); for(Iterator> it = map.entrySet().iterator();it.hasNext();) { Map.Entry entry = it.next(); SnapShot snapshot = modelMapper.map(entry, SnapShot.class); } } }

 

4、结论

不使用转义字符的 json形式

{"confirm":7,"dead":0,"heal":2,"weight":6.6,"mapId":"c320100_2","updateTime":"2020-03-03 07:20:39","name":"玄武区"}

 

比使用转义字符的json:

"{\"id\":999,\"distId\":999,\"distName\":\"玄武区\",\"confirm\":7,\"suspect\":0,\"dead\":0,\"heal\":2,\"weight\":6.6,\"level\":\"area\",\"mapId\":\"c320100_2\",\"updateTime\":\"2020-03-03 07:20:39\"}"

转换成实体快。

 

同样的配置机器,使用不带转义字符的json形式存储,然后再用modelMapper反序列化实体, 均值快2.705毫秒,速度提升2.705/6.972=38.798%

 

笔者最终把上面的 Map getCacheMap(String key, Class clazz);重构成以下代码

    @Override
    public  Map getCacheMap(String key, Class clazz) {
        Map raw = redisTemplate.opsForHash().entries(key);
        Map map = Maps.newLinkedHashMapWithExpectedSize(raw.size());

        for (Map.Entry entry : raw.entrySet()) {
            final String hashKey = entry.getKey();
            final Object value = entry.getValue();

            /***
             * 假设redis中hash值类型存储的hashValue是不带转义字符的json,形如:
             * {"confirm":7,"dead":0,"heal":2,"weight":6.6,"mapId":"c320100_2","updateTime":"2020-03-03 07:20:39","name":"玄武区"}
             * spring redis template(jedis)会把他转换成LinkedHashMap,这个时候利用ModelMapper将其转换成目标实体类型,速度比带转义字符类型快38.798%
            * */
            if(value instanceof Map)
            {
                map.put(hashKey,modelMapper.map(value, clazz));
                //处理带转义字符类型的json字符串
                //"{\"id\":999,\"distId\":999,\"name\":\"玄武区\",\"confirm\":7,\"suspect\":0,\"dead\":0,\"heal\":2,\"weight\":6.6,\"level\":\"area\",\"mapId\":\"c320100_2\",\"updateTime\":\"2020-03-03 07:20:39\"}"
            }else if(value instanceof String) {
                map.put(hashKey, gson.fromJson((String)value, clazz));
            }
        }
        return map;
    }

 

你可能感兴趣的:(springboot使用jmh基准测试评估json反序列化、实体转换的性能差异)