Bean Copy性能對比

一、前提

  • bean 有10個屬性

各方法對應(yīng)組件及版本說明:

方法名 所屬模塊 模塊版本號
apacheBeanUtilsCopyTest commons-beanutils 1.9.4
springBeanUtilsCopyTest spring-beans 5.1.12.RELEASE
cglibBeanCopierTest cglib 3.3.0
springBeanCopierTest spring-core 5.1.12.RELEASE
getSetTest jdk 8
mapstructTest mapstruct 1.3.1.Final

二、結(jié)論

對比常用幾種bean copy得出如下結(jié)論:

  • 通過Set/Get、cglib bean copier、mapstruct進行屬性copy,其性能基本持平。
  • spring core包中的bean copier的copy相比上面三種,性能略有下降,但不大。
  • spring bean包中的bean utils的copy相比上面四種,性能有較大差距,性能大概慢21.5倍左右。
  • 性能最差為apache common-beanutils的bean utils的copy,性能和最高差距大概 慢460倍,比倒數(shù)第二慢21倍。

三、測試結(jié)果:

Benchmark                              Mode  Cnt       Score       Error   Units
BeanCopyTest.apacheBeanUtilsCopyTest  thrpt    9     542.549 ±    43.868  ops/ms
BeanCopyTest.cglibBeanCopierTest      thrpt    9  250689.491 ±  3690.880  ops/ms
BeanCopyTest.getSetTest               thrpt    9  250695.090 ±  4487.843  ops/ms
BeanCopyTest.mapstructTest            thrpt    9  249712.148 ±  4686.468  ops/ms
BeanCopyTest.springBeanCopierTest     thrpt    9  238908.247 ± 37465.537  ops/ms
BeanCopyTest.springBeanUtilsCopyTest  thrpt    9   11528.375 ±   797.580  ops/ms

四、測試代碼

Person/Person內(nèi)只有10個屬性,名稱一致,太簡單不再放出,測試代碼如下:

import net.sf.cglib.beans.BeanCopier;
import org.apache.commons.beanutils.BeanUtils;
import org.openjdk.jmh.annotations.*;


import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.concurrent.TimeUnit;

@State(Scope.Benchmark)
@BenchmarkMode(value = Mode.Throughput)
@Warmup(iterations = 1)
@Measurement(iterations = 3, time = 5, timeUnit = TimeUnit.SECONDS)
@Threads(10)
@Fork(3)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public class BeanCopyTest {
    private Person sourcePerson;
    private BeanCopier beanCopier = BeanCopier.create(Person.class, Person2.class, false);
    private org.springframework.cglib.beans.BeanCopier beanCopier2 = org.springframework.cglib.beans.BeanCopier.create(
            Person.class, Person2.class, false);
    @Setup
    public void init() {
        sourcePerson = new Person();
        sourcePerson.setId(1000021L);
        sourcePerson.setCode("IM-Test-1001");
        sourcePerson.setName("小明");
        sourcePerson.setCountry("中國");
        sourcePerson.setCity("上海");
        sourcePerson.setEmail("xiaoming.wang@demo.com");
        sourcePerson.setMobile("18810088888");
        sourcePerson.setPosition("閑雜人員");
        sourcePerson.setUserType(2);
        sourcePerson.setBirthday(new Date(1990, 10, 24));
    }

    @Benchmark
    public Person2 apacheBeanUtilsCopyTest() throws InvocationTargetException, IllegalAccessException {
        Person2 target = new Person2();
        BeanUtils.copyProperties(target, sourcePerson);
        return target;
    }

    @Benchmark
    public Person2 springBeanUtilsCopyTest() {
        Person2 target = new Person2();
        org.springframework.beans.BeanUtils.copyProperties(sourcePerson, target);
        return target;
    }

    @Benchmark
    public Person2 cglibBeanCopierTest() {
        Person2 target = new Person2();
        beanCopier.copy(sourcePerson, target, null);
        return target;
    }

    @Benchmark
    public Person2 springBeanCopierTest() {
        Person2 target = new Person2();
        beanCopier2.copy(sourcePerson, target, null);
        return target;
    }

    @Benchmark
    public Person2 getSetTest() {
        Person2 target = new Person2();
        target.setId(sourcePerson.getId());
        target.setBirthday(sourcePerson.getBirthday());
        target.setCity(sourcePerson.getCity());
        target.setCode(sourcePerson.getCode());
        target.setCountry(sourcePerson.getCountry());
        target.setEmail(sourcePerson.getEmail());
        target.setMobile(sourcePerson.getMobile());
        target.setUserType(sourcePerson.getUserType());
        target.setPosition(sourcePerson.getPosition());
        target.setName(sourcePerson.getName());
        return target;
    }

    @Benchmark
    public Person2 mapstructTest() {
        Person2  target = PersonMapping.INSTANCE.castToPerson2(sourcePerson);
        return target;
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容