Code best practices and performance optimization · Code best practices and performance...
Transcript of Code best practices and performance optimization · Code best practices and performance...
![Page 1: Code best practices and performance optimization · Code best practices and performance optimization Alf Nilsson Lead System Developer & EMVP. ... Race Condition Locks Race Condition](https://reader034.fdocuments.net/reader034/viewer/2022051910/5fff6ac6bae13b7b5d1b309e/html5/thumbnails/1.jpg)
Code best practices and
performance optimization
Alf Nilsson
Lead System Developer
& EMVP
![Page 2: Code best practices and performance optimization · Code best practices and performance optimization Alf Nilsson Lead System Developer & EMVP. ... Race Condition Locks Race Condition](https://reader034.fdocuments.net/reader034/viewer/2022051910/5fff6ac6bae13b7b5d1b309e/html5/thumbnails/2.jpg)
Code best practices and performance optimization
What to look at?
• Tools
• Distractions
• Scenarios
• SessionState
• Loading Content & EPiServer API
• Cache
• Race Condition Locks
![Page 3: Code best practices and performance optimization · Code best practices and performance optimization Alf Nilsson Lead System Developer & EMVP. ... Race Condition Locks Race Condition](https://reader034.fdocuments.net/reader034/viewer/2022051910/5fff6ac6bae13b7b5d1b309e/html5/thumbnails/3.jpg)
Tools
Code best practices and performance optimization
![Page 4: Code best practices and performance optimization · Code best practices and performance optimization Alf Nilsson Lead System Developer & EMVP. ... Race Condition Locks Race Condition](https://reader034.fdocuments.net/reader034/viewer/2022051910/5fff6ac6bae13b7b5d1b309e/html5/thumbnails/4.jpg)
Tools
• Monitor CPU & Memory usage
• Call number and time
• License cost
• Other options
• Visual Studio
• ANTS Performance Profiler
DotTrace
![Page 5: Code best practices and performance optimization · Code best practices and performance optimization Alf Nilsson Lead System Developer & EMVP. ... Race Condition Locks Race Condition](https://reader034.fdocuments.net/reader034/viewer/2022051910/5fff6ac6bae13b7b5d1b309e/html5/thumbnails/5.jpg)
Tools
• Monitor database requests and
CPU usage
• Can use lots of resources in
production environment
SQL Server Profiler
![Page 6: Code best practices and performance optimization · Code best practices and performance optimization Alf Nilsson Lead System Developer & EMVP. ... Race Condition Locks Race Condition](https://reader034.fdocuments.net/reader034/viewer/2022051910/5fff6ac6bae13b7b5d1b309e/html5/thumbnails/6.jpg)
Distractions
Code best practices and performance optimization
![Page 7: Code best practices and performance optimization · Code best practices and performance optimization Alf Nilsson Lead System Developer & EMVP. ... Race Condition Locks Race Condition](https://reader034.fdocuments.net/reader034/viewer/2022051910/5fff6ac6bae13b7b5d1b309e/html5/thumbnails/7.jpg)
Distractions
Scheduled Jobs
Executes code
• Often complex and heavy
• Sometimes very frequently
• Configuration
<configuration>
<episerver>
<applicationSettings
enableScheduler=“false” />
</episerver>
</configuration>
![Page 8: Code best practices and performance optimization · Code best practices and performance optimization Alf Nilsson Lead System Developer & EMVP. ... Race Condition Locks Race Condition](https://reader034.fdocuments.net/reader034/viewer/2022051910/5fff6ac6bae13b7b5d1b309e/html5/thumbnails/8.jpg)
Distractions
Frontend resources
• Creates requests to server
• Requests to Media can cause
database traffic
![Page 9: Code best practices and performance optimization · Code best practices and performance optimization Alf Nilsson Lead System Developer & EMVP. ... Race Condition Locks Race Condition](https://reader034.fdocuments.net/reader034/viewer/2022051910/5fff6ac6bae13b7b5d1b309e/html5/thumbnails/9.jpg)
Distractions
Frontend resources
• Creates requests to server
• Requests to Media can cause
database traffic
Example of tools
• LINQPad – www.linqpad.net
• Postman Chrome plugin
• Create a console application
![Page 10: Code best practices and performance optimization · Code best practices and performance optimization Alf Nilsson Lead System Developer & EMVP. ... Race Condition Locks Race Condition](https://reader034.fdocuments.net/reader034/viewer/2022051910/5fff6ac6bae13b7b5d1b309e/html5/thumbnails/10.jpg)
Scenarios
Code best practices and performance optimization
![Page 11: Code best practices and performance optimization · Code best practices and performance optimization Alf Nilsson Lead System Developer & EMVP. ... Race Condition Locks Race Condition](https://reader034.fdocuments.net/reader034/viewer/2022051910/5fff6ac6bae13b7b5d1b309e/html5/thumbnails/11.jpg)
Server
Request 1
SessionState
SessionState
Server queues incoming requests to handle
session information between requests
• Enabled by default in .NET
• Practice at NetRelations to disable to
increase speed
Request 2
Request 3
![Page 12: Code best practices and performance optimization · Code best practices and performance optimization Alf Nilsson Lead System Developer & EMVP. ... Race Condition Locks Race Condition](https://reader034.fdocuments.net/reader034/viewer/2022051910/5fff6ac6bae13b7b5d1b309e/html5/thumbnails/12.jpg)
SessionState
SessionState
MVC
[SessionState(SessionStateBehavior.Disabled)]
WebForms
<%@ Page EnableSessionState="false" ... %>
Server
Request 1
Request 2
Request 3
![Page 13: Code best practices and performance optimization · Code best practices and performance optimization Alf Nilsson Lead System Developer & EMVP. ... Race Condition Locks Race Condition](https://reader034.fdocuments.net/reader034/viewer/2022051910/5fff6ac6bae13b7b5d1b309e/html5/thumbnails/13.jpg)
?What about the sessions?
Do you need them?
Save session key into cookie (beware of regional laws)
Store information on server
Some VisitorGroups and Add-ons use SessionState
![Page 14: Code best practices and performance optimization · Code best practices and performance optimization Alf Nilsson Lead System Developer & EMVP. ... Race Condition Locks Race Condition](https://reader034.fdocuments.net/reader034/viewer/2022051910/5fff6ac6bae13b7b5d1b309e/html5/thumbnails/14.jpg)
Cut to the code
![Page 15: Code best practices and performance optimization · Code best practices and performance optimization Alf Nilsson Lead System Developer & EMVP. ... Race Condition Locks Race Condition](https://reader034.fdocuments.net/reader034/viewer/2022051910/5fff6ac6bae13b7b5d1b309e/html5/thumbnails/15.jpg)
EPiServer API
Loading Content & EPiServer API
• GetChildren
• GetDescendents + Get
• GetDescendents + GetItems
• FindPagesWithCriteria
![Page 16: Code best practices and performance optimization · Code best practices and performance optimization Alf Nilsson Lead System Developer & EMVP. ... Race Condition Locks Race Condition](https://reader034.fdocuments.net/reader034/viewer/2022051910/5fff6ac6bae13b7b5d1b309e/html5/thumbnails/16.jpg)
EPiServer API
Loading Content - GetChildren
+ Easy
- Need to iterate and recurse for each
child
- Very expensive if big page tree!
![Page 17: Code best practices and performance optimization · Code best practices and performance optimization Alf Nilsson Lead System Developer & EMVP. ... Race Condition Locks Race Condition](https://reader034.fdocuments.net/reader034/viewer/2022051910/5fff6ac6bae13b7b5d1b309e/html5/thumbnails/17.jpg)
Cut to the code
![Page 18: Code best practices and performance optimization · Code best practices and performance optimization Alf Nilsson Lead System Developer & EMVP. ... Race Condition Locks Race Condition](https://reader034.fdocuments.net/reader034/viewer/2022051910/5fff6ac6bae13b7b5d1b309e/html5/thumbnails/18.jpg)
EPiServer API
Loading Content –GetDescendents & Get
+ Optimized for tree structure - don’t
need to iterate and recurse
- Returns ContentReference and
needs to Get content
- Expensive if big page tree
![Page 19: Code best practices and performance optimization · Code best practices and performance optimization Alf Nilsson Lead System Developer & EMVP. ... Race Condition Locks Race Condition](https://reader034.fdocuments.net/reader034/viewer/2022051910/5fff6ac6bae13b7b5d1b309e/html5/thumbnails/19.jpg)
Cut to the code
![Page 20: Code best practices and performance optimization · Code best practices and performance optimization Alf Nilsson Lead System Developer & EMVP. ... Race Condition Locks Race Condition](https://reader034.fdocuments.net/reader034/viewer/2022051910/5fff6ac6bae13b7b5d1b309e/html5/thumbnails/20.jpg)
EPiServer API
Loading Content –GetDescendents & GetItems
+ Faster than get each content
+ Use when having multiple
ContentReferences
- Expensive if big page tree
![Page 21: Code best practices and performance optimization · Code best practices and performance optimization Alf Nilsson Lead System Developer & EMVP. ... Race Condition Locks Race Condition](https://reader034.fdocuments.net/reader034/viewer/2022051910/5fff6ac6bae13b7b5d1b309e/html5/thumbnails/21.jpg)
Cut to the code
![Page 22: Code best practices and performance optimization · Code best practices and performance optimization Alf Nilsson Lead System Developer & EMVP. ... Race Condition Locks Race Condition](https://reader034.fdocuments.net/reader034/viewer/2022051910/5fff6ac6bae13b7b5d1b309e/html5/thumbnails/22.jpg)
EPiServer API
Loading Content – FindPagesWithCriteria
+ One database call to find pages
+ Stored Procedure netPropertySearchValueMeta
+ Good in a big tree structure
- Does only work with Pages
- Does not cache!
![Page 23: Code best practices and performance optimization · Code best practices and performance optimization Alf Nilsson Lead System Developer & EMVP. ... Race Condition Locks Race Condition](https://reader034.fdocuments.net/reader034/viewer/2022051910/5fff6ac6bae13b7b5d1b309e/html5/thumbnails/23.jpg)
Cut to the code
![Page 24: Code best practices and performance optimization · Code best practices and performance optimization Alf Nilsson Lead System Developer & EMVP. ... Race Condition Locks Race Condition](https://reader034.fdocuments.net/reader034/viewer/2022051910/5fff6ac6bae13b7b5d1b309e/html5/thumbnails/24.jpg)
Caching
Caching
• IObjectInstanceCache and ISynchronizedObjectInstanceCache wraps
HttpRuntime.Cache
• ISynchronizedObjectInstanceCache synchronizes in a load balanced
environment
• CacheEvictionPolicy to clear out cache when needed
![Page 25: Code best practices and performance optimization · Code best practices and performance optimization Alf Nilsson Lead System Developer & EMVP. ... Race Condition Locks Race Condition](https://reader034.fdocuments.net/reader034/viewer/2022051910/5fff6ac6bae13b7b5d1b309e/html5/thumbnails/25.jpg)
Caching
ISynchronizedObjectInstanceCache
var cache = ServiceLocator.Current
.GetInstance<ISynchronizedObjectInstanceCache>();
cache.Insert(key, value, cacheEvictionPolicy);
Server 1 Server 2
cacheKey cacheKey
cache.Remove(cacheKey,value, cacheEvictionPolicy);
![Page 26: Code best practices and performance optimization · Code best practices and performance optimization Alf Nilsson Lead System Developer & EMVP. ... Race Condition Locks Race Condition](https://reader034.fdocuments.net/reader034/viewer/2022051910/5fff6ac6bae13b7b5d1b309e/html5/thumbnails/26.jpg)
cache.Insert(“cacheKey2”,x, new CacheEvictionPolicy(new [] {”cacheKey1”}));
No “cacheKey1”!
cache.Insert(“cacheKey1”, x, CacheEvictionPolicy.Empty);cache.Insert(“cacheKey2”, x, new CacheEvictionPolicy(new [] {”cacheKey1”}));cache.Remove(“cacheKey1”);
Caching
CacheEvictionPolicy - cacheKeys
new CacheEvictionPolicy(new string[] { ”cacheKey” });
• Make cached object dependant on previously
cached keys being updated or removed
• Won’t cache if depending key is not already cached
Server
cacheKey1 cacheKey2
![Page 27: Code best practices and performance optimization · Code best practices and performance optimization Alf Nilsson Lead System Developer & EMVP. ... Race Condition Locks Race Condition](https://reader034.fdocuments.net/reader034/viewer/2022051910/5fff6ac6bae13b7b5d1b309e/html5/thumbnails/27.jpg)
Caching
CacheEvictionPolicy - masterKeys
new CacheEvictionPolicy(
null, // for files
null, // for cacheKeys
new string[] { ”masterKey” });
• Same as cacheKeys, but does not require key to
already be cached
![Page 28: Code best practices and performance optimization · Code best practices and performance optimization Alf Nilsson Lead System Developer & EMVP. ... Race Condition Locks Race Condition](https://reader034.fdocuments.net/reader034/viewer/2022051910/5fff6ac6bae13b7b5d1b309e/html5/thumbnails/28.jpg)
Cut to the code
![Page 29: Code best practices and performance optimization · Code best practices and performance optimization Alf Nilsson Lead System Developer & EMVP. ... Race Condition Locks Race Condition](https://reader034.fdocuments.net/reader034/viewer/2022051910/5fff6ac6bae13b7b5d1b309e/html5/thumbnails/29.jpg)
Race Condition Locks
Race Condition Locks
Important when using resources that takes long time and can't handle
usages by multiple threads, such as reading or writing files on disk!
• Locking
• Mini Locking
![Page 30: Code best practices and performance optimization · Code best practices and performance optimization Alf Nilsson Lead System Developer & EMVP. ... Race Condition Locks Race Condition](https://reader034.fdocuments.net/reader034/viewer/2022051910/5fff6ac6bae13b7b5d1b309e/html5/thumbnails/30.jpg)
Race Condition Locks
Race Condition Locks - Simple lock
• Prevents exceptions and that the system
locks because of multiple threads using
the same resource
• Queues threads that want to use locked
functionality
![Page 31: Code best practices and performance optimization · Code best practices and performance optimization Alf Nilsson Lead System Developer & EMVP. ... Race Condition Locks Race Condition](https://reader034.fdocuments.net/reader034/viewer/2022051910/5fff6ac6bae13b7b5d1b309e/html5/thumbnails/31.jpg)
Cut to the code
![Page 32: Code best practices and performance optimization · Code best practices and performance optimization Alf Nilsson Lead System Developer & EMVP. ... Race Condition Locks Race Condition](https://reader034.fdocuments.net/reader034/viewer/2022051910/5fff6ac6bae13b7b5d1b309e/html5/thumbnails/32.jpg)
Scenarios
Mini Locking
• If only using a simple lock, threads are
locking up even if they don't need to
• Mini locks are more granular and only
queues up threads based on keys
![Page 33: Code best practices and performance optimization · Code best practices and performance optimization Alf Nilsson Lead System Developer & EMVP. ... Race Condition Locks Race Condition](https://reader034.fdocuments.net/reader034/viewer/2022051910/5fff6ac6bae13b7b5d1b309e/html5/thumbnails/33.jpg)
Cut to the code
![Page 34: Code best practices and performance optimization · Code best practices and performance optimization Alf Nilsson Lead System Developer & EMVP. ... Race Condition Locks Race Condition](https://reader034.fdocuments.net/reader034/viewer/2022051910/5fff6ac6bae13b7b5d1b309e/html5/thumbnails/34.jpg)
Scenarios
Wanna play?
github.com/alfnilsson/ascend2015
![Page 35: Code best practices and performance optimization · Code best practices and performance optimization Alf Nilsson Lead System Developer & EMVP. ... Race Condition Locks Race Condition](https://reader034.fdocuments.net/reader034/viewer/2022051910/5fff6ac6bae13b7b5d1b309e/html5/thumbnails/35.jpg)
Code best practices and performance optimization
Judge of a man by his questions
rather than by his answers.
Voltaire
Questions?
![Page 36: Code best practices and performance optimization · Code best practices and performance optimization Alf Nilsson Lead System Developer & EMVP. ... Race Condition Locks Race Condition](https://reader034.fdocuments.net/reader034/viewer/2022051910/5fff6ac6bae13b7b5d1b309e/html5/thumbnails/36.jpg)
Code best practices and performance optimization
Thanks and have a good Ascend!
Alf Nilssona @alfnilsson
a github.com/alfnilsson
http://talk.alfnilsson.se