-
Notifications
You must be signed in to change notification settings - Fork 325
Improve jackson benchmark in java #303
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| // | ||
| // MessagePack for Java | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| // | ||
| package org.msgpack.jackson.dataformat.benchmark; | ||
|
|
||
| import org.apache.commons.math3.stat.StatUtils; | ||
| import org.apache.commons.math3.stat.descriptive.moment.StandardDeviation; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
|
|
||
| public class Benchmarker | ||
| { | ||
| private final List<Benchmarkable> benchmarkableList = new ArrayList<Benchmarkable>(); | ||
|
|
||
| public abstract static class Benchmarkable | ||
| { | ||
| private final String label; | ||
|
|
||
| protected Benchmarkable(String label) | ||
| { | ||
| this.label = label; | ||
| } | ||
|
|
||
| public abstract void run() throws Exception; | ||
| } | ||
|
|
||
| public void addBenchmark(Benchmarkable benchmark) | ||
| { | ||
| benchmarkableList.add(benchmark); | ||
| } | ||
|
|
||
| private static class Tuple<F, S> | ||
| { | ||
| F first; | ||
| S second; | ||
|
|
||
| public Tuple(F first, S second) | ||
| { | ||
| this.first = first; | ||
| this.second = second; | ||
| } | ||
| } | ||
|
|
||
| public void run(int count, int warmupCount) | ||
| throws Exception | ||
| { | ||
| List<Tuple<String, double[]>> benchmarksResults = new ArrayList<Tuple<String, double[]>>(benchmarkableList.size()); | ||
| for (Benchmarkable benchmark : benchmarkableList) { | ||
| benchmarksResults.add(new Tuple<String, double[]>(benchmark.label, new double[count])); | ||
| } | ||
|
|
||
| for (int i = 0; i < count + warmupCount; i++) { | ||
| for (int bi = 0; bi < benchmarkableList.size(); bi++) { | ||
| Benchmarkable benchmark = benchmarkableList.get(bi); | ||
| long currentTimeNanos = System.nanoTime(); | ||
| benchmark.run(); | ||
|
|
||
| if (i >= warmupCount) { | ||
| benchmarksResults.get(bi).second[i - warmupCount] = (System.nanoTime() - currentTimeNanos) / 1000000.0; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| for (Tuple<String, double[]> benchmarkResult : benchmarksResults) { | ||
| printStat(benchmarkResult.first, benchmarkResult.second); | ||
| } | ||
| } | ||
|
|
||
| private void printStat(String label, double[] origValues) | ||
| { | ||
| double[] values = origValues; | ||
| Arrays.sort(origValues); | ||
| if (origValues.length > 2) { | ||
| values = Arrays.copyOfRange(origValues, 1, origValues.length - 1); | ||
| } | ||
| StandardDeviation standardDeviation = new StandardDeviation(); | ||
| System.out.println(label + ":"); | ||
| System.out.println(String.format(" mean : %8.3f", StatUtils.mean(values))); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The mean value including outliers (min, max) is not a good measure in evaluating the performance. We also need to add median or mean (average) value without containing outliers (e.g., by removing max and min, or a few percent of the top results).
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. According to the result of profiling, this method accounts for 1% of the total duration. Also, we compare the performance of Jackson and MessagePack relatively with this benchmark even if this method affects a bit.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just meant we should see median or average (which is not biased by too distant values).
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I misread your comment. I'll take care of it |
||
| System.out.println(String.format(" min : %8.3f", StatUtils.min(values))); | ||
| System.out.println(String.format(" max : %8.3f", StatUtils.max(values))); | ||
| System.out.println(String.format(" stdev: %8.3f", standardDeviation.evaluate(values))); | ||
| System.out.println(""); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The order of running programs also affects the performance results in JVM. Simply running System.gc() as an warmup is not sufficient.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The benchmark has 4 times warmup runs for each one and it avoids the affect of running order, I think. BTW, how does your benchmark avoid the order of running programs?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For example, if you have two programs P1, P2, it runs the program P1, P2, P1, P2,... to reduce the order effect.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay. The original benchmark code (https://github.com/msgpack/msgpack-java/blob/v07-develop/msgpack-jackson/src/test/java/org/msgpack/jackson/dataformat/benchmark/MessagePackDataformatPojoBenchmarkTest.java#L98-L130) does the same thing, right?