I have read tons of topics discussing the performance of GSON, and many have suggested to use streaming if you are worried about performance. It seems like nobody is having a performance issue to the degree of mine, though.

Here's how I am initializing my GSON object:

gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();

Here is the .toJson() statement I am executing that is the only statement in the benchmark results I am about to show:

String resultString = gson.toJson(results.get(0));

... where results.get(0) evaluates to a List<PrblFldr> object that contains 1499 PrblFldr objects (I will show the non-primitive classes that are serialized later). Here are the results from my makeshift benchmark:

INFO: Starting: GSON CONVERSION
INFO: Finished: GSON CONVERSION  ---  Time: 161705 ms

Finally, here are the classes I am serializing. I am only showing those with the @Expose annotation since those are the only fields that should be serialized.

PrblFldr:

public class PrblFldr implements Serializable {

    @Expose
    @SerializedName("name")
    @Column(name="FLDR_NM")
    private String fldrNm;

    @Expose
    @SerializedName("template")
    @ManyToOne
    @JoinColumn(name="FLDR_TYP_SEQ_ID", insertable=false, updatable=false)
    private PrblTmplt prblTmplt;

    @Expose
    @SerializedName("attributes")
    @OneToMany(mappedBy="prblFldr")
    private List<PrblFldrAtrbtVal> prblFldrAtrbtVals;

    // other fields omitted...
}

PrblTmplt:

public class PrblTmplt implements Serializable {

    @Expose
    @SerializedName("id")
    @Id
    @Column(name="TMPLT_SEQ_ID")
    private long tmpltSeqId;

    @Expose
    @SerializedName("name")
    @Column(name="TMPLT_NM")
    private String tmpltNm;

    // other fields omitted...
}

PrblFldrAtrbtVal:

public class PrblFldrAtrbtVal implements Serializable {

    @Expose
    @SerializedName("id")
    @Id
    @Column(name="FLDR_ATRBT_VAL_SEQ_ID")
    private long fldrAtrbtValSeqId;

    @Expose
    @SerializedName("val")
    @Column(name="FLDR_ATRBT_VAL")
    private String fldrAtrbtVal;

    @Expose
    @SerializedName("attributeTemplate")
    @ManyToOne
    @JoinColumn(name="FLDR_ATRBT_SEQ_ID")
    private PrblTmpltAtrbt prblTmpltAtrbt;

    // other fields omitted...
}

And finally, PrblTmpltAtrbt:

public class PrblTmpltAtrbt implements Serializable {

    @Expose
    @SerializedName("id")
    @Id
    @Column(name="TMPLT_ATRBT_SEQ_ID")
    private long tmpltAtrbtSeqId;

    @Expose
    @SerializedName("name")
    @Column(name="TMPLT_ATRBT_LBL_NM")
    private String tmpltAtrbtLblNm;

    @Expose
    @SerializedName("required")
    @Column(name="TMPLT_ATRBT_RQRD_IND")
    private String tmpltAtrbtRqrdInd;

    // other fields omitted...
}

As you can see, everything breaks down to either a long or a String object. Even though there are 4 layers of object serialization going on here, I still don't think I should be seeing such a drastically long serialization time. Also, as you might have noticed, I am using EclipseLink JPA (hence the @Id and @Column annotations). Could that be a source of the issue?

Question: What is causing this extremely long serialization time?

EDIT: It also might be worth noting that the List<PrblFlderAtrbtVal> prblFldrAtrbtVals field in the PrblFldr class probably has an average size of 5 or 6, with a maximum of 15 or so.

有帮助吗?

解决方案

I have arrived at the conclusion that this issue is caused by the fact that I am running this on a locally run server and not a dedicated remote server. I'm assuming my laptop is throttling the performance, which is skewing the results.

I determined this by incrementally relying less and less on GSON's built-in functionality of serialization. I even went so far as to writing my own serializer from scratch using nothing but StringBuilder. Unfortunately, I only sped the process up by about 5 seconds, which is a 3% performance increase.

I'm sure I'll see actual performance measures when I place this on a remote server.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top