jython30个并发性能下降比较明显,有超时现象:
引用
<urlopen error timed out> /napi/blog/detail/?blog_id=45756664
cost:2.00654006004
cost:1.49004387856
cost:0.0366380214691
cost:1.3818898201
cost:1.11271309853
cost:0.0867030620575
cost:0.51217007637
<urlopen error timed out> /napi/blog/detail/?blog_id=45705708
jstack看了一下大量被IdImpl.id()BLOCKED了
引用
"catalina-exec-111" daemon prio=10 tid=0x00002aaac0844000 nid=0x1a91 waiting for monitor entry [0x0000000052436000]
java.lang.Thread.State: BLOCKED (on object monitor)
at org.python.core.IdImpl.id(IdImpl.java:76)
- waiting to lock <0x0000000704407ec8> (a org.python.core.IdImpl)
at org.python.core.Py.id(Py.java:1830)
at org.python.core.__builtin__.id(__builtin__.java:667)
at org.python.core.BuiltinFunctions.__call__(__builtin__.java:82)
at org.python.core.PyObject.__call__(PyObject.java:407)
at copy$py._keep_alive$13(/data1/duitang/dist/sys/jython/Lib/copy.py:281)
at copy$py.call_function(/data1/duitang/dist/sys/jython/Lib/copy.py)
at org.python.core.PyTableCode.call(PyTableCode.java:165)
at org.python.core.PyBaseCode.call(PyBaseCode.java:149)
at org.python.core.PyFunction.__call__(PyFunction.java:357)
at copy$py.deepcopy$7(/data1/duitang/dist/sys/jython/Lib/copy.py:194)
at copy$py.call_function(/data1/duitang/dist/sys/jython/Lib/copy.py)
at org.python.core.PyTableCode.call(PyTableCode.java:165)
at org.python.core.PyBaseCode.call(PyBaseCode.java:301)
at org.python.core.PyFunction.function___call__(PyFunction.java:406)
at org.python.core.PyFunction.__call__(PyFunction.java:401)
at django.db.models.sql.query$py.clone$18(/duitang/dist/sys/tomcat/webapps/ROOT/WEB-INF/lib-python/Lib/site-packages/django/db/models/sql/query.py:291)
at django.db.models.sql.query$py.call_function(/duitang/dist/sys/tomcat/webapps/ROOT/WEB-INF/lib-python/Lib/site-packages/django/db/models/sql/query.py)
at org.python.core.PyTableCode.call(PyTableCode.java:165)
at org.python.core.PyBaseCode.call(PyBaseCode.java:301)
at org.python.core.PyBaseCode.call(PyBaseCode.java:127)
at org.python.core.PyFunction.__call__(PyFunction.java:347)
at org.python.core.PyMethod.__call__(PyMethod.java:109)
at django.db.models.query$py._clone$47(/duitang/dist/sys/tomcat/webapps/ROOT/WEB-INF/lib-python/Lib/site-packages/django/db/models/query.py:762)
at django.db.models.query$py.call_function(/duitang/dist/sys/tomcat/webapps/ROOT/WEB-INF/lib-python/Lib/site-packages/django/db/models/query.py)
at org.python.core.PyTableCode.call(PyTableCode.java:165)
at org.python.core.PyBaseCode.call(PyBaseCode.java:301)
at org.python.core.PyBaseCode.call(PyBaseCode.java:127)
at org.python.core.PyFunction.__call__(PyFunction.java:347)
看了jython的实现大概明白了,他自己实现了一个WeakIdentityMap用来做cache,但不是线程安全,就粗暴的在 public synchronized long id(PyObject o)方法前面直接加上 synchronized。
package org.python.core;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;
import java.util.Map;
import org.python.util.Generic;
public class IdImpl {
private WeakIdentityMap idMap = new WeakIdentityMap();
private long sequentialId;
public synchronized long id(PyObject o) {
Object javaProxy = o.getJavaProxy();
if (javaProxy != null) {
return java_obj_id(javaProxy);
} else {
return java_obj_id(o);
}
}
public String idstr(PyObject o) {
return String.format("0x%x", id(o));
}
public synchronized long java_obj_id(Object o) {
Long cand = (Long)idMap.get(o);
if (cand == null) {
long new_id = ++sequentialId;
idMap.put(o, new_id);
return new_id;
}
return cand.longValue();
}
}
看了大概明白了,他自己实现了一个WeakIdentityMap用来做cache,但不是线程安全,就粗暴的在方法前面直接加上 synchronized。
这是他自己实现的WeakIdentityMap:
public static class WeakIdentityMap {
private transient ReferenceQueue<Object> idKeys = new ReferenceQueue<Object>();
private Map<WeakIdKey, Object> objHashcodeToPyId = Generic.map();
@SuppressWarnings("element-type-mismatch")
private void cleanup() {
Object k;
while ((k = idKeys.poll()) != null) {
objHashcodeToPyId.remove(k);
}
}
private class WeakIdKey extends WeakReference<Object> {
private final int hashcode;
WeakIdKey(Object obj) {
super(obj, idKeys);
hashcode = System.identityHashCode(obj);
}
@Override
public int hashCode() {
return hashcode;
}
@Override
public boolean equals(Object other) {
Object obj = get();
if (obj != null) {
return obj == ((WeakIdKey)other).get();
} else {
return this == other;
}
}
}
// Used by test_jy_internals
public int _internal_map_size() {
return objHashcodeToPyId.size();
}
public void put(Object key, Object val) {
cleanup();
objHashcodeToPyId.put(new WeakIdKey(key), val);
}
public Object get(Object key) {
cleanup();
return objHashcodeToPyId.get(new WeakIdKey(key));
}
public void remove(Object key) {
cleanup();
objHashcodeToPyId.remove(new WeakIdKey(key));
}
}