package com.db.service.framemark.bean;

import com.db.service.model.TTest;
import com.elab.core.spring.common.utils.BeanUtils;
import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.impl.DefaultMapperFactory;
import org.junit.Test;

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

/**
 * @author : liukx
 * @create : 2018/9/19 15:54
 * @email : liukx@elab-plus.com
 */

public class BeanCopyTest {
    private MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();


    public void beanCreateTest(){





    }



    @Test
    public void concurrentTest() throws InvocationTargetException, IllegalAccessException {
        TTest test = new TTest();
        test.setTestId("1");
        test.setUsername("asdfasd");
        test.setId(1);
        test.setName("ggg");
        test.setGirlName("asdfasd");
        test.setGirlStatus("aa");
        test.setTime(new Date());
        test.setSex("man");

        TTest t2 = new TTest();

        int count = 520000;

        // testCommon(test, t2, count);

        System.out.println("-----------spring----------------");

        testSpring(test, t2, count);
        System.out.println("-----------orika----------------");
        testOrika(test, t2, count);

    }

    private void testCommon(TTest test, TTest t2, int count) throws IllegalAccessException, InvocationTargetException {
        long start = System.currentTimeMillis();
        for (int i = 0; i < count; i++) {
            BeanUtils.copyProperties(test, t2);
        }
        long time = System.currentTimeMillis() - start;
        System.out.println(time);
    }

    private void testSpring(TTest test, TTest t2, int count) {
        long start = System.currentTimeMillis();
        for (int i = 0; i < count; i++) {
            org.springframework.beans.BeanUtils.copyProperties(test, t2);
            if (i == 0) {
                System.out.println(test.toString());
            }
        }
        long time = System.currentTimeMillis() - start;
        System.out.println(time);
    }

    private void testOrika(TTest test, TTest t2, int count) {
//        ClassMapBuilder<TTest, TTest> tTestTTestClassMapBuilder = mapperFactory.classMap(TTest.class, TTest.class);
//        tTestTTestClassMapBuilder.register();
        long start = System.currentTimeMillis();
        for (int i = 0; i < count; i++) {
//            tTestTTestClassMapBuilder.
            TTest map = mapperFactory.getMapperFacade().map(test, TTest.class);
            if (i == 0) {
                System.out.println(map.toString());
            }
        }
        long time = System.currentTimeMillis() - start;
        System.out.println(time);
    }


}
