開發的效能與效率-twMVC#15

121
twMVC#15 開發的效能與效率 Kevin Tseng

description

講者:Kevin Tseng (http://kevintsengtw.blogspot.tw) 講者簡介:現任微軟最有價值專家、twMVC 核心講者、SkillTree 專任講師 簡介: 在系統開發時往往會被忽略的就是效能問題,而且通常都是在最後上線前才想到要去做效能調校,這次將為各位介紹如何透過工具的使用讓我們在開發期間就可以觀察與調校系統效能。 除了使用工具去觀察效能之外,也將會介紹如何利用工具來幫助我們提高開發的效率。 課程時間:70分鐘

Transcript of 開發的效能與效率-twMVC#15

Page 1: 開發的效能與效率-twMVC#15

twMVC#15 – 開發的效能與效率

Kevin Tseng

Page 2: 開發的效能與效率-twMVC#15

http://mvc.tw

微軟最有價值專家(MVP)2013,2014

twMVC 核心成員及講師 (http://mvc.tw)

SkillTree 專任講師 (http://skilltree.my)

部落格「mrkt 的程式學習筆記」

http://kevintsengtw.blogspot.tw

Google+ 專頁

https://plus.google.com/+KevintsengtwBlogspot

簡介

2

Page 3: 開發的效能與效率-twMVC#15

3

效能 Performance

Page 4: 開發的效能與效率-twMVC#15

http://mvc.tw

twMVC#8「超。光速 網站最佳化實戰」by demo

http://mvc.tw/Event/2013/3/30

簡報

http://goo.gl/P7P8h2

今天不講怎麼調校網頁讓執行速度更快

4

Page 5: 開發的效能與效率-twMVC#15

http://mvc.tw

TechEd North America 2014 [DEV-B418]

Performance Optimize Your ASP.NET Web App

Mads Kristensen

http://goo.gl/wFZBCU

今天不講怎麼調校網頁讓執行速度更快

5

Page 6: 開發的效能與效率-twMVC#15

Yahoo – Yslow (Why Slow)

6https://developer.yahoo.com/yslow/

Page 7: 開發的效能與效率-twMVC#15

Web Developer Checklist –ASP.NET Performance

7

http://webdevchecklist.com/asp.net/performance/

Page 8: 開發的效能與效率-twMVC#15

Web Developer Checklist

8

http://webdevchecklist.com/

Page 9: 開發的效能與效率-twMVC#15

Web Developer Checklist – ASP.NET

9

http://webdevchecklist.com/asp.net/

Page 10: 開發的效能與效率-twMVC#15

Web Developer Checklist –Chrome Extensions

10

http://goo.gl/2DqFl

Page 11: 開發的效能與效率-twMVC#15

http://mvc.tw

不要太早關注效能

網頁執行慢,常常是開發時忽略了應注意而未注意的項目

很多時候都是程式寫法而造成

不當的 LINQ 操作

關於效能……

11

Page 12: 開發的效能與效率-twMVC#15

http://mvc.tw

如何觀察 EF 所產生的 SQL Command

12

Page 13: 開發的效能與效率-twMVC#15

http://mvc.tw

如何觀察 EF 所產生的 SQL Command

13

Page 14: 開發的效能與效率-twMVC#15

http://mvc.tw

如何觀察 EF 所產生的 SQL Command

14

Page 15: 開發的效能與效率-twMVC#15

http://mvc.tw

如何觀察 EF 所產生的 SQL Command – EF6

15

Page 16: 開發的效能與效率-twMVC#15

http://mvc.tw

如何觀察 EF 所產生的 SQL Command – EF6

16

Page 17: 開發的效能與效率-twMVC#15

http://mvc.tw

如何觀察 EF 所產生的 SQL Command – EF6

17

Page 18: 開發的效能與效率-twMVC#15

http://mvc.tw

只有 Ultimate 版本才有此功能

Visual Studio - IntelliTrace

18

Page 19: 開發的效能與效率-twMVC#15

http://mvc.tw

Visual Studio - IntelliTrace

19

Page 20: 開發的效能與效率-twMVC#15

http://mvc.tw

Clutch.Diagnostics.EntityFramework

https://github.com/Kukkimonsuta/Clutch

Nuget

Install-Package Clutch.Diagnostics.EntityFramework

Entity Framework 5

20

P.S. 與 NLog 搭配使用

Page 21: 開發的效能與效率-twMVC#15

http://mvc.tw

Clutch.Diagnostics.EntityFramework

21

Page 22: 開發的效能與效率-twMVC#15

http://mvc.tw

Clutch.Diagnostics.EntityFramework

22

Page 23: 開發的效能與效率-twMVC#15

http://mvc.tw

Clutch.Diagnostics.EntityFramework

23

Page 24: 開發的效能與效率-twMVC#15

http://mvc.tw

Logging and Intercepting Database Operations

http://msdn.microsoft.com/zh-tw/data/dn469464.aspx

Entity Framework 6 Only

與 NLog 搭配使用(也可使用 Log4Net …etc)

EF6 – Intercepting, Logging

24

Page 25: 開發的效能與效率-twMVC#15

http://mvc.tw

EF6 – Intercepting, Logging

25

Page 26: 開發的效能與效率-twMVC#15

http://mvc.tw

EF6 – Intercepting, Logging

26

Page 27: 開發的效能與效率-twMVC#15

http://mvc.tw

EF6 – Intercepting, Logging

27

Page 28: 開發的效能與效率-twMVC#15

http://mvc.tw

EF6 – Intercepting, Logging

28

Page 29: 開發的效能與效率-twMVC#15

29

MiniProfiler

Page 30: 開發的效能與效率-twMVC#15

http://mvc.tw

MiniProfiler

30

http://miniprofiler.com/

Page 31: 開發的效能與效率-twMVC#15

http://mvc.tw

可分析系統裡透過 ADO.NET 所執行的 SQL Command

顯示頁面執行所耗費的時間

MS SQL Server, Oracle, MySql, Postgres …

LINQ to SQL, Entity Framework, ADO.NET

ASP.NET MVC, ASP.NET WebForms

由 Stack Overflow 團隊所設計開發

在 Stack Exchange 等系列網站都有使用.

MiniProfiler

31

Page 32: 開發的效能與效率-twMVC#15

http://mvc.tw

透過 NuGet 安裝:

MiniProfiler,MiniProfiler.Mvc4,MiniProfiler.EF6

MiniProfiler

32

Page 33: 開發的效能與效率-twMVC#15

http://mvc.tw

修改 _Layout.cshtml

MiniProfiler

33

Page 34: 開發的效能與效率-twMVC#15

http://mvc.tw

修改 Global.asax

MiniProfiler

34

Page 35: 開發的效能與效率-twMVC#15

http://mvc.tw

修改 Global.asax

MiniProfiler

35

Page 36: 開發的效能與效率-twMVC#15

http://mvc.tw

修改 Web.Config

MiniProfiler

36

<system.webServer> ... <handlers>

<add name="MiniProfiler" path="mini-profiler-resources/*" verb="*" type="System.Web.Routing.UrlRoutingModule" resourceType="Unspecified" preCondition="integratedMode" />

</handlers></system.webServer>

Page 37: 開發的效能與效率-twMVC#15

http://mvc.tw

MiniProfiler

37

Page 38: 開發的效能與效率-twMVC#15

http://mvc.tw

MiniProfiler

38

Page 39: 開發的效能與效率-twMVC#15

http://mvc.tw

MiniProfiler

39

Page 40: 開發的效能與效率-twMVC#15

http://mvc.tw

安全性釋疑:預設只有在 Local 端才會顯示,Remote 下

並不會顯示

MiniProfiler

40

Page 41: 開發的效能與效率-twMVC#15

http://mvc.tw

可在 Web.Config 裡加上開關,用來控制是否顯示

MiniProfiler

41

Page 42: 開發的效能與效率-twMVC#15

http://mvc.tw

MiniProfiler

42

Page 43: 開發的效能與效率-twMVC#15

http://mvc.tw

改由 enable_profiling 與 Request.IsLocal

MiniProfiler

43

Page 44: 開發的效能與效率-twMVC#15

http://mvc.tw

分層架構、AJAX

結合權限機制限定角色才會顯示 MiniProfiler 視窗

MiniProfiler – 進階操作

44

Page 45: 開發的效能與效率-twMVC#15

45

Glimpse

Page 46: 開發的效能與效率-twMVC#15

http://mvc.tw

Glimpse

46

http://getglimpse.com/

Page 47: 開發的效能與效率-twMVC#15

http://mvc.tw

Glimpse

47

線上操作就知道有什麼功用

http://play.getglimpse.com/

Page 48: 開發的效能與效率-twMVC#15

http://mvc.tw

視覺化的系統執行追蹤訊息

顯示系統 Configuration 內容

Route Debugging

顯示 View Rander 的細節

顯示 Http Request 的內容

多種擴充的 Third Party Extensions

Glimpse

48

Page 49: 開發的效能與效率-twMVC#15

http://mvc.tw

NuGet:

Glimpse Core

Glimpse ASP.NET

Glimpse Mvc5

Glimpse EF6

Glimpse

49

Page 50: 開發的效能與效率-twMVC#15

http://mvc.tw

安裝之後,開啟使用

Glimpse

50

http://localhost/Glimpse.axd

Page 51: 開發的效能與效率-twMVC#15

http://mvc.tw

觀察 EntityFramework 所執行的 SQL Command

Glimpse

51

Page 52: 開發的效能與效率-twMVC#15

http://mvc.tw

Glimpse

52

Page 53: 開發的效能與效率-twMVC#15

http://mvc.tw

Glimpse Extensions

53

http://getglimpse.com/Extensions/

Page 54: 開發的效能與效率-twMVC#15

http://mvc.tw

分層架構、AJAX

Glimpse.Performance

Glimpse – 進階操作

54

Page 55: 開發的效能與效率-twMVC#15

http://mvc.tw

Glimpse 有支援 ASP.NET WebForms

Glimpse – ASP.NET WebForms

55

https://www.youtube.com/watch?v=vS6ONq6YwyI

Page 56: 開發的效能與效率-twMVC#15

http://mvc.tw

Glimpse – ASP.NET WebForms

56

Page 57: 開發的效能與效率-twMVC#15

57

Microsoft Monitoring Agent

Page 58: 開發的效能與效率-twMVC#15

http://mvc.tw

Microsoft Monitoring Agent 會監視電腦基礎結構和應

用程式健全狀況。它會收集診斷資料,例如效能標準、事件

記錄檔和追蹤。Microsoft Monitoring Agent 可以做為

獨立工具使用,或是與 System Center Operations

Manager 搭配使用。

下載位置

http://www.microsoft.com/zh-TW/download/details.aspx?id=40316

Microsoft Monitoring Agent (MMA)

58

Page 59: 開發的效能與效率-twMVC#15

http://mvc.tw

Microsoft Monitoring Agent (MMA)

59

Page 60: 開發的效能與效率-twMVC#15

http://mvc.tw

Microsoft Monitoring Agent (MMA)

60

Page 61: 開發的效能與效率-twMVC#15

61

Performance and Diagnostics Hub (效能與分析中樞)

Page 62: 開發的效能與效率-twMVC#15

http://mvc.tw

效能與診斷

62

Page 63: 開發的效能與效率-twMVC#15

http://mvc.tw

效能與診斷

63

Page 64: 開發的效能與效率-twMVC#15

http://mvc.tw

效能與診斷

64

Page 65: 開發的效能與效率-twMVC#15

http://mvc.tw

效能與診斷

65

Page 66: 開發的效能與效率-twMVC#15

http://mvc.tw

效能與診斷

66

Page 67: 開發的效能與效率-twMVC#15

67

ANTS Performance/ANTS Memory Profiler

Page 68: 開發的效能與效率-twMVC#15

http://mvc.tw

ANTS Performance Profiler

68

http://www.red-gate.com/products/dotnet-development/ants-performance-profiler/

Page 69: 開發的效能與效率-twMVC#15

http://mvc.tw

ANTS Memory Profiler

69

http://www.red-gate.com/products/dotnet-development/ants-memory-profiler/

Page 70: 開發的效能與效率-twMVC#15

http://mvc.tw

ANTS Performance/Memory Profiler

70

Page 71: 開發的效能與效率-twMVC#15

71

Telerik JustTracePerformance/Memory Profiler

Page 72: 開發的效能與效率-twMVC#15

http://mvc.tw

Telerik JustTrace

72

http://www.telerik.com/products/memory-performance-profiler.aspx

Page 73: 開發的效能與效率-twMVC#15

http://mvc.tw

Telerik JustTrace Key Features

73

Page 74: 開發的效能與效率-twMVC#15

http://mvc.tw

Telerik JustTrace Profoiler

74

Page 75: 開發的效能與效率-twMVC#15

75

開發的效率

Page 76: 開發的效能與效率-twMVC#15

http://mvc.tw

多螢幕,效率高!

開發的效率

76

Page 77: 開發的效能與效率-twMVC#15

http://mvc.tw

換 SSD ,效率高!

開發的效率

77

Page 78: 開發的效能與效率-twMVC#15

http://mvc.tw

善用你手邊的開發工具

Visual Studio 號稱地表上最強的工具,要懂得如何使用

熟悉 Visual Studio 快捷鍵

安裝好用的 Visual Studio Extensions

開發的效率

78

Page 79: 開發的效能與效率-twMVC#15

http://mvc.tw

熟悉 Visua Studio 快捷鍵

79

http://visualstudioshortcuts.com/2013/

Page 80: 開發的效能與效率-twMVC#15

http://mvc.tw

Products and Extensions for Visual Studio

80

http://visualstudiogallery.msdn.microsoft.com/

Page 81: 開發的效能與效率-twMVC#15

推薦好套件 by demo

81http://vs.demo.tc/

Page 82: 開發的效能與效率-twMVC#15

http://mvc.tw

Productivity Power Tools

http://visualstudiogallery.msdn.microsoft.com/d0d33361-18e2-

46c0-8ff2-4adea1e34fef/

VSCommands

http://vscommands.squaredinfinity.com/features

Web Essentials

http://vswebessentials.com/

這些套件你裝了沒?

82

Page 85: 開發的效能與效率-twMVC#15

http://mvc.tw

ReSharper

http://www.jetbrains.com/resharper/

OzCode

http://www.oz-code.com/

.NET Reflector Visual Studio Extension

http://www.red-gate.com/products/dotnet-

development/reflector/

我還有哪些必裝的套件

85

Page 86: 開發的效能與效率-twMVC#15

http://mvc.tw

Bing Code Search for C#

86

http://goo.gl/OoXzsf

Page 87: 開發的效能與效率-twMVC#15

http://mvc.tw

Alt + A

Bing Code Search for C#

87

Page 88: 開發的效能與效率-twMVC#15

http://mvc.tw

Web Search

88

http://goo.gl/199ByB

Page 89: 開發的效能與效率-twMVC#15

http://mvc.tw

Web Search

89

Page 90: 開發的效能與效率-twMVC#15

http://mvc.tw

Web Search

90

Page 91: 開發的效能與效率-twMVC#15

91

ReShaper

Page 92: 開發的效能與效率-twMVC#15

http://mvc.tw

JetBrain - ReSharper

92

http://www.jetbrains.com/resharper/

Page 93: 開發的效能與效率-twMVC#15

http://mvc.tw

JetBrain - ReSharper

93

Page 94: 開發的效能與效率-twMVC#15

94

行動網站-用來偵錯的瀏覽器

Page 95: 開發的效能與效率-twMVC#15

http://mvc.tw

Ctrl + Shift + M

Firefox

95

Page 96: 開發的效能與效率-twMVC#15

http://mvc.tw

Chrome

96

Page 97: 開發的效能與效率-twMVC#15

http://mvc.tw

Mobilizer

97

Page 98: 開發的效能與效率-twMVC#15

http://mvc.tw

Electric Mobile Studio

98

http://www.electricplum.com/

Page 99: 開發的效能與效率-twMVC#15

http://mvc.tw

Electric Mobile Studio

99

Page 100: 開發的效能與效率-twMVC#15

http://mvc.tw

BrowserStack

100

http://www.browserstack.com

Page 101: 開發的效能與效率-twMVC#15

http://mvc.tw

BrowserStack

101

Page 102: 開發的效能與效率-twMVC#15

http://mvc.tw

BrowserStack

102

Page 103: 開發的效能與效率-twMVC#15

http://mvc.tw

BrowserStack - Chrome Extensions

103

Page 104: 開發的效能與效率-twMVC#15

104

Windows Phone Emulator

Page 105: 開發的效能與效率-twMVC#15

http://mvc.tw

Windows Phone 開發人員中心

105

https://dev.windowsphone.com/zh-tw

Page 106: 開發的效能與效率-twMVC#15

http://mvc.tw

Windos Phone 8.1 模擬器

106

https://dev.windowsphone.com/zh-tw/downloadsdk

Page 107: 開發的效能與效率-twMVC#15

http://mvc.tw

Windows Phone Emulator

107

Page 108: 開發的效能與效率-twMVC#15

http://mvc.tw

Windows Phone Emulator

108

Page 109: 開發的效能與效率-twMVC#15

http://mvc.tw

使用 Windows Phone Emulator 偵錯

109

Page 110: 開發的效能與效率-twMVC#15

110

modern.IE

Page 111: 開發的效能與效率-twMVC#15

http://mvc.tw

modern.IE

111

http://loc.modern.ie/zh-tw

Page 112: 開發的效能與效率-twMVC#15

http://mvc.tw

下載虛擬機器,測試各版本 IE 瀏覽器

112

Page 113: 開發的效能與效率-twMVC#15

http://mvc.tw

下載虛擬機器,測試各版本 IE 瀏覽器

113

Page 114: 開發的效能與效率-twMVC#15

http://mvc.tw

Modern.IE …… BrowserStack

114

Page 115: 開發的效能與效率-twMVC#15

115

Page 116: 開發的效能與效率-twMVC#15

http://mvc.tw

Introducing Microsoft Monitoring Agent

Performance Details in IntelliTrace

IntelliTrace MVC Navigation

UI Enhancements for IntelliTrace with Visual Studio

2013 Update 2

使用 Microsoft Monitoring Agent 監視部署中的應用程式

使用 Visual Studio 和 Microsoft Monitoring Agent 診斷

部署中的問題

Microsoft Monitoring Agent 相關連結

116

Page 117: 開發的效能與效率-twMVC#15

http://mvc.tw

Performance and Diagnostics Hub in Visual

Studio 2013

New CPU Usage tool in the Performance and

Diagnostics hub in Visual Studio 2013

MSDN 使用程式碼剖析工具分析應用程式效能

Visual Studio 2013的進階偵錯與效能分析技巧

Performance and Diagnostics Hub

117

Page 118: 開發的效能與效率-twMVC#15

http://mvc.tw

好活動需要支持

感謝 KKTIX 贊助 twMVC 活動報名平台

118

Page 119: 開發的效能與效率-twMVC#15

http://mvc.tw

好課程需要支持

119

http://skilltree.my

Page 120: 開發的效能與效率-twMVC#15

http://mvc.tw

好課程別放過 – ASP.NET MVC 5 實戰訓練營

120

http://skilltree.kktix.cc/events/skilltree2

Page 121: 開發的效能與效率-twMVC#15

謝謝各位

• 本投影片所包含的商標與文字皆屬原著作者所有。• 本投影片使用的圖片皆從網路搜尋。• 本著作係採用姓名標示-非商業性-相同方式分享 3.0 台灣授權。閱讀本授權條款,請到

http://creativecommons.org/licenses/by-nc-sa/3.0/tw/,或寫信至Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.

h t t p : / / m v c . t w