0
votes

Is there any official supported way to show the exact response time of call or send mediator from WSO2 ESB ?

1

1 Answers

1
votes

You can use the inbuilt Synapse Message Context Property and do this easily. The Synapse Message Context Property can retrieve the current time in milliseconds and you can set this value to a property at any desired point of the flow and access it whenever you need it.

You can set the property as shown below.

<property name="TIME" expression="get-property('SYSTEM_TIME')" scope="default" type="LONG"/>
...
<log>
 <property name="Time : " expression="get-property('TIME')"/>
</log>
...

Let's say you've logged the starting time and the end time as two properties named 'TIME_START' and 'TIME_END', you can take the time difference using a script mediator as shown in the example below.

<script language="js">
 var time1 = mc.getProperty("TIME_START");
 var time2 = mc.getProperty("TIME_END");
 var timeDifference = time2 - time1;
 print("Response Time :  " + timeDifference + " ms ");
 mc.setProperty("RESPONSE_TIME", timeDifference);
</script>

The following is a sample proxy service with the response time calculation bit.

<proxy xmlns="http://ws.apache.org/ns/synapse"
       name="SampleTimeProxy"
       transports="https,http"
       statistics="disable"
       trace="disable"
       startOnLoad="true">
   <target>
      <inSequence>
         <property name="TIME_START"
                   expression="get-property('SYSTEM_TIME')"
                   scope="default"
                   type="LONG"/>
      </inSequence>
      <outSequence>
         <send/>
         <property name="TIME_END"
                   expression="get-property('SYSTEM_TIME')"
                   scope="default"
                   type="LONG"/>
         <script language="js">
              var time1 = mc.getProperty("TIME_START");
              var time2 = mc.getProperty("TIME_END");
              var timeDifference = time2 - time1;
              print("--------------  " + timeDifference + " ms  -----------------");
              mc.setProperty("RESPONSE_TIME", timeDifference);
         </script>
         <log>
            <property name="Response Time in ms: " expression="get-property('RESPONSE_TIME')"/>
         </log>
      </outSequence>
      <endpoint>
         <address uri="http://localhost:8080/axis2/services/SampleService"/>
      </endpoint>
   </target>
   <publishWSDL uri="http://localhost:8080/axis2/services/SampleService?wsdl"/>
   <description/>
</proxy>

Here we are setting the response time as another property named 'RESPONSE_TIME'.

You can read more on this blog: http://sumedhask.blogspot.com/2013/10/a-simple-way-to-log-time-durations-in.html