Saturday, July 4, 2015

Improving performance by keeping payload simple and small

XML Schema Definition (XSD) is a means to represent the input and output parameters for web service operation. This is more popular choice compared to the other models like DTD because it doesn’t need additional parsers.

The size and complexity of the payload have immense impact on the performance. If the payload is huge and complex it would affect the performance as it takes long time to parse the content.

It is often observed that the payload usually has many fields with only a few being populated. Reducing the payload to include only the fields with values would help to reduce the network traffic and increase the processing speed. This can be achieved by explicitly setting the minOccurs value to 0.By default this is set to 1 in XSD.

The following example shows the definition of Meal type schema.

 <xs:element name="mealType">
   <xs:complexType>
     <xs:sequence>
      <xs:element name="category" type="xs:string"/>
       <xs:element name="starter"  minOccurs="0" maxOccurs="1"  type="xs:int"/>
       <xs:element name="mainCourse" minOccurs="1" maxOccurs="3"   type="xs:string"/>
       <xs:element name="dessert" minOccurs="0" maxOccurs="2"   type="xs:string"/>   
 </xs:sequence>
  </xs:complexType>
 </xs:element>

The starter and dessert field are optional and may not be populated for all meal types, hence the minoccurs is set to 0 for these fields.

(i) A sample payload with all fields populated would be as follows:

<mealType>
     <category>Executive Lunch </category>
     <starter>samosa </starter>
    <mainCourse>Rice <mainCourse>
    <mainCourse>Naan <mainCourse>
    <dessert>Ice cream </dessert>
 </mealType>

(ii )A sample payload with only few fields populated would be as follows:

 <mealType>
       <category>Budget Lunch </category>
       <mainCourse>Rice <mainCourse>
 </mealType>