ColdFusion 8 ValueObjects vs Typed Structs for Flex RemoteObjects

March 28, 2008

ColdFusion Objects transfer nicely over to Flex, making the remote data connection for Flex run like a dream.  It can get out of hand, however, when there are a large number of Objects to return from ColdFusion, for example, an array of ValueObjects, which might have other value objects as members.  In researching this, I ran across a few very helpful postings.  Basically, CF8 has a workaround wherein you use typed Structs instead of full blown Objects to pass VO’s to Flex:

http://www.briankotek.com/blog/index.cfm/2008/1/28/Returning-Typed-Structs-vs-CFCs-to-Flex

http://groups.google.com/group/transfer-dev/browse_thread/thread/0abdb2191dcfd226#2cbf827754e75376


So, I decided to run a few very simple tests to see what the impact of CreateObject was on a large dataset.  I put together a CF script with a loop to create 5000 objects using CreateObject (with init), another for 5000 with CreateObject (without init), another to simply duplicate a template Object and another to create 5000 structs.  The breakdown and the code follow:
 

Test

Iterations Elapsed Time (ms) ms/Instance
structTest 5000 157 0.0314
FullCreateTest 5000 17468 3.4936
createTest 5000 16610 3.322
dupeTest 5000 12469 2.4938
structTest 5000 15 0.003


This was run on a CF8 Box with JRun running on Java 1.5 I had similar results on a CFMX 7 Box with JRun running on Java 1.42 The option of rolling query results into typed structs is really a lifesaver given the performance numbers.       

<cfset t1 = GetTickCount() />

<cfset myArray=ArrayNew(1)>
<cfloop from=”1″ to=”5000″ index=”i”>
<cfset newStruct=StructNew()>
<cfset newStruct[“iter”] = #i# />
<cfset ArrayAppend(myArray,newStruct) />
</cfloop>
<cfset t2 = GetTickCount() />
<cfset elapse1 = t2 – t1 />
<cfset myArray2=ArrayNew(1)>

<cfloop from=”1″ to=”5000″ index=”i”>
<cfset newObj=CreateObject(“component”,”com.test.TestIter”) />
<cfset newObj[“iter”] = #i# />
<cfset ArrayAppend(myArray2,newObj) />
</cfloop>
<cfset t3 = GetTickCount() />
<cfset elapse2 = t3 – t2 />
<cfset myArray3=ArrayNew(1)>

<cfloop from=”1″ to=”5000″ index=”i”>
<cfset newObj2=CreateObject(“component”,”com.test.TestIter”).init() />
<cfset newObj2[“iter”] = #i# />
<cfset ArrayAppend(myArray3,newObj2) />
</cfloop>
<cfset t4 = GetTickCount() />
<cfset elapse3 = t4 – t3 />
<cfoutput>Results<br/>Structs : #elapse1# ms for 5000</br>

CreateObject (no init) #elapse2# ms for 5000<br/>CreateObject (with init) #elapse3# ms for 5000<br/></cfoutput>

<cfcomponent name="TestIter"> 
      <cfproperty name="iter" type="numeric"> 
      <cffunction name="init"> 
            <cfreturn this /> 
      </cffunction> 
</cfcomponent>
Advertisements

Get Outta my namespace (revisited)

November 18, 2007

Earlier, I discussed some of the problems/issues with Flex Builder’s default configuration of outputting the swf into the “bin” folder. (see Get Outta My Namespace). Operationally, the dot Net server’s claim to the “bin” folder in server root context was one of the roadblocks to use of the “bin” folder. It’s easy enough to use another name, to avoid conditions like this one.

Well, I was recently concerned with the performance of a web application, running on IIS on the JRun4 J2EE server distributed with ColdFusion. I decided to try running the application on a JBoss server, with ColdFusion 8, and do some performance benchmarking, and analysis, to see if the problem’s root was in JRun’s management of the memory objects, or in the tuning of the JRun server, or if the change in platform would make a difference at all. The performance problem is likely rooted in the application’s architecture, and the database configuration, but it was worthwhile and easy enough to see if there was any difference in changing the platform this way.

I have a JBoss 4.05 Server running Tomcat and ColdFusion 8, basically following the easy instructions at Steve Brownlee’s blog when I had first set it up with ColdFusion7, then updating it with ColdFusion 8, using the advice from Adobe. When I attempted to use the Flex application deployed in a “bin” folder which was *not* in the root, that is it was several subfolders away from the root, the server acted as if the html container and the swf file were simply not there. I tried several things before eventually simply renaming the deploy folder to “out”. Immediately, the server recognized the deployed html and swf. I haven’t yet determined what exactly is going on, but I know the bin folder has various application specific uses for JBoss and Tomcat. It seems as though this is interefering, once again, with the default “bin” folder output for Flex deployment. I’ve updated the project, locally, to build to “out” and mapped the application to “out” and it is all working just fine.

As for benchmarking performance, I have started. I’ll try to post anything interesting I might find.


Flex Builder Plugin for Linux – Alpha

October 6, 2007

I was pretty excited to see that Adobe has Flex Builder for Linux on their labs page.

http://labs.adobe.com/technologies/flex/flexbuilder_linux/

I was wondering what I was going to do with all my free time. I run dual boot (dual hard drive) Windows XP and Ubuntu Linux. I have Eclipse set up, but it was at version 3.2, so I had to upgrade it to 3.3, make sure the classpath was set for Java 1.5. The following teminal line was helpful in keeping that manageable:

sudo update-alternatives --config java

Nowdays, with the package installers and all of the helpful people on the net, it does not take a rocket scientist to set up Linux (but it’s always nice to know one, just in case you run into trouble on re-entry!) and have XWindows running. The package installers aren’t 100% there, so there is always a little bit of file tweaking required.

Flex Builder Alpha for Linux

More later…


HTML embedded in Flex

October 6, 2007

This has been out in the wild for a while, but it is interesting to note that Flex can run inside of html embedded in Flex.  With AIR (Apollo) , the html inside of Flex problem has been more than solved, but some environments can’t or won’t run AIR on the desktops.

HTML in Flex

http://www.deitte.com/IFrameDemo3/IFrameDemo.html

http://www.deitte.com/archives/2007/09/html_in_flex_wi.htm


Flex build numbers using Ant and flexTasks

July 28, 2007

There are times when it is quite handy to be able to verify the version of a Flex Object which is loaded into the browser. These times include occasions when you think the browser is caching content (swf), when there is an errant build process, or when you are swapping a swf application, or even subcomponents in and out for testing some behavior. Having the Ant builder automatically set a build number and timestamp on the swf, by setting a public var with the values, allows you to put the value setting code in a single module, or in multiple modules and use the same mechanism.

To facilitate this with minimal disruption of the remainder of the code, it is handy to utilize the include directive for Flex builder/mxmlc. The import directive is so commonly used, that the include directive is often forgotten. Adobe Livedoc on include directive. Here is an example:

<?xml version="1.0"?>

<!-- example with Version.as -->

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">

    <mx:Script><![CDATA[

        include "Version.as";

        import  mx.controls.Alert;

    ]]></mx:Script>

    <mx:Button id="versionButton" label="Show current version"

               click="Alert.show(build_number + ' ' + build_time);"/>

</mx:Application>

And here is an example of a template Version.as.template file, prior to copying over to Version.as and replacing the @ tokens:

      public var build_number:String="@buildnumber@";

      public var build_time:String="@buildtime@";

Version.as file with @buildnumber@ and @buildtime@ tokens replaced.

      public var build_number:String="1.0.12";

      public var build_time:String="JUL 08 07 10:01 am";

Ant script to copy the template Version.as and then modify the result with the generated build number and buildtime.  Note that this requires a variable set up for modeldir  (the name modeldir is not important, it’s the path to the directory where where the Version.as file will be).

<taskdef resource="flexTasks.tasks"

         classpath="${ANT_HOME}/lib/flexTasks.jar" />

<target name="cvsbuild">

     <buildnumber file="mybuild.number" />

     <tstamp prefix="build"/>

     <tstamp>

         <format property="build.time" pattern="MMM dd yy hh:mm aa"

              offset="-1" unit="hour"/>

     </tstamp>

     <echo>Build Number ${build.number} ${build.time}</echo>

     <copy file="${modeldir}/Version.as.template"

              tofile="${modeldir}/Version.as" />

     <replace file="${modeldir}/Version.as"

            token="@buildnumber@" value="${build.number}" />

     <replace file="${modeldir}/Version.as"

            token="@buildtime@" value="${build.time}" />

</target>

The nice thing about setting vars like this, is it is possible to reach into any such object and pull out the version number for verification. This could be checked at runtime, or in your UnitTesting, or manually to follow up on a user reported issue.

Ant References: BuildNumber , tstamp, replace .


Degrafa – Flex Framework for Visual Elements

July 23, 2007

  When Flex was first introduced, there was, most prevalent, Cairngorm and Arp as support frameworks (Flex *is* a framework, don’t forget).  Both emphasized the data connection element, which was the biggest problem with Flash Remoting, so it makes a lot of sense.  Today, however, we are seeing a real need for the extension of some of that kind of methodology for the designer/graphics side of things.

  The Degrapha framework (http://www.degrafa.com/) promises to help ease some of the pain, or close the divide between the designer and the developer.  Called a “Declarative Graphics Framework” by its designers, Jason Hawryluk and Juan Sanchez, it provides support for re-use and extensibility on the graphics and visual side of the Flex development milieu.

  I took a moment this week to catch up on the Gausax framework developments (http://ablesa.wordpress.com/), and Angel Blesa has done quite a bit of good work there.  Check out the comparison of implementations of the Flex Store using no framework, Cairngorm, and then Gausax.  This treatment helps to easily see the difference.  The simplification of frameworks is what developers want, in order to remain Agile. 

  None of these is a case of replacing one thing with another.  There are benefits and strengths shown by each.  It is always a case of matching the solution with the problem, taking into account the complete context of the project at hand, customer requirements and the available resources. 


Value Object Bustifiers

July 2, 2007

  I’ve been fashioning Brian Renaldi’s the <a href=”https://howardscholz.wordpress.com/wp-admin/code.google.com/p/cfcgenerator/&#8221; target=”_blank”>Illudium PU-36 Code Generator</a>  package to produce scaffolding for the Cairngorm framework.

People tend to have opinions about code generation, scaffolding being a euphemism for a specific portion of code generation.  I really like the dynamic scaffolding employed by Groovy/Grails and Ruby/Rails, wherin the functionality of the scaffolding is generated, but the code is not.  That seems pretty cool.  It would be Uber Cool to generate the entire application that way, and have no code except for that very special case stuff.  But even generated scaffolding serves its purpose, and helps a group development effort have some consistency.
Typically, what I do in my VO’s is have them accept a parameter that is of type object, and let the constructor act as a sort of copy constructor, if it is fed an object, otherwise leaving the default values.
A further item that is handy in scaffolding, is making the assignment case insensitive.  Coldfusion is case insenstive, and Actionscript, is case sensitive.  So those assignments can be troublespots sometimes.  Also, sometimes there may be a mismatch between the VO properties and the properties returned from the persistent store, so any mismatches are observed and traced to the console output.

To handle these items, I’ve added the following:

/**
* Default constructor.
*/
public function CategoryVO(data : Object = null) : void {
if (data == null) return;
var dataDesc:Object = ObjectUtil.getClassInfo(data);
var myDesc:Object = ObjectUtil.getClassInfo(this);
var props:Array = dataDesc.properties;
var myProps:Array = myDesc.properties;
var found:Boolean = false;
for each (var name:String in props) {
found = false;
for each (var myName:String in myProps) {
if (!found &amp;&amp; myName.toUpperCase() == name.toUpperCase() ) {
this[myName.toUpperCase()] = data[name.toUpperCase()];
found = true;
break;
}
}
if (!found)
trace("CategoryVO - Input Object has extra property " + name);
}

The business delegate layer I’ve seen done in a pretty boilerplate manner, where it acts as kind of a pass through layer to the Commands.  More correctly, any data conversion that may be platform specific, like Coldfusion or PHP related Object constructs, should be handled in the business delegate layer, isolating the platform details from the remainder of the Actionscript code.  That makes this layer a little more interesting for matters of code generation.