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条
分别将snapshot1,snapshot转换成实体,现在将对他们进行基准测试,以评测两种存储方式的性能差异
2、基准测试
2个独立进程,分别依1次热身/热加载,批量执行20次,总计40次
2.1、snapshot1相关测试
这里用的实体转换框架是ModelMapper,官网:http://modelmapper.org/ 其性能比较高,主要用于实体属性之间的拷贝,
是笔者工作中项目实践中通过对比,选择到的这个框架。
maven依赖
<dependency> <groupId>org.modelmapper</groupId> <artifactId>modelmapper</artifactId> <version>0.7.7</version> </dependency>
以下modelMapper转换成实体代码要加
modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
不同的方法会影响基准测试的最终结果
@Benchmark public void testMapToPojo() { ModelMapper modelMapper = new ModelMapper(); Map<String, Map> map = cacheService.getCacheMap("snapshot1"); Map<String,SnapShot> result = new HashMap<>(); for(Iterator<Map.Entry<String, Map>> it = map.entrySet().iterator(); it.hasNext();) { Map.Entry<String, Map> entry = it.next(); String key = entry.getKey(); SnapShot snapshot = modelMapper.map(entry, SnapShot.class); result.put(key, snapshot); } }
/** * 获得缓存的Map * * @param key * @return */ @Override public <T> Map<String, T> getCacheMap(String key) { Map<String, T> map = redisTemplate.opsForHash().entries(key); return map; }
测试日志:
D:developjavajdk1.8.0_181injava.exe "-javaagent:D:Program FilesJetBrainsIntelliJ IDEA 2018.3.4libidea_rt.jar=52204:D:Program FilesJetBrainsIntelliJ IDEA 2018.3.4in" -Dfile.encoding=UTF-8 -classpath D:developjavajdk1.8.0_181jrelibcharsets.jar;D:developjavajdk1.8.0_181jrelibdeploy.jar;D:developjavajdk1.8.0_181jrelibextaccess-bridge-64.jar;D:developjavajdk1.8.0_181jrelibextcldrdata.jar;D:developjavajdk1.8.0_181jrelibextdnsns.jar;D:developjavajdk1.8.0_181jrelibextjaccess.jar;D:developjavajdk1.8.0_181jrelibextjfxrt.jar;D:developjavajdk1.8.0_181jrelibextlocaledata.jar;D:developjavajdk1.8.0_181jrelibext ashorn.jar;D:developjavajdk1.8.0_181jrelibextsunec.jar;D:developjavajdk1.8.0_181jrelibextsunjce_provider.jar;D:developjavajdk1.8.0_181jrelibextsunmscapi.jar;D:developjavajdk1.8.0_181jrelibextsunpkcs11.jar;D:developjavajdk1.8.0_181jrelibextzipfs.jar;D:developjavajdk1.8.0_181jrelibjavaws.jar;D:developjavajdk1.8.0_181jrelibjce.jar;D:developjavajdk1.8.0_181jrelibjfr.jar;D:developjavajdk1.8.0_181jrelibjfxswt.jar;D:developjavajdk1.8.0_181jrelibjsse.jar;D:developjavajdk1.8.0_181jrelibmanagement-agent.jar;D:developjavajdk1.8.0_181jrelibplugin.jar;D:developjavajdk1.8.0_181jrelib esources.jar;D:developjavajdk1.8.0_181jrelib t.jar;E:projectjavaspringBootJmh arget est-classes;E:projectjavaspringBootJmh argetclasses;D:developapache-maven-3.5.4 epoorgapachecommonscommons-pool22.5.0commons-pool2-2.5.0.jar;D:developapache-maven-3.5.4 epoorgspringframeworkootspring-boot-starter-cache2.2.4.RELEASEspring-boot-starter-cache-2.2.4.RELEASE.jar;D:developapache-maven-3.5.4 epoorgspringframeworkootspring-boot-starter2.2.4.RELEASEspring-boot-starter-2.2.4.RELEASE.jar;D:developapache-maven-3.5.4 epoorgspringframeworkootspring-boot2.2.4.RELEASEspring-boot-2.2.4.RELEASE.jar;D:developapache-maven-3.5.4 epoorgspringframeworkootspring-boot-autoconfigure2.2.4.RELEASEspring-boot-autoconfigure-2.2.4.RELEASE.jar;D:developapache-maven-3.5.4 epoorgspringframeworkootspring-boot-starter-logging2.2.4.RELEASEspring-boot-starter-logging-2.2.4.RELEASE.jar;D:developapache-maven-3.5.4 epochqoslogbacklogback-classic1.2.3logback-classic-1.2.3.jar;D:developapache-maven-3.5.4 epochqoslogbacklogback-core1.2.3logback-core-1.2.3.jar;D:developapache-maven-3.5.4 epoorgapachelogginglog4jlog4j-to-slf4j2.12.1log4j-to-slf4j-2.12.1.jar;D:developapache-maven-3.5.4 epoorgapachelogginglog4jlog4j-api2.12.1log4j-api-2.12.1.jar;D:developapache-maven-3.5.4 epoorgslf4jjul-to-slf4j1.7.30jul-to-slf4j-1.7.30.jar;D:developapache-maven-3.5.4 epojakartaannotationjakarta.annotation-api1.3.5jakarta.annotation-api-1.3.5.jar;D:developapache-maven-3.5.4 epoorgyamlsnakeyaml1.25snakeyaml-1.25.jar;D:developapache-maven-3.5.4 epoorgspringframeworkspring-context-support5.2.3.RELEASEspring-context-support-5.2.3.RELEASE.jar;D:developapache-maven-3.5.4 epoorgspringframeworkspring-beans5.2.3.RELEASEspring-beans-5.2.3.RELEASE.jar;D:developapache-maven-3.5.4 epoorgspringframeworkspring-context5.2.3.RELEASEspring-context-5.2.3.RELEASE.jar;D:developapache-maven-3.5.4 epoorgspringframeworkspring-expression5.2.3.RELEASEspring-expression-5.2.3.RELEASE.jar;D:developapache-maven-3.5.4 epoorgspringframeworkootspring-boot-starter-data-redis2.2.4.RELEASEspring-boot-starter-data-redis-2.2.4.RELEASE.jar;D:developapache-maven-3.5.4 epoorgspringframeworkdataspring-data-redis2.2.4.RELEASEspring-data-redis-2.2.4.RELEASE.jar;D:developapache-maven-3.5.4 epoorgspringframeworkdataspring-data-keyvalue2.2.4.RELEASEspring-data-keyvalue-2.2.4.RELEASE.jar;D:developapache-maven-3.5.4 epoorgspringframeworkdataspring-data-commons2.2.4.RELEASEspring-data-commons-2.2.4.RELEASE.jar;D:developapache-maven-3.5.4 epoorgspringframeworkspring-tx5.2.3.RELEASEspring-tx-5.2.3.RELEASE.jar;D:developapache-maven-3.5.4 epoorgspringframeworkspring-oxm5.2.3.RELEASEspring-oxm-5.2.3.RELEASE.jar;D:developapache-maven-3.5.4 epoorgspringframeworkspring-aop5.2.3.RELEASEspring-aop-5.2.3.RELEASE.jar;D:developapache-maven-3.5.4 epoorgslf4jslf4j-api1.7.30slf4j-api-1.7.30.jar;D:developapache-maven-3.5.4 epoiolettucelettuce-core5.2.1.RELEASElettuce-core-5.2.1.RELEASE.jar;D:developapache-maven-3.5.4 epoio etty etty-common4.1.45.Final etty-common-4.1.45.Final.jar;D:developapache-maven-3.5.4 epoio etty etty-handler4.1.45.Final etty-handler-4.1.45.Final.jar;D:developapache-maven-3.5.4 epoio etty etty-buffer4.1.45.Final etty-buffer-4.1.45.Final.jar;D:developapache-maven-3.5.4 epoio etty etty-codec4.1.45.Final etty-codec-4.1.45.Final.jar;D:developapache-maven-3.5.4 epoio etty etty-transport4.1.45.Final etty-transport-4.1.45.Final.jar;D:developapache-maven-3.5.4 epoio etty etty-resolver4.1.45.Final etty-resolver-4.1.45.Final.jar;D:developapache-maven-3.5.4 epoioprojectreactor eactor-core3.3.2.RELEASE eactor-core-3.3.2.RELEASE.jar;D:developapache-maven-3.5.4 epoorg eactivestreams eactive-streams1.0.3 eactive-streams-1.0.3.jar;D:developapache-maven-3.5.4 epocomdyuprojectprotostuffprotostuff-runtime1.1.3protostuff-runtime-1.1.3.jar;D:developapache-maven-3.5.4 epocomdyuprojectprotostuffprotostuff-collectionschema1.1.3protostuff-collectionschema-1.1.3.jar;D:developapache-maven-3.5.4 epocomdyuprojectprotostuffprotostuff-api1.0.8protostuff-api-1.0.8.jar;D:developapache-maven-3.5.4 epocomdyuprojectprotostuffprotostuff-core1.0.8protostuff-core-1.0.8.jar;D:developapache-maven-3.5.4 epoorgprojectlomboklombok1.18.10lombok-1.18.10.jar;D:developapache-maven-3.5.4 epoorgspringframeworkootspring-boot-starter-test2.2.4.RELEASEspring-boot-starter-test-2.2.4.RELEASE.jar;D:developapache-maven-3.5.4 epoorgspringframeworkootspring-boot-test2.2.4.RELEASEspring-boot-test-2.2.4.RELEASE.jar;D:developapache-maven-3.5.4 epoorgspringframeworkootspring-boot-test-autoconfigure2.2.4.RELEASEspring-boot-test-autoconfigure-2.2.4.RELEASE.jar;D:developapache-maven-3.5.4 epocomjaywayjsonpathjson-path2.4.0json-path-2.4.0.jar;D:developapache-maven-3.5.4 epo etminidevjson-smart2.3json-smart-2.3.jar;D:developapache-maven-3.5.4 epo etminidevaccessors-smart1.2accessors-smart-1.2.jar;D:developapache-maven-3.5.4 epoorgow2asmasm5.0.4asm-5.0.4.jar;D:developapache-maven-3.5.4 epojakartaxmlindjakarta.xml.bind-api2.3.2jakarta.xml.bind-api-2.3.2.jar;D:developapache-maven-3.5.4 epojakartaactivationjakarta.activation-api1.2.1jakarta.activation-api-1.2.1.jar;D:developapache-maven-3.5.4 epoorgjunitjupiterjunit-jupiter5.5.2junit-jupiter-5.5.2.jar;D:developapache-maven-3.5.4 epoorgjunitjupiterjunit-jupiter-api5.5.2junit-jupiter-api-5.5.2.jar;D:developapache-maven-3.5.4 epoorgapiguardianapiguardian-api1.1.0apiguardian-api-1.1.0.jar;D:developapache-maven-3.5.4 epoorgopentest4jopentest4j1.2.0opentest4j-1.2.0.jar;D:developapache-maven-3.5.4 epoorgjunitplatformjunit-platform-commons1.5.2junit-platform-commons-1.5.2.jar;D:developapache-maven-3.5.4 epoorgjunitjupiterjunit-jupiter-params5.5.2junit-jupiter-params-5.5.2.jar;D:developapache-maven-3.5.4 epoorgjunitjupiterjunit-jupiter-engine5.5.2junit-jupiter-engine-5.5.2.jar;D:developapache-maven-3.5.4 epoorgjunitplatformjunit-platform-engine1.5.2junit-platform-engine-1.5.2.jar;D:developapache-maven-3.5.4 epoorgmockitomockito-junit-jupiter3.1.0mockito-junit-jupiter-3.1.0.jar;D:developapache-maven-3.5.4 epoorgassertjassertj-core3.13.2assertj-core-3.13.2.jar;D:developapache-maven-3.5.4 epoorghamcresthamcrest2.1hamcrest-2.1.jar;D:developapache-maven-3.5.4 epoorgmockitomockito-core3.1.0mockito-core-3.1.0.jar;D:developapache-maven-3.5.4 epo etytebuddybyte-buddy1.10.6byte-buddy-1.10.6.jar;D:developapache-maven-3.5.4 epo etytebuddybyte-buddy-agent1.10.6byte-buddy-agent-1.10.6.jar;D:developapache-maven-3.5.4 epoorgobjenesisobjenesis2.6objenesis-2.6.jar;D:developapache-maven-3.5.4 epoorgskyscreamerjsonassert1.5.0jsonassert-1.5.0.jar;D:developapache-maven-3.5.4 epocomvaadinexternalgoogleandroid-json .0.20131108.vaadin1android-json-0.0.20131108.vaadin1.jar;D:developapache-maven-3.5.4 epoorgspringframeworkspring-core5.2.3.RELEASEspring-core-5.2.3.RELEASE.jar;D:developapache-maven-3.5.4 epoorgspringframeworkspring-jcl5.2.3.RELEASEspring-jcl-5.2.3.RELEASE.jar;D:developapache-maven-3.5.4 epoorgspringframeworkspring-test5.2.3.RELEASEspring-test-5.2.3.RELEASE.jar;D:developapache-maven-3.5.4 epoorgxmlunitxmlunit-core2.6.3xmlunit-core-2.6.3.jar;D:developapache-maven-3.5.4 epocomgoogleguavaguava20.0guava-20.0.jar;D:developapache-maven-3.5.4 epocomgooglecodegsongson2.8.4gson-2.8.4.jar;D:developapache-maven-3.5.4 epoorgmodelmappermodelmapper .7.7modelmapper-0.7.7.jar;D:developapache-maven-3.5.4 epoorgopenjdkjmhjmh-core1.21jmh-core-1.21.jar;D:developapache-maven-3.5.4 epo etsfjopt-simplejopt-simple4.6jopt-simple-4.6.jar;D:developapache-maven-3.5.4 epoorgapachecommonscommons-math33.2commons-math3-3.2.jar;D:developapache-maven-3.5.4 epoorgopenjdkjmhjmh-generator-annprocess1.21jmh-generator-annprocess-1.21.jar;D:developapache-maven-3.5.4 epocomfasterxmljacksoncorejackson-annotations2.10.2jackson-annotations-2.10.2.jar;D:developapache-maven-3.5.4 epocomfasterxmljacksoncorejackson-databind2.10.2jackson-databind-2.10.2.jar;D:developapache-maven-3.5.4 epocomfasterxmljacksoncorejackson-core2.10.2jackson-core-2.10.2.jar;D:developapache-maven-3.5.4 epocomfasterxmljacksondatatypejackson-datatype-jsr3102.10.2jackson-datatype-jsr310-2.10.2.jar;D:developapache-maven-3.5.4 epocommons-beanutilscommons-beanutils1.9.3commons-beanutils-1.9.3.jar;D:developapache-maven-3.5.4 epocommons-loggingcommons-logging1.2commons-logging-1.2.jar;D:developapache-maven-3.5.4 epocommons-collectionscommons-collections3.2.2commons-collections-3.2.2.jar;D:developapache-maven-3.5.4 epojunitjunit4.12junit-4.12.jar;D:developapache-maven-3.5.4 epoorghamcresthamcrest-core2.1hamcrest-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:developjavajdk1.8.0_181jreinjava.exe # VM options: -javaagent:D:Program FilesJetBrainsIntelliJ IDEA 2018.3.4libidea_rt.jar=52204:D:Program FilesJetBrainsIntelliJ IDEA 2018.3.4in -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:projectjavaspringBootJmh) 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:projectjavaspringBootJmh) 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<String, SnapShot> map = cacheService.getCacheMap("snapshot", SnapShot.class); }
@Override public <T> Map<String, T> getCacheMap(String key, Class<T> clazz) { Map<String, String> raw = redisTemplate.opsForHash().entries(key); Map<String, T> map = Maps.newLinkedHashMapWithExpectedSize(raw.size()); for (Map.Entry<String, String> entry : raw.entrySet()) { String hashKey = entry.getKey(); String value = entry.getValue(); map.put(hashKey, gson.fromJson(value, clazz)); } return map; }
测试日志:
D:developjavajdk1.8.0_181injava.exe "-javaagent:D:Program FilesJetBrainsIntelliJ IDEA 2018.3.4libidea_rt.jar=52899:D:Program FilesJetBrainsIntelliJ IDEA 2018.3.4in" -Dfile.encoding=UTF-8 -classpath D:developjavajdk1.8.0_181jrelibcharsets.jar;D:developjavajdk1.8.0_181jrelibdeploy.jar;D:developjavajdk1.8.0_181jrelibextaccess-bridge-64.jar;D:developjavajdk1.8.0_181jrelibextcldrdata.jar;D:developjavajdk1.8.0_181jrelibextdnsns.jar;D:developjavajdk1.8.0_181jrelibextjaccess.jar;D:developjavajdk1.8.0_181jrelibextjfxrt.jar;D:developjavajdk1.8.0_181jrelibextlocaledata.jar;D:developjavajdk1.8.0_181jrelibext ashorn.jar;D:developjavajdk1.8.0_181jrelibextsunec.jar;D:developjavajdk1.8.0_181jrelibextsunjce_provider.jar;D:developjavajdk1.8.0_181jrelibextsunmscapi.jar;D:developjavajdk1.8.0_181jrelibextsunpkcs11.jar;D:developjavajdk1.8.0_181jrelibextzipfs.jar;D:developjavajdk1.8.0_181jrelibjavaws.jar;D:developjavajdk1.8.0_181jrelibjce.jar;D:developjavajdk1.8.0_181jrelibjfr.jar;D:developjavajdk1.8.0_181jrelibjfxswt.jar;D:developjavajdk1.8.0_181jrelibjsse.jar;D:developjavajdk1.8.0_181jrelibmanagement-agent.jar;D:developjavajdk1.8.0_181jrelibplugin.jar;D:developjavajdk1.8.0_181jrelib esources.jar;D:developjavajdk1.8.0_181jrelib t.jar;E:projectjavaspringBootJmh arget est-classes;E:projectjavaspringBootJmh argetclasses;D:developapache-maven-3.5.4 epoorgapachecommonscommons-pool22.5.0commons-pool2-2.5.0.jar;D:developapache-maven-3.5.4 epoorgspringframeworkootspring-boot-starter-cache2.2.4.RELEASEspring-boot-starter-cache-2.2.4.RELEASE.jar;D:developapache-maven-3.5.4 epoorgspringframeworkootspring-boot-starter2.2.4.RELEASEspring-boot-starter-2.2.4.RELEASE.jar;D:developapache-maven-3.5.4 epoorgspringframeworkootspring-boot2.2.4.RELEASEspring-boot-2.2.4.RELEASE.jar;D:developapache-maven-3.5.4 epoorgspringframeworkootspring-boot-autoconfigure2.2.4.RELEASEspring-boot-autoconfigure-2.2.4.RELEASE.jar;D:developapache-maven-3.5.4 epoorgspringframeworkootspring-boot-starter-logging2.2.4.RELEASEspring-boot-starter-logging-2.2.4.RELEASE.jar;D:developapache-maven-3.5.4 epochqoslogbacklogback-classic1.2.3logback-classic-1.2.3.jar;D:developapache-maven-3.5.4 epochqoslogbacklogback-core1.2.3logback-core-1.2.3.jar;D:developapache-maven-3.5.4 epoorgapachelogginglog4jlog4j-to-slf4j2.12.1log4j-to-slf4j-2.12.1.jar;D:developapache-maven-3.5.4 epoorgapachelogginglog4jlog4j-api2.12.1log4j-api-2.12.1.jar;D:developapache-maven-3.5.4 epoorgslf4jjul-to-slf4j1.7.30jul-to-slf4j-1.7.30.jar;D:developapache-maven-3.5.4 epojakartaannotationjakarta.annotation-api1.3.5jakarta.annotation-api-1.3.5.jar;D:developapache-maven-3.5.4 epoorgyamlsnakeyaml1.25snakeyaml-1.25.jar;D:developapache-maven-3.5.4 epoorgspringframeworkspring-context-support5.2.3.RELEASEspring-context-support-5.2.3.RELEASE.jar;D:developapache-maven-3.5.4 epoorgspringframeworkspring-beans5.2.3.RELEASEspring-beans-5.2.3.RELEASE.jar;D:developapache-maven-3.5.4 epoorgspringframeworkspring-context5.2.3.RELEASEspring-context-5.2.3.RELEASE.jar;D:developapache-maven-3.5.4 epoorgspringframeworkspring-expression5.2.3.RELEASEspring-expression-5.2.3.RELEASE.jar;D:developapache-maven-3.5.4 epoorgspringframeworkootspring-boot-starter-data-redis2.2.4.RELEASEspring-boot-starter-data-redis-2.2.4.RELEASE.jar;D:developapache-maven-3.5.4 epoorgspringframeworkdataspring-data-redis2.2.4.RELEASEspring-data-redis-2.2.4.RELEASE.jar;D:developapache-maven-3.5.4 epoorgspringframeworkdataspring-data-keyvalue2.2.4.RELEASEspring-data-keyvalue-2.2.4.RELEASE.jar;D:developapache-maven-3.5.4 epoorgspringframeworkdataspring-data-commons2.2.4.RELEASEspring-data-commons-2.2.4.RELEASE.jar;D:developapache-maven-3.5.4 epoorgspringframeworkspring-tx5.2.3.RELEASEspring-tx-5.2.3.RELEASE.jar;D:developapache-maven-3.5.4 epoorgspringframeworkspring-oxm5.2.3.RELEASEspring-oxm-5.2.3.RELEASE.jar;D:developapache-maven-3.5.4 epoorgspringframeworkspring-aop5.2.3.RELEASEspring-aop-5.2.3.RELEASE.jar;D:developapache-maven-3.5.4 epoorgslf4jslf4j-api1.7.30slf4j-api-1.7.30.jar;D:developapache-maven-3.5.4 epoiolettucelettuce-core5.2.1.RELEASElettuce-core-5.2.1.RELEASE.jar;D:developapache-maven-3.5.4 epoio etty etty-common4.1.45.Final etty-common-4.1.45.Final.jar;D:developapache-maven-3.5.4 epoio etty etty-handler4.1.45.Final etty-handler-4.1.45.Final.jar;D:developapache-maven-3.5.4 epoio etty etty-buffer4.1.45.Final etty-buffer-4.1.45.Final.jar;D:developapache-maven-3.5.4 epoio etty etty-codec4.1.45.Final etty-codec-4.1.45.Final.jar;D:developapache-maven-3.5.4 epoio etty etty-transport4.1.45.Final etty-transport-4.1.45.Final.jar;D:developapache-maven-3.5.4 epoio etty etty-resolver4.1.45.Final etty-resolver-4.1.45.Final.jar;D:developapache-maven-3.5.4 epoioprojectreactor eactor-core3.3.2.RELEASE eactor-core-3.3.2.RELEASE.jar;D:developapache-maven-3.5.4 epoorg eactivestreams eactive-streams1.0.3 eactive-streams-1.0.3.jar;D:developapache-maven-3.5.4 epocomdyuprojectprotostuffprotostuff-runtime1.1.3protostuff-runtime-1.1.3.jar;D:developapache-maven-3.5.4 epocomdyuprojectprotostuffprotostuff-collectionschema1.1.3protostuff-collectionschema-1.1.3.jar;D:developapache-maven-3.5.4 epocomdyuprojectprotostuffprotostuff-api1.0.8protostuff-api-1.0.8.jar;D:developapache-maven-3.5.4 epocomdyuprojectprotostuffprotostuff-core1.0.8protostuff-core-1.0.8.jar;D:developapache-maven-3.5.4 epoorgprojectlomboklombok1.18.10lombok-1.18.10.jar;D:developapache-maven-3.5.4 epoorgspringframeworkootspring-boot-starter-test2.2.4.RELEASEspring-boot-starter-test-2.2.4.RELEASE.jar;D:developapache-maven-3.5.4 epoorgspringframeworkootspring-boot-test2.2.4.RELEASEspring-boot-test-2.2.4.RELEASE.jar;D:developapache-maven-3.5.4 epoorgspringframeworkootspring-boot-test-autoconfigure2.2.4.RELEASEspring-boot-test-autoconfigure-2.2.4.RELEASE.jar;D:developapache-maven-3.5.4 epocomjaywayjsonpathjson-path2.4.0json-path-2.4.0.jar;D:developapache-maven-3.5.4 epo etminidevjson-smart2.3json-smart-2.3.jar;D:developapache-maven-3.5.4 epo etminidevaccessors-smart1.2accessors-smart-1.2.jar;D:developapache-maven-3.5.4 epoorgow2asmasm5.0.4asm-5.0.4.jar;D:developapache-maven-3.5.4 epojakartaxmlindjakarta.xml.bind-api2.3.2jakarta.xml.bind-api-2.3.2.jar;D:developapache-maven-3.5.4 epojakartaactivationjakarta.activation-api1.2.1jakarta.activation-api-1.2.1.jar;D:developapache-maven-3.5.4 epoorgjunitjupiterjunit-jupiter5.5.2junit-jupiter-5.5.2.jar;D:developapache-maven-3.5.4 epoorgjunitjupiterjunit-jupiter-api5.5.2junit-jupiter-api-5.5.2.jar;D:developapache-maven-3.5.4 epoorgapiguardianapiguardian-api1.1.0apiguardian-api-1.1.0.jar;D:developapache-maven-3.5.4 epoorgopentest4jopentest4j1.2.0opentest4j-1.2.0.jar;D:developapache-maven-3.5.4 epoorgjunitplatformjunit-platform-commons1.5.2junit-platform-commons-1.5.2.jar;D:developapache-maven-3.5.4 epoorgjunitjupiterjunit-jupiter-params5.5.2junit-jupiter-params-5.5.2.jar;D:developapache-maven-3.5.4 epoorgjunitjupiterjunit-jupiter-engine5.5.2junit-jupiter-engine-5.5.2.jar;D:developapache-maven-3.5.4 epoorgjunitplatformjunit-platform-engine1.5.2junit-platform-engine-1.5.2.jar;D:developapache-maven-3.5.4 epoorgmockitomockito-junit-jupiter3.1.0mockito-junit-jupiter-3.1.0.jar;D:developapache-maven-3.5.4 epoorgassertjassertj-core3.13.2assertj-core-3.13.2.jar;D:developapache-maven-3.5.4 epoorghamcresthamcrest2.1hamcrest-2.1.jar;D:developapache-maven-3.5.4 epoorgmockitomockito-core3.1.0mockito-core-3.1.0.jar;D:developapache-maven-3.5.4 epo etytebuddybyte-buddy1.10.6byte-buddy-1.10.6.jar;D:developapache-maven-3.5.4 epo etytebuddybyte-buddy-agent1.10.6byte-buddy-agent-1.10.6.jar;D:developapache-maven-3.5.4 epoorgobjenesisobjenesis2.6objenesis-2.6.jar;D:developapache-maven-3.5.4 epoorgskyscreamerjsonassert1.5.0jsonassert-1.5.0.jar;D:developapache-maven-3.5.4 epocomvaadinexternalgoogleandroid-json .0.20131108.vaadin1android-json-0.0.20131108.vaadin1.jar;D:developapache-maven-3.5.4 epoorgspringframeworkspring-core5.2.3.RELEASEspring-core-5.2.3.RELEASE.jar;D:developapache-maven-3.5.4 epoorgspringframeworkspring-jcl5.2.3.RELEASEspring-jcl-5.2.3.RELEASE.jar;D:developapache-maven-3.5.4 epoorgspringframeworkspring-test5.2.3.RELEASEspring-test-5.2.3.RELEASE.jar;D:developapache-maven-3.5.4 epoorgxmlunitxmlunit-core2.6.3xmlunit-core-2.6.3.jar;D:developapache-maven-3.5.4 epocomgoogleguavaguava20.0guava-20.0.jar;D:developapache-maven-3.5.4 epocomgooglecodegsongson2.8.4gson-2.8.4.jar;D:developapache-maven-3.5.4 epoorgmodelmappermodelmapper .7.7modelmapper-0.7.7.jar;D:developapache-maven-3.5.4 epoorgopenjdkjmhjmh-core1.21jmh-core-1.21.jar;D:developapache-maven-3.5.4 epo etsfjopt-simplejopt-simple4.6jopt-simple-4.6.jar;D:developapache-maven-3.5.4 epoorgapachecommonscommons-math33.2commons-math3-3.2.jar;D:developapache-maven-3.5.4 epoorgopenjdkjmhjmh-generator-annprocess1.21jmh-generator-annprocess-1.21.jar;D:developapache-maven-3.5.4 epocomfasterxmljacksoncorejackson-annotations2.10.2jackson-annotations-2.10.2.jar;D:developapache-maven-3.5.4 epocomfasterxmljacksoncorejackson-databind2.10.2jackson-databind-2.10.2.jar;D:developapache-maven-3.5.4 epocomfasterxmljacksoncorejackson-core2.10.2jackson-core-2.10.2.jar;D:developapache-maven-3.5.4 epocomfasterxmljacksondatatypejackson-datatype-jsr3102.10.2jackson-datatype-jsr310-2.10.2.jar;D:developapache-maven-3.5.4 epocommons-beanutilscommons-beanutils1.9.3commons-beanutils-1.9.3.jar;D:developapache-maven-3.5.4 epocommons-loggingcommons-logging1.2commons-logging-1.2.jar;D:developapache-maven-3.5.4 epocommons-collectionscommons-collections3.2.2commons-collections-3.2.2.jar;D:developapache-maven-3.5.4 epojunitjunit4.12junit-4.12.jar;D:developapache-maven-3.5.4 epoorghamcresthamcrest-core2.1hamcrest-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:developjavajdk1.8.0_181jreinjava.exe # VM options: -javaagent:D:Program FilesJetBrainsIntelliJ IDEA 2018.3.4libidea_rt.jar=52899:D:Program FilesJetBrainsIntelliJ IDEA 2018.3.4in -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:projectjavaspringBootJmh) 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:projectjavaspringBootJmh) 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)
"{"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<String, SnapShot> map = cacheService.getCacheMap("snapshot", SnapShot.class); System.out.println(map); cacheService.setCacheMap("snapshot1",map); } @Test public void testMapToPojo() { Map<String, Map> map = cacheService.getCacheMap("snapshot1"); for(Iterator<Map.Entry<String, Map>> it = map.entrySet().iterator();it.hasNext();) { Map.Entry<String, Map> 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%
笔者最终把上面的<T> Map<String, T> getCacheMap(String key, Class<T> clazz);重构成以下代码
@Override public <T> Map<String, T> getCacheMap(String key, Class<T> clazz) { Map<String, Object> raw = redisTemplate.opsForHash().entries(key); Map<String, T> map = Maps.newLinkedHashMapWithExpectedSize(raw.size()); for (Map.Entry<String, Object> entry : raw.entrySet()) { final String hashKey = entry.getKey(); final Object value = entry.getValue(); /*** * 假设redis中hash值类型存储的hashValue是不带转义字符的json,形如: * <code>{"confirm":7,"dead":0,"heal":2,"weight":6.6,"mapId":"c320100_2","updateTime":"2020-03-03 07:20:39","name":"玄武区"}</code> * 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; }