Skip to content

Commit

Permalink
update for statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
liaohuqiu committed Mar 7, 2015
1 parent 4eec3cc commit 128adc3
Show file tree
Hide file tree
Showing 18 changed files with 222 additions and 161 deletions.
6 changes: 3 additions & 3 deletions README-cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Demo 项目移到了这里: https://github.com/liaohuqiu/android-cube-app

#### 依赖源

最新版版本号: `1.0.44.14-SNAPSHOT`, 发布到了: `https://oss.sonatype.org/content/repositories/snapshots`
最新版版本号: `1.0.44.15-SNAPSHOT`, 发布到了: `https://oss.sonatype.org/content/repositories/snapshots`

* 在gradle中:

Expand Down Expand Up @@ -61,7 +61,7 @@ mavenCentral()
<type>aar</type>
<!-- or apklib format, if you want -->
<!-- <type>apklib</type> -->
<version>1.0.44.14-SNAPSHOT</version>
<version>1.0.44.15-SNAPSHOT</version>
</dependency>
```

Expand All @@ -81,7 +81,7 @@ mavenCentral()
* gradle / Android Studio, 最新版

```
compile 'in.srain.cube:cube-sdk:1.0.44.14-SNAPSHOT@aar'
compile 'in.srain.cube:cube-sdk:1.0.44.15-SNAPSHOT@aar'
```

* gradle / Android Studio, 稳定版
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ http://cube-sdk.liaohuqiu.net

#### Repository

The latest version: `1.0.44.14-SNAPSHOT`, has been published to: https://oss.sonatype.org/content/repositories/snapshots, in gradle:
The latest version: `1.0.44.15-SNAPSHOT`, has been published to: https://oss.sonatype.org/content/repositories/snapshots, in gradle:

* gradle

Expand Down Expand Up @@ -62,7 +62,7 @@ The stable version: `1.0.42`, https://oss.sonatype.org/content/repositories/rele
<type>aar</type>
<!-- or apklib format, if you want -->
<!-- <type>apklib</type> -->
<version>1.0.44.14-SNAPSHOT</version>
<version>1.0.44.15-SNAPSHOT</version>
</dependency>
```

Expand All @@ -82,7 +82,7 @@ The stable version: `1.0.42`, https://oss.sonatype.org/content/repositories/rele
* gradle, latest version:

```
compile 'in.srain.cube:cube-sdk:1.0.44.14-SNAPSHOT@aar'
compile 'in.srain.cube:cube-sdk:1.0.44.15-SNAPSHOT@aar'
```

* gradle, stable version:
Expand Down
2 changes: 1 addition & 1 deletion core/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION_NAME=1.0.44.14-SNAPSHOT
VERSION_NAME=1.0.44.15-SNAPSHOT

ANDROID_BUILD_MIN_SDK_VERSION=8
ANDROID_BUILD_TARGET_SDK_VERSION=16
Expand Down
2 changes: 1 addition & 1 deletion core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<artifactId>cube-sdk</artifactId>
<packaging>aar</packaging>
<name>Cube SDK</name>
<version>1.0.44.14-SNAPSHOT</version>
<version>1.0.44.15-SNAPSHOT</version>

<dependencies>
<dependency>
Expand Down
13 changes: 13 additions & 0 deletions core/src/in/srain/cube/image/ImageDiskCacheProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,19 @@ public static ImageDiskCacheProvider createLru(long size, File path) {
return provider;
}

public long getSize(String key) {
if (!mDiskCache.has(key)) {
return -1;
}
try {
CacheEntry cacheEntry = mDiskCache.getEntry(key);
return cacheEntry.getSize();
} catch (IOException e) {
e.printStackTrace();
}
return -1;
}

public FileInputStream getInputStream(String key) {
if (!mDiskCache.has(key)) {
return null;
Expand Down
10 changes: 2 additions & 8 deletions core/src/in/srain/cube/image/ImageLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public boolean queryCache(ImageTask imageTask, CubeImageView imageView) {
BitmapDrawable drawable = mImageProvider.getBitmapFromMemCache(imageTask);

if (imageTask.getStatistics() != null) {
imageTask.getStatistics().afterMemoryCache(drawable != null);
imageTask.getStatistics().s0_afterCheckMemoryCache(drawable != null);
}
if (drawable == null) {
return false;
Expand Down Expand Up @@ -277,7 +277,7 @@ public void doInBackground() {
}

if (mImageTask.getStatistics() != null) {
mImageTask.getStatistics().beginLoad();
mImageTask.getStatistics().s1_beginLoad();
}
Bitmap bitmap = null;
// Wait here if work is paused and the task is not cancelled
Expand All @@ -303,14 +303,8 @@ public void doInBackground() {
if (DEBUG) {
CLog.d(LOG_TAG, MSG_TASK_AFTER_fetchBitmapData, this, mImageTask, isCancelled());
}
if (mImageTask != null && mImageTask.getStatistics() != null) {
mImageTask.getStatistics().afterDecode();
}
mDrawable = mImageLoader.mImageProvider.createBitmapDrawable(mImageLoader.mResources, bitmap);
mImageLoader.mImageProvider.addBitmapToMemCache(mImageTask.getIdentityKey(), mDrawable);
if (mImageTask.getStatistics() != null) {
mImageTask.getStatistics().afterCreateBitmapDrawable();
}
} catch (Exception e) {
e.printStackTrace();
} catch (OutOfMemoryError e) {
Expand Down
5 changes: 4 additions & 1 deletion core/src/in/srain/cube/image/ImageLoaderFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import android.text.TextUtils;
import in.srain.cube.cache.DiskFileUtils;
import in.srain.cube.image.iface.*;
import in.srain.cube.image.impl.*;
import in.srain.cube.image.impl.DefaultImageLoadHandler;
import in.srain.cube.image.impl.DefaultImageReSizer;
import in.srain.cube.image.impl.DefaultImageTaskExecutor;
import in.srain.cube.image.impl.DefaultMemoryCache;

/**
* Create an {@link ImageLoader}.
Expand Down
17 changes: 17 additions & 0 deletions core/src/in/srain/cube/image/ImagePerformanceStatistics.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package in.srain.cube.image;

import in.srain.cube.image.iface.ImageLoadProfiler;

public class ImagePerformanceStatistics {

private static int sSAMPLE_NUM = 0;
private static ImageLoadProfiler sImageLoadProfiler;

public static void setSample(int num) {
sSAMPLE_NUM = num;
Expand All @@ -11,4 +14,18 @@ public static void setSample(int num) {
public static boolean sample(int id) {
return sSAMPLE_NUM != 0 && id % sSAMPLE_NUM == 0;
}

/**
* @param profiler
*/
@SuppressWarnings({"unused"})
public static void setImageLoadProfile(ImageLoadProfiler profiler) {
sImageLoadProfiler = profiler;
}

public static void onImageLoaded(ImageTask task, ImageTaskStatistics stat) {
if (sImageLoadProfiler != null) {
sImageLoadProfiler.onImageLoaded(task, stat);
}
}
}
136 changes: 70 additions & 66 deletions core/src/in/srain/cube/image/ImageProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ private static int getBytesPerPixel(Config config) {
* @return size in bytes
*/
@TargetApi(19) // @TargetApi(VERSION_CODES.KITKAT)
public static int getBitmapSize(BitmapDrawable value) {
public static long getBitmapSize(BitmapDrawable value) {
if (null == value) {
return 0;
}
Expand Down Expand Up @@ -162,87 +162,91 @@ public void cancelTask(ImageTask task) {
*/
public Bitmap fetchBitmapData(ImageLoader imageLoader, ImageTask imageTask, ImageReSizer imageReSizer) {
Bitmap bitmap = null;
if (mDiskCacheProvider != null) {
FileInputStream inputStream = null;
if (mDiskCacheProvider == null) {
return null;
}
FileInputStream inputStream = null;

String fileCacheKey = imageTask.getFileCacheKey();
ImageReuseInfo reuseInfo = imageTask.getImageReuseInfo();
String fileCacheKey = imageTask.getFileCacheKey();
ImageReuseInfo reuseInfo = imageTask.getImageReuseInfo();

if (DEBUG) {
Log.d(TAG, String.format(MSG_FETCH_BEGIN, imageTask));
Log.d(TAG, String.format(MSG_FETCH_BEGIN_IDENTITY_KEY, imageTask, imageTask.getIdentityKey()));
Log.d(TAG, String.format(MSG_FETCH_BEGIN_FILE_CACHE_KEY, imageTask, fileCacheKey));
Log.d(TAG, String.format(MSG_FETCH_BEGIN_ORIGIN_URL, imageTask, imageTask.getOriginUrl()));
Log.d(TAG, String.format(MSG_FETCH_BEGIN_IDENTITY_URL, imageTask, imageTask.getIdentityUrl()));
}
if (DEBUG) {
Log.d(TAG, String.format(MSG_FETCH_BEGIN, imageTask));
Log.d(TAG, String.format(MSG_FETCH_BEGIN_IDENTITY_KEY, imageTask, imageTask.getIdentityKey()));
Log.d(TAG, String.format(MSG_FETCH_BEGIN_FILE_CACHE_KEY, imageTask, fileCacheKey));
Log.d(TAG, String.format(MSG_FETCH_BEGIN_ORIGIN_URL, imageTask, imageTask.getOriginUrl()));
Log.d(TAG, String.format(MSG_FETCH_BEGIN_IDENTITY_URL, imageTask, imageTask.getIdentityUrl()));
}

// read from file cache
inputStream = mDiskCacheProvider.getInputStream(fileCacheKey);
// read from file cache
inputStream = mDiskCacheProvider.getInputStream(fileCacheKey);

// try to reuse
if (inputStream == null) {
if (reuseInfo != null && reuseInfo.getReuseSizeList() != null && reuseInfo.getReuseSizeList().length > 0) {
if (DEBUG) {
Log.d(TAG, String.format(MSG_FETCH_TRY_REUSE, imageTask));
}
// try to reuse
if (inputStream == null) {
if (reuseInfo != null && reuseInfo.getReuseSizeList() != null && reuseInfo.getReuseSizeList().length > 0) {
if (DEBUG) {
Log.d(TAG, String.format(MSG_FETCH_TRY_REUSE, imageTask));
}

final String[] sizeKeyList = reuseInfo.getReuseSizeList();
for (int i = 0; i < sizeKeyList.length; i++) {
String size = sizeKeyList[i];
final String key = imageTask.generateFileCacheKeyForReuse(size);
inputStream = mDiskCacheProvider.getInputStream(key);

final String[] sizeKeyList = reuseInfo.getReuseSizeList();
for (int i = 0; i < sizeKeyList.length; i++) {
String size = sizeKeyList[i];
final String key = imageTask.generateFileCacheKeyForReuse(size);
inputStream = mDiskCacheProvider.getInputStream(key);

if (inputStream != null) {
if (DEBUG) {
Log.d(TAG, String.format(MSG_FETCH_REUSE_SUCCESS, imageTask, size));
}
break;
} else {
if (DEBUG) {
Log.d(TAG, String.format(MSG_FETCH_REUSE_FAIL, imageTask, size, key));
}
if (inputStream != null) {
if (DEBUG) {
Log.d(TAG, String.format(MSG_FETCH_REUSE_SUCCESS, imageTask, size));
}
break;
} else {
if (DEBUG) {
Log.d(TAG, String.format(MSG_FETCH_REUSE_FAIL, imageTask, size, key));
}
}
}
} else {
if (DEBUG) {
Log.d(TAG, String.format(MSG_FETCH_HIT_DISK_CACHE, imageTask));
}
}
} else {
if (DEBUG) {
Log.d(TAG, String.format(MSG_FETCH_HIT_DISK_CACHE, imageTask));
}
}

if (imageTask.getStatistics() != null) {
imageTask.getStatistics().s2_afterCheckFileCache(inputStream != null);
}

// We've got nothing from file cache
if (inputStream == null) {
String url = imageReSizer.getRemoteUrl(imageTask);
if (DEBUG) {
Log.d(TAG, String.format(MSG_FETCH_DOWNLOAD, imageTask, url));
}
inputStream = mDiskCacheProvider.downloadAndGetInputStream(imageLoader.getImageDownloader(), imageTask, fileCacheKey, url);
if (imageTask.getStatistics() != null) {
imageTask.getStatistics().afterFileCache(inputStream != null);
imageTask.getStatistics().s3_afterDownload();
}

// We've got nothing from file cache
if (inputStream == null) {
String url = imageReSizer.getRemoteUrl(imageTask);
if (DEBUG) {
Log.d(TAG, String.format(MSG_FETCH_DOWNLOAD, imageTask, url));
}
inputStream = mDiskCacheProvider.downloadAndGetInputStream(imageLoader.getImageDownloader(), imageTask, fileCacheKey, url);
if (imageTask.getStatistics() != null) {
imageTask.getStatistics().afterDownload();
}
if (inputStream == null) {
imageTask.setError(ImageTask.ERROR_NETWORK);
CLog.e(TAG, "%s download fail: %s %s", imageTask, fileCacheKey, url);
}
imageTask.setError(ImageTask.ERROR_NETWORK);
CLog.e(TAG, "%s download fail: %s %s", imageTask, fileCacheKey, url);
}
if (inputStream != null) {
try {
bitmap = decodeSampledBitmapFromDescriptor(inputStream.getFD(), imageTask, imageReSizer);
if (bitmap == null) {
imageTask.setError(ImageTask.ERROR_BAD_FORMAT);
CLog.e(TAG, "%s decode bitmap fail, bad format. %s, %s", imageTask, fileCacheKey, imageReSizer.getRemoteUrl(imageTask));
}
} catch (IOException e) {
CLog.e(TAG, "%s decode bitmap fail, may be out of memory. %s, %s", imageTask, fileCacheKey, imageReSizer.getRemoteUrl(imageTask));
e.printStackTrace();
}
if (inputStream != null) {
try {
bitmap = decodeSampledBitmapFromDescriptor(inputStream.getFD(), imageTask, imageReSizer);
if (bitmap == null) {
imageTask.setError(ImageTask.ERROR_BAD_FORMAT);
CLog.e(TAG, "%s decode bitmap fail, bad format. %s, %s", imageTask, fileCacheKey, imageReSizer.getRemoteUrl(imageTask));
}
} else {
CLog.e(TAG, "%s fetch bitmap fail. %s, %s", imageTask, fileCacheKey, imageReSizer.getRemoteUrl(imageTask));
} catch (IOException e) {
CLog.e(TAG, "%s decode bitmap fail, may be out of memory. %s, %s", imageTask, fileCacheKey, imageReSizer.getRemoteUrl(imageTask));
e.printStackTrace();
}
} else {
CLog.e(TAG, "%s fetch bitmap fail. %s, %s", imageTask, fileCacheKey, imageReSizer.getRemoteUrl(imageTask));
}
if (imageTask != null && imageTask.getStatistics() != null) {
imageTask.getStatistics().s4_afterDecode(mDiskCacheProvider.getSize(fileCacheKey));
}
return bitmap;
}
Expand Down
5 changes: 3 additions & 2 deletions core/src/in/srain/cube/image/ImageTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ public void onLoadTaskFinish(BitmapDrawable drawable, ImageLoadHandler handler)
}

if (null != mImageTaskStatistics) {
mImageTaskStatistics.showBegin();
mImageTaskStatistics.s5_beforeShow();
}
if (mFirstImageViewHolder == null) {
handler.onLoadFinish(this, null, drawable);
Expand All @@ -366,7 +366,8 @@ public void onLoadTaskFinish(BitmapDrawable drawable, ImageLoadHandler handler)
} while ((holder = holder.mNext) != null);
}
if (null != mImageTaskStatistics) {
mImageTaskStatistics.showComplete(ImageProvider.getBitmapSize(drawable));
mImageTaskStatistics.s6_afterShow(ImageProvider.getBitmapSize(drawable));
ImagePerformanceStatistics.onImageLoaded(this, mImageTaskStatistics);
}
}

Expand Down
Loading

0 comments on commit 128adc3

Please sign in to comment.