<?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>Diogo Coelho, Author at Blog IT</title>
	<atom:link href="https://blogit.create.pt/author/diogocoelho/feed/" rel="self" type="application/rss+xml" />
	<link>https://blogit.create.pt/author/diogocoelho/</link>
	<description>Create IT blogger community</description>
	<lastBuildDate>Fri, 23 Apr 2021 16:17:30 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.1</generator>
	<item>
		<title>Performance Testing An API, How To Start Doing It With JMeter?</title>
		<link>https://blogit.create.pt/diogocoelho/2021/04/23/performance-testing-an-api-how-to-start-doing-it-with-jmeter/</link>
					<comments>https://blogit.create.pt/diogocoelho/2021/04/23/performance-testing-an-api-how-to-start-doing-it-with-jmeter/#respond</comments>
		
		<dc:creator><![CDATA[Diogo Coelho]]></dc:creator>
		<pubDate>Fri, 23 Apr 2021 16:15:09 +0000</pubDate>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Automatic Testing]]></category>
		<category><![CDATA[JMeter]]></category>
		<category><![CDATA[Performance Test]]></category>
		<category><![CDATA[Testing]]></category>
		<guid isPermaLink="false">https://blogit.create.pt/?p=12241</guid>

					<description><![CDATA[<p>This article will talk about how to start using JMeter for Testing an API. How to install JMeter, what are the components of it and how to use it. It will, also, show a short introduction of what is performance testing. First&#160;of&#160;all,&#160;let&#8217;s&#160;talk&#160;about&#160;Performance&#160;Testing.&#160;What&#160;is&#160;Performance&#160;Testing? Performance&#160;Testing&#160;is&#160;a&#160;software&#160;testing&#160;process&#160;used&#160;for&#160;testing&#160;the&#160;speed,&#160;response&#160;time,&#160;stability,&#160;reliability,&#160;scalability&#160;and&#160;resource&#160;usage&#160;of&#160;a&#160;software&#160;application&#160;under&#160;a particular&#160;workload.&#160;&#160;The&#160;focus&#160;of&#160;Performance&#160;Testing&#160;is&#160;checking&#160;a&#160;software&#160;program&#8217;s Speed&#160;&#8211;&#160;Determines&#160;whether&#160;the&#160;application&#160;responds&#160;quickly Scalability&#160;&#8211;&#160;Determines&#160;the maximum user load&#160;the&#160;software&#160;application&#160;can&#160;handle. Stability&#160;&#8211;&#160;Determines&#160;if&#160;the&#160;application&#160;is&#160;stable&#160;under&#160;varying&#160;loads What&#160;is&#160;the&#160;Purpose&#160;of&#160;doing&#160;it? The&#160;main&#160;purpose&#160;of&#160;performance&#160;testing&#160;is&#160;to&#160;identify&#160;and&#160;eliminate&#160;the&#160;performance&#160;bottlenecks&#160;in&#160;the&#160;software&#160;application. Now let&#8217;s introduce [&#8230;]</p>
<p>The post <a href="https://blogit.create.pt/diogocoelho/2021/04/23/performance-testing-an-api-how-to-start-doing-it-with-jmeter/">Performance Testing An API, How To Start Doing It With JMeter?</a> appeared first on <a href="https://blogit.create.pt">Blog IT</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>This article will talk about how to start using JMeter for Testing an API. </p>



<p>How to install JMeter, what are the components of it and how to use it. It will, also, show a short introduction of what is performance testing. </p>



<h2 class="wp-block-heading"><strong>First&nbsp;of&nbsp;all,&nbsp;let&#8217;s&nbsp;talk&nbsp;about&nbsp;Performance&nbsp;Testing.&nbsp;What&nbsp;is&nbsp;Performance&nbsp;Testing?</strong></h2>



<p>Performance&nbsp;Testing&nbsp;is&nbsp;a&nbsp;software&nbsp;testing&nbsp;process&nbsp;used&nbsp;for&nbsp;testing&nbsp;the&nbsp;speed,&nbsp;response&nbsp;time,&nbsp;stability,&nbsp;reliability,&nbsp;scalability&nbsp;and&nbsp;resource&nbsp;usage&nbsp;of&nbsp;a&nbsp;software&nbsp;application&nbsp;under&nbsp;a particular&nbsp;workload.&nbsp;&nbsp;<br>The&nbsp;focus&nbsp;of&nbsp;Performance&nbsp;Testing&nbsp;is&nbsp;checking&nbsp;a&nbsp;software&nbsp;program&#8217;s</p>



<ul class="wp-block-list"><li>Speed&nbsp;&#8211;&nbsp;Determines&nbsp;whether&nbsp;the&nbsp;application&nbsp;responds&nbsp;quickly</li><li>Scalability&nbsp;&#8211;&nbsp;Determines&nbsp;the maximum user load&nbsp;the&nbsp;software&nbsp;application&nbsp;can&nbsp;handle.</li><li>Stability&nbsp;&#8211;&nbsp;Determines&nbsp;if&nbsp;the&nbsp;application&nbsp;is&nbsp;stable&nbsp;under&nbsp;varying&nbsp;loads</li></ul>



<h3 class="wp-block-heading"><strong>What&nbsp;is&nbsp;the&nbsp;Purpose&nbsp;of&nbsp;doing&nbsp;it?</strong></h3>



<p>The&nbsp;main&nbsp;purpose&nbsp;of&nbsp;performance&nbsp;testing&nbsp;is&nbsp;to&nbsp;identify&nbsp;and&nbsp;eliminate&nbsp;the&nbsp;performance&nbsp;bottlenecks&nbsp;in&nbsp;the&nbsp;software&nbsp;application.</p>



<h2 class="wp-block-heading">Now let&#8217;s introduce JMeter</h2>



<h3 class="wp-block-heading">How to install it?</h3>



<p>JMeter&nbsp;is&nbsp;an&nbsp;application&nbsp;base&nbsp;in&nbsp;java.&nbsp;This&nbsp;means,&nbsp;that&nbsp;every&nbsp;computer&nbsp;with&nbsp;java&nbsp;installed&nbsp;can&nbsp;run&nbsp;this&nbsp;application.&nbsp;So&nbsp;let&#8217;s&nbsp;see&nbsp;how&nbsp;to&nbsp;install&nbsp;it.</p>



<h3 class="wp-block-heading">1- Download Java.</h3>



<p>&nbsp;First&nbsp;of&nbsp;all,&nbsp;you&nbsp;will&nbsp;need&nbsp;to&nbsp;install&nbsp;Java&nbsp;on&nbsp;your&nbsp;computer.&nbsp;Here&nbsp;you&nbsp;can&nbsp;download&nbsp;and&nbsp;Install&nbsp;the&nbsp;latest&nbsp;version&nbsp;of&nbsp;Java&nbsp;SE&nbsp;Development&nbsp;Kit <a href="https://www.oracle.com/java/technologies/javase-downloads.html">https://www.oracle.com/java/technologies/javase-downloads.html</a>.</p>



<h3 class="wp-block-heading">2- Download JMeter. </h3>



<p>At&nbsp;the&nbsp;point&nbsp;of&nbsp;writing,&nbsp;the&nbsp;latest&nbsp;version&nbsp;is&nbsp;Apache&nbsp;JMeter&nbsp;5.4.1&nbsp;(Requires&nbsp;Java&nbsp;8+)&nbsp;and&nbsp;you&nbsp;can&nbsp;download&nbsp;it here <a href="http://jmeter.apache.org/download_jmeter.cgi">http://jmeter.apache.org/download_jmeter.cgi</a>. Choose&nbsp;the&nbsp;Binaries&nbsp;file&nbsp;(either&nbsp;apache-jmeter-5.4.1.zip&nbsp;or&nbsp;apache-jmeter-5.4.1.tgz).</p>



<h3 class="wp-block-heading">3- Installation.</h3>



<p>Installing&nbsp;Jmeter&nbsp;is&nbsp;easy!&nbsp;You&nbsp;only&nbsp;need&nbsp;to&nbsp;unzip&nbsp;the&nbsp;file&nbsp;to&nbsp;a&nbsp;directory&nbsp;of&nbsp;your&nbsp;choice&nbsp;and&nbsp;that&#8217;s&nbsp;it!</p>



<h3 class="wp-block-heading">4- Start JMeter </h3>



<p>JMeter can be used in 3 modes. These 3 modes are: </p>



<ol class="wp-block-list"><li>GUI Mode</li><li>Server Mode</li><li>Command Line Mode</li></ol>



<p>In&nbsp;this&nbsp;article,&nbsp;we&nbsp;will&nbsp;focus&nbsp;on&nbsp;<strong>GUI&nbsp;Mode</strong>.&nbsp;So,&nbsp;to&nbsp;start&nbsp;the&nbsp;GUI&nbsp;Mode,&nbsp;you&nbsp;will&nbsp;need&nbsp;to&nbsp;execute&nbsp;the&nbsp;following&nbsp;file&nbsp;<strong>/bin/ApacheJMeter.jar</strong>.&nbsp;This&nbsp;will&nbsp;open&nbsp;the&nbsp;application&nbsp;as&nbsp;the&nbsp;following&nbsp;example.</p>



<figure class="wp-block-image size-large is-resized"><img fetchpriority="high" decoding="async" src="https://blogit.create.pt/wp-content/uploads/2021/04/AppOpen-1024x560.png" alt="" class="wp-image-12245" width="1070" height="586" srcset="https://blogit.create.pt/wp-content/uploads/2021/04/AppOpen-1024x560.png 1024w, https://blogit.create.pt/wp-content/uploads/2021/04/AppOpen-300x164.png 300w, https://blogit.create.pt/wp-content/uploads/2021/04/AppOpen-768x420.png 768w, https://blogit.create.pt/wp-content/uploads/2021/04/AppOpen-1536x840.png 1536w, https://blogit.create.pt/wp-content/uploads/2021/04/AppOpen-696x381.png 696w, https://blogit.create.pt/wp-content/uploads/2021/04/AppOpen-1068x584.png 1068w, https://blogit.create.pt/wp-content/uploads/2021/04/AppOpen.png 1920w" sizes="(max-width: 1070px) 100vw, 1070px" /></figure>



<h2 class="wp-block-heading">How to use JMeter?</h2>



<p>Now&nbsp;that&nbsp;we&nbsp;have&nbsp;JMeter&nbsp;installed&nbsp;and&nbsp;running,&nbsp;we&nbsp;need&nbsp;to&nbsp;understand&nbsp;the&nbsp;components&nbsp;that&nbsp;are&nbsp;used&nbsp;to&nbsp;create&nbsp;a&nbsp;Test&nbsp;Plan.</p>



<p>When&nbsp;we&nbsp;open&nbsp;JMeter,&nbsp;we&nbsp;can&nbsp;see&nbsp;that&nbsp;there&nbsp;is&nbsp;already&nbsp;a&nbsp;&#8220;Test&nbsp;Plan&#8221;&nbsp;component.&nbsp;This&nbsp;component&nbsp;is&nbsp;where&nbsp;we&nbsp;add&nbsp;components&nbsp;for&nbsp;our&nbsp;JMeter&nbsp;Test.&nbsp;Here&nbsp;is&nbsp;an&nbsp;image&nbsp;of&nbsp;how&nbsp;these&nbsp;components&nbsp;are&nbsp;organized.</p>



<figure class="wp-block-image size-large"><img decoding="async" width="742" height="290" src="https://blogit.create.pt/wp-content/uploads/2021/04/Jmeter.png" alt="" class="wp-image-12246" srcset="https://blogit.create.pt/wp-content/uploads/2021/04/Jmeter.png 742w, https://blogit.create.pt/wp-content/uploads/2021/04/Jmeter-300x117.png 300w, https://blogit.create.pt/wp-content/uploads/2021/04/Jmeter-696x272.png 696w" sizes="(max-width: 742px) 100vw, 742px" /></figure>



<p>The&nbsp;components&nbsp;that&nbsp;we&nbsp;are&nbsp;gonna&nbsp;talk&nbsp;here&nbsp;will&nbsp;be:</p>



<ul class="wp-block-list"><li>Thread Group</li><li>Samplers </li><li>Listeners </li><li>Config Elements</li></ul>



<h4 class="wp-block-heading">Thread Group</h4>



<p>Thread&nbsp;group&nbsp;is&nbsp;a&nbsp;collection&nbsp;of&nbsp;threads.&nbsp;This&nbsp;is&nbsp;the&nbsp;component&nbsp;where&nbsp;you&nbsp;define&nbsp;how&nbsp;many&nbsp;threads&nbsp;will&nbsp;run.&nbsp;Essentially,&nbsp;each&nbsp;thread represents&nbsp;a&nbsp;user&nbsp;using&nbsp;the&nbsp;application&nbsp;under&nbsp;test.</p>



<p>The&nbsp;thread&nbsp;group&nbsp;let&#8217;s&nbsp;you&nbsp;set&nbsp;the&nbsp;value&nbsp;of&nbsp;threads&nbsp;that&nbsp;you&nbsp;want&nbsp;to&nbsp;run(simulating&nbsp;the&nbsp;number&nbsp;of&nbsp;users, that&nbsp;you&nbsp;to&nbsp;test,&nbsp;using&nbsp;your&nbsp;app).For&nbsp;example,&nbsp;if&nbsp;you&nbsp;set&nbsp;threads&nbsp;number&nbsp;to&nbsp;100.&nbsp;JMeter&nbsp;will&nbsp;create&nbsp;and&nbsp;make&nbsp;100&nbsp;request&nbsp;to&nbsp;the&nbsp;application&nbsp;under&nbsp;test.</p>



<h4 class="wp-block-heading">Samplers</h4>



<p>JMeter&nbsp;supports&nbsp;testing&nbsp;HTTP,&nbsp;FTP,&nbsp;JDBC&nbsp;and&nbsp;many&nbsp;other&nbsp;protocols.</p>



<p>We&nbsp;already&nbsp;know&nbsp;that&nbsp;Thread&nbsp;Groups&nbsp;simulate&nbsp;user&nbsp;request&nbsp;to&nbsp;the&nbsp;server, but&nbsp;how&nbsp;does&nbsp;a&nbsp;Thread&nbsp;Group&nbsp;know&nbsp;which&nbsp;type&nbsp;of&nbsp;requests&nbsp;(HTTP,&nbsp;FTP&nbsp;etc.)&nbsp;it&nbsp;needs&nbsp;to&nbsp;make?&nbsp;The&nbsp;answer&nbsp;is&nbsp;Samplers.</p>



<p>The&nbsp;user&nbsp;request&nbsp;could&nbsp;be&nbsp;FTP&nbsp;Request,&nbsp;HTTP&nbsp;Request,&nbsp;JDBC&nbsp;Request, etc. In&nbsp;this&nbsp;example, we&nbsp;will&nbsp;concentrate&nbsp;in<strong> HTTP&nbsp;Request</strong>.</p>



<h4 class="wp-block-heading">Listeners</h4>



<p>Listeners&nbsp;shows&nbsp;the&nbsp;results&nbsp;of&nbsp;the&nbsp;test&nbsp;execution.&nbsp;They&nbsp;can&nbsp;show&nbsp;results&nbsp;in&nbsp;a&nbsp;different&nbsp;format&nbsp;such&nbsp;as&nbsp;a&nbsp;tree,&nbsp;table,&nbsp;graph&nbsp;or&nbsp;log&nbsp;file.</p>



<p>Graph&nbsp;result&nbsp;listeners&nbsp;display&nbsp;the&nbsp;server&nbsp;response&nbsp;times&nbsp;on&nbsp;a&nbsp;Graph.</p>



<p>View&nbsp;Result&nbsp;Tree&nbsp;show&nbsp;results&nbsp;of&nbsp;the&nbsp;user&nbsp;request&nbsp;in&nbsp;basic&nbsp;HTML&nbsp;format.</p>



<p>Table&nbsp;Result&nbsp;show&nbsp;summary&nbsp;of&nbsp;a&nbsp;test&nbsp;result&nbsp;in&nbsp;table&nbsp;format.</p>



<p>Log&nbsp;show&nbsp;summary&nbsp;of&nbsp;a&nbsp;test&nbsp;result&nbsp;in&nbsp;the&nbsp;text&nbsp;file.</p>



<h4 class="wp-block-heading">Config Elements</h4>



<p>Set&nbsp;up&nbsp;defaults&nbsp;and&nbsp;variables&nbsp;for&nbsp;later&nbsp;use&nbsp;by&nbsp;samplers.</p>



<p>The&nbsp;figure&nbsp;below&nbsp;shows&nbsp;some&nbsp;commonly&nbsp;used&nbsp;configuration&nbsp;elements&nbsp;in&nbsp;JMeter.</p>



<figure class="wp-block-image size-large is-resized"><img decoding="async" src="https://blogit.create.pt/wp-content/uploads/2021/04/ConfigurationElements.png" alt="" class="wp-image-12248" width="1058" height="277" srcset="https://blogit.create.pt/wp-content/uploads/2021/04/ConfigurationElements.png 632w, https://blogit.create.pt/wp-content/uploads/2021/04/ConfigurationElements-300x78.png 300w" sizes="(max-width: 1058px) 100vw, 1058px" /></figure>



<h3 class="wp-block-heading">Let&#8217;s create a test</h3>



<p>As&nbsp;was&nbsp;shown&nbsp;before,&nbsp;JMeter&nbsp;starts&nbsp;with&nbsp;a&nbsp;Test&nbsp;Plan&nbsp;Component&nbsp;already&nbsp;created&nbsp;(you&nbsp;can&nbsp;change&nbsp;the&nbsp;name&nbsp;to&nbsp;anything&nbsp;more&nbsp;suitable&nbsp;to&nbsp;your&nbsp;case).&nbsp;</p>



<p>Now&nbsp;let&#8217;s&nbsp;start&nbsp;creating&nbsp;our&nbsp;test.&nbsp;In&nbsp;this&nbsp;case&nbsp;we&nbsp;will&nbsp;use&nbsp;<a href="https://my-json-server.typicode.com/">https://my-json-server.typicode.com/</a>&nbsp;to&nbsp;simulate&nbsp;an&nbsp;API. </p>



<p>Initially,&nbsp;let&#8217;s&nbsp;add&nbsp;a&nbsp;Thread&nbsp;Group.&nbsp;Right&nbsp;click&nbsp;on&nbsp;the&nbsp;test&nbsp;plan,&nbsp;then&nbsp;<strong>Add&nbsp;-&gt;&nbsp;Threads(Users)&nbsp;-&gt;&nbsp;Thread&nbsp;Group</strong>.</p>



<figure class="wp-block-image size-large is-resized"><img decoding="async" src="https://blogit.create.pt/wp-content/uploads/2021/04/ThreadGroup-1024x576.png" alt="" class="wp-image-12250" width="1069" height="602" srcset="https://blogit.create.pt/wp-content/uploads/2021/04/ThreadGroup-1024x576.png 1024w, https://blogit.create.pt/wp-content/uploads/2021/04/ThreadGroup-300x169.png 300w, https://blogit.create.pt/wp-content/uploads/2021/04/ThreadGroup-768x432.png 768w, https://blogit.create.pt/wp-content/uploads/2021/04/ThreadGroup-1536x864.png 1536w, https://blogit.create.pt/wp-content/uploads/2021/04/ThreadGroup-696x392.png 696w, https://blogit.create.pt/wp-content/uploads/2021/04/ThreadGroup-1068x601.png 1068w, https://blogit.create.pt/wp-content/uploads/2021/04/ThreadGroup-747x420.png 747w, https://blogit.create.pt/wp-content/uploads/2021/04/ThreadGroup.png 1920w" sizes="(max-width: 1069px) 100vw, 1069px" /></figure>



<p>You&nbsp;can&nbsp;see&nbsp;that&nbsp;there&nbsp;is&nbsp;some&nbsp;settings&nbsp;that&nbsp;we&nbsp;need&nbsp;to&nbsp;adjust.&nbsp;At&nbsp;the&nbsp;top,&nbsp;there&nbsp;is&nbsp;a&nbsp;setting&nbsp;that&nbsp;says&nbsp;Action&nbsp;to&nbsp;be&nbsp;taken&nbsp;after&nbsp;a&nbsp;Sampler&nbsp;Error.&nbsp;Normally,&nbsp;I&nbsp;always&nbsp;use&nbsp;this&nbsp;in&nbsp;<strong>Continue</strong>&nbsp;but&nbsp;you&nbsp;can&nbsp;use&nbsp;the&nbsp;one&nbsp;that&nbsp;suits&nbsp;your&nbsp;case.</p>



<p>The&nbsp;next&nbsp;3&nbsp;settings&nbsp;will&nbsp;be&nbsp;the&nbsp;ones&nbsp;that&nbsp;we&nbsp;gonna&nbsp;focus&nbsp;for&nbsp;now.</p>



<ul class="wp-block-list"><li>Number&nbsp;of&nbsp;Threads&nbsp;(users)&nbsp;&#8211;&nbsp;is&nbsp;the&nbsp;number&nbsp;of&nbsp;threads(users)&nbsp;that&nbsp;will&nbsp;be&nbsp;running&nbsp;your&nbsp;test&nbsp;plan.</li><li>Ramp-up&nbsp;period&nbsp;(seconds)&nbsp;&#8211;&nbsp;is&nbsp;the&nbsp;value&nbsp;in&nbsp;seconds&nbsp;that&nbsp;defines&nbsp;how&nbsp;much&nbsp;time&nbsp;until&nbsp;all&nbsp;threads&nbsp;are&nbsp;working.&nbsp;Example:&nbsp;100&nbsp;Threads&nbsp;and&nbsp;10&nbsp;seconds&nbsp;of&nbsp;ramp-up&nbsp;period,&nbsp;this&nbsp;means&nbsp;that&nbsp;the&nbsp;threads&nbsp;will&nbsp;start,&nbsp;gradually,&nbsp;until&nbsp;at&nbsp;the&nbsp;point&nbsp;of&nbsp;10&nbsp;seconds&nbsp;all&nbsp;100&nbsp;threads&nbsp;will&nbsp;be&nbsp;working.</li><li>Loop&nbsp;Count&nbsp;&#8211;&nbsp;You&nbsp;can&nbsp;set&nbsp;this&nbsp;to&nbsp;the <strong>Infinite</strong>&nbsp;and&nbsp;will&nbsp;only&nbsp;stop&nbsp;threads&nbsp;when&nbsp;you&nbsp;press&nbsp;the&nbsp;button&nbsp;to&nbsp;stop.&nbsp;Also,&nbsp;you&nbsp;can&nbsp;set&nbsp;a&nbsp;loop&nbsp;count,&nbsp;that&nbsp;is&nbsp;the&nbsp;times&nbsp;that&nbsp;each&nbsp;thread&nbsp;will&nbsp;execute&nbsp;the&nbsp;test&nbsp;plan.&nbsp;Example,&nbsp;if&nbsp;you&nbsp;put&nbsp;the&nbsp;value&nbsp;3,&nbsp;the&nbsp;threads&nbsp;will&nbsp;execute&nbsp;the&nbsp;plan&nbsp;3&nbsp;times&nbsp;and&nbsp;then&nbsp;will&nbsp;stop.</li></ul>



<p>So,&nbsp;in&nbsp;our&nbsp;example&nbsp;lets&nbsp;try&nbsp;10&nbsp;threads,&nbsp;with&nbsp;5&nbsp;seconds&nbsp;of&nbsp;Ramp-up&nbsp;Time&nbsp;and&nbsp;the&nbsp;value&nbsp;3&nbsp;in&nbsp;the&nbsp;loop&nbsp;count.&nbsp;I&nbsp;will&nbsp;also&nbsp;change&nbsp;the&nbsp;name&nbsp;of&nbsp;the&nbsp;thread&nbsp;group&nbsp;to&nbsp;know&nbsp;how&nbsp;many&nbsp;threads&nbsp;I&nbsp;am&nbsp;using.&nbsp;It&nbsp;Should&nbsp;look&nbsp;like&nbsp;this.</p>



<figure class="wp-block-image size-large is-resized"><img decoding="async" src="https://blogit.create.pt/wp-content/uploads/2021/04/10Threads-1024x576.png" alt="" class="wp-image-12251" width="1068" height="602" srcset="https://blogit.create.pt/wp-content/uploads/2021/04/10Threads-1024x576.png 1024w, https://blogit.create.pt/wp-content/uploads/2021/04/10Threads-300x169.png 300w, https://blogit.create.pt/wp-content/uploads/2021/04/10Threads-768x432.png 768w, https://blogit.create.pt/wp-content/uploads/2021/04/10Threads-1536x864.png 1536w, https://blogit.create.pt/wp-content/uploads/2021/04/10Threads-696x392.png 696w, https://blogit.create.pt/wp-content/uploads/2021/04/10Threads-1068x601.png 1068w, https://blogit.create.pt/wp-content/uploads/2021/04/10Threads-747x420.png 747w, https://blogit.create.pt/wp-content/uploads/2021/04/10Threads.png 1920w" sizes="(max-width: 1068px) 100vw, 1068px" /></figure>



<p>Let&#8217;s&nbsp;continue&nbsp;adding&nbsp;more&nbsp;components.&nbsp;This&nbsp;time, let&#8217;s&nbsp;add&nbsp;a&nbsp;way&nbsp;to&nbsp;define&nbsp;variables.&nbsp;Right-click&nbsp;on&nbsp;the&nbsp;Thread&nbsp;Group,&nbsp;then&nbsp;<strong>Add&nbsp;-&gt;&nbsp;Config-Element&nbsp;-&gt;&nbsp;User&nbsp;Defined&nbsp;Variables</strong>.&nbsp;</p>



<figure class="wp-block-image size-large is-resized"><img decoding="async" src="https://blogit.create.pt/wp-content/uploads/2021/04/Variables-1024x251.png" alt="" class="wp-image-12252" width="1067" height="262" srcset="https://blogit.create.pt/wp-content/uploads/2021/04/Variables-1024x251.png 1024w, https://blogit.create.pt/wp-content/uploads/2021/04/Variables-300x73.png 300w, https://blogit.create.pt/wp-content/uploads/2021/04/Variables-768x188.png 768w, https://blogit.create.pt/wp-content/uploads/2021/04/Variables-1536x376.png 1536w, https://blogit.create.pt/wp-content/uploads/2021/04/Variables-696x170.png 696w, https://blogit.create.pt/wp-content/uploads/2021/04/Variables-1068x261.png 1068w, https://blogit.create.pt/wp-content/uploads/2021/04/Variables-1716x420.png 1716w, https://blogit.create.pt/wp-content/uploads/2021/04/Variables.png 1920w" sizes="(max-width: 1067px) 100vw, 1067px" /></figure>



<p>This&nbsp;component&nbsp;will&nbsp;let&nbsp;you&nbsp;define&nbsp;some&nbsp;variable&nbsp;to&nbsp;use&nbsp;afterwards.&nbsp;Let&#8217;s&nbsp;define&nbsp;some.</p>



<ul class="wp-block-list"><li>url:&nbsp;my-json-server.typicode.com</li><li>postId:&nbsp;1</li></ul>



<p>&nbsp;Now,&nbsp;to&nbsp;use&nbsp;these&nbsp;variables&nbsp;in&nbsp;other&nbsp;components&nbsp;you&nbsp;will&nbsp;need&nbsp;to&nbsp;do&nbsp;it&nbsp;in&nbsp;the&nbsp;following&nbsp;way.</p>



<ul class="wp-block-list"><li>${url}&nbsp;for&nbsp;the&nbsp;<strong>url</strong>&nbsp;Value&nbsp;</li><li>${postId}&nbsp;for&nbsp;the&nbsp;<strong>postId</strong>&nbsp;Value</li></ul>



<figure class="wp-block-image size-large is-resized"><img decoding="async" src="https://blogit.create.pt/wp-content/uploads/2021/04/userVariable-1024x203.png" alt="" class="wp-image-12253" width="1069" height="213" srcset="https://blogit.create.pt/wp-content/uploads/2021/04/userVariable-1024x203.png 1024w, https://blogit.create.pt/wp-content/uploads/2021/04/userVariable-300x59.png 300w, https://blogit.create.pt/wp-content/uploads/2021/04/userVariable-768x152.png 768w, https://blogit.create.pt/wp-content/uploads/2021/04/userVariable-1536x304.png 1536w, https://blogit.create.pt/wp-content/uploads/2021/04/userVariable-696x138.png 696w, https://blogit.create.pt/wp-content/uploads/2021/04/userVariable-1068x212.png 1068w, https://blogit.create.pt/wp-content/uploads/2021/04/userVariable.png 1923w" sizes="(max-width: 1069px) 100vw, 1069px" /></figure>



<p>Finally,&nbsp;let&#8217;s&nbsp;add&nbsp;some&nbsp;real&nbsp;requests.&nbsp;Here&nbsp;as&nbsp;example&nbsp;we&nbsp;will&nbsp;make&nbsp;3&nbsp;different&nbsp;requests.&nbsp;Http&nbsp;Request&nbsp;to&nbsp;get&nbsp;all&nbsp;posts,&nbsp;Http&nbsp;Request&nbsp;to&nbsp;get&nbsp;one&nbsp;Post,&nbsp;and&nbsp;Http&nbsp;Request&nbsp;to&nbsp;Post&nbsp;a&nbsp;new&nbsp;Post.&nbsp;Remember&nbsp;that&nbsp;the&nbsp;site&nbsp;used&nbsp;in&nbsp;this&nbsp;example&nbsp;has&nbsp;paths&nbsp;to&nbsp;request&nbsp;posts&nbsp;and&nbsp;to&nbsp;add&nbsp;posts.</p>



<p>How&nbsp;to&nbsp;add&nbsp;an&nbsp;HTTP&nbsp;Request&nbsp;?&nbsp;Right-click&nbsp;on&nbsp;the&nbsp;Thread&nbsp;Group,&nbsp;then&nbsp;<strong>Add&nbsp;-&gt;&nbsp;Smapler&nbsp;-&gt;&nbsp;Http&nbsp;Request</strong>.</p>



<figure class="wp-block-image size-large is-resized"><img decoding="async" src="https://blogit.create.pt/wp-content/uploads/2021/04/HttpRequest-1024x277.png" alt="" class="wp-image-12255" width="1069" height="290" srcset="https://blogit.create.pt/wp-content/uploads/2021/04/HttpRequest-1024x277.png 1024w, https://blogit.create.pt/wp-content/uploads/2021/04/HttpRequest-300x81.png 300w, https://blogit.create.pt/wp-content/uploads/2021/04/HttpRequest-768x208.png 768w, https://blogit.create.pt/wp-content/uploads/2021/04/HttpRequest-1536x416.png 1536w, https://blogit.create.pt/wp-content/uploads/2021/04/HttpRequest-696x188.png 696w, https://blogit.create.pt/wp-content/uploads/2021/04/HttpRequest-1068x289.png 1068w, https://blogit.create.pt/wp-content/uploads/2021/04/HttpRequest-1551x420.png 1551w, https://blogit.create.pt/wp-content/uploads/2021/04/HttpRequest.png 1935w" sizes="(max-width: 1069px) 100vw, 1069px" /></figure>



<p>Now&nbsp;that&nbsp;we&nbsp;have&nbsp;an&nbsp;Http&nbsp;Request&nbsp;component,&nbsp;let&#8217;s&nbsp;configure&nbsp;to&nbsp;make&nbsp;a&nbsp;Http&nbsp;Request&nbsp;to&nbsp;get&nbsp;all&nbsp;posts.&nbsp;The&nbsp;path&nbsp;to&nbsp;this&nbsp;request&nbsp;is&nbsp;<span style="text-decoration: underline">https://jsonplaceholder.typicode.com/posts</span>. The&nbsp;configurations&nbsp;of&nbsp;this&nbsp;component&nbsp;are:</p>



<ul class="wp-block-list"><li><strong>Protocol</strong>&nbsp;-&gt;&nbsp;&#8220;https&#8221;</li><li><strong>Server&nbsp;Name&nbsp;or&nbsp;IP</strong>&nbsp;-&gt;&nbsp;${url}&nbsp;(because&nbsp;we&nbsp;have&nbsp;this&nbsp;value&nbsp;defined&nbsp;in&nbsp;a&nbsp;variable)</li><li><strong>Port&nbsp;Number</strong> -&gt;&nbsp;in&nbsp;this&nbsp;case, nothing but&nbsp;if&nbsp;you&nbsp;need&nbsp;to&nbsp;your&nbsp;specific&nbsp;case,&nbsp;here&nbsp;is&nbsp;where&nbsp;you&nbsp;put&nbsp;the&nbsp;port&nbsp;number.&nbsp;Example:&nbsp;to&nbsp;make&nbsp;a&nbsp;request&nbsp;to&nbsp;https://localhost:80,&nbsp;you&nbsp;would&nbsp;need&nbsp;to&nbsp;put&nbsp;the&nbsp;value&nbsp;80&nbsp;in&nbsp;here.</li><li><strong>HTTP&nbsp;Request</strong>&nbsp;-&gt;&nbsp;GET</li><li><strong>Path</strong>&nbsp;-&gt;&nbsp;&#8220;/typicode/demo/posts&#8221;</li></ul>



<p>Now&nbsp;that&nbsp;we&nbsp;have&nbsp;everything,&nbsp;let&#8217;s&nbsp;run&nbsp;?&nbsp;Click&nbsp;on&nbsp;the&nbsp;start&nbsp;Button&nbsp;above&nbsp;and&nbsp;see&nbsp;what&nbsp;happens.&nbsp;Nothing&nbsp;?&nbsp;Stop&nbsp;the&nbsp;process&nbsp;and&nbsp;let&#8217;s&nbsp;see&nbsp;what&nbsp;is&nbsp;missing.</p>



<p>The&nbsp;piece&nbsp;that&nbsp;is&nbsp;missing&nbsp;is&nbsp;<strong>listeners</strong>.&nbsp;We&nbsp;need&nbsp;listeners&nbsp;to&nbsp;understand&nbsp;what&nbsp;is&nbsp;happening.&nbsp;In&nbsp;this&nbsp;case,&nbsp;the&nbsp;<strong>View&nbsp;Results&nbsp;Tree</strong>&nbsp;and&nbsp;<strong>Summary&nbsp;Report</strong>.&nbsp;Let&#8217;s&nbsp;add&nbsp;these&nbsp;two&nbsp;listeners&nbsp;by&nbsp;Right-clicking&nbsp;in&nbsp;the&nbsp;Thread&nbsp;Group,&nbsp;then&nbsp;<strong>Add&nbsp;-&gt;&nbsp;Listener&nbsp;-&gt;&nbsp;View&nbsp;Results&nbsp;Tree</strong>&nbsp;and&nbsp;<strong>Add&nbsp;-&gt;&nbsp;Listener&nbsp;-&gt;&nbsp;Summary&nbsp;Report</strong>.</p>



<figure class="wp-block-image size-large is-resized"><img decoding="async" src="https://blogit.create.pt/wp-content/uploads/2021/04/Listeners-1024x246.png" alt="" class="wp-image-12256" width="1070" height="258" srcset="https://blogit.create.pt/wp-content/uploads/2021/04/Listeners-1024x246.png 1024w, https://blogit.create.pt/wp-content/uploads/2021/04/Listeners-300x72.png 300w, https://blogit.create.pt/wp-content/uploads/2021/04/Listeners-768x184.png 768w, https://blogit.create.pt/wp-content/uploads/2021/04/Listeners-1536x369.png 1536w, https://blogit.create.pt/wp-content/uploads/2021/04/Listeners-696x167.png 696w, https://blogit.create.pt/wp-content/uploads/2021/04/Listeners-1750x420.png 1750w, https://blogit.create.pt/wp-content/uploads/2021/04/Listeners.png 1908w" sizes="(max-width: 1070px) 100vw, 1070px" /></figure>



<p>Now,&nbsp;if&nbsp;you&nbsp;start&nbsp;the&nbsp;process,&nbsp;you&nbsp;will&nbsp;see&nbsp;that&nbsp;these&nbsp;listeners&nbsp;represent&nbsp;information&nbsp;about&nbsp;the&nbsp;request.&nbsp;</p>



<p>The&nbsp;View&nbsp;Results&nbsp;Tree&nbsp;show&nbsp;every&nbsp;request&nbsp;information.&nbsp;In&nbsp;here,&nbsp;you&nbsp;can&nbsp;see&nbsp;the&nbsp;request&nbsp;body&nbsp;and&nbsp;headers,&nbsp;and&nbsp;the&nbsp;response&nbsp;body&nbsp;and&nbsp;headers.</p>



<p>In&nbsp;this&nbsp;example,&nbsp;when&nbsp;we&nbsp;choose&nbsp;the&nbsp;<strong>Response&nbsp;Body</strong>&nbsp;of&nbsp;one&nbsp;request&nbsp;we&nbsp;will&nbsp;see&nbsp;that&nbsp;there&nbsp;will&nbsp;be&nbsp;a&nbsp;JSON&nbsp;with&nbsp;data&nbsp;relative&nbsp;to&nbsp;what&nbsp;they&nbsp;call&nbsp;as&nbsp;posts.</p>



<figure class="wp-block-image size-large is-resized"><img decoding="async" src="https://blogit.create.pt/wp-content/uploads/2021/04/PostsExample-1024x551.png" alt="" class="wp-image-12257" width="1072" height="578" srcset="https://blogit.create.pt/wp-content/uploads/2021/04/PostsExample-1024x551.png 1024w, https://blogit.create.pt/wp-content/uploads/2021/04/PostsExample-300x161.png 300w, https://blogit.create.pt/wp-content/uploads/2021/04/PostsExample-768x413.png 768w, https://blogit.create.pt/wp-content/uploads/2021/04/PostsExample-1536x827.png 1536w, https://blogit.create.pt/wp-content/uploads/2021/04/PostsExample-696x375.png 696w, https://blogit.create.pt/wp-content/uploads/2021/04/PostsExample-1068x575.png 1068w, https://blogit.create.pt/wp-content/uploads/2021/04/PostsExample-780x420.png 780w, https://blogit.create.pt/wp-content/uploads/2021/04/PostsExample.png 1906w" sizes="(max-width: 1072px) 100vw, 1072px" /></figure>



<p>The&nbsp;Summary&nbsp;Report&nbsp;Aggregates&nbsp;the&nbsp;information&nbsp;of&nbsp;different&nbsp;types&nbsp;of&nbsp;request.&nbsp;In&nbsp;this&nbsp;case,&nbsp;we&nbsp;only&nbsp;have&nbsp;one&nbsp;request,&nbsp;so,&nbsp;it&nbsp;will&nbsp;show&nbsp;informations&nbsp;about&nbsp;that&nbsp;request.What&nbsp;are&nbsp;the&nbsp;informations&nbsp;that&nbsp;this&nbsp;will&nbsp;show&nbsp;?&nbsp;</p>



<ul class="wp-block-list"><li>#Samples&nbsp;&#8211;&nbsp;are&nbsp;the&nbsp;number&nbsp;of&nbsp;requests&nbsp;made&nbsp;</li><li>Average&nbsp;&#8211;&nbsp;shows&nbsp;&nbsp;the&nbsp;average&nbsp;time&nbsp;needed&nbsp;to&nbsp;make&nbsp;these&nbsp;requests&nbsp;</li><li>Min&nbsp;&#8211;&nbsp;shows&nbsp;the&nbsp;value&nbsp;of&nbsp;the&nbsp;request&nbsp;that&nbsp;take&nbsp;less&nbsp;time&nbsp;to&nbsp;finish</li><li>Max&nbsp;&#8211;&nbsp;shows&nbsp;the&nbsp;value&nbsp;of&nbsp;the&nbsp;request&nbsp;that&nbsp;take&nbsp;more&nbsp;time&nbsp;to&nbsp;finish</li><li>Std.&nbsp;Dev.&nbsp;&#8211;&nbsp;shows&nbsp;the&nbsp;standard&nbsp;deviation&nbsp;of&nbsp;the&nbsp;request&nbsp;times&nbsp;to&nbsp;the&nbsp;average</li><li>Error&nbsp;%&nbsp;&#8211;&nbsp;shows&nbsp;the&nbsp;percentage&nbsp;of&nbsp;requests&nbsp;that&nbsp;failed</li><li>Throughput&nbsp;&#8211;&nbsp;shows&nbsp;the&nbsp;number&nbsp;of&nbsp;requests&nbsp;processed&nbsp;by&nbsp;time&nbsp;</li><li>Received&nbsp;KB/sec&nbsp;&#8211;&nbsp;&nbsp;shows&nbsp;the&nbsp;amount&nbsp;of&nbsp;data&nbsp;downloaded&nbsp;from&nbsp;the&nbsp;server&nbsp;&nbsp;time</li><li>Sent&nbsp;KB/sec&nbsp;&#8211;&nbsp;shows&nbsp;the&nbsp;amount&nbsp;of&nbsp;data&nbsp;uploaded&nbsp;to&nbsp;the&nbsp;server&nbsp;time&nbsp;</li><li>Avg.&nbsp;Bytes&nbsp;&#8211;&nbsp;shows&nbsp;the&nbsp;average&nbsp;size&nbsp;of&nbsp;the&nbsp;response&nbsp;sample</li></ul>



<figure class="wp-block-image size-large is-resized"><img decoding="async" src="https://blogit.create.pt/wp-content/uploads/2021/04/SummaryReport-1024x181.png" alt="" class="wp-image-12259" width="1069" height="190" srcset="https://blogit.create.pt/wp-content/uploads/2021/04/SummaryReport-1024x181.png 1024w, https://blogit.create.pt/wp-content/uploads/2021/04/SummaryReport-300x53.png 300w, https://blogit.create.pt/wp-content/uploads/2021/04/SummaryReport-768x136.png 768w, https://blogit.create.pt/wp-content/uploads/2021/04/SummaryReport-1536x271.png 1536w, https://blogit.create.pt/wp-content/uploads/2021/04/SummaryReport-696x123.png 696w, https://blogit.create.pt/wp-content/uploads/2021/04/SummaryReport-1068x189.png 1068w, https://blogit.create.pt/wp-content/uploads/2021/04/SummaryReport.png 1920w" sizes="(max-width: 1069px) 100vw, 1069px" /></figure>



<p>We&nbsp;have&nbsp;all&nbsp;we&nbsp;need,&nbsp;let&#8217;s&nbsp;add&nbsp;more&nbsp;Requests.&nbsp;Let&#8217;s&nbsp;create&nbsp;a&nbsp;request&nbsp;that&nbsp;creates&nbsp;a&nbsp;post,&nbsp;and&nbsp;create&nbsp;a&nbsp;request&nbsp;that&nbsp;obtains&nbsp;only&nbsp;one&nbsp;post.&nbsp;Also,&nbsp;change&nbsp;the&nbsp;name&nbsp;of&nbsp;the&nbsp;Http&nbsp;Request&nbsp;components&nbsp;to&nbsp;now&nbsp;what&nbsp;is&nbsp;the&nbsp;correspondent&nbsp;request.&nbsp;Remember&nbsp;to&nbsp;put&nbsp;the&nbsp;Http&nbsp;Request&nbsp;before&nbsp;the&nbsp;listeners.&nbsp;The&nbsp;Listeners&nbsp;will&nbsp;only&nbsp;listen&nbsp;to&nbsp;what&nbsp;are&nbsp;before&nbsp;them.&nbsp;Remember&nbsp;this!&nbsp;</p>



<p>The&nbsp;create&nbsp;a&nbsp;post&nbsp;request&nbsp;configurations&nbsp;are:</p>



<ol class="wp-block-list"><li><strong>Protocol</strong>&nbsp;-&gt;&nbsp;&#8220;https&#8221;</li><li><strong>Server&nbsp;Name&nbsp;or&nbsp;IP</strong>&nbsp;-&gt;&nbsp;${url}&nbsp;(because&nbsp;we&nbsp;have&nbsp;this&nbsp;value&nbsp;defined&nbsp;in&nbsp;a&nbsp;variable)</li><li><strong>HTTP&nbsp;Request</strong>&nbsp;-&gt;&nbsp;Post</li><li><strong>Path</strong>&nbsp;-&gt;&nbsp;&#8220;/typicode/demo/posts&#8221;</li><li><strong>Body&nbsp;Data</strong>&nbsp;&#8211;&nbsp;we&nbsp;will&nbsp;put&nbsp;a&nbsp;JSON&nbsp;like&nbsp;this&nbsp;{&#8220;title&#8221;:&nbsp;&#8220;Car&nbsp;Crash&#8221;}</li></ol>



<figure class="wp-block-image size-large is-resized"><img decoding="async" src="https://blogit.create.pt/wp-content/uploads/2021/04/CreatePost-1024x306.png" alt="" class="wp-image-12261" width="1086" height="326" srcset="https://blogit.create.pt/wp-content/uploads/2021/04/CreatePost-1024x306.png 1024w, https://blogit.create.pt/wp-content/uploads/2021/04/CreatePost-300x90.png 300w, https://blogit.create.pt/wp-content/uploads/2021/04/CreatePost-1536x459.png 1536w, https://blogit.create.pt/wp-content/uploads/2021/04/CreatePost-696x208.png 696w" sizes="(max-width: 1086px) 100vw, 1086px" /></figure>



<p>And,&nbsp;The&nbsp;get&nbsp;one&nbsp;post&nbsp;request&nbsp;configurations&nbsp;are:</p>



<ol class="wp-block-list" id="block-add80d93-dc9f-41db-9cbc-14ed3602e60e"><li><strong>Protocol</strong>&nbsp;-&gt;&nbsp;&#8220;https&#8221;</li><li><strong>Server&nbsp;Name&nbsp;or&nbsp;IP</strong>&nbsp;-&gt;&nbsp;${url}&nbsp;(because&nbsp;we&nbsp;have&nbsp;this&nbsp;value&nbsp;defined&nbsp;in&nbsp;a&nbsp;variable)</li><li><strong>HTTP&nbsp;Request</strong>&nbsp;-&gt;&nbsp;Get</li><li><strong>Path</strong>&nbsp;-&gt;&nbsp;&#8220;/typicode/demo/posts/${postId}&#8221;</li></ol>



<p>Run&nbsp;the&nbsp;test&nbsp;plan&nbsp;and&nbsp;see&nbsp;the&nbsp;results.&nbsp;Try&nbsp;to&nbsp;do&nbsp;it&nbsp;for&nbsp;another&nbsp;API.&nbsp;</p>



<p>These&nbsp;are&nbsp;the&nbsp;basic&nbsp;examples&nbsp;that&nbsp;we&nbsp;have&nbsp;for&nbsp;you&nbsp;right&nbsp;now.&nbsp;More&nbsp;advanced&nbsp;tips&nbsp;will&nbsp;come.&nbsp;</p>



<p>See&nbsp;you&nbsp;next&nbsp;time.</p>



<h3 class="wp-block-heading">References</h3>



<ul class="wp-block-list"><li><a href="https://www.guru99.com/jmeter-element-reference.html ">https://www.guru99.com/jmeter-element-reference.html</a></li><li><a href="https://www.guru99.com/hands-on-with-jmeter-gui.html">https://www.guru99.com/hands-on-with-jmeter-gui.html</a></li></ul>



<p></p>
<p>The post <a href="https://blogit.create.pt/diogocoelho/2021/04/23/performance-testing-an-api-how-to-start-doing-it-with-jmeter/">Performance Testing An API, How To Start Doing It With JMeter?</a> appeared first on <a href="https://blogit.create.pt">Blog IT</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blogit.create.pt/diogocoelho/2021/04/23/performance-testing-an-api-how-to-start-doing-it-with-jmeter/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>How to Handle known Exceptions?</title>
		<link>https://blogit.create.pt/diogocoelho/2020/10/22/how-to-handle-known-exceptions/</link>
					<comments>https://blogit.create.pt/diogocoelho/2020/10/22/how-to-handle-known-exceptions/#respond</comments>
		
		<dc:creator><![CDATA[Diogo Coelho]]></dc:creator>
		<pubDate>Thu, 22 Oct 2020 18:11:58 +0000</pubDate>
				<category><![CDATA[Design/Integration Patterns]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[Exception]]></category>
		<category><![CDATA[Exception Handling]]></category>
		<guid isPermaLink="false">https://blogit.create.pt/?p=12069</guid>

					<description><![CDATA[<p>This post will describe several ways to handle known Exceptions in a project. For each possible implementation, we will do an overview of the flow that the exception does to understand the context of each implementation. First, let’s do an overview of what is, normally, a basic flow in any Project. The basic flow of [&#8230;]</p>
<p>The post <a href="https://blogit.create.pt/diogocoelho/2020/10/22/how-to-handle-known-exceptions/">How to Handle known Exceptions?</a> appeared first on <a href="https://blogit.create.pt">Blog IT</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>This post will describe several ways to handle known <strong>Exceptions </strong>in a project.</p>



<p>For each possible implementation, we will do an overview of the flow that the exception does to understand the context of each implementation.</p>



<p>First, let’s do an overview of what is, normally, a basic flow in any Project.</p>



<h2 class="wp-block-heading">The basic flow of an API Project</h2>



<h2 class="wp-block-heading"></h2>



<figure class="wp-block-image size-large is-resized"><img decoding="async" src="https://blogit.create.pt/wp-content/uploads/2020/10/ControllerServiceRepositoryFlow-1.png" alt="" class="wp-image-12086" width="1064" height="641" /></figure>



<p><em>The previous Figure shows the basic flow of events inside the IDM project. The API receives an HTTP Request in a certain endpoint. The endpoints of the API are mapped in the controller layer, so when the API receives the HTTP request the controller is the first to interact with that request and sends that request to the respective service that will orchestrate all the necessities of that request. Those necessities could be, for example, retrieve some information from the Database, and that is done in the repository layer. The repository layer is where we map all the interactions with external services(DataBases, API, etc). This is a basic overview of what could be a basic API project.</em></p>



<h2 class="wp-block-heading">The Exception Handling Problem</h2>



<p>Taking into account the previous overview of the project, we want to make the process of handling exceptions the more clearest and efficient possible in terms of computational needs, but also in terms of code structure. To achieve this purpose we will describe three different ways of handling the exceptions.</p>



<p>Also, in this project, we have 2 different types of exceptions. They are:</p>



<ul class="wp-block-list"><li>DomainException &#8211; This is the type of exception that is thrown when the error that happens is something that the API already expects;</li><li>Exception &#8211; This could actually be any type of exception different from DomainException. This occurs when an unexpected error occurs in the API;</li></ul>



<h5 class="wp-block-heading">Reference to Middleware</h5>



<p>In this case, we will use the <strong>ExceptionHandler </strong>middleware layer. The middleware layer is specific to .NET Core and we will not focus on that here. Take into account that we will use <strong>ExceptionHandler </strong>in the middleware pipeline to catch all the exceptions that are not treated.</p>



<p>Here is the reference to <a href="https://docs.microsoft.com/en-us/aspnet/core/fundamentals/middleware/?view=aspnetcore-3.1">ASP.NET Core Middleware</a>.</p>



<h2 class="wp-block-heading">First Approach: Try-Catch on Controller Layer</h2>



<p>This approach consists of catching and treating the DomainExceptions that are thrown by any of the layers (Controller, Service, or Repository) of the APIProject in the Controller layers. Take into account that all the unexpected exceptions are always treated in the ExceptionHandler of the Middleware.</p>



<figure class="wp-block-image size-large is-resized"><img decoding="async" src="https://blogit.create.pt/wp-content/uploads/2020/10/ControllerServiceRepositoryFlow_FirstApproachV2-1.png" alt="" class="wp-image-12085" width="1064" height="1071" /></figure>



<h2 class="wp-block-heading">Second Approach: Try-Catch in all Layers</h2>



<p>This approach consists of all layers of the API Project being capable of catching exceptions and sending again the exception to the upper layers. The Controller layer is where the Domain Exceptions will be treated.</p>



<p>Take into account that in this case, the unexpected exceptions are caught by the layer that throws her and also by the rest of the upper layers. However, it is treated in the ExceptionHandler of the Middleware.</p>



<figure class="wp-block-image size-large is-resized"><img decoding="async" src="https://blogit.create.pt/wp-content/uploads/2020/10/ControllerServiceRepositoryFlow_SecondApproach-1.png" alt="" class="wp-image-12084" width="1067" height="1095" /></figure>



<h2 class="wp-block-heading">Third Approach: Encapsulate Exception in Response Object</h2>



<p>This approach consists, as the name says, in encapsulating the exception in the Response Object. Instead of throwing the exception, we will send the Response object with a Failed message with an instance of the exception in the Response Object as a variable. This means that the system will not need to operate the throws of exceptions for the expected errors. They will be passed in the response object.</p>



<p>Take into account that only the unexpected exceptions are thrown to Middleware &#8211; <strong>ExceptionHandler </strong>to be treated.</p>



<p></p>



<h4 class="wp-block-heading" id="Normal-Response-Object">Normal Response Object</h4>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: java; title: ; notranslate">
public class OperationResult
{
        public OperationResult();

        public static OperationResult Success { get; }
        public bool Succeeded { get; protected set; }
        public IEnumerable&lt;OperationError&gt; Errors { get; }

        public static OperationResult Failed(params OperationError&#x5B;] errors);
        public override string ToString();
}
</pre></div>


<h4 class="wp-block-heading" id="New-Response-Object-for-this-approach">New Response Object for this approach</h4>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: vb; title: ; notranslate">
public class OperationResult
{
        public OperationResult();
           
        public static OperationResult Success { get; }
        public bool Succeeded { get; protected set; }
        public IEnumerable&lt;OperationError&gt; Errors { get; }

        public static OperationResult Failed(params OperationError&#x5B;] errors);
        public override string ToString();
        public Exception ExceptionHolder { get; set;} 
}
</pre></div>


<figure class="wp-block-image size-large is-resized"><img decoding="async" src="https://blogit.create.pt/wp-content/uploads/2020/10/ControllerServiceRepositoryFlow_ThirdApproach-2.png" alt="" class="wp-image-12083" width="1067" height="1061" /></figure>



<h2 class="wp-block-heading">Fourth Approach: Catch all exceptions in Middleware &#8211; Exception Handler</h2>



<p>The approach consists of throwing the exceptions whenever they happen and then let them be caught and treated by the middleware &#8211; ExceptionHandler. In this approach, we would create a different type of response for each type of exception for the ExceptionHandler send as Response.</p>



<p>This would centralize all the exception handling in one place.</p>



<figure class="wp-block-image size-large"><img decoding="async" width="682" height="671" src="https://blogit.create.pt/wp-content/uploads/2020/10/ControllerServiceRepositoryFlow_FourthApproach-7.png" alt="" class="wp-image-12117" srcset="https://blogit.create.pt/wp-content/uploads/2020/10/ControllerServiceRepositoryFlow_FourthApproach-7.png 682w, https://blogit.create.pt/wp-content/uploads/2020/10/ControllerServiceRepositoryFlow_FourthApproach-7-300x295.png 300w, https://blogit.create.pt/wp-content/uploads/2020/10/ControllerServiceRepositoryFlow_FourthApproach-7-427x420.png 427w" sizes="(max-width: 682px) 100vw, 682px" /></figure>



<p></p>



<h2 class="wp-block-heading">Comparison of Exception Handling approaches</h2>



<figure class="wp-block-table is-style-regular"><table><tbody><tr><th><strong>Exception Handling Approach</strong></th><th><strong>Pros</strong></th><th><strong>Cons</strong></th></tr><tr><td>First Approach: Try-Catch on Controller Layer</td><td>One place to treats the expected exceptions and another for the unexpected;<br><br>No need to make calls to the error page when is an expected error, thus making the operation quicker;<br><br>Only adds exception related code in the controller Layer;<br></td><td>Catch &amp; Throw of exception in the controller layer uses more computational resources;<br><br>Every endpoint mapped in the controller will need try-catch;</td></tr><tr><td>Second Approach: Try-Catch in all Layers</td><td>Easy to know where the exception happened and where it is (easy to understand the flow);<br><br>One place to treats the expected exceptions and another for the unexpected;</td><td>Exception handling code everywhere;<br><br>Can cause a bottleneck in the app because of the inherent addition of time to the response and use of computational resources;<br><br>Repeated Code;<br><br>Make logical code harder to understand;<br></td></tr><tr><td>Third Approach: Encapsulate Exception in Response Object</td><td>The runtime doesn’t need to handle the throw of expected Exceptions;<br><br>One place to treats the expected exceptions (in this case inside the response Object) and another for the unexpected;<br><br>App maintains a normal flow even to it occurred an expected exception;<br></td><td>Handling of the expected exceptions is not explicit;</td></tr><tr><td>Fourth Approach: Catch all exceptions in Middleware &#8211; Exception Handler</td><td>Aggregates the handling of all exceptions in one place;<br><br>Exception handling code is in one place and logical/business code in other;</td><td>Unexpected and Expected exceptions handling are in the same place;<br><br>Add another request to the operation because it will always need to request the errors page when an exception happens (Middleware- Exception Handler Implementation);<br></td></tr></tbody></table><figcaption></figcaption></figure>



<p></p>
<p>The post <a href="https://blogit.create.pt/diogocoelho/2020/10/22/how-to-handle-known-exceptions/">How to Handle known Exceptions?</a> appeared first on <a href="https://blogit.create.pt">Blog IT</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blogit.create.pt/diogocoelho/2020/10/22/how-to-handle-known-exceptions/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
