chaokunyang commented on code in PR #2242: URL: https://github.com/apache/fury/pull/2242#discussion_r2094152454
########## java/benchmark/src/main/java/org/apache/fury/benchmark/util/MsgpackUtil.java: ########## @@ -0,0 +1,599 @@ +/* + * 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.fury.benchmark.utils; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import org.apache.fury.benchmark.data.Image; +import org.apache.fury.benchmark.data.Media; +import org.apache.fury.benchmark.data.MediaContent; +import org.apache.fury.benchmark.data.Sample; +import org.msgpack.core.MessagePack; +import org.msgpack.core.MessagePacker; +import org.msgpack.core.MessageUnpacker; + +/** Gen by qwen3. And make some modifications. */ +public class MsgpackUtil { + + public static byte[] serialize(MediaContent mediaContent, ByteArrayOutputStream bos) + throws IOException { + MessagePacker messagePacker = MessagePack.newDefaultPacker(bos); + + packMediaContent(messagePacker, mediaContent); + messagePacker.close(); + + return bos.toByteArray(); + } + + public static MediaContent deserialize(ByteArrayInputStream bis) throws IOException { + MessageUnpacker messageUnpacker = MessagePack.newDefaultUnpacker(bis); + + MediaContent mediaContent = unpackMediaContent(messageUnpacker); + messageUnpacker.close(); + + return mediaContent; + } + + private static void packMediaContent(MessagePacker messagePacker, MediaContent mediaContent) + throws IOException { + messagePacker.packMapHeader(2); + + messagePacker.packString("media"); + if (mediaContent.media != null) { + packMedia(messagePacker, mediaContent.media); + } else { + messagePacker.packNil(); + } + + messagePacker.packString("images"); + if (mediaContent.images != null) { + messagePacker.packArrayHeader(mediaContent.images.size()); + for (Image image : mediaContent.images) { + packImage(messagePacker, image); + } + } else { + messagePacker.packNil(); + } + } + + private static MediaContent unpackMediaContent(MessageUnpacker messageUnpacker) + throws IOException { + int mapSize = messageUnpacker.unpackMapHeader(); + MediaContent mediaContent = new MediaContent(); + + for (int i = 0; i < mapSize; i++) { + String key = messageUnpacker.unpackString(); + + switch (key) { + case "media": + if (!messageUnpacker.tryUnpackNil()) { + mediaContent.media = unpackMedia(messageUnpacker); + } else { + mediaContent.media = null; + } + break; + case "images": + if (!messageUnpacker.tryUnpackNil()) { + int arraySize = messageUnpacker.unpackArrayHeader(); + List<Image> images = new ArrayList<>(); + for (int j = 0; j < arraySize; j++) { + images.add(unpackImage(messageUnpacker)); + } + mediaContent.images = images; + } else { + mediaContent.images = null; + } + break; + default: + messageUnpacker.skipValue(); + break; + } + } + + return mediaContent; + } + + private static void packMedia(MessagePacker messagePacker, Media media) throws IOException { Review Comment: Is there an automatic way to serialize it ? This is basically a manually-written serializer. We are benchmarking with the serializer you written instead of msgpack itself. We only use the msgpack protocol -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
