I have a critical user facing Go API in which I call an internal API. The API response time is <200ms but the response unmarshalling with goccy JSON is taking 500ms. The response has about 1k objects so I don't think it should take this much time, not sure though.
NOTE: Earlier I was using Go's default encoding/JSON which was taking 1.5s just to unmarshal. Migrating to goccy reduced to 500ms but I think it is still pretty high considering API response time is 200ms. Attaching API call code below.
func (a *AllePimGatewayImpl) GetProductsByIDsBulk(productIDs []int) []models.AlleProductDTO {
if len(productIDs) == 0 {
return []models.AlleProductDTO{}
}
headers := map[string]string{
"Content-Type": "application/json",
"Authorization": fmt.Sprintf("Bearer %s", a.authToken),
}
req := PimBulkIDRequest{IDs: productIDs}
resp, err := a.client.R().
SetHeaders(headers).
SetBody(req).
Post(a.config.PimConfig.BaseUrl + "/pim/api/v1/products/bulk")
if err != nil {
log.WithError(err).Errorf("Error calling PIM API to get products by IDs bulk: %d IDs", len(productIDs))
return []models.AlleProductDTO{}
}
if resp.StatusCode() != http.StatusOK {
log.Errorf("Received non-200 status from PIM API: %s for products bulk request", resp.Status())
return []models.AlleProductDTO{}
}
var response PimProductsBulkResponse
err = json.Unmarshal(resp.Body(), &response)
if err != nil {
log.WithError(err).Errorf("Error unmarshaling products bulk response")
return []models.AlleProductDTO{}
}
return response.Products
}
Tried migrating to goccy/go-json, easyjson, stream decoding but goccy was the fastest.
This is an internal API which I have control over. So, If there is some change that can be done on the other side of the API to reduce this overhead, please suggest that too.