一、阿姆達爾定律html
1.1 加速比=優化後的耗時/優化前的耗時web
1.2 阿姆達爾定律 s<=1/F+(1-F)/Nspring
其中:s爲加速比,F爲程序的串行化比重,n爲cpu處理核數數據庫
二、調優層次(設計調優、代碼調優、JVM調優、數據庫調優)設計模式
2.1 設計模式緩存
2.1.1 單列模式:常見應用spring註解,以及實現當前在線人數的統計session
基本要素:構造方法私有化,提供公共的獲取實例的方法app
public class SingleSessionUserList { private static final SingleSessionUserList sessionUserList=new SingleSessionUserList(); //提供公共的方法接口 public static SingleSessionUserList getInstance() { return sessionUserList; } //將構造方法私有化 private SingleSessionUserList() { } }
2.1.2 代理模式jsp
核心思想:只有在真心須要時才進行初始化,不然只返回當前對象的代理對象ide
基本要素:主題接口、真實主題、代理類
/** * 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>