Skip to content

Instantly share code, notes, and snippets.

@benelog
Created August 14, 2026 09:41
Show Gist options
  • Select an option

  • Save benelog/a9db4ac3018d6222baea32d5c2f783b5 to your computer and use it in GitHub Desktop.

Select an option

Save benelog/a9db4ac3018d6222baea32d5c2f783b5 to your computer and use it in GitHub Desktop.
benchmark-test (migrated from github.com/benelog/benchmark)
.classpath
.project
.settings/
target/

benchmark-test

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>net.benelog.test</groupId>
<artifactId>performance</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>performance</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.1.3</version>
</dependency>
<dependency>
<groupId>com.carrotsearch</groupId>
<artifactId>junit-benchmarks</artifactId>
<version>0.3.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.3.166</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>1.4</version>
</dependency>
</dependencies>
</project>
package net.benelog.test.performance;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.CoreConnectionPNames;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import com.carrotsearch.junitbenchmarks.AbstractBenchmark;
import com.carrotsearch.junitbenchmarks.BenchmarkOptions;
import com.carrotsearch.junitbenchmarks.annotation.BenchmarkMethodChart;
/**
* @author benelog
*/
@BenchmarkMethodChart(filePrefix = "benchmark-sample")
public class SampleBenchmarkTest extends AbstractBenchmark {
String imageUrl = "http://localhost/screen_shot/123.jpg";
static HttpClient httpClient;
static ThreadSafeClientConnManager connManager;
@BeforeClass
public static void prepareHttpClient(){
createHttpClient(5000, 5000);
}
@AfterClass
public static void releaseConnectionPool(){
connManager.shutdown();
}
@Test
@BenchmarkOptions(benchmarkRounds = 8, warmupRounds = 0, concurrency=8)
public void testCreateApk() throws ClientProtocolException, IOException, InterruptedException {
String requestUrl = "http://localhost/create";
HttpPost request = new HttpPost(requestUrl);
UrlEncodedFormEntity formEntity = createEntity();
request.setEntity(formEntity);
HttpResponse response = httpClient.execute(request);
HttpEntity entity = response.getEntity();
String message = IOUtils.toString(entity.getContent());
System.out.println(message);
int statusCode = response.getStatusLine().getStatusCode();
assertThat(statusCode, is(200));
}
private UrlEncodedFormEntity createEntity() throws UnsupportedEncodingException {
List<BasicNameValuePair> form = new ArrayList<BasicNameValuePair>();
add(form,"versionCode", "1");
return new UrlEncodedFormEntity(form, "UTF-8");
}
private void add(List<BasicNameValuePair> formParams, String key, String value) {
formParams.add(new BasicNameValuePair(key, value));
}
private static void createHttpClient(int connectTimeoutMilsec, int readTimeoutMilsec){
connManager = new ThreadSafeClientConnManager();
connManager.setMaxTotal(9);
connManager.setDefaultMaxPerRoute(9);
HttpClient client = new DefaultHttpClient(connManager);
client.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, connectTimeoutMilsec);
client.getParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT, readTimeoutMilsec);
httpClient = client;
}
}
package net.benelog.test.performance;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.CoreConnectionPNames;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
/**
* @author benelog
*/
public class SimpleRequestTest {
static HttpClient httpClient;
static AtomicInteger widgetIdSeq = new AtomicInteger();
static ThreadSafeClientConnManager connManager;
String content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<bang type=\"yaml\">--- !ruby/object:Time {}\n" +
"</bang>";
@BeforeClass
public static void prepareHttpClient(){
createHttpClient(5000, 5000);
}
@AfterClass
public static void releaseConnectionPool(){
connManager.shutdown();
}
@Test
public void query() throws ClientProtocolException, IOException, InterruptedException {
String requestUrl = "http://localhost";
HttpGet request = new HttpGet(requestUrl);
BasicHttpParams params = new BasicHttpParams();
params.setParameter("query", content);
request.setParams(params);
HttpResponse response = httpClient.execute(request);
HttpEntity entity = response.getEntity();
String message = IOUtils.toString(entity.getContent());
System.out.println(message);
int statusCode = response.getStatusLine().getStatusCode();
assertThat(statusCode, is(200));
}
private static void createHttpClient(int connectTimeoutMilsec, int readTimeoutMilsec){
connManager = new ThreadSafeClientConnManager();
connManager.setMaxTotal(9);
connManager.setDefaultMaxPerRoute(9);
HttpClient client = new DefaultHttpClient(connManager);
client.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, connectTimeoutMilsec);
client.getParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT, readTimeoutMilsec);
httpClient = client;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment