-
-
Notifications
You must be signed in to change notification settings - Fork 157
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Marshal Takes too much time #331
Comments
|
I can't reproduce this issue... |
func TestIssue331(t *testing.T) {
type SellerProductImage struct {
ID int `json:"id"`
Display bool `json:"display"`
Image string `json:"image"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Edges struct{} `json:"edges"`
}
type Edges struct {
SellerProductImages []SellerProductImage `json:"seller_product_images"`
}
type Product struct {
ID int `json:"id"`
Name string `json:"name"`
Slug string `json:"slug"`
SellingPrice string `json:"selling_price"`
ProductPrice string `json:"product_price"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Edges Edges `json:"edges"`
OfferPrice int `json:"offer_price,omitempty"`
OfferPriceStart time.Time `json:"offer_price_start,omitempty"`
OfferPriceEnd time.Time `json:"offer_price_end,omitempty"`
}
products := []Product{
{ID: 1, Name: "samsung", Slug: "samsung", SellingPrice: "40000", ProductPrice: "0", Edges: Edges{SellerProductImages: []SellerProductImage{{ID: 1, Display: true, Image: "e8f49d7d15c54bd0b2d05f8c4bc4ab9a.jpeg"}}}},
{ID: 2, Name: "samsung s10", Slug: "samsung-s10", SellingPrice: "40000", ProductPrice: "0", Edges: Edges{SellerProductImages: []SellerProductImage{{ID: 4, Display: true, Image: "ac013a294da14320a8f991ed91db1d5f.jpg"}}}},
{ID: 3, Name: "samsung s10", Slug: "samsung-s10-7943", SellingPrice: "50000", ProductPrice: "0", OfferPrice: 50, OfferPriceStart: time.Date(2022, 2, 1, 6, 0, 0, 0, time.UTC), OfferPriceEnd: time.Date(2022, 2, 7, 6, 0, 0, 0, time.UTC), Edges: Edges{SellerProductImages: []SellerProductImage{{ID: 7, Display: true, Image: "fb5d1fa7e2834d2fbe2f725b509a7bf2.jpg"}}}},
{ID: 4, Name: "nokia 7", Slug: "nokia-7", SellingPrice: "50000", ProductPrice: "0", OfferPrice: 50, OfferPriceStart: time.Date(2022, 2, 1, 6, 0, 0, 0, time.UTC), OfferPriceEnd: time.Date(2022, 2, 7, 6, 0, 0, 0, time.UTC), Edges: Edges{SellerProductImages: []SellerProductImage{{ID: 10, Display: true, Image: "65cd5529119640e09d85acf76f88ebaa.jpg"}}}},
}
begin := time.Now()
b, err := json.Marshal(products)
if err != nil {
t.Fatal(err)
}
_ = b
t.Log(time.Now().Sub(begin)) // output: 495.218µs
}
|
I didn't understand why the following output was from the code. Are any other programs running in parallel ? I would like to know if the same thing happens with only
|
This is another request which is made by frontend. Ignore this. |
i dont know why it takes too much time for me. I use ent for sql. But other package like std package, gojay, json-iterator works fine but this package works fine but takes too time. |
If CPU usage is high, can you get the pprof? |
It is not reproducible and may be an environment dependent issue. I can't investigate as it is, so could you measure it with pprof and attach a capture of the go-json part? |
You should use Fiber's https://github.com/gofiber/fiber/tree/master/middleware/pprof middleware and go tool pprof for more detailed output |
actually i dont know about that package. |
with go-json
with other json
@goccy is this type of pprof you need? |
Maybe it is a memory profile. I think that we need a CPU profile. |
with ffjson:
With go-json
|
Thanks! |
I reproduced this issue. |
My request's logs:
|
yes. first time when the server starts and idle for a long time. later it caches data and returns that. |
My repro repo: I think the reason is that when linkRecursiveCode is applied to pprof.alloc_objects.alloc_space.inuse_objects.inuse_space.005.pb.gz The performance of go-json may be poor for entc-generated types. @goccy how should we solve this problem? |
I also tried with GORM orm. I faced the same problem. I thought about the problem with GORM but later after I changed my orm to EntGo, the issue still existed. but other json packages like ffjson, easyjson, std json all work good. |
I was able to figure out the problem. thank you :) |
Great. @goccy . I hope you resolve that problem soon but could you please what is the issue occur in this problem? |
This cause is mentioned by @orisano . Dedicated processing is performed when encoding a struct type with a recursive structure. At this time, the copy process runs, so if you target a struct type with many fields, it will take a long time to copy at first time only. |
Improve performance on linkRecursiveCode - cache linked recursive codes for compile
I have a product struct when to make it to json using Marshal, It takes too time around 10s-20s to convert. Is there any problem with my code or its a bug?
The text was updated successfully, but these errors were encountered: