Application consuming too much server resource

Asked

Viewed 97 times

0

I’m facing a problem with an app in Java for Android, the app apparently arrives at a point that consumes excessive server resource, if many users connect in the app reaches the point of taking down the server, the app is in Java integrated with webservice in Laravel.

Below I will post the routine that apparently causes problem by what we detect on the server, since this routine calls the url we detected is consuming excessive resource on the server.

    private class UploadDataToServer extends AsyncTask<String, String, String> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(String... params) {
        try {
            HttpParams myParams = new BasicHttpParams();

            myParams.setParameter("http.useragent", "Apache-HttpClient/UNAVAILABLE (java 1.4)");
            myParams.setIntParameter("http.socket.timeout", 60 * 1000);
            myParams.setBooleanParameter("http.connection.stalecheck", false);
            myParams.setIntParameter("http.connection.timeout", 60 * 1000);
            myParams.setBooleanParameter("http.protocol.handle-redirects", false);
            myParams.setIntParameter("http.socket.buffer-size", 8192);

            //HttpConnectionParams.setConnectionTimeout(myParams, 100000); //100000
            //HttpConnectionParams.setSoTimeout(myParams, 100000); //100000


            HttpClient httpClient = new DefaultHttpClient(myParams);
            ResponseHandler<String> res = new BasicResponseHandler();
            HttpPost postMethod = new HttpPost(
                    AndyConstants.ServiceType.UPDATE_PROVIDER_LOCATION);
            // HttpRequest httpRequest = new HttpRequest();
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair(
                    AndyConstants.Params.ID, id));
            nameValuePairs.add(new BasicNameValuePair(
                    AndyConstants.Params.TOKEN, token));
            nameValuePairs.add(new BasicNameValuePair(
                    AndyConstants.Params.LATITUDE, latitude));
            nameValuePairs.add(new BasicNameValuePair(
                    AndyConstants.Params.LONGITUDE, longitude));
            nameValuePairs.add(new BasicNameValuePair(
                    AndyConstants.Params.BEARING, bearing + ""));
            preferenceHelper.putLatitude(Double.parseDouble(latitude));
            preferenceHelper.putLongitude(Double.parseDouble(longitude));

            postMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            String response = httpClient.execute(postMethod, res);
            // String response = httpRequest.postData(
            // AndyConstants.ServiceType.UPDATE_PROVIDER_LOCATION,
            // nameValuePairs);
            AppLog.Log("TAG", "location send Response:::" + response);

            JSONObject jsonObject = new JSONObject(response);
            if (jsonObject.getBoolean("success")) {
                if (jsonObject.getString("is_active").equals("1"))
                    preferenceHelper.putIsActive(true);
                else
                    preferenceHelper.putIsActive(false);
            }

            return response;
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        stopSelf();
    }
}

This routine is in the file Locationupdateservice.java, this file sends to the server from time to time the user’s location (Latitude/Longitude).

I would like to count on your help to have at least an idea of what can be done to solve this problem.

  • If the problem is in server consumption, you need to scan the server. What is the Laravel routine when you receive this call?

2 answers

0

I am also developing a WS (Restfull)

  1. Check your server.
  2. Check the machine where the Server is.
  3. I make unit test of 10 request/second.
  4. Split your time so 100/1s, 100/0.5s, 100/0.25s until you catch the stack overflow (stack over flow ).
  5. Done that you now know that if you synchronize the req/s could very well meet thousands of requests in one minute.

Obs: Some Web Service like Americas.com Submarine Shoptime control access. In 2014 in Black Fraud thousands of people stood in front of the pc for a few minutes in a virtual queue at the time I was not a programmer but I understood that it was not to overthrow or even lock the site. Today I know why to do it. Summary The problem is not to meet 3600 requests per minute the problem is if they are received at the last second of the last minute. Control your stack burst.

0

I will put together some ideas for this optimization here, in order to assist or start the debate on some of them.

I believe you won’t have a cake batter solution in this case, we’ll need to test.

  1. Limit the sending time of the request in the APP

It is possible to send, for example, a request every two seconds?

  1. Check the webserver configuration to understand if it and it is keeping the process for a long time even after completing the request (this maintains an unnecessary consumption)

Apache/Nginx creates a process for each request, a process that can be optimized if you better understand the application’s need. In other words, a process may not need to start consuming 20mb of memory, just as it may not need to have a very high timeout. But then we are leaving for the optimization of the same webserver.

  1. Test the 'unitario' consumption of the request in order to make it leaner

Is it possible to make the process simpler? Less query, maybe a simpler loop. These things can make consumption lower.

  • I understood, there are many possibilities, but the return of the part of the server I have until then is that there are many calls in sequence of the constant URL in this routine, in this same file where this routine is, below the routine is called according to the code below: if (preferenceHelper.getRequestId() == Andyconstants.NO_REQUEST) { isNoRequest = true; new Uploaddatatoserver(). execute(); } Else { isNoRequest = false; new Uploadtriplocationdata(). execute(); } Is it possible to limit calls in this routine? If so, how do they recommend doing?

  • But when it comes to thinking about what to do, if the business rule requires that calls happen that way, you’ll need to do something to profile. If not, we need to limit these requests to make the process healthier.

  • I don’t understand Java to guide you, but you can do something that validates if the last call time is less than 2 seconds, for example. Or allow calls only in seconds (or minutes) pairs, something like that.

  • It’s true, I thought more or less something in that sense, wait some time before calling these routines above, like it waits x time and then calls the routine, if you have any help ideas then all help is well life. Thanks! .

  • So, the code you posted is still insufficient to make a deeper analysis of the problem, as for example, this uses Fusedlocation to get the location of the device? If I’m not mistaken, you have an option to pick up location only when the device is moving, and you have a running time limiter, but I still believe the problem is server availability and handling requests, in case you can use websocket which would solve this problem of several requests

Browser other questions tagged

You are not signed in. Login or sign up in order to post.