Skip to content

Instantly share code, notes, and snippets.

@apcj
Last active August 29, 2015 14:06
Show Gist options
  • Save apcj/e52bd7b15249d182a29a to your computer and use it in GitHub Desktop.
Save apcj/e52bd7b15249d182a29a to your computer and use it in GitHub Desktop.
Cypher HTTP

Run simple cypher queries via the Neo4j Transactional Cypher HTTP endpoint to estimate the HTTP overhead.

Request serialisation and response deserialisation using Jackson. HTTP using Apache HTTP Client.

Example output:

result = 1
round-trip time: 123ms
result = 1
round-trip time: 5ms
result = 1
round-trip time: 4ms
result = 1
round-trip time: 3ms
result = 1
round-trip time: 3ms
result = 1
round-trip time: 3ms
result = 1
round-trip time: 3ms
result = 1
round-trip time: 4ms
result = 1
round-trip time: 3ms
result = 1
round-trip time: 3ms
<?xml version="1.0" encoding="UTF-8"?>
<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>apcj</groupId>
<artifactId>remote-vs-embedded</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.3.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
</dependencies>
</project>
import java.util.concurrent.Callable;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.junit.Test;
public class RemoteOverhead
{
@Test
public void transactionalEndpoint() throws Exception
{
try ( CloseableHttpClient httpClient = HttpClientBuilder.create().build() )
{
final ObjectMapper jsonMapper = new ObjectMapper();
for ( int i = 0; i < 10; i++ )
{
time( new Callable<Void>()
{
@Override
public Void call() throws Exception
{
ObjectNode requestJson = JsonNodeFactory.instance.objectNode();
requestJson.withArray( "statements" ).addObject().put( "statement", "RETURN 1" );
HttpPost request = new HttpPost( "http://localhost:7474/db/data/transaction/commit" );
request.setEntity( new StringEntity( jsonMapper.writeValueAsString( requestJson ),
ContentType.APPLICATION_JSON ) );
try ( CloseableHttpResponse response = httpClient.execute( request ) )
{
JsonNode responseJson = jsonMapper.readTree( response.getEntity().getContent() );
System.out.println( "result = " +
responseJson.get( "results" ).get( 0 ).get( "data" ).get( 0 ).get( "row" ).get( 0 ) );
}
return null;
}
} );
}
}
}
private static void time( Callable<Void> callable ) throws Exception
{
long before = System.currentTimeMillis();
callable.call();
long after = System.currentTimeMillis();
System.out.printf( "round-trip time: %dms%n", after - before );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment