a1342772 commented on issue #1945:
URL: https://github.com/apache/fury/issues/1945#issuecomment-2487219319

   The application scenario is search and recommendation, where proto objects 
need to be constructed for model inference,The proto file is as follows:
   
[grpc_service.proto.txt](https://github.com/user-attachments/files/17823908/grpc_service.proto.txt)
   
   The test code is as follows:
   
   `static` Fury furyTest;
   
       static {
           furyTest = Fury.builder()
                   .withLanguage(Language.JAVA)
                   .requireClassRegistration(false)
                   .withCodegen(false)
                   .withRefTracking(false)
                   .withAsyncCompilation(true)
                   .build();
       }
   
       public static void main(String[] args) {
           Map<String, ArrayList<FeaOutputV3>> itemsFeatDataV3 = new 
HashMap<>();
           int numFeatures = 320;
           int numColumns = 20;
   
           // 模拟特征数据
           for (int i = 0; i < numFeatures; i++) {
               String featureName = "feature_" + i;
               ArrayList<FeaOutputV3> featureDataList = new ArrayList<>();
   
               for (int j = 0; j < numColumns; j++) {
                   IntList featureValues = new IntArrayList();
                   for (int k = 0; k < numColumns; k++) {
                       featureValues.add(i * numColumns + k);
                   }
                   FeaOutputV3 feaOutputV3 = new FeaOutputV3();
                   feaOutputV3.setFixSizeList(featureValues);
                   featureDataList.add(feaOutputV3);
               }
   
               itemsFeatDataV3.put(featureName, featureDataList);
           }
   
           // 构建输入张量
           GrpcService.ModelInferRequest.InferInputTensor.Builder tensorBuilder 
=
                   GrpcService.ModelInferRequest.InferInputTensor.newBuilder();
           GrpcService.InferTensorContents.Builder tensorContentBuilder =
                   GrpcService.InferTensorContents.newBuilder();
   
           List<GrpcService.ModelInferRequest.InferInputTensor> inputs = new 
ArrayList<>();
           itemsFeatDataV3.forEach((name, featList) -> {
               int l1 = featList.size();
               if 
(CollectionUtils.isNotEmpty(featList.get(0).getFixSizeList())) {
                   int l2 = featList.get(0).getFixSizeList().size();
                   IntList features = new IntArrayList(l1 * l2);
   
                   featList.forEach(feaOutput -> 
features.addAll(feaOutput.getFixSizeList()));
                   inputs.add(featureBuildWithFast(tensorBuilder, 
tensorContentBuilder, features, name, l1, l2));
               }
           });
   
           // 测试序列化性能
           for (int count = 0; count < 50; count++) {
               long startSerialization = System.nanoTime();
               GrpcService.ModelInferRequest resp = 
GrpcService.ModelInferRequest
                       .newBuilder()
                       .addAllInputs(inputs)
                       .build();
   
               byte[] data = furyTest.serializeJavaObject(resp);
               furyTest.deserializeJavaObject(data, 
GrpcService.ModelInferRequest.class);
   
               //protobuf
               //byte[] data = resp.toByteArray();
               //GrpcService.ModelInferRequest.parseFrom(data);
               long endSerialization = System.nanoTime();
               long serializationTimeMs = (endSerialization - 
startSerialization) / 1_000_000;
   
               System.out.println("Serialization time (milliseconds): " + 
serializationTimeMs + " size: " + data.length);
           }
       }
   
   
   
       public static GrpcService.ModelInferRequest.InferInputTensor 
featureBuildWithFast(
               GrpcService.ModelInferRequest.InferInputTensor.Builder 
tensorBuilder1,
               GrpcService.InferTensorContents.Builder tensorContentBuilder2,
               IntList features, String featureName, int l1, int l2) {
   
           GrpcService.ModelInferRequest.InferInputTensor.Builder tensorBuilder 
=
                   GrpcService.ModelInferRequest.InferInputTensor.newBuilder();
           GrpcService.InferTensorContents.Builder tensorContentBuilder =
                   GrpcService.InferTensorContents.newBuilder();
           if (features == null) {
               features = new IntArrayList();
           }
           tensorContentBuilder.addAllIntContents(features);
           GrpcService.ModelInferRequest.InferInputTensor input = 
tensorBuilder.setName(featureName)
                   .setDatatype("INT32")
                   .addShape(l1)
                   .addShape(l2)
                   .setContents(tensorContentBuilder)
                   .buildPartial();
           return input;
       }`


-- 
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]

Reply via email to