<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>performance Archives - CodeJourney.net</title>
	<atom:link href="https://www.codejourney.net/tag/performance/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.codejourney.net/tag/performance/</link>
	<description>Become a better .NET full stack web developer</description>
	<lastBuildDate>Sat, 17 Nov 2018 13:12:39 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9</generator>

<image>
	<url>https://i0.wp.com/www.codejourney.net/wp-content/uploads/2018/10/cropped-512px-na-512px-JPEG-BEZ-NAPISU-1.jpg?fit=32%2C32&#038;ssl=1</url>
	<title>performance Archives - CodeJourney.net</title>
	<link>https://www.codejourney.net/tag/performance/</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">123174533</site>	<item>
		<title>Solving performance issues with dotTrace</title>
		<link>https://www.codejourney.net/solving-performance-issues-with-dottrace/</link>
					<comments>https://www.codejourney.net/solving-performance-issues-with-dottrace/#comments</comments>
		
		<dc:creator><![CDATA[Dawid Sibiński]]></dc:creator>
		<pubDate>Sun, 12 Mar 2017 14:40:24 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[dajsiepoznac2017]]></category>
		<category><![CDATA[performance]]></category>
		<guid isPermaLink="false">http://www.dsibinski.pl/?p=728</guid>

					<description><![CDATA[<p>If you&#8217;ve ever had any performance issues with your .NET applications, you may find this writing interesting. In this post I&#8217;d like to present to you a tool produced by JetBrains &#8211; dotTrace, showing how using only one of its functionalities can help in finding causes of performance issues in .NET applications. Performance issue on production Months&#8230;</p>
<p>The post <a href="https://www.codejourney.net/solving-performance-issues-with-dottrace/">Solving performance issues with dotTrace</a> appeared first on <a href="https://www.codejourney.net">CodeJourney.net</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>If you&#8217;ve ever had any performance issues with your .NET applications, you may find this writing interesting.</p>
<p>In this post I&#8217;d like to present to you a tool produced by JetBrains &#8211; <strong>dotTrace</strong>, showing how using only one of its functionalities can help in finding causes of performance issues in .NET applications.<br />
<span id="more-728"></span></p>
<h2>Performance issue on production</h2>
<p>Months ago some of your colleagues implemented an application and delivered it to the Customer. The app is &#8220;Countries and Cities Manager&#8221;, which is used by some administrator working in local government unit. That&#8217;s how it looks:</p>
<figure id="attachment_735" aria-describedby="caption-attachment-735" style="width: 380px" class="wp-caption aligncenter"><a href="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/countriesAndCitiesManager.png?ssl=1"><img data-recalc-dims="1" fetchpriority="high" decoding="async" data-attachment-id="735" data-permalink="https://www.codejourney.net/solving-performance-issues-with-dottrace/countriesandcitiesmanager/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/countriesAndCitiesManager.png?fit=646%2C232&amp;ssl=1" data-orig-size="646,232" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="countriesAndCitiesManager" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/countriesAndCitiesManager.png?fit=300%2C108&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/countriesAndCitiesManager.png?fit=646%2C232&amp;ssl=1" class="wp-image-735 " src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/countriesAndCitiesManager.png?resize=380%2C137&#038;ssl=1" alt="" width="380" height="137" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/countriesAndCitiesManager.png?resize=300%2C108&amp;ssl=1 300w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/countriesAndCitiesManager.png?w=646&amp;ssl=1 646w" sizes="(max-width: 380px) 100vw, 380px" /></a><figcaption id="caption-attachment-735" class="wp-caption-text">Countries and Cities Manager</figcaption></figure>
<p>That&#8217;s cool, isn&#8217;t it ? This &#8220;administration panel&#8221; allows to add a new city to the country. User fills name of the city and provides ISO code of the country considered. You may think &#8220;who would ever use it and for what?&#8221;. True. But first of all, this is just an example <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /> , and secondly, imagine that Russia annexes some big European country. What happens then ? User administrating local government system must use your manager application to insert hundreds or thousands of new cities in Russia. Gotcha!</p>
<p>The next day you receive a ticket in your company&#8217;s issues tracking system. Taking into consideration that an average number of cities in east-European countries is ~600, the ticket&#8217;s priority is set to &#8220;Blocking&#8221;. The description of the issue is:</p>
<ol>
<li>User fills the &#8220;City&#8221; and &#8220;Country ISO Code&#8221;, clicks &#8220;Add city&#8221;</li>
<li>Waits for 10 seconds&#8230;</li>
<li>Waits for 20 seconds&#8230;</li>
<li>Waits for 30 seconds&#8230;</li>
<li>Error message received:<br />
<figure id="attachment_738" aria-describedby="caption-attachment-738" style="width: 395px" class="wp-caption alignnone"><img data-recalc-dims="1" decoding="async" data-attachment-id="738" data-permalink="https://www.codejourney.net/solving-performance-issues-with-dottrace/errorisonotfound/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/errorISONotFound.png?fit=702%2C319&amp;ssl=1" data-orig-size="702,319" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="errorISONotFound" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/errorISONotFound.png?fit=300%2C136&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/errorISONotFound.png?fit=702%2C319&amp;ssl=1" class="wp-image-738 " src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/errorISONotFound.png?resize=395%2C179&#038;ssl=1" alt="" width="395" height="179" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/errorISONotFound.png?resize=300%2C136&amp;ssl=1 300w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/errorISONotFound.png?w=702&amp;ssl=1 702w" sizes="(max-width: 395px) 100vw, 395px" /><figcaption id="caption-attachment-738" class="wp-caption-text">ISO not found</figcaption></figure>
</li>
<li>User corrects &#8220;RU&#8221; to &#8220;RUS&#8221;
<div class="mceTemp"> </div>
</li>
<li>Waits for 10 seconds&#8230;</li>
<li>Waits for 20 seconds&#8230;</li>
<li>Waits for 30 seconds&#8230;</li>
<li> City added!</li>
<li>Scenario repeats for each new city to be added. Performance is unacceptable.</li>
</ol>
<p>Initially you have no idea what could be the reason. Such simple operation and such terrible performance? Let&#8217;s see&#8230;</p>
<h2>Looking for issue with dotTrace profiling</h2>
<p>In such cases, especially if I don&#8217;t know the code of the application considered, the very first thing I do it to use <a href="https://www.jetbrains.com/profiler/" target="_blank" rel="noopener"><strong>dotTrace</strong></a> in order to profile the application when executing bad-performance actions. It provides many features, including:</p>
<ul>
<li>detecting performance bottlenecks in .NET applications</li>
<li>getting information about calls execution times</li>
<li>analyzing calls order on a timeline</li>
<li>profilling SQL queries and HTTP requests</li>
<li>profiling unit tests</li>
<li>&#8230; and <a href="https://www.jetbrains.com/profiler/" target="_blank" rel="noopener">many more</a>.</li>
</ul>
<p>We will use only the simplest profiling mode which is called <em>Sampling</em>.</p>
<p>After you <a href="https://www.jetbrains.com/profiler/" target="_blank" rel="noopener">downloaded and installed dotTrace</a>, launch the application you want to profile first. Then, launch <em>dotTrace </em>and in <em>Attach to Process</em> section find and select process of your app and select <em>Sampling</em> mode:</p>
<figure id="attachment_740" aria-describedby="caption-attachment-740" style="width: 514px" class="wp-caption aligncenter"><a href="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/dotTrace_attaching.png?ssl=1"><img data-recalc-dims="1" decoding="async" data-attachment-id="740" data-permalink="https://www.codejourney.net/solving-performance-issues-with-dottrace/dottrace_attaching/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/dotTrace_attaching.png?fit=1245%2C583&amp;ssl=1" data-orig-size="1245,583" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="dotTrace_attaching" data-image-description="" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/dotTrace_attaching.png?fit=300%2C140&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/dotTrace_attaching.png?fit=1024%2C480&amp;ssl=1" class="wp-image-740" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/dotTrace_attaching.png?resize=514%2C240&#038;ssl=1" alt="" width="514" height="240" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/dotTrace_attaching.png?resize=300%2C140&amp;ssl=1 300w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/dotTrace_attaching.png?resize=768%2C360&amp;ssl=1 768w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/dotTrace_attaching.png?resize=1024%2C480&amp;ssl=1 1024w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/dotTrace_attaching.png?resize=720%2C337&amp;ssl=1 720w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/dotTrace_attaching.png?w=1245&amp;ssl=1 1245w" sizes="(max-width: 514px) 100vw, 514px" /></a><figcaption id="caption-attachment-740" class="wp-caption-text">dotTrace &#8211; Sampling mode</figcaption></figure>
<p>Then, click &#8220;Run&#8221; button just below <em>Sampling </em>mode selection &#8211; you&#8217;ll see the profiling session has started:</p>
<figure id="attachment_742" aria-describedby="caption-attachment-742" style="width: 402px" class="wp-caption aligncenter"><a href="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/dotTrace_session_running.png?ssl=1"><img data-recalc-dims="1" loading="lazy" decoding="async" data-attachment-id="742" data-permalink="https://www.codejourney.net/solving-performance-issues-with-dottrace/dottrace_session_running/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/dotTrace_session_running.png?fit=826%2C122&amp;ssl=1" data-orig-size="826,122" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="dotTrace_session_running" data-image-description="" data-image-caption="&lt;p&gt;dotTrace session&lt;/p&gt;
" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/dotTrace_session_running.png?fit=300%2C44&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/dotTrace_session_running.png?fit=826%2C122&amp;ssl=1" class=" wp-image-742" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/dotTrace_session_running.png?resize=402%2C59&#038;ssl=1" alt="" width="402" height="59" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/dotTrace_session_running.png?resize=300%2C44&amp;ssl=1 300w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/dotTrace_session_running.png?resize=768%2C113&amp;ssl=1 768w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/dotTrace_session_running.png?resize=720%2C106&amp;ssl=1 720w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/dotTrace_session_running.png?w=826&amp;ssl=1 826w" sizes="auto, (max-width: 402px) 100vw, 402px" /></a><figcaption id="caption-attachment-742" class="wp-caption-text">dotTrace session</figcaption></figure>
<p>Next, go to your application and execute bad-performance action. In our case, we click on &#8220;Add city&#8221; button:</p>
<figure id="attachment_779" aria-describedby="caption-attachment-779" style="width: 460px" class="wp-caption aligncenter"><a href="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/addCity_Click-3.png?ssl=1"><img data-recalc-dims="1" loading="lazy" decoding="async" data-attachment-id="779" data-permalink="https://www.codejourney.net/solving-performance-issues-with-dottrace/addcity_click-4/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/addCity_Click-3.png?fit=430%2C154&amp;ssl=1" data-orig-size="430,154" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="addCity_Click" data-image-description="" data-image-caption="&lt;p&gt;Add City clicked&lt;/p&gt;
" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/addCity_Click-3.png?fit=300%2C107&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/addCity_Click-3.png?fit=430%2C154&amp;ssl=1" class=" wp-image-779" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/addCity_Click-3.png?resize=460%2C164&#038;ssl=1" alt="" width="460" height="164" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/addCity_Click-3.png?resize=300%2C107&amp;ssl=1 300w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/addCity_Click-3.png?w=430&amp;ssl=1 430w" sizes="auto, (max-width: 460px) 100vw, 460px" /></a><figcaption id="caption-attachment-779" class="wp-caption-text">Add City clicked</figcaption></figure>
<p>App is not responding &#8211; cool! Now we wait those 10, 20, 30 seconds until we get the error message described by the user. To be sure what&#8217;s happening, we can click the button few more times (maybe the next executions are better?). I clicked it 3 times are the times were pretty the same.</p>
<p>That&#8217;s the end of our bad-performance action, so in <em>dotTrace</em> session&#8217;s window click &#8220;Get Snapshot and Wait&#8221;:</p>
<figure id="attachment_744" aria-describedby="caption-attachment-744" style="width: 460px" class="wp-caption aligncenter"><a href="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/dotTrace_GetSnapshot.png?ssl=1"><img data-recalc-dims="1" loading="lazy" decoding="async" data-attachment-id="744" data-permalink="https://www.codejourney.net/solving-performance-issues-with-dottrace/dottrace_getsnapshot/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/dotTrace_GetSnapshot.png?fit=828%2C159&amp;ssl=1" data-orig-size="828,159" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="dotTrace_GetSnapshot" data-image-description="&lt;p&gt;dotTrace &amp;#8211; getting snapshot&lt;/p&gt;
" data-image-caption="" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/dotTrace_GetSnapshot.png?fit=300%2C58&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/dotTrace_GetSnapshot.png?fit=828%2C159&amp;ssl=1" class="wp-image-744 " src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/dotTrace_GetSnapshot.png?resize=460%2C89&#038;ssl=1" alt="" width="460" height="89" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/dotTrace_GetSnapshot.png?resize=300%2C58&amp;ssl=1 300w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/dotTrace_GetSnapshot.png?resize=768%2C147&amp;ssl=1 768w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/dotTrace_GetSnapshot.png?resize=720%2C138&amp;ssl=1 720w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/dotTrace_GetSnapshot.png?w=828&amp;ssl=1 828w" sizes="auto, (max-width: 460px) 100vw, 460px" /></a><figcaption id="caption-attachment-744" class="wp-caption-text">dotTrace &#8211; getting snapshot</figcaption></figure>
<p>After few seconds, <em>JetBrains dotTrace Performance Viewer</em> opens. Open &#8220;Threads Tree&#8221; on &#8220;All Calls&#8221; tab:</p>
<figure id="attachment_747" aria-describedby="caption-attachment-747" style="width: 435px" class="wp-caption aligncenter"><a href="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/perfViewer_ThreadsTree.png?ssl=1"><img data-recalc-dims="1" loading="lazy" decoding="async" data-attachment-id="747" data-permalink="https://www.codejourney.net/solving-performance-issues-with-dottrace/perfviewer_threadstree/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/perfViewer_ThreadsTree.png?fit=709%2C326&amp;ssl=1" data-orig-size="709,326" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="perfViewer_ThreadsTree" data-image-description="" data-image-caption="&lt;p&gt;Threads Tree&lt;/p&gt;
" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/perfViewer_ThreadsTree.png?fit=300%2C138&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/perfViewer_ThreadsTree.png?fit=709%2C326&amp;ssl=1" class=" wp-image-747" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/perfViewer_ThreadsTree.png?resize=435%2C200&#038;ssl=1" alt="" width="435" height="200" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/perfViewer_ThreadsTree.png?resize=300%2C138&amp;ssl=1 300w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/perfViewer_ThreadsTree.png?w=709&amp;ssl=1 709w" sizes="auto, (max-width: 435px) 100vw, 435px" /></a><figcaption id="caption-attachment-747" class="wp-caption-text">Threads Tree</figcaption></figure>
<p>In this section we normally see all the threads running within our application during profiling time. We had only one thread, so let&#8217;s expand the details to see the time and percentage duration of particular calls from the calls stack:</p>
<figure id="attachment_748" aria-describedby="caption-attachment-748" style="width: 665px" class="wp-caption aligncenter"><a href="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/perfViewer_LongestCall.png?ssl=1"><img data-recalc-dims="1" loading="lazy" decoding="async" data-attachment-id="748" data-permalink="https://www.codejourney.net/solving-performance-issues-with-dottrace/perfviewer_longestcall/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/perfViewer_LongestCall.png?fit=1269%2C311&amp;ssl=1" data-orig-size="1269,311" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="perfViewer_LongestCall" data-image-description="" data-image-caption="&lt;p&gt;Calls duration in performance viewer&lt;/p&gt;
" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/perfViewer_LongestCall.png?fit=300%2C74&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/perfViewer_LongestCall.png?fit=1024%2C251&amp;ssl=1" class=" wp-image-748" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/perfViewer_LongestCall.png?resize=665%2C164&#038;ssl=1" alt="" width="665" height="164" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/perfViewer_LongestCall.png?resize=300%2C74&amp;ssl=1 300w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/perfViewer_LongestCall.png?resize=768%2C188&amp;ssl=1 768w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/perfViewer_LongestCall.png?resize=1024%2C251&amp;ssl=1 1024w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/perfViewer_LongestCall.png?resize=720%2C176&amp;ssl=1 720w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/perfViewer_LongestCall.png?w=1269&amp;ssl=1 1269w" sizes="auto, (max-width: 665px) 100vw, 665px" /></a><figcaption id="caption-attachment-748" class="wp-caption-text">Calls duration in performance viewer</figcaption></figure>
<p>At this moment, without even opening the code, we already see that method called <em>GetAllEuropeanCountries</em> takes <strong>83.63%</strong> of our thread&#8217;s execution time. What&#8217;s more, we can see that this method is defined in <strong><em>CountriesCitiesDbService</em></strong><strong> </strong>which apperently retrieves list of European countries from the database or some external source.</p>
<p>The most percipient of you probably noticed there is <em>Thread.Sleep(Int32) </em>method called one level down &#8211; yes, that&#8217;s for example&#8217;s purposes <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /> , but in real world this would be a database query or a web service call here. The most important is that we know what takes the longest during our operation. Let&#8217;s now fix it!</p>
<h2>Fixing the issue</h2>
<p>Let&#8217;s open <em>CountriesCitiesManager</em> solution and use the knowledge we already have from the profiling. We go to the <em>AddNewCity(String, String)</em> method, in which the method for getting countries was called (this is deductible from the calls tree in performance viewer). Here&#8217;s the code:</p>
<p><style>.gist table { margin-bottom: 0; }</style>
<div style="tab-size: 8" id="gist45629395" class="gist">
<div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
<div class="gist-data">
<div class="js-gist-file-update-container js-task-list-container">
<div id="file-addnewcity-cs" class="file my-2">
<div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-c  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="AddNewCity.cs content, created by dsibinski on 01:02PM on March 12, 2017."
    ></p>
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">
<p>  <template class="js-file-alert-template"></p>
<div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
    <span><br />
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.<br />
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a><br />
    </span></p>
<div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters<br />
</a>
</div>
</div>
<p></template><br />
<template class="js-line-alert-template"><br />
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e"><br />
    <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
</span></template></p>
<table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="AddNewCity.cs">
<tr>
<td id="file-addnewcity-cs-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
<td id="file-addnewcity-cs-LC1" class="blob-code blob-code-inner js-file-line">private bool AddNewCity(string cityName, string countryIso)</td>
</tr>
<tr>
<td id="file-addnewcity-cs-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
<td id="file-addnewcity-cs-LC2" class="blob-code blob-code-inner js-file-line">{</td>
</tr>
<tr>
<td id="file-addnewcity-cs-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
<td id="file-addnewcity-cs-LC3" class="blob-code blob-code-inner js-file-line">    var city = CountriesCitiesDbService.GetNewCity();</td>
</tr>
<tr>
<td id="file-addnewcity-cs-L4" class="blob-num js-line-number js-blob-rnum" data-line-number="4"></td>
<td id="file-addnewcity-cs-LC4" class="blob-code blob-code-inner js-file-line">    var europeanCountries = CountriesCitiesDbService.GetAllEuropeanCountries();</td>
</tr>
<tr>
<td id="file-addnewcity-cs-L5" class="blob-num js-line-number js-blob-rnum" data-line-number="5"></td>
<td id="file-addnewcity-cs-LC5" class="blob-code blob-code-inner js-file-line">    var country = europeanCountries.FirstOrDefault(ec =&gt; ec.IsoCode.Equals(countryIso));</td>
</tr>
<tr>
<td id="file-addnewcity-cs-L6" class="blob-num js-line-number js-blob-rnum" data-line-number="6"></td>
<td id="file-addnewcity-cs-LC6" class="blob-code blob-code-inner js-file-line">
</td>
</tr>
<tr>
<td id="file-addnewcity-cs-L7" class="blob-num js-line-number js-blob-rnum" data-line-number="7"></td>
<td id="file-addnewcity-cs-LC7" class="blob-code blob-code-inner js-file-line">    if (country == null)</td>
</tr>
<tr>
<td id="file-addnewcity-cs-L8" class="blob-num js-line-number js-blob-rnum" data-line-number="8"></td>
<td id="file-addnewcity-cs-LC8" class="blob-code blob-code-inner js-file-line">        throw new ArgumentException($&quot;Country with ISO Code {countryIso} does not exist!&quot;);</td>
</tr>
<tr>
<td id="file-addnewcity-cs-L9" class="blob-num js-line-number js-blob-rnum" data-line-number="9"></td>
<td id="file-addnewcity-cs-LC9" class="blob-code blob-code-inner js-file-line">
</td>
</tr>
<tr>
<td id="file-addnewcity-cs-L10" class="blob-num js-line-number js-blob-rnum" data-line-number="10"></td>
<td id="file-addnewcity-cs-LC10" class="blob-code blob-code-inner js-file-line">    city.Name = cityName;</td>
</tr>
<tr>
<td id="file-addnewcity-cs-L11" class="blob-num js-line-number js-blob-rnum" data-line-number="11"></td>
<td id="file-addnewcity-cs-LC11" class="blob-code blob-code-inner js-file-line">    city.Country = country;</td>
</tr>
<tr>
<td id="file-addnewcity-cs-L12" class="blob-num js-line-number js-blob-rnum" data-line-number="12"></td>
<td id="file-addnewcity-cs-LC12" class="blob-code blob-code-inner js-file-line">
</td>
</tr>
<tr>
<td id="file-addnewcity-cs-L13" class="blob-num js-line-number js-blob-rnum" data-line-number="13"></td>
<td id="file-addnewcity-cs-LC13" class="blob-code blob-code-inner js-file-line">    CountriesCitiesDbService.SaveCity(city);</td>
</tr>
<tr>
<td id="file-addnewcity-cs-L14" class="blob-num js-line-number js-blob-rnum" data-line-number="14"></td>
<td id="file-addnewcity-cs-LC14" class="blob-code blob-code-inner js-file-line">    return true;</td>
</tr>
<tr>
<td id="file-addnewcity-cs-L15" class="blob-num js-line-number js-blob-rnum" data-line-number="15"></td>
<td id="file-addnewcity-cs-LC15" class="blob-code blob-code-inner js-file-line">
</td>
</tr>
<tr>
<td id="file-addnewcity-cs-L16" class="blob-num js-line-number js-blob-rnum" data-line-number="16"></td>
<td id="file-addnewcity-cs-LC16" class="blob-code blob-code-inner js-file-line">}</td>
</tr>
</table>
</div></div>
</p></div>
</div></div>
<div class="gist-meta">
        <a href="https://gist.github.com/dsibinski/9e459fb3d6cd16567fc09f90ba948efb/raw/716d4066a91294a0b47dc8a30432887a07906b4e/AddNewCity.cs" style="float:right" class="Link--inTextBlock">view raw</a><br />
        <a href="https://gist.github.com/dsibinski/9e459fb3d6cd16567fc09f90ba948efb#file-addnewcity-cs" class="Link--inTextBlock"><br />
          AddNewCity.cs<br />
        </a><br />
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
</p></div>
</div>
<p><em>AddNewCity</em> method is called every time the &#8220;Add city&#8221; button is pressed. In line number 4. we are getting the list of all 46 European countries:</p>
<figure id="attachment_754" aria-describedby="caption-attachment-754" style="width: 483px" class="wp-caption aligncenter"><a href="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/getAllEUCountriesList.png?ssl=1"><img data-recalc-dims="1" loading="lazy" decoding="async" data-attachment-id="754" data-permalink="https://www.codejourney.net/solving-performance-issues-with-dottrace/getalleucountrieslist/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/getAllEUCountriesList.png?fit=839%2C63&amp;ssl=1" data-orig-size="839,63" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="getAllEUCountriesList" data-image-description="" data-image-caption="&lt;p&gt;Variable storing 46 EU countries&lt;/p&gt;
" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/getAllEUCountriesList.png?fit=300%2C23&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/getAllEUCountriesList.png?fit=839%2C63&amp;ssl=1" class="wp-image-754 " src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/getAllEUCountriesList.png?resize=483%2C37&#038;ssl=1" alt="" width="483" height="37" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/getAllEUCountriesList.png?resize=300%2C23&amp;ssl=1 300w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/getAllEUCountriesList.png?resize=768%2C58&amp;ssl=1 768w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/getAllEUCountriesList.png?resize=720%2C54&amp;ssl=1 720w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/getAllEUCountriesList.png?w=839&amp;ssl=1 839w" sizes="auto, (max-width: 483px) 100vw, 483px" /></a><figcaption id="caption-attachment-754" class="wp-caption-text">Variable storing 46 EU countries</figcaption></figure>
<p>How probable it is that the number of countries in Europe changes during our application&#8217;s runtime? Very close to 0, I&#8217;d say. <strong>So there is totally no sense in retrieving the list of EU countries (from the DB, web service or whatever) every time the button is clicked!</p>
<p></strong></p>
<p>We need to cache this list somehow. Let&#8217;s simply extract the variable for storing EU countries as read-only class property initialized only when used for the first time:</p>
<p><style>.gist table { margin-bottom: 0; }</style>
<div style="tab-size: 8" id="gist45630185" class="gist">
<div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
<div class="gist-data">
<div class="js-gist-file-update-container js-task-list-container">
<div id="file-europeancitiesproperty-cs" class="file my-2">
<div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-c  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="EuropeanCitiesProperty.cs content, created by dsibinski on 01:50PM on March 12, 2017."
    ></p>
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">
<p>  <template class="js-file-alert-template"></p>
<div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
    <span><br />
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.<br />
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a><br />
    </span></p>
<div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters<br />
</a>
</div>
</div>
<p></template><br />
<template class="js-line-alert-template"><br />
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e"><br />
    <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
</span></template></p>
<table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="EuropeanCitiesProperty.cs">
<tr>
<td id="file-europeancitiesproperty-cs-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
<td id="file-europeancitiesproperty-cs-LC1" class="blob-code blob-code-inner js-file-line">private List&lt;Country&gt; _europeanCountries;</td>
</tr>
<tr>
<td id="file-europeancitiesproperty-cs-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
<td id="file-europeancitiesproperty-cs-LC2" class="blob-code blob-code-inner js-file-line">
</td>
</tr>
<tr>
<td id="file-europeancitiesproperty-cs-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
<td id="file-europeancitiesproperty-cs-LC3" class="blob-code blob-code-inner js-file-line">public List&lt;Country&gt; EuropeanCountries =&gt; </td>
</tr>
<tr>
<td id="file-europeancitiesproperty-cs-L4" class="blob-num js-line-number js-blob-rnum" data-line-number="4"></td>
<td id="file-europeancitiesproperty-cs-LC4" class="blob-code blob-code-inner js-file-line">    _europeanCountries ?? (_europeanCountries = CountriesCitiesDbService.GetAllEuropeanCountries());</td>
</tr>
</table>
</div></div>
</p></div>
</div></div>
<div class="gist-meta">
        <a href="https://gist.github.com/dsibinski/5c1d7f2e77f61ac081d6b20f4cf1f86b/raw/4e3bc93235f938e4e0b2f0ac9473186230e3fe2f/EuropeanCitiesProperty.cs" style="float:right" class="Link--inTextBlock">view raw</a><br />
        <a href="https://gist.github.com/dsibinski/5c1d7f2e77f61ac081d6b20f4cf1f86b#file-europeancitiesproperty-cs" class="Link--inTextBlock"><br />
          EuropeanCitiesProperty.cs<br />
        </a><br />
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
</p></div>
</div>
<p>and use it in <em>AddNewCity</em> method:</p>
<p><style>.gist table { margin-bottom: 0; }</style>
<div style="tab-size: 8" id="gist45630262" class="gist">
<div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
<div class="gist-data">
<div class="js-gist-file-update-container js-task-list-container">
<div id="file-addnewcity_corrected-cs" class="file my-2">
<div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-c  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="AddNewCity_Corrected.cs content, created by dsibinski on 01:54PM on March 12, 2017."
    ></p>
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">
<p>  <template class="js-file-alert-template"></p>
<div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
    <span><br />
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.<br />
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a><br />
    </span></p>
<div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters<br />
</a>
</div>
</div>
<p></template><br />
<template class="js-line-alert-template"><br />
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e"><br />
    <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
</span></template></p>
<table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="AddNewCity_Corrected.cs">
<tr>
<td id="file-addnewcity_corrected-cs-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
<td id="file-addnewcity_corrected-cs-LC1" class="blob-code blob-code-inner js-file-line">private bool AddNewCity(string cityName, string countryIso)</td>
</tr>
<tr>
<td id="file-addnewcity_corrected-cs-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
<td id="file-addnewcity_corrected-cs-LC2" class="blob-code blob-code-inner js-file-line">{</td>
</tr>
<tr>
<td id="file-addnewcity_corrected-cs-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
<td id="file-addnewcity_corrected-cs-LC3" class="blob-code blob-code-inner js-file-line">    var city = CountriesCitiesDbService.GetNewCity();</td>
</tr>
<tr>
<td id="file-addnewcity_corrected-cs-L4" class="blob-num js-line-number js-blob-rnum" data-line-number="4"></td>
<td id="file-addnewcity_corrected-cs-LC4" class="blob-code blob-code-inner js-file-line">
</td>
</tr>
<tr>
<td id="file-addnewcity_corrected-cs-L5" class="blob-num js-line-number js-blob-rnum" data-line-number="5"></td>
<td id="file-addnewcity_corrected-cs-LC5" class="blob-code blob-code-inner js-file-line">    var country = EuropeanCountries.FirstOrDefault(ec =&gt; ec.IsoCode.Equals(countryIso));</td>
</tr>
<tr>
<td id="file-addnewcity_corrected-cs-L6" class="blob-num js-line-number js-blob-rnum" data-line-number="6"></td>
<td id="file-addnewcity_corrected-cs-LC6" class="blob-code blob-code-inner js-file-line">    // the rest of method&#39;s code&#8230;</td>
</tr>
<tr>
<td id="file-addnewcity_corrected-cs-L7" class="blob-num js-line-number js-blob-rnum" data-line-number="7"></td>
<td id="file-addnewcity_corrected-cs-LC7" class="blob-code blob-code-inner js-file-line">}</td>
</tr>
</table>
</div></div>
</p></div>
</div></div>
<div class="gist-meta">
        <a href="https://gist.github.com/dsibinski/e767ddb0a04208ba01729e0c460ca537/raw/0b61f0394e1fe7f7f5e37c8e7a89d603b59f010d/AddNewCity_Corrected.cs" style="float:right" class="Link--inTextBlock">view raw</a><br />
        <a href="https://gist.github.com/dsibinski/e767ddb0a04208ba01729e0c460ca537#file-addnewcity_corrected-cs" class="Link--inTextBlock"><br />
          AddNewCity_Corrected.cs<br />
        </a><br />
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
</p></div>
</div>
<p>When the application is now launched, s<em>ampling </em>started and button clicked 3 times, we already see the improvement:</p>
<figure id="attachment_759" aria-describedby="caption-attachment-759" style="width: 707px" class="wp-caption aligncenter"><a href="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/perfViewer_Corrected1.png?ssl=1"><img data-recalc-dims="1" loading="lazy" decoding="async" data-attachment-id="759" data-permalink="https://www.codejourney.net/solving-performance-issues-with-dottrace/perfviewer_corrected1/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/perfViewer_Corrected1.png?fit=1265%2C296&amp;ssl=1" data-orig-size="1265,296" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="perfViewer_Corrected1" data-image-description="" data-image-caption="&lt;p&gt;Calls after improvement&lt;/p&gt;
" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/perfViewer_Corrected1.png?fit=300%2C70&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/perfViewer_Corrected1.png?fit=1024%2C240&amp;ssl=1" class=" wp-image-759" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/perfViewer_Corrected1.png?resize=707%2C165&#038;ssl=1" alt="" width="707" height="165" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/perfViewer_Corrected1.png?resize=300%2C70&amp;ssl=1 300w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/perfViewer_Corrected1.png?resize=768%2C180&amp;ssl=1 768w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/perfViewer_Corrected1.png?resize=1024%2C240&amp;ssl=1 1024w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/perfViewer_Corrected1.png?resize=720%2C168&amp;ssl=1 720w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/perfViewer_Corrected1.png?w=1265&amp;ssl=1 1265w" sizes="auto, (max-width: 707px) 100vw, 707px" /></a><figcaption id="caption-attachment-759" class="wp-caption-text">Calls after improvement</figcaption></figure>
<p><em>AddNewCity</em> takes <strong>65.96%</strong> now. This is average value from all the calls we made, so let&#8217;s now perform <em>sampling</em> starting from the 2nd click:</p>
<figure id="attachment_761" aria-describedby="caption-attachment-761" style="width: 762px" class="wp-caption aligncenter"><a href="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/perfViewer_Corrected2.png?ssl=1"><img data-recalc-dims="1" loading="lazy" decoding="async" data-attachment-id="761" data-permalink="https://www.codejourney.net/solving-performance-issues-with-dottrace/perfviewer_corrected2/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/perfViewer_Corrected2.png?fit=1271%2C277&amp;ssl=1" data-orig-size="1271,277" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="perfViewer_Corrected2" data-image-description="" data-image-caption="&lt;p&gt;Performance omitting 1st click&lt;/p&gt;
" data-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/perfViewer_Corrected2.png?fit=300%2C65&amp;ssl=1" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/perfViewer_Corrected2.png?fit=1024%2C223&amp;ssl=1" class=" wp-image-761" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/perfViewer_Corrected2.png?resize=762%2C165&#038;ssl=1" alt="" width="762" height="165" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/perfViewer_Corrected2.png?resize=300%2C65&amp;ssl=1 300w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/perfViewer_Corrected2.png?resize=768%2C167&amp;ssl=1 768w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/perfViewer_Corrected2.png?resize=1024%2C223&amp;ssl=1 1024w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/perfViewer_Corrected2.png?resize=720%2C157&amp;ssl=1 720w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/03/perfViewer_Corrected2.png?w=1271&amp;ssl=1 1271w" sizes="auto, (max-width: 762px) 100vw, 762px" /></a><figcaption id="caption-attachment-761" class="wp-caption-text">Performance omitting 1st click</figcaption></figure>
<p>The whole <em>AddNewCity</em> method is now taking only <strong>0.41% of the calls</strong> during the operation.</p>
<p>Now, if we want to make Customer&#8217;s life beautiful, we&#8217;d extract initialization of EU countries list to another thread, so it can happen when the application launches and UI is not frozen. We will however stop here. You get the idea, don&#8217;t you ? <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<h2>Summary</h2>
<p>Today we&#8217;ve seen how easily &#8211; by performing just the simplest profiling session that can be done in <em>dotTrace </em>&#8211; it may be to detect what causes real performance issues in .NET application. Of course the example was pretty naive and simple, but I didn&#8217;t make it up &#8211; many times I&#8217;ve already worked with performance issues in production code which were caused by retrieving so called &#8220;dictionary&#8221; or static data using some API or database connection every time in a loop or repetitive process, which was totally unnecessary. By only implementing some kind of cache (maybe a bit more reasonable than the one we did, which in our case was actually enough), I managed to optimize processes executing time from 3 or 4 hours to several seconds.</p>
<p>Obviously if you&#8217;re an experienced developer, you&#8217;d probably identify such places in which caching or similar mechanism should be used when writing code. Profiling may however be useful for less experienced programmers or when working with legacy code we didn&#8217;t write (or we wrote months or years ago <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /> ). It doesn&#8217;t cost much, except the cost of <em>dotTrace </em>itself, which is a part of <a href="https://www.jetbrains.com/resharper/buy/index.jsp?product=ultimate#edition=commercial" target="_blank" rel="noopener">ReSharper Ultimate</a>, but this tool is just an example, you are not limited to it. For sure there are many other tools that offer the same (or maybe even more?) functionalities.</p>
<p><span style="text-decoration: underline;"><strong>What are the tools you use for debugging performance issues ? Maybe you also use <em>dotTrace</em>, but can share some experience in using its other functionalities?</strong></span></p>
<p>The post <a href="https://www.codejourney.net/solving-performance-issues-with-dottrace/">Solving performance issues with dotTrace</a> appeared first on <a href="https://www.codejourney.net">CodeJourney.net</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codejourney.net/solving-performance-issues-with-dottrace/feed/</wfw:commentRss>
			<slash:comments>3</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">728</post-id>	</item>
	</channel>
</rss>
