Skip to main content
Published: October 14 2010, 10:00:00 PMUpdated: November 28 2020, 9:01:06 PM

Enable call metrics logging in Java SDK

Summary 

You can use the JAVA SDK MetricsLogger class to report the amount of time spent by an API call in each phase of execution.  The sample application below illustrates how MatricsLogger is used.


 Detailed Description

   The sample applicaiton generates API call metrics report in milliseconds.

   eBay Java SDK is based on JAX-WS RI which creates proxy instance at runtim, as shown in the bottom of this KB, the performance for the first call is slow ( marked as PreJAXB in the report) but the subsequent calls are much quicker than the initial one. 

import com.ebay.sdk.*;
import com.ebay.sdk.call.VerifyAddFixedPriceItemCall;
import com.ebay.sdk.logging.*;
import com.ebay.soap.eBLBaseComponents.*;

/**
*
* @author zhuyang
*/
public class SimplePerformanceTest {

    private ApiContext apiContext = new ApiContext();
    private ApiLogging apiLogging = new ApiLogging();
    private CallMetrics matrics;
    private String TOKEN="";
    private String APISERVERURL = "https://api.ebay.com/wsapi";

    public SimplePerformanceTest() {
        matrics = new CallMetrics();
        ApiCredential cred = new ApiCredential();
        apiContext.setApiCredential(cred);
        // Set api call credential
        apiContext.getApiCredential().seteBayToken(TOKEN);
        // Enable api logging
        apiContext.setApiLogging(apiLogging);
        apiContext.setSite(SiteCodeType.UK);
        apiContext.setApiServerUrl(APISERVERURL);

    }

    public static void main(String[] args) {
        // Enable MatricsLogging
        MetricsLogger.enableMetricsLogging();
        SimplePerformanceTest results = new SimplePerformanceTest();
        try {
            for (int i = 0; i < 10; i++) {
                FeesType fee = addFixedItem(results.apiContext);
                //collect matric data
                MetricsLogger.collectMetrics(results.matrics);
                if (fee == null) {  
                    return;
                }
            }
        } catch (Exception e) {
            //e.printStackTrace();
        } finally {
            //  print out matrics report to console
            results.matrics.generateReport(System.out);
        }
    }

    private static FeesType addFixedItem(ApiContext apiContext throws Exception {

        FeesType fee = null;
        VerifyAddFixedPriceItemCall request = new VerifyAddFixedPriceItemCall(
                apiContext);
        request.setSite(SiteCodeType.UK);

        ItemType item = new ItemType();
        item.setConditionID(1000);
        item.setTitle("Testing item. Do not bid");
        item.setDescription("Testing item. Do not bid");
        item.setSite(SiteCodeType.UK);
        item.setConditionID(1000);
        item.setPostalCode("SE6 1AL");
        item.setListingDuration("Days_7");

        BuyerPaymentMethodCodeType[] arrPaymentMethods = new BuyerPaymentMethodCodeType[]{
            BuyerPaymentMethodCodeType.PAY_PAL
        };
        item.setPayPalEmailAddress("test@test.com");
        item.setPaymentMethods(arrPaymentMethods);
        item.setStartPrice(getAmount(11.0));
        item.setCountry(CountryCodeType.GB);
        item.setCurrency(CurrencyCodeType.GBP);
        CategoryType category = new CategoryType();
        category.setCategoryID("57991");
        item.setPrimaryCategory(category);
        item.setQuantity(5);  
        item.setShippingDetails(getShippingDetails());  
        item.setDispatchTimeMax(2);
        item.setReturnPolicy(getReturnPolicy());  
        request.setItem(item);    
        fee = request.verifyAddFixedPriceItem();
        return fee;
    }

    private static ReturnPolicyType getReturnPolicy() {
        ReturnPolicyType rp = new ReturnPolicyType();
        rp.setReturnsAcceptedOption("ReturnsAccepted");
        rp.setRefundOption("MoneyBack");
        rp.setReturnsWithinOption("Days_30");
        rp.setDescription("Text description of return policy details");
        rp.setShippingCostPaidByOption("Buyer");
        return rp;
    }

    private static ShippingDetailsType getShippingDetails() {
        ShippingDetailsType sd = new ShippingDetailsType();  
        ShippingServiceOptionsType st1 = new ShippingServiceOptionsType();
        st1.setShippingService("UK_SellersStandardRate");
        st1.setShippingServiceCost(getAmount(1));
        st1.setShippingServiceAdditionalCost(getAmount(1));  

         sd.setShippingServiceOptions(new ShippingServiceOptionsType[]{st1});  
         return sd;
    }

    private static AmountType getAmount(double amount) {
        AmountType a = new AmountType();
        a.setValue(amount);
        return a;
    }
}

 

Here is a sample report ( in ms)

 

== VerifyAddFixedPriceItem ==
Number of calls recorded: 10
Number of calls for statistics: 8
Total    PreJAXB   PostJAXB  Network   Server    Start Time
=========================================================================
9516      7234      32                044         1206        2010-10-15 06:06:34
1843      0            15                578        1250         2010-10-15 06:06:46
1750      0            0                  570        1180         2010-10-15 06:06:51
1469      0            0                  216        1253         2010-10-15 06:06:54
1406      0            0                  170        1236         2010-10-15 06:06:52
1359      0           16                 101        1242         2010-10-15 06:06:49
1359      0            0                  138        1221         2010-10-15 06:06:55
1343      0           15                 127        1201         2010-10-15 06:06:44
1282      0            0                  156        1126         2010-10-15 06:06:45
1188      0           16                  65         1107         2010-10-15 06:06:48
=========================================================================
1476      0            5                   257        1213    

 


 Version Info

The code example above was based on the versions specified below:

Java SDK Version javasdk v673 Full release

How well did this answer your question?
Answers others found helpful