### Question:
Element implements Serializable, Cloneable { public final long getSerializedSize() { if (!isSerializable()) { return 0; } long size = 0; ByteArrayOutputStream bout = new ByteArrayOutputStream(); ObjectOutputStream oos = null; try { oos = new ObjectOutputStream(bout); oos.writeObject(this); size = bout.size(); return size; } catch (IOException e) { LOG.log(Level.FINE, "Error measuring element size for element with key " + key + ". Cause was: " + e.getMessage()); } finally { try { if (oos != null) { oos.close(); } } catch (Exception e) { LOG.log(Level.SEVERE, "Error closing ObjectOutputStream"); } } return size; } Element(Serializable key, Serializable value, long version);  Element(Object key, Object value, long version);  Element(Object key, Object value, long version,
                   long creationTime, long lastAccessTime,
                   long nextToLastAccessTime, long lastUpdateTime,
                   long hitCount);  Element(Object key, Object value,
                   Boolean eternal, Integer timeToIdleSeconds, Integer timeToLiveSeconds);  Element(Serializable key, Serializable value);  Element(Object key, Object value); final Serializable getKey(); final Object getObjectKey(); final Serializable getValue(); final Object getObjectValue(); final boolean equals(Object object); void setTimeToLive(int timeToLiveSeconds); void setTimeToIdle(int timeToIdleSeconds); final int hashCode(); final void setVersion(long version); final long getCreationTime(); final long getLatestOfCreationAndUpdateTime(); final void setCreateTime(); final long getVersion(); final long getLastAccessTime(); final long getNextToLastAccessTime(); final long getHitCount(); final void resetAccessStatistics(); final void updateAccessStatistics(); final void updateUpdateStatistics(); final String toString(); final Object clone(); final long getSerializedSize(); final boolean isSerializable(); final boolean isKeySerializable(); long getLastUpdateTime(); boolean isExpired(); long getExpirationTime(); boolean isEternal(); void setEternal(boolean eternal); boolean isLifespanSet(); int getTimeToLive(); int getTimeToIdle();  }### Answer:
@Test public void testSerializationPerformanceByteArray() throws CacheException { Serializable key = "key"; ByteArrayOutputStream bout = new ByteArrayOutputStream(); for (int j = 0; j < 10000; j++) { try { bout.write("abcdefghijklmnopqrstv1234567890".getBytes()); } catch (IOException e) { LOG.log(Level.SEVERE, "This should not happen"); } } byte[] value = bout.toByteArray(); Element element = new Element(key, value); StopWatch stopWatch = new StopWatch(); for (int i = 0; i < 100; i++) { element.getSerializedSize(); } long elapsed = stopWatch.getElapsedTime() / 100; LOG.log(Level.INFO, "In-memory size in bytes: " + element.getSerializedSize() + " time to serialize in ms: " + elapsed); assertTrue("Large object clone takes more than than 100ms", elapsed < 100); }
@Test public void testSerializationPerformanceJavaObjects() throws Exception { HashMap map = new HashMap(10000); for (int j = 0; j < 10000; j++) { map.put("key" + j, new String[]{"adfdafs", "asdfdsafa", "sdfasdf"}); } Element element = new Element("key1", map); StopWatch stopWatch = new StopWatch(); for (int i = 0; i < 100; i++) { element.getSerializedSize(); } long elapsed = stopWatch.getElapsedTime() / 100; LOG.log(Level.INFO, "In-memory size in bytes: " + element.getSerializedSize() + " time to serialize in ms: " + elapsed); assertTrue("Large object clone took more than 500ms", elapsed < 500); }