Skip to content
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

Use jsoniter to encode object #54289

Closed

Conversation

ggaaooppeenngg
Copy link
Contributor

@ggaaooppeenngg ggaaooppeenngg commented Oct 20, 2017

standard encoding/json has performance issue, use jsoniter
can help to reduce half of the encoding time.

Signed-off-by: Peng Gao peng.gao.dut@gmail.com

What this PR does / why we need it:
This PR change encoding/json to jsoniter, from the profiling it reduce the half of processing time.

Which issue this PR fixes (optional, in fixes #<issue number>(, fixes #<issue_number>, ...) format, will close that issue when PR gets merged): fixes #

Special notes for your reviewer:

Release note:

NONE

/sig api-machinery

standard encoding/json has performance issue,use jsoniter
can help to reduce half of the encoding time.

Signed-off-by: Peng Gao <peng.gao.dut@gmail.com>
@k8s-ci-robot k8s-ci-robot added release-note-none Denotes a PR that doesn't merit a release note. sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Oct 20, 2017
@k8s-ci-robot
Copy link
Contributor

Hi @ggaaooppeenngg. Thanks for your PR.

I'm waiting for a kubernetes member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. I understand the commands that are listed here.

@k8s-ci-robot k8s-ci-robot added needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Oct 20, 2017
@k8s-github-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: ggaaooppeenngg
We suggest the following additional approver: caesarxuchao

Assign the PR to them by writing /assign @caesarxuchao in a comment when ready.

No associated issue. Update pull-request body to add a reference to an issue, or get approval with /approve no-issue

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these OWNERS Files:

You can indicate your approval by writing /approve in a comment
You can cancel your approval by writing /approve cancel in a comment

@ggaaooppeenngg
Copy link
Contributor Author

/assign @caesarxuchao

@mkumatag
Copy link
Member

I just see only this entry in the whole file using encoding/json. I think we can use jsoniter to remove the complete dependency of encoding/json in this file.

@mkumatag
Copy link
Member

/cc @thockin

@ggaaooppeenngg
Copy link
Contributor Author

I think it's ok to remove encoding/json dependency in this file, json.Number underling type is string.

@mkumatag
Copy link
Member

/ok-to-test

@k8s-ci-robot k8s-ci-robot removed the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Oct 20, 2017
@liggitt
Copy link
Member

liggitt commented Oct 20, 2017

it doesn't appear that jsoniter normalizes key order:

--- FAIL: TestDeepCopyApiObjects (3.32s)
	copy_test.go:74: {"name":"iåǩ�湶&Ŭ;稫nT咜p��ɇ","generateName":"cÓäɉ礈×ĒP稊�ɔ","selfLink":";c","uid":"ųɃ�髀_K耋顬霧(-+","resourceVersion":"9655338397498521902","creationTimestamp":null,"Spec":{"AccessModes":["Ŀv釉�`S矘胄"],"Selector":{"matchLabels":{}},"Resources":{"Limits":{"
		
		A: storage":"28","cpu":"88","memory":"63"},"Requests":{"cpu":"88","memory":"63","storage":"28"}},"VolumeName":"姥HÊ‹éŸ›åª æ±›6é…™T'","StorageClassName":""},"Status":{"Phase":"Lost","AccessModes":["Ɖ#å¨ˆç ¿V觻z呕¥åJÅ¡AIÆŒpš金ś"],"Capacity":null,"Conditions":[]}}
		
		B: cpu":"88","memory":"63","storage":"28"},"Requests":{"memory":"63","storage":"28","cpu":"88"}},"VolumeName":"姥HÊ‹éŸ›åª æ±›6é…™T'","StorageClassName":""},"Status":{"Phase":"Lost","AccessModes":["Ɖ#å¨ˆç ¿V觻z呕¥åJÅ¡AIÆŒpš金ś"],"Capacity":null,"Conditions":[]}}

stable order is required

@liggitt
Copy link
Member

liggitt commented Oct 20, 2017

@thockin did you exercise encoding in jsoniter as thoroughly as decoding?

Signed-off-by: Peng Gao <peng.gao.dut@gmail.com>
Signed-off-by: Peng Gao <peng.gao.dut@gmail.com>
@k8s-ci-robot k8s-ci-robot added size/S Denotes a PR that changes 10-29 lines, ignoring generated files. and removed size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Oct 21, 2017
@@ -216,7 +215,7 @@ func (s *Serializer) Encode(obj runtime.Object, w io.Writer) error {
_, err = w.Write(data)
return err
}
encoder := json.NewEncoder(w)
encoder := jsoniter.ConfigCompatibleWithStandardLibrary.NewEncoder(w)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything looks OK except this. Doesn't this have a negative impact of performance? It did when I measured it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I offered a bench commit, on my machine it's

pkg: k8s.io/kubernetes/staging/src/k8s.io/apimachinery/pkg/runtime/serializer/json
BenchmarkJsonEncoding-4       	 3000000	       569 ns/op
BenchmarkJsoniterEncoding-4   	 5000000	       316 ns/op

And from my cluster testing, the time share decreases from 60%s to 25%. How did you measure it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The benchmark doesn't use data that is similar to actual objects; can you get e.g. a big pod spec or something and run again?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lavalamp I pushed a commit, the result was not better, but just 20% improved.

pkg: k8s.io/kubernetes/staging/src/k8s.io/apimachinery/pkg/runtime/serializer/json
BenchmarkJsonEncoding-4       	  100000	     13080 ns/op
BenchmarkJsoniterEncoding-4   	  200000	     11158 ns/op
PASS

Signed-off-by: Peng Gao <peng.gao.dut@gmail.com>
@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Oct 23, 2017
Copy link
Contributor Author

@ggaaooppeenngg ggaaooppeenngg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made up a large pod for benchmark.

@@ -216,7 +215,7 @@ func (s *Serializer) Encode(obj runtime.Object, w io.Writer) error {
_, err = w.Write(data)
return err
}
encoder := json.NewEncoder(w)
encoder := jsoniter.ConfigCompatibleWithStandardLibrary.NewEncoder(w)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lavalamp I pushed a commit, the result was not better, but just 20% improved.

pkg: k8s.io/kubernetes/staging/src/k8s.io/apimachinery/pkg/runtime/serializer/json
BenchmarkJsonEncoding-4       	  100000	     13080 ns/op
BenchmarkJsoniterEncoding-4   	  200000	     11158 ns/op
PASS

Signed-off-by: Peng Gao <peng.gao.dut@gmail.com>
@ggaaooppeenngg
Copy link
Contributor Author

ggaaooppeenngg commented Oct 25, 2017

/retest

1 similar comment
@ggaaooppeenngg
Copy link
Contributor Author

/retest

@liggitt
Copy link
Member

liggitt commented Oct 26, 2017

consistent failure:

--- FAIL: TestGetJobFromTemplate (0.00s)
	utils_test.go:96: Wrong length for created-by annotation, expected 160 got 159
	utils_test.go:99: Wrong value for created-by annotation, expected {"kind":"SerializedReference","apiVersion":"v1","reference":{"kind":"CronJob","namespace":"snazzycats","name":"mycronjob","uid":"1a2b3c","apiVersion":"batch"}}
		 got {"kind":"SerializedReference","apiVersion":"v1","reference":{"kind":"CronJob","namespace":"snazzycats","name":"mycronjob","uid":"1a2b3c","apiVersion":"batch"}}

Signed-off-by: Peng Gao <peng.gao.dut@gmail.com>
Signed-off-by: Peng Gao <peng.gao.dut@gmail.com>
Signed-off-by: Peng Gao <peng.gao.dut@gmail.com>
@k8s-ci-robot
Copy link
Contributor

@ggaaooppeenngg: The following tests failed, say /retest to rerun them all:

Test name Commit Details Rerun command
pull-kubernetes-unit ec29346 link /test pull-kubernetes-unit
pull-kubernetes-e2e-kops-aws ec29346 link /test pull-kubernetes-e2e-kops-aws
pull-kubernetes-e2e-gce ec29346 link /test pull-kubernetes-e2e-gce
pull-kubernetes-bazel-test ec29346 link /test pull-kubernetes-bazel-test
pull-kubernetes-verify ec29346 link /test pull-kubernetes-verify

Full PR test history. Your PR dashboard. Please help us cut down on flakes by linking to an open issue when you hit one in your PR.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. I understand the commands that are listed here.

@ggaaooppeenngg
Copy link
Contributor Author

In order to make json-iter to be compatible with standard library, it needs some work, wait for the lib stable then open it again.

@ggaaooppeenngg ggaaooppeenngg deleted the api-json-encoding branch October 29, 2017 15:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. release-note-none Denotes a PR that doesn't merit a release note. sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants