Skip to content

Commit

Permalink
Create modified wsclient that skips response deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
ryn5 committed Apr 29, 2024
1 parent 7be79eb commit d925d3e
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ protected void initChannel(final SocketChannel ch) {
new HttpClientCodec(),
new HttpObjectAggregator(65536),
wsHandler,
new WebSocketGremlinRequestEncoder(true, serializer),
new WebSocketGremlinRequestEncoder(true, skipDeserializer),
new WebSocketGremlinResponseDecoder(skipDeserializer),
callbackResponseHandler);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,13 @@
import java.util.Iterator;
import java.util.List;

import static java.util.concurrent.TimeUnit.MILLISECONDS;

@Warmup(iterations = 1)
@Measurement(iterations = 2)

@Warmup(time = 2001, timeUnit = MILLISECONDS, iterations = 1)
@Measurement(time = 2002, timeUnit = MILLISECONDS, iterations = 2)
//@Warmup(iterations = 1)
//@Measurement(iterations = 2)
@BenchmarkMode(Mode.AverageTime)
public class SimpleBenchmark extends AbstractBenchmarkBase2 {
@State(Scope.Thread)
Expand All @@ -59,6 +63,9 @@ public void doSetup() throws Exception {
@TearDown(Level.Trial)
public void doTearDown() throws Exception {
cluster.close();
skipClient.close();
simpleClient.close();
client.close();
}
}

Expand Down Expand Up @@ -100,7 +107,7 @@ public List<ResponseMessage> countGratefulSimple(BenchmarkState state) throws Ex
// 0.047s (slow iterations, doesn't exit)
}

@Benchmark
// @Benchmark
public Iterator<ResponseMessage> both2IteratorSimple(BenchmarkState state) throws Exception {
Iterator<ResponseMessage> res = state.simpleClient.submit("ggrateful.V().both().both();")
.iterator();
Expand All @@ -122,14 +129,18 @@ public List<ResponseMessage> both2ToListSimple(BenchmarkState state) throws Exce
}


// @Benchmark
@Benchmark
public List<ResponseMessage> simpleSkipDeser(BenchmarkState state) throws Exception {
final List<ResponseMessage> res = state.skipClient.submit("g.V().count()");
return res;
// 0.046s (slow iterations, doesn't exit)
}

// @Benchmark
public static void main(String[] args) throws Exception {
SimpleClient skipClient = TestClientFactory.createWebSocketClientSkipDeser(URI.create("ws://ec2-35-91-97-124.us-west-2.compute.amazonaws.com:45940/gremlin"));
final List<ResponseMessage> res = skipClient.submit("ggrateful.V().count();");
}
@Benchmark
public List<ResponseMessage> countGratefulSkipDeser(BenchmarkState state) throws Exception {
final List<ResponseMessage> res = state.skipClient.submit("ggrateful.V().count();");
return res;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,16 @@
import org.apache.tinkerpop.gremlin.structure.io.binary.types.CustomTypeSerializer;
import org.apache.tinkerpop.gremlin.util.message.RequestMessage;
import org.apache.tinkerpop.gremlin.util.message.ResponseMessage;
import org.apache.tinkerpop.gremlin.util.message.ResponseResult;
import org.apache.tinkerpop.gremlin.util.message.ResponseStatus;
import org.apache.tinkerpop.gremlin.util.message.ResponseStatusCode;
import org.apache.tinkerpop.gremlin.util.ser.binary.RequestMessageSerializer;
import org.apache.tinkerpop.gremlin.util.ser.binary.ResponseMessageSerializer;
import org.apache.tinkerpop.gremlin.util.ser.binary.ResponseMessageSerializer2;
import org.javatuples.Pair;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.Base64;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

import static java.nio.charset.StandardCharsets.UTF_8;
Expand All @@ -67,7 +62,7 @@ public class GraphBinaryMessageSerializerV1SkipDeser extends AbstractMessageSeri
private GraphBinaryReader reader;
private GraphBinaryWriter writer;
private RequestMessageSerializer requestSerializer;
private ResponseMessageSerializer responseSerializer;
private ResponseMessageSerializer2 responseSerializer;
private GraphBinaryMapper mapper;

/**
Expand All @@ -83,7 +78,7 @@ public GraphBinaryMessageSerializerV1SkipDeser(final TypeSerializerRegistry regi
mapper = new GraphBinaryMapper(writer, reader);

requestSerializer = new RequestMessageSerializer();
responseSerializer = new ResponseMessageSerializer();
responseSerializer = new ResponseMessageSerializer2();
}

public GraphBinaryMessageSerializerV1SkipDeser(final TypeSerializerRegistry.Builder builder) {
Expand Down Expand Up @@ -141,7 +136,7 @@ public void configure(final Map<String, Object> config, final Map<String, Graph>
writer = new GraphBinaryWriter(registry);

requestSerializer = new RequestMessageSerializer();
responseSerializer = new ResponseMessageSerializer();
responseSerializer = new ResponseMessageSerializer2();
}

@Override
Expand Down Expand Up @@ -219,20 +214,12 @@ public RequestMessage deserializeRequest(final String msg) throws SerializationE

@Override
public ResponseMessage deserializeResponse(final String msg) throws SerializationException {
UUID uuid = UUID.randomUUID();
Map<String,Object> map = new HashMap<>();
ResponseStatus responseStatus = new ResponseStatus(ResponseStatusCode.NO_CONTENT, "", map);
ResponseResult responseResult = new ResponseResult(1, map);
return new ResponseMessage(uuid, responseStatus, responseResult);
return deserializeResponse(convertToByteBuf(msg));
}

@Override
public ResponseMessage deserializeResponse(final ByteBuf msg) throws SerializationException {
UUID uuid = UUID.randomUUID();
Map<String,Object> map = new HashMap<>();
ResponseStatus responseStatus = new ResponseStatus(ResponseStatusCode.NO_CONTENT, "", map);
ResponseResult responseResult = new ResponseResult(1, map);
return new ResponseMessage(uuid, responseStatus, responseResult);
return responseSerializer.readValue(msg, reader);
}

private byte[] convertToBytes(final ByteBuf bb) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* 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.
*/
package org.apache.tinkerpop.gremlin.util.ser.binary;

import io.netty.buffer.ByteBuf;
import org.apache.tinkerpop.gremlin.structure.io.Buffer;
import org.apache.tinkerpop.gremlin.structure.io.binary.GraphBinaryReader;
import org.apache.tinkerpop.gremlin.structure.io.binary.GraphBinaryWriter;
import org.apache.tinkerpop.gremlin.util.message.ResponseMessage;
import org.apache.tinkerpop.gremlin.util.message.ResponseResult;
import org.apache.tinkerpop.gremlin.util.message.ResponseStatus;
import org.apache.tinkerpop.gremlin.util.message.ResponseStatusCode;
import org.apache.tinkerpop.gremlin.util.ser.NettyBufferFactory;
import org.apache.tinkerpop.gremlin.util.ser.SerializationException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.UUID;

public class ResponseMessageSerializer2 {
private static final NettyBufferFactory bufferFactory = new NettyBufferFactory();

public ResponseMessage readValue(final ByteBuf byteBuf, final GraphBinaryReader context) throws SerializationException {
// Wrap netty's buffer
final Buffer buffer = bufferFactory.create(byteBuf);
final int version = buffer.readByte() & 0xff;

if (version >>> 7 != 1) {
// This is an indication that the response buffer was incorrectly built
// Or the buffer offsets are wrong
throw new SerializationException("The most significant bit should be set according to the format");
}
LinkedHashMap<String,Object> dummyMap = new LinkedHashMap<>();
// dummyMap.put("host", "/localhost:8182");
LinkedHashMap<String,Object> dummyMap2 = new LinkedHashMap<>();
ArrayList<Long> dummyList = new ArrayList<>();
// dummyList.add(808L);
try {
return ResponseMessage.build(context.readValue(buffer, UUID.class, true)) // c256d92e-738b-4081-aa45-ccd708311823// read values use register.serializer
.code(ResponseStatusCode.SUCCESS)
.statusMessage("")
.statusAttributes(dummyMap)
.responseMetaData(dummyMap2)
.result(dummyList)
.create();
} catch (IOException ex) {
throw new SerializationException(ex);
}
}

public void writeValue(final ResponseMessage value, final ByteBuf byteBuf, final GraphBinaryWriter context) throws SerializationException {
// Wrap netty's buffer
final Buffer buffer = bufferFactory.create(byteBuf);

final ResponseResult result = value.getResult();
final ResponseStatus status = value.getStatus();

try {
// Version
buffer.writeByte(GraphBinaryWriter.VERSION_BYTE);
// Nullable request id
context.writeValue(value.getRequestId(), buffer, true);
// Status code
context.writeValue(status.getCode().getValue(), buffer, false);
// Nullable status message
context.writeValue(status.getMessage(), buffer, true);
// Status attributes
context.writeValue(status.getAttributes(), buffer, false);
// Result meta
context.writeValue(result.getMeta(), buffer, false);
// Fully-qualified value
context.write(result.getData(), buffer);
} catch (IOException ex) {
throw new SerializationException(ex);
}
}
}

0 comments on commit d925d3e

Please sign in to comment.