java性能调优01

1、阿姆达尔定律

  1.1 加速比=优化后的耗时/优化前的耗时

  1.2 阿姆达尔定律   s<=1/F+(1-F)/N

    其中:s为加速比,F为程序的串行化比重,n为cpu处理核数

2、调优层次(设计调优、代码调优、JVM调优、数据库调优

  2.1 设计模式

    2.1.1 单列模式:常见应用spring注解,以及实现当前在线人数的统计

      基本要素:构造方法私有化,提供公共的获取实例的方法

public class SingleSessionUserList {

    private static final SingleSessionUserList sessionUserList=new SingleSessionUserList();
    
    //提供公共的方法接口
    public static SingleSessionUserList getInstance() {
        return sessionUserList;
    }
    
    //将构造方法私有化
    private SingleSessionUserList() {
    }
}

    2.1.2 代理模式

      核心思想:只有在真心需要时才进行初始化,否则只返回当前对象的代理对象

      基本要素:主题接口、真实主题、代理类

/**
 * IDbQuery 
 * @Description 主题接口
 */
public interface IDbQuery {
    //比较耗时的方法
    public String request();
}

/**
 * DbQuery 
 * @Description 真实主题
 */
public class DbQuery implements IDbQuery {
    @Override
    public String request() {
        return "比较耗时的操作结果";
    }
}

/**
 * DbQueryProxy 
 * @Description 代理类
 */
public class DbQueryProxy implements IDbQuery {
    private DbQuery dbQuery;//真实主题
    @Override
    public String request() {
        //真正需要使用DbQuery
        if (dbQuery==null) {
            dbQuery=new DbQuery();
        }return dbQuery.request();
    }
}

/**
 * ProxyTest
 * @Description 代理类的测试 
 */
public class ProxyTest {
    @Test
    public void testProxy() {
        IDbQuery dbQuery=new DbQueryProxy();
        dbQuery.request();
    }
}

    动态代理的实现

/**
 * JdkDbQueryHandler
 * @Description 使用jdk反射的反射获取动态代理
 */
public class JdkDbQueryHandler implements InvocationHandler {
    IDbQuery dbquery;
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        if (dbquery == null) {
            dbquery = new DbQuery();
        }
        //调用真实主题的方法
        return method.invoke(dbquery, args);
    }

    //获取动态代理对象
    public static IDbQuery getJdkDbQueryProxy() {
        IDbQuery jdkDbQueryProxy=(IDbQuery) Proxy.newProxyInstance(ClassLoader.getSystemClassLoader(),
                new Class[] { IDbQuery.class }, new JdkDbQueryHandler());
        return jdkDbQueryProxy;
    }
}

/**
 * CglibDbQueryHandler
 * @Description 使用cglib创建动态代理
 */
public class CglibDbQueryHandler implements MethodInterceptor {
    private DbQuery dbQuery;
    @Override
    public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy)
            throws Throwable {
        if (dbQuery==null) {
            dbQuery=new DbQuery();
        }
        //调用真实主题的方法
        return method.invoke(dbQuery, args);
    }
    
    //cglib方法获取动态代理对象
    public static IDbQuery getCglibDbQueryProxy() {
        Enhancer enhancer=new Enhancer();
        enhancer.setCallback(new CglibDbQueryHandler());
        enhancer.setInterfaces(new Class[]{IDbQuery.class});
        return (IDbQuery) enhancer.create();
    }   
}

     2.1.3 享元模式

      核心思想:如果系统中多次使用相同的对象,每次只需要获取对象的拷贝,不需要重新创建一个新的对象

/**
 * IReportManager
 * @Description 报表管理器
 */
public interface IReportManager {
    //生成报表
    public String createReport();
}

/**
 * FinancialReportManager
 * @Description 财务报表生成器实现类
 */
public class FinancialReportManager implements IReportManager {
    @Override
    public String createReport() {
        return "财务报表";
    }
}

/**
 * EmployeeReportManager
 * @Description 员工报表生成器
 */
public class EmployeeReportManager implements IReportManager {
    @Override
    public String createReport() {
        return "员工报表";
    }
}

/**
 * ReportManagerFactory
 * @Description 报表生成器工厂
 */
public class ReportManagerFactory { 
    Map<String, IReportManager> financialReportManagers=new HashMap<String, IReportManager>();
    Map<String, IReportManager> employeeReportManagers=new HashMap<String, IReportManager>();

    public IReportManager getFinancialReportManager(String tid) {
        IReportManager financialReportManager=financialReportManagers.get(tid);
        if (financialReportManager==null) {
            financialReportManager=new FinancialReportManager();
            financialReportManagers.put(tid, financialReportManager);
        }
        return financialReportManager;
    }
    
    public IReportManager getEmployeeReportManager(String tid) {
        IReportManager employeeReportManager=employeeReportManagers.get(tid);
        if (employeeReportManager==null) {
            employeeReportManager=new EmployeeReportManager();
            employeeReportManagers.put(tid, employeeReportManager);
        }
        return employeeReportManager;
    }
}

/**
 * ReportManagerFactoryTest
 * @Description 享元模式测试类
 */
public class ReportManagerFactoryTest {
    @Test
    public void testCreateReport() {
        ReportManagerFactory factory=new ReportManagerFactory();
        IReportManager reportManager1=factory.getEmployeeReportManager("a");
        IReportManager reportManager2=factory.getEmployeeReportManager("a");
        IReportManager reportManager3=factory.getEmployeeReportManager("a");
        IReportManager reportManager4=factory.getFinancialReportManager("a");
        IReportManager reportManager5=factory.getFinancialReportManager("a");
        IReportManager reportManager6=factory.getFinancialReportManager("a");
        System.out.println(reportManager1.createReport());
        System.out.println(reportManager2.createReport());
        System.out.println(reportManager3.createReport());
        System.out.println(reportManager4.createReport());
        System.out.println(reportManager5.createReport());
        System.out.println(reportManager6.createReport());   
    }
}

     2.1.4 装饰模式

/**
 * IPacketCreator
 * @Description 内容处理接口
 */
public interface IPacketCreator {
    public String handContent();
}

/**
 * PacketCreator
 * @Description 内容处理具体实现
 */
public class PacketCreator implements IPacketCreator {
    @Override
    public String handContent() {
        return "装饰模式";
    }
}

/**
 * PacketDecorator
 * @Description 装饰器
 */
public abstract class PacketDecorator implements IPacketCreator {
    IPacketCreator packetCreator;
    public PacketDecorator(IPacketCreator packetCreator) {
        this.packetCreator = packetCreator;
    }
}

/**
 * PacketHtmlDecorator
 * @Description 功能详细描述 
 */
public class PacketHtmlDecorator extends PacketDecorator {
    public PacketHtmlDecorator(IPacketCreator packetCreator) {
        super(packetCreator);
    }
    @Override
    public String handContent() {
        StringBuffer buffer=new StringBuffer("<html>");
        buffer.append(packetCreator.handContent()).append("</hmtl>");
        return buffer.toString();
    }
}

/**
 * DecoratorTest
 * @Description 装饰模式测试类
 */
public class DecoratorTest {
    @Test
    public void decoratorTest() {
        String content=new PacketHtmlDecorator(new PacketCreator()).handContent();
        System.out.println(content);   
    }
}

   2.2 使用缓冲区提高性能

    尽量使用含有缓冲区的类进行操作,如stringBuffer,BufferInputStream等

  2.3 使用缓冲技术提升性能

    1.使用oscache缓存jsp局部缓冲

     1.1 导入oscache的相关jar包

<dependency>
    <groupId>opensymphony</groupId>
    <artifactId>oscache</artifactId>
    <version>2.4.1</version>
</dependency>

     1.2 将oscache.properties文件放入到resource目录下

     1.3 在jsp页面中导入lib文件  <%@taglib prefix="oscache" uri="http://www.opensymphony.com/oscache" %>

     1.4 将需要缓存的代码使用<oscache:cache></oscache:cache>标签进行包裹

<oscache:cache key="${param.refresh }"  refresh="${param.refresh }">
    <div>登录时间<%=new Date() %></div>
</oscache:cache>

    2.使用oscache全局缓冲

     2.1 在web.xml配置oscacheFilter过滤器  

<filter>
    <filter-name>oscacheFilter</filter-name>
    <filter-class>com.opensymphony.oscache.web.filter.CacheFilter</filter-class>
    <init-param>
        <param-name>time</param-name>
        <param-value>600</param-value>p    
    </init-param>
    <init-param>
        <param-name>scope</param-name>
        <param-value>application</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>oscacheFilter</filter-name>
  <!--缓存的路径--> <url-pattern>*.jsp</url-pattern> </filter-mapping>

  

      

原文地址:https://www.cnblogs.com/lifeone/p/6269219.html