Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/

package org.apache.http.impl.client.cache;

/**
* Problem while serializing or deserializing a cache entry.
*/
public class MemcachedCacheEntryHttpException extends RuntimeException {
public MemcachedCacheEntryHttpException(final String message) {
super(message);
}

public MemcachedCacheEntryHttpException(final String message, final Throwable cause) {
super(message, cause);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/

package org.apache.http.impl.client.cache;

import org.apache.http.client.cache.HttpCacheEntry;
import org.apache.http.impl.client.cache.memcached.MemcachedCacheEntry;
import org.apache.http.impl.client.cache.memcached.MemcachedCacheEntryFactory;

public class MemcachedCacheEntryHttpFactory implements MemcachedCacheEntryFactory {
@Override
public MemcachedCacheEntry getMemcachedCacheEntry(final String key, final HttpCacheEntry entry) {
return new MemcachedCacheEntryHttp(key, entry);
}

@Override
public MemcachedCacheEntry getUnsetCacheEntry() {
return new MemcachedCacheEntryHttp();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/

package org.apache.http.impl.client.cache;

import java.io.File;
import java.io.FileInputStream;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.client.cache.HttpCacheEntry;
import org.apache.http.impl.client.cache.memcached.MemcachedCacheEntry;
import org.apache.http.impl.client.cache.memcached.MemcachedCacheEntryFactory;
import org.apache.http.impl.client.cache.memcached.MemcachedCacheEntryFactoryImpl;
import org.junit.Before;
import org.junit.Test;

import static org.apache.http.impl.client.cache.MemcachedCacheEntryHttpTestUtils.buildSimpleTestObjectFromTemplate;
import static org.apache.http.impl.client.cache.MemcachedCacheEntryHttpTestUtils.makeTestFileObject;
import static org.apache.http.impl.client.cache.MemcachedCacheEntryHttpTestUtils.memcachedCacheEntryFromBytes;
import static org.apache.http.impl.client.cache.MemcachedCacheEntryHttpTestUtils.readFully;
import static org.apache.http.impl.client.cache.MemcachedCacheEntryHttpTestUtils.verifyHttpCacheEntryFromBytes;

public class BenchmarkMemcachedCacheEntryHttp {
private static final String TEST_CONTENT_FILE_NAME = "ApacheLogo.png";
private static final String TEST_STORAGE_KEY = "xyzzy";

private MemcachedCacheEntryFactory cacheEntryFactory;
private String newCacheName;

@Before
public void before() {
cacheEntryFactory = new MemcachedCacheEntryHttpFactory();
newCacheName = "HTTP";
}

@Test
public void simpleTestBenchmark() throws Exception {
final HttpCacheEntry testEntry = buildSimpleTestObjectFromTemplate(Collections.<String, Object>emptyMap());

benchmarkSerializeDeserialize(newCacheName + " simple object", TEST_STORAGE_KEY, testEntry, cacheEntryFactory);
}

@Test
public void fileTestBenchmark() throws Exception {
final Map<String, Object> cacheObjectValues = new HashMap<String, Object>();
cacheObjectValues.put("resource", new FileResource(makeTestFileObject(TEST_CONTENT_FILE_NAME)));
final HttpCacheEntry testEntry = buildSimpleTestObjectFromTemplate(cacheObjectValues);

benchmarkSerializeDeserialize(newCacheName + " file object", TEST_STORAGE_KEY, testEntry, cacheEntryFactory);
}

@Test
public void oldSimpleTestBenchmark() throws Exception {
final HttpCacheEntry testEntry = buildSimpleTestObjectFromTemplate(Collections.<String, Object>emptyMap());
final MemcachedCacheEntryFactory factory = new MemcachedCacheEntryFactoryImpl();

benchmarkSerializeDeserialize("Java simple object", TEST_STORAGE_KEY, testEntry, factory);
}

@Test
public void oldFileTestBenchmark() throws Exception {
final Map<String, Object> cacheObjectValues = new HashMap<String, Object>();
final File testFile = makeTestFileObject(TEST_CONTENT_FILE_NAME);
// Turn this into a heap resource, otherwise the Java serializer doesn't serialize the whole body.
final byte[] testBytes = readFully(new FileInputStream(testFile), (int) testFile.length());
cacheObjectValues.put("resource", new HeapResource(testBytes));
final HttpCacheEntry testEntry = buildSimpleTestObjectFromTemplate(cacheObjectValues);
final MemcachedCacheEntryFactory factory = new MemcachedCacheEntryFactoryImpl();

benchmarkSerializeDeserialize("Java file object", TEST_STORAGE_KEY, testEntry, factory);
}

// Helper methods

/**
* Benchmark the given factory's serialization size and time, and deserialization time, printing a summary to
* System.out.
*
* @param testName Name of test for printing
* @param storageKey Storage key for test object
* @param testEntry Test object to serialize/deserialize
* @param factory Factory for serialization/deserialization
* @throws Exception if anything goes wrong
*/
private static void benchmarkSerializeDeserialize(final String testName, final String storageKey, final HttpCacheEntry testEntry, final MemcachedCacheEntryFactory factory) throws Exception {
final MemcachedCacheEntry memcachedCacheEntry = factory.getMemcachedCacheEntry(storageKey, testEntry);
final byte[] testBytes = memcachedCacheEntry.toByteArray();

// Verify once to make sure everything is right, and maybe warm things up a little
verifyHttpCacheEntryFromBytes(storageKey, testEntry, factory, testBytes);

System.out.printf("%40s: %6d bytes\n", testName + " serialized size", testBytes.length);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

\n > %n


simpleBenchmark(testName + " serialize", new Runnable() {
public void run () {
factory.getMemcachedCacheEntry(storageKey, testEntry).toByteArray();
}
});

simpleBenchmark(testName + " deserialize", new Runnable() {
public void run () {
final MemcachedCacheEntry testMemcachedCacheEntry = memcachedCacheEntryFromBytes(factory, testBytes);
testMemcachedCacheEntry.getStorageKey();
testMemcachedCacheEntry.getHttpCacheEntry();
}
});
}

/**
* Benchmark the given function object, and print timing information to System.out.
*
* @param testName Name of benchmark for printing
* @param func Function to benchmark
*/
static void simpleBenchmark(final String testName, final Runnable func) {
// Warm Up
final int warmupRuns = 5000;
for(int i=0;i<warmupRuns;i++) {
func.run();
}

final long start = System.nanoTime();
final int runs = 5000;
for(int i=0;i<runs;i++) {
func.run();
}
final long end = System.nanoTime();
final long elapsed = end - start;
final long each = elapsed / runs;
System.out.printf("%40s: %6d runs in %7.3f ms, %7.3f ms/run\n", testName, runs, elapsed/1000000.0, each/1000000.0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

Also use TimeUnit..toMilli.. it makes the code more readable.

}
}
Loading