Quantcast
Channel: Adobe Community: Message List - ColdFusion
Viewing all articles
Browse latest Browse all 21760

Re: Optimise CFHTTP Requests/Requests Being Throttled?

$
0
0

I have tested a number of CFHTTP scenarios. There is good news and bad news.

 

Firstly, the bad news. It seems that CFHTTP is sluggish by design. No amount of refactoring or code-efficiency that I implemented could get it to run faster than a modest threshold.

 

To emulate your scenario, I did the following tests:

 

TestComp.cfc

 

cfcomponent>  

    <cffunction name="getURL" returntype="struct">

        <cfhttp method="get" url="http://www.myDomain.com">

        <cfreturn cfhttp>

    </cffunction>

</cfcomponent>

 

Test scenario 1

 

The following CFM page invokes the CFC 10 times. Each invocation creates an instance of the component, then calls a method that makes a CFHTTP Get request and returns the result as a struct. The result is stored as an array element. Each such invocation is timed.

 

cfhttpTest1.cfm

 

<cfset arr=arrayNew(1)>

<!--- <cfset obj=createobject("component","TestComp")> --->

<cfloop from="1" to="10" index="i">

    <cfset start = getTickCount()>

    <cfset obj=createobject("component","TestComp")>

    <cfset arr[i] = obj.getURL()>

    <cfoutput>count #i#: #getTickCount() - start# milliseconds<br></cfoutput>

</cfloop>

 

Scenario 1 result

 

count 1: 1665 milliseconds

count 2: 1774 milliseconds

count 3: 1634 milliseconds

count 4: 1771 milliseconds

count 5: 1667 milliseconds

count 6: 2045 milliseconds

count 7: 1645 milliseconds

count 8: 3004 milliseconds

count 9: 1656 milliseconds

count 10: 1844 milliseconds

 

Instantiating the component object outside the loop made no difference in the execution times.

 

Test scenario 2

 

Here the CFHTTP tag comes in as an included CFM file, rather than via a method in a CFC. The hope is that this will be much faster than the above scenario, as there is no object creation and no method invocation. As before, the CFHTTP Get request is run 10 times and the resulting struct is stored as an array element.

 

cfhttpRequest.cfm

 

<cfhttp method="get" url="http://www.myDomain.com">

 

 

cfhttpTest2.cfm

 

<cfset arr=arrayNew(1)>

<cfloop from="1" to="10" index="i">

    <cfset start = getTickCount()>

    <cfinclude template="cfhttpRequest.cfm">

    <cfset arr[i] = cfhttp>

    <cfoutput>count #i#: #getTickCount() - start# milliseconds<br></cfoutput>

</cfloop>

 

Scenario 2 result

 

count 1: 1678 milliseconds

count 2: 1740 milliseconds

count 3: 1894 milliseconds

count 4: 1695 milliseconds

count 5: 1769 milliseconds

count 6: 1721 milliseconds

count 7: 1538 milliseconds

count 8: 1683 milliseconds

count 9: 1569 milliseconds

count 10: 1795 milliseconds

 

Scenario 2 is faster than scenario 1, but only marginally. So, sadly, I have to confirm your findings. I cannot think of any other way to make the CFHTTP requests faster in these scenarios. CFHTTP seems to be slow by design. That was then the bad news.

 

On to the good news. I found a much faster alternative to CFHTTP, namely, Curl.

 

Test scenario 3

 

My Operating System is 64 Bit Windows 7. I downloaded the latest Curl file curl-7.35.0-win64.zipand extracted it to c:\curl-7.35.0-win64. I then ran Curl in ColdFusion, using cfexecute. The loop runs 10 times, as before, each result, being stored as a text file.

 

curlTest.cfm

 

<cfset outputPath=arrayNew(1)>

<cfloop from="1" to="10" index="i">

    <cfset start = getTickCount()>

    <cfset outputPath[i]= "C:\Temp\output#i#.txt">

    <cfexecute name = "C:\curl-7.35.0-win64\bin\curl.exe"

        arguments = "http://www.myDomain.com"

        outputFile = "#outputPath[i]#">

    </cfexecute>

    <cfoutput>count #i#: #getTickCount() - start# milliseconds<br></cfoutput>

</cfloop>

 

Scenario 3 result

 

count 1: 392 milliseconds

count 2: 128 milliseconds

count 3: 119 milliseconds

count 4: 191 milliseconds

count 5: 49 milliseconds

count 6: 12 milliseconds

count 7: 24 milliseconds

count 8: 23 milliseconds

count 9: 6 milliseconds

count 10: 21 milliseconds

 

Evidently, Curl is faster than CFHTTP is this scenario, by several orders of magnitude!


Viewing all articles
Browse latest Browse all 21760

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>