summaryrefslogtreecommitdiff
path: root/benchmarks
diff options
context:
space:
mode:
authorTobias Thierer <tobiast@google.com>2018-07-04 17:55:23 +0100
committerTobias Thierer <tobiast@google.com>2018-07-04 18:41:04 +0100
commit615af4beb606edc393becc2f2d9078cdb46d93f0 (patch)
treef2a9f96254f8a81dc220e4dd550f1af9d7b742a6 /benchmarks
parent43dc1d55b7b030213ffebce6ad26bc4c2f573d40 (diff)
Add benchmark for System.getProperty() vs. AccessController/GetPropertyAction.
Comparing the performance of these options is interesting because libcore carries patches to short-circuit such logic in a few places. This CL came out of a verbal discussion of http://r.android.com/710427 Benchmark results on a 5.5" Google Pixel device from 2016: Using the legacy security code costs approx. 500 nanoseconds per invocation (1136 vs. 1061 ns) when not specifying a default value, or, or 320 nanoseconds (1041 vs. 710 ns) when specifying a default value (1061 vs. 740 ns). Both properties that are accessed (user.language and user.region) have nonempty values on the device that was tested. I ran the benchmark a couple of times; here's the output from one run: Trial Report (1 of 4): Experiment {instrument=runtime, benchmarkMethod=timeAccessController_getPropertyAction, vm=default, parameters={}} Results: runtime(ns): min=1136.75, 1st qu.=1136.75, median=1136.75, mean=1136.75, 3rd qu.=1136.75, max=1136.75 Trial Report (2 of 4): Experiment {instrument=runtime, benchmarkMethod=timeAccessController_getPropertyAction_default, vm=default, parameters={}} Results: runtime(ns): min=1061.38, 1st qu.=1061.38, median=1061.38, mean=1061.38, 3rd qu.=1061.38, max=1061.38 Trial Report (3 of 4): Experiment {instrument=runtime, benchmarkMethod=timeSystem_getProperty, vm=default, parameters={}} Results: runtime(ns): min=634.09, 1st qu.=634.09, median=634.09, mean=634.09, 3rd qu.=634.09, max=634.09 Trial Report (4 of 4): Experiment {instrument=runtime, benchmarkMethod=timeSystem_getProperty_default, vm=default, parameters={}} Results: runtime(ns): min=740.08, 1st qu.=740.08, median=740.08, mean=740.08, 3rd qu.=740.08, max=740.08 Test: vogar --benchmark --verbose benchmarks/GetSystemPropertyBenchmark.java Change-Id: I77461a5007351b144b2dc09ac7c194240332636d
Diffstat (limited to 'benchmarks')
-rw-r--r--benchmarks/src/benchmarks/GetSystemPropertyBenchmark.java60
1 files changed, 60 insertions, 0 deletions
diff --git a/benchmarks/src/benchmarks/GetSystemPropertyBenchmark.java b/benchmarks/src/benchmarks/GetSystemPropertyBenchmark.java
new file mode 100644
index 0000000000..3a93285db3
--- /dev/null
+++ b/benchmarks/src/benchmarks/GetSystemPropertyBenchmark.java
@@ -0,0 +1,60 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * 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 benchmarks;
+
+import java.security.AccessController;
+
+import sun.security.action.GetPropertyAction;
+
+/**
+ * Compares performance of accessing system properties via
+ * legacy security code
+ * {@code AccessController.doPrivileged(new GetPropertyAction(key[, default]))}
+ * vs. direct invocation of {@code System.getProperty(key[, default])}.
+ *
+ * As of 2018-07, libcore carries some patches to perform such short-circuiting,
+ * so it's interesting to know how much better it performs.
+ */
+public class GetSystemPropertyBenchmark {
+
+ public void timeSystem_getProperty_default(int reps) {
+ for (int i = 0; i < reps; i++) {
+ System.getProperty("user.language", "en");
+ }
+ }
+
+ public void timeSystem_getProperty(int reps) {
+ for (int i = 0; i < reps; i++) {
+ System.getProperty("user.region");
+ }
+ }
+
+ public void timeAccessController_getPropertyAction(int reps) {
+ for (int i = 0; i < reps; i++) {
+ AccessController.doPrivileged(
+ new GetPropertyAction("user.language", "en"));
+ }
+ }
+
+ public void timeAccessController_getPropertyAction_default(int reps) {
+ for (int i = 0; i < reps; i++) {
+ AccessController.doPrivileged(
+ new GetPropertyAction("user.region"));
+ }
+ }
+
+}