I have an e-commerce application and it works perfectly when 5 to 10 users are using it.
But it becomes really slow when it is used by 50-60 people.
Currently I am using MySQL & PHP.
I am calling .php
file which has MySQL
connectivity code. And from there I am fetching JSON response.
Below is my code:
class Items extends AsyncTask<String, String, String> {
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("id", Itemid));
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_allitems, "GET",
params);
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
products = json.getJSONArray(TAG_ITEMS);
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_ITEMID);
String name = c.getString(TAG_NAME);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_PID, id);
map.put(TAG_NAME, name);
// adding HashList to ArrayList
productsList.add(map);
}
This is my PHP code:
$result = mysql_query("SELECT * FROM item_master") ;
// check for empty result
if (mysql_num_rows($result) > 0) {
$response["items"] = array();
while ($row = mysql_fetch_array($result)) {
// temp user array
$item = array();
$item["id"] = $row["id"];
$item["code"] = $row["code"];
$item["name"] = $row["name"];
$item["description"] = $row["description"];
$item["price"] = $row["price"];
$item["image1"] = $row["image1"];
// push single product into final response array
array_push($response["items"], $product);
}
// success
$response["psuccess"] = 1;
....
}
So what are the best approaches for optimization in this scenario ? When more than 1000 users will access this app, the app should be responsive and load items quickly.
On the server side, you need to look into techniques for managing a large number of connections, such as enabling servers to handle high-volume traffic, as well as network-level issues like dynamic load balancing.
On the client side, you can make use of Volley
with okHttp
. You'd also have to enable the usage of okHttp
on the server side for that to work properly.
References:
1. Tactics for using PHP in a high-load site.
2. Separating dynamic and static content on high traffic website.