A Performance Tuning – Android – JSON Parsing & Database insertion

For some reason, we need to carry out a task that is composed of the following steps, in an Android Application.

Fetch web data from a Uri (could be in a stream)
Obtain a JSON Object out of the data in step [1].
Parse a Java object out of the JSON Object in step[2].
Extract some data (which is of quite a number, around 2k tuples) out of the same data stream in step [1] and insert them all into the db.
With a quick and naïve implementation, I got the following on every step’s time consumptions.

— FetchWeb (http://*******/test_shop_meta.php) ends in 3.55s
—  JsonParser from stream ends in  20.92s
—  clearAllRecords ends in  0.502s
—  Parse Shop.ShopCur ends in  0.051s
—  pourShopMetaIntoDB ends in  137.008s

The step 2,3,4 took about 160s, way too much. The heavy consumers are JSON Parsing and DB insertion.
After I correted the wrting to avoid open and close the writable db within the iteration, I got

—  pourShopMetaIntoDB ends in  108.123s

It made the performance better, but not radically.

 

After my turnning on the “transactional mode” of db operation, it was dramatically improved.

db.setTransactionSuccessful();
db.endTransaction();

—  JsonParser from stream ends in  19.486s
—  pourShopMetaIntoDB ends in  18.001s

On the other hand, I broke the “JSON parsing” into a more granual view:
—  JsonParser from stream ends in  20.92s
— Util.stringFromStream ends in 15.888s
— new JSONObject(String) ends2.433s

The stream->string operation took too long. Tweaking the buffer size in my buffer reader, which i used to read line by line from the stream, I got an improvement as this
—  Util.stringFromStream ends in  5.214s

Still takes too much. For the db inserting, it has to scan again the whole structure (no matter whether the string or the json object) To save the unnecessary double scanning, I tried a streaming parser instead of loading the whole string into json object and scanning the json object later again.

The GSON core streaming API is adopted since android SDK level-11 (named android.util.json***, not the org.json), before that you need to download GSON lib and include it yourself.

After all that, I can finish the above mentioned task step 2,3,4 in a total 12s now. (Step 1 depends on the network still.)

摘自 TouchOn.asia

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。