1
\$\begingroup\$

I am working on struts 2 based web application, where an Action Class render 7000+ records in UI. The query which fetches the record is already been optimized by the DB developer, but instead it took around 7+ seconds to load UI it also freeze the UI for a moment,on deep inspection I have found there is something wrong with java code itself, but for more deep analysis, I would like my code to be review by community here.

In Action class, the fetched data collected and one by one putting into the JSONObject later on it then putting into in the JSONArray, it seems, such kind of implementation took time.

public String execute() throws Exception {
        logger.debug("---------Enter In HoldingMembersDataAction---------");

        JSONObject obj = new JSONObject();
        JSONObject obj1 = null;
        ArrayList<JSONArray> extlist =  new ArrayList<JSONArray>();
        JSONArray arr = new JSONArray(extlist);
        /* Field is use to store the session id */
        int total = 0;
        /* List is use to store the account details object */
        List<Account> accountList = null;

        try {
            HttpServletRequest request = ServletActionContext.getRequest();
            String holdingId = request.getParameter("holdingId");

            CreditTrade creditTrade = new CreditTradeDAO();
            logger.debug("Sort In Add Member" + getSort());
            PagingInfo pageInfo= new PagingInfo(getStart(), getLimit(), getSort(), getDir());
            pageInfo.setSearchText(getSearchText());

        /*  accountList = creditTrade.getAccountMemberList(holdingId,
                    getCoreProduct(), getSearchText().trim(), getMemberType(),
                    getMemberCountry(), getSort(), getDir());*/

            accountList = creditTrade.getAccountMembersList(holdingId,
                    getCoreProduct(),getMemberType(),
                    getMemberCountry(),pageInfo);
            /*
             * Iterate account list and prepare JSONObject
             */
            for (Account ac : accountList) {
                String memberType=ac.getMemberType();
                memberType=memberType.replaceAll(",", ", ");
                obj1 = new JSONObject();
                obj1.put("accountId", ac.getId());
                obj1.put("externalReferenceID", ac.getExternalReferenceId());
                obj1.put("name", ac.getName());
                obj1.put("countryName", ac.getVisitingCountryName());
                obj1.put("membercategory", memberType);
                obj1.put("membershipId", ac.getMemberID());
                obj1.put("isPartOfHolding", ac.getIsPartOfHolding());
                obj1.put("isPartOfSameHolding", ac.getIsPartOfSameHolding());
                arr.put(obj1);

            }
            //obj.put("totalCount", total);  
            //obj.put("data", arr);
            /* Write JSONData into output stream */
            writeJSONData(arr);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return null;
    }

I wanna ask if such usage of JSONObject and JSONArray is correct, or it can be furhter optimized to reduce time.

Network Activity Log:

enter image description here

enter image description here

RENDER TIME:

TimeAnazer timeAna = new TimeAnazer();
            timeAna.startMillSec();
            accountList = creditTrade.getAccountMemberList(holdingId,
                    getCoreProduct(), getSearchText().trim(), getMemberType(),
                    getMemberCountry(), getSort(), getDir());
            timeAna.endMillSec();
            timeAna.showTimeTaken("HoldingMembersDataAction::getAccountMemberList");
            timeAna.resetTime();

enter image description here

\$\endgroup\$
  • \$\begingroup\$ Does it work? Is the output correct? \$\endgroup\$ – Mast Jan 24 at 7:11
  • \$\begingroup\$ Is there an actual measured timing problem? If you render 7000 objects in HTML your DOM becomes huge and it is no surprise that the application has poor response time. However, measure the time of your method to make sure that you find the correct place to "optimize". How long does it take from start to return? How long does the call to creditTrade.getAccountMembersList() take? Without this information, there's nothing you (or we) can do. \$\endgroup\$ – mtj Jan 24 at 7:20
  • \$\begingroup\$ output is correct and perfect, but it take lot of time to render \$\endgroup\$ – user9634982 Jan 24 at 8:53
  • \$\begingroup\$ @mtj see my edits please \$\endgroup\$ – user9634982 Jan 24 at 8:59
  • \$\begingroup\$ @user9634982 Your measurement shows 1.5s for the backend call, which means, that from the 7+ seconds for rendering the page, at least 5.5 seconds are taken by the client. Furthermore, as I cannot see any long-runners in your method, you definitely need measurements on backend side, as stated in my first comment. Especially, as the conversion from one object type to another is usually small, I suspect that the main runtime will be taken by the call to creditTrade.getAccountMembersList(). \$\endgroup\$ – mtj Jan 24 at 9:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.