* All asset cache operations can now work on smart cache

* Added better test for asset cache

git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@7917 75d07b2b-3a1a-0410-a2c5-0572b91ccdca
This commit is contained in:
sha..rd 2011-07-25 16:05:21 +00:00
parent ae2fda8626
commit f15107da95
2 changed files with 130 additions and 63 deletions
engine/src
desktop/com/jme3/asset
test/jme3test/asset

@ -84,12 +84,12 @@ public class AssetCache {
* <font color="red">Thread-safe.</font> * <font color="red">Thread-safe.</font>
*/ */
public boolean deleteFromCache(AssetKey key){ public boolean deleteFromCache(AssetKey key){
if (key.useSmartCache()){
throw new UnsupportedOperationException("You cannot delete from the smart cache");
}
synchronized (regularCache){ synchronized (regularCache){
return regularCache.remove(key) != null; if (key.useSmartCache()){
return smartCache.remove(key) != null;
}else{
return regularCache.remove(key) != null;
}
} }
} }
@ -125,6 +125,7 @@ public class AssetCache {
public void deleteAllAssets(){ public void deleteAllAssets(){
synchronized (regularCache){ synchronized (regularCache){
regularCache.clear(); regularCache.clear();
smartCache.clear();
} }
} }
} }

@ -32,74 +32,140 @@
package jme3test.asset; package jme3test.asset;
import com.jme3.asset.Asset;
import com.jme3.asset.AssetCache; import com.jme3.asset.AssetCache;
import com.jme3.asset.AssetKey; import com.jme3.asset.AssetKey;
import java.util.ArrayList;
import java.util.List;
public class TestAssetCache { public class TestAssetCache {
private static class MyAsset {
private String name;
private byte[] bytes = new byte[100];
public MyAsset(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
// private static final long memoryUsage(){ /**
// return Runtime.getRuntime(). * Keep references to loaded assets
// } */
// private final static boolean KEEP_REFERENCES = false;
public static void main(String[] args){
AssetCache cache = new AssetCache(); /**
* Enable smart cache use
*/
private final static boolean USE_SMART_CACHE = true;
/**
* Enable cloneable asset use
*/
private final static boolean CLONEABLE_ASSET = true;
System.gc(); private static int counter = 0;
System.gc();
System.gc(); private static class DummyData implements Asset {
System.gc();
long startMem = Runtime.getRuntime().freeMemory(); private AssetKey key;
private byte[] data = new byte[10000];
for (int i = 0; i < 10000; i++){ public byte[] getData(){
MyAsset asset = new MyAsset("asset"+i); return data;
AssetKey key = new AssetKey(asset.getName()); }
public AssetKey getKey() {
return key;
} }
long endMem = Runtime.getRuntime().freeMemory(); public void setKey(AssetKey key) {
System.out.println("No cache diff:\t"+(startMem-endMem)); this.key = key;
}
System.gc(); }
System.gc();
System.gc(); private static class SmartKey extends AssetKey {
System.gc();
public SmartKey(){
endMem = Runtime.getRuntime().freeMemory(); super(".");
System.out.println("No cache gc diff:\t"+(startMem-endMem)); counter++;
startMem = endMem; }
for (int i = 0; i < 10000; i++){ @Override
MyAsset asset = new MyAsset("asset"+i); public int hashCode(){
AssetKey key = new AssetKey(asset.getName()); return 0;
cache.addToCache(key, asset); }
@Override
public boolean equals(Object other){
return false;
}
@Override
public boolean useSmartCache(){
return true;
}
@Override
public Object createClonedInstance(Object asset){
DummyData data = new DummyData();
return data;
}
}
private static class DumbKey extends AssetKey {
public DumbKey(){
super(".");
counter++;
}
@Override
public int hashCode(){
return 0;
}
@Override
public boolean equals(Object other){
return false;
}
@Override
public Object createClonedInstance(Object asset){
if (CLONEABLE_ASSET){
DummyData data = new DummyData();
return data;
}else{
return asset;
}
}
}
public static void main(String[] args){
List<Object> refs = new ArrayList<Object>(5000);
AssetCache cache = new AssetCache();
System.gc();
System.gc();
System.gc();
System.gc();
long memory = Runtime.getRuntime().freeMemory();
while (true){
AssetKey key;
if (USE_SMART_CACHE){
key = new SmartKey();
}else{
key = new DumbKey();
}
DummyData data = new DummyData();
cache.addToCache(key, data);
if (KEEP_REFERENCES){
refs.add(data);
}
if ((counter % 100) == 0){
long newMem = Runtime.getRuntime().freeMemory();
System.out.println("Allocated objects: " + counter);
System.out.println("Allocated memory: " + ((memory - newMem)/1024) + "K" );
memory = newMem;
}
} }
endMem = Runtime.getRuntime().freeMemory();
System.out.println("Cache diff:\t"+(startMem-endMem));
System.gc();
System.gc();
System.gc();
System.gc();
endMem = Runtime.getRuntime().freeMemory();
System.out.println("Cache gc diff:\t"+(startMem-endMem));
// System.out.println("Estimated usage: "+)
} }
} }