diff options
14 files changed, 1415 insertions, 0 deletions
diff --git a/apct-tests/perftests/core/AndroidManifest.xml b/apct-tests/perftests/core/AndroidManifest.xml index e0c11cf0e3d5..56fa70cfc220 100644 --- a/apct-tests/perftests/core/AndroidManifest.xml +++ b/apct-tests/perftests/core/AndroidManifest.xml @@ -10,6 +10,7 @@ <uses-permission android:name="android.permission.GET_ACCOUNTS" /> <uses-permission android:name="android.permission.INSTALL_PACKAGES"/> <uses-permission android:name="android.permission.INTERACT_ACROSS_USERS" /> + <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.VIBRATE" /> <application> diff --git a/apct-tests/perftests/core/src/android/libcore/regression/DateFormatPerfTest.java b/apct-tests/perftests/core/src/android/libcore/regression/DateFormatPerfTest.java new file mode 100644 index 000000000000..4dba1391438c --- /dev/null +++ b/apct-tests/perftests/core/src/android/libcore/regression/DateFormatPerfTest.java @@ -0,0 +1,69 @@ +/* + * Copyright (C) 2022 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 android.libcore.regression; + +import android.perftests.utils.BenchmarkState; +import android.perftests.utils.PerfStatusReporter; +import android.test.suitebuilder.annotation.LargeTest; + +import androidx.test.runner.AndroidJUnit4; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; + +import java.text.DateFormat; +import java.util.Locale; + +@RunWith(AndroidJUnit4.class) +@LargeTest +public final class DateFormatPerfTest { + @Rule public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter(); + + private Locale mLocale1; + private Locale mLocale2; + private Locale mLocale3; + private Locale mLocale4; + + @Before + public void setUp() throws Exception { + mLocale1 = Locale.TAIWAN; + mLocale2 = Locale.GERMANY; + mLocale3 = Locale.FRANCE; + mLocale4 = Locale.ITALY; + } + + @Test + public void timeGetDateTimeInstance() throws Exception { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + DateFormat.getDateTimeInstance(); + } + } + + @Test + public void timeGetDateTimeInstance_multiple() throws Exception { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, mLocale1); + DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, mLocale2); + DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, mLocale3); + DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, mLocale4); + } + } +} diff --git a/apct-tests/perftests/core/src/android/libcore/regression/DecimalFormatPerfTest.java b/apct-tests/perftests/core/src/android/libcore/regression/DecimalFormatPerfTest.java new file mode 100644 index 000000000000..f3eddab0e77c --- /dev/null +++ b/apct-tests/perftests/core/src/android/libcore/regression/DecimalFormatPerfTest.java @@ -0,0 +1,221 @@ +/* + * Copyright (C) 2022 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 android.libcore.regression; + +import android.perftests.utils.BenchmarkState; +import android.perftests.utils.PerfStatusReporter; +import android.test.suitebuilder.annotation.LargeTest; + +import androidx.test.runner.AndroidJUnit4; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; + +import java.math.BigDecimal; +import java.text.DecimalFormat; +import java.text.NumberFormat; +import java.util.Locale; + +@RunWith(AndroidJUnit4.class) +@LargeTest +public class DecimalFormatPerfTest { + @Rule public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter(); + + private static final String EXP_PATTERN = "##E0"; + + private static final DecimalFormat DF = (DecimalFormat) DecimalFormat.getInstance(); + // Keep PATTERN_INSTANCE for timing with patterns, to not dirty the plain instance. + private static final DecimalFormat PATTERN_INSTANCE = (DecimalFormat) + DecimalFormat.getInstance(); + private static final DecimalFormat DF_CURRENCY_US = (DecimalFormat) + NumberFormat.getCurrencyInstance(Locale.US); + private static final DecimalFormat DF_CURRENCY_FR = (DecimalFormat) + NumberFormat.getInstance(Locale.FRANCE); + + private static final BigDecimal BD10E3 = new BigDecimal("10E3"); + private static final BigDecimal BD10E9 = new BigDecimal("10E9"); + private static final BigDecimal BD10E100 = new BigDecimal("10E100"); + private static final BigDecimal BD10E1000 = new BigDecimal("10E1000"); + + private static final int WHOLE_NUMBER = 10; + private static final double TWO_DP_NUMBER = 3.14; + + public void formatWithGrouping(Object obj) { + DF.setGroupingSize(3); + DF.setGroupingUsed(true); + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + DF.format(obj); + } + } + + public void format(String pattern, Object obj) { + PATTERN_INSTANCE.applyPattern(pattern); + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + PATTERN_INSTANCE.format(obj); + } + } + + public void format(Object obj) { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + DF.format(obj); + } + } + + public void formatToCharacterIterator(Object obj) { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + DF.formatToCharacterIterator(obj); + } + } + + + public void formatCurrencyUS(Object obj) { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + DF_CURRENCY_US.format(obj); + } + } + + public void formatCurrencyFR(Object obj) { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + DF_CURRENCY_FR.format(obj); + } + } + + @Test + public void time_formatGrouping_BigDecimal10e3() { + formatWithGrouping(BD10E3); + } + + @Test + public void time_formatGrouping_BigDecimal10e9() { + formatWithGrouping(BD10E9); + } + + @Test + public void time_formatGrouping_BigDecimal10e100() { + formatWithGrouping(BD10E100); + } + + @Test + public void time_formatGrouping_BigDecimal10e1000() { + formatWithGrouping(BD10E1000); + } + + @Test + public void time_formatBigDecimal10e3() { + format(BD10E3); + } + + @Test + public void time_formatBigDecimal10e9() { + format(BD10E9); + } + + @Test + public void time_formatBigDecimal10e100() { + format(BD10E100); + } + + @Test + public void time_formatBigDecimal10e1000() { + format(BD10E1000); + } + + @Test + public void time_formatPi() { + format(Math.PI); + } + + @Test + public void time_formatE() { + format(Math.E); + } + + @Test + public void time_formatUSD() { + formatCurrencyUS(WHOLE_NUMBER); + } + + @Test + public void time_formatUsdWithCents() { + formatCurrencyUS(TWO_DP_NUMBER); + } + + @Test + public void time_formatEur() { + formatCurrencyFR(WHOLE_NUMBER); + } + + @Test + public void time_formatEurWithCents() { + formatCurrencyFR(TWO_DP_NUMBER); + } + + @Test + public void time_formatAsExponent10e3() { + format(EXP_PATTERN, BD10E3); + } + + @Test + public void time_formatAsExponent10e9() { + format(EXP_PATTERN, BD10E9); + } + + @Test + public void time_formatAsExponent10e100() { + format(EXP_PATTERN, BD10E100); + } + + @Test + public void time_formatAsExponent10e1000() { + format(EXP_PATTERN, BD10E1000); + } + + @Test + public void time_formatToCharacterIterator10e3() { + formatToCharacterIterator(BD10E3); + } + + @Test + public void time_formatToCharacterIterator10e9() { + formatToCharacterIterator(BD10E9); + } + + @Test + public void time_formatToCharacterIterator10e100() { + formatToCharacterIterator(BD10E100); + } + + @Test + public void time_formatToCharacterIterator10e1000() { + formatToCharacterIterator(BD10E1000); + } + + @Test + public void time_instantiation() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + new DecimalFormat(); + } + } +} diff --git a/apct-tests/perftests/core/src/android/libcore/regression/DecimalFormatSymbolsPerfTest.java b/apct-tests/perftests/core/src/android/libcore/regression/DecimalFormatSymbolsPerfTest.java new file mode 100644 index 000000000000..2bf04180fa79 --- /dev/null +++ b/apct-tests/perftests/core/src/android/libcore/regression/DecimalFormatSymbolsPerfTest.java @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2022 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 android.libcore.regression; + +import android.perftests.utils.BenchmarkState; +import android.perftests.utils.PerfStatusReporter; +import android.test.suitebuilder.annotation.LargeTest; + +import androidx.test.runner.AndroidJUnit4; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; + +import java.text.DecimalFormatSymbols; +import java.util.Locale; + +@RunWith(AndroidJUnit4.class) +@LargeTest +public class DecimalFormatSymbolsPerfTest { + @Rule public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter(); + + private static Locale sLocale = Locale.getDefault(Locale.Category.FORMAT); + + @Test + public void time_instantiation() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + new DecimalFormatSymbols(sLocale); + } + } +} diff --git a/apct-tests/perftests/core/src/android/libcore/regression/DefaultCharsetPerfTest.java b/apct-tests/perftests/core/src/android/libcore/regression/DefaultCharsetPerfTest.java new file mode 100644 index 000000000000..c3320a4e5a20 --- /dev/null +++ b/apct-tests/perftests/core/src/android/libcore/regression/DefaultCharsetPerfTest.java @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2022 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 android.libcore.regression; + +import android.perftests.utils.BenchmarkState; +import android.perftests.utils.PerfStatusReporter; +import android.test.suitebuilder.annotation.LargeTest; + +import androidx.test.runner.AndroidJUnit4; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; + +import java.nio.charset.Charset; + +@RunWith(AndroidJUnit4.class) +@LargeTest +public class DefaultCharsetPerfTest { + @Rule public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter(); + + @Test + public void time_defaultCharset() throws Exception { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + Charset.defaultCharset(); + } + } +} diff --git a/apct-tests/perftests/core/src/android/libcore/regression/DnsPerfTest.java b/apct-tests/perftests/core/src/android/libcore/regression/DnsPerfTest.java new file mode 100644 index 000000000000..7c52ac424a2a --- /dev/null +++ b/apct-tests/perftests/core/src/android/libcore/regression/DnsPerfTest.java @@ -0,0 +1,65 @@ +/* + * Copyright (C) 2022 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 android.libcore.regression; + +import android.perftests.utils.BenchmarkState; +import android.perftests.utils.PerfStatusReporter; +import android.test.suitebuilder.annotation.LargeTest; + +import androidx.test.runner.AndroidJUnit4; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; + +import java.net.InetAddress; +import java.net.UnknownHostException; + +@RunWith(AndroidJUnit4.class) +@LargeTest +public class DnsPerfTest { + @Rule public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter(); + + @Test + public void timeDns() throws Exception { + String[] hosts = new String[] { + "www.amazon.com", + "z-ecx.images-amazon.com", + "g-ecx.images-amazon.com", + "ecx.images-amazon.com", + "ad.doubleclick.com", + "bpx.a9.com", + "d3dtik4dz1nej0.cloudfront.net", + "uac.advertising.com", + "servedby.advertising.com", + "view.atdmt.com", + "rmd.atdmt.com", + "spe.atdmt.com", + "www.google.com", + "www.cnn.com", + "bad.host.mtv.corp.google.com", + }; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + int i = 0; + while (state.keepRunning()) { + try { + InetAddress.getByName(hosts[++i % hosts.length]); + } catch (UnknownHostException ex) { + } + } + } +} diff --git a/apct-tests/perftests/core/src/android/libcore/regression/DoPrivilegedPerfTest.java b/apct-tests/perftests/core/src/android/libcore/regression/DoPrivilegedPerfTest.java new file mode 100644 index 000000000000..d13335906ec6 --- /dev/null +++ b/apct-tests/perftests/core/src/android/libcore/regression/DoPrivilegedPerfTest.java @@ -0,0 +1,95 @@ +/* + * Copyright (C) 2022 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 android.libcore.regression; + +import android.perftests.utils.BenchmarkState; +import android.perftests.utils.PerfStatusReporter; +import android.test.suitebuilder.annotation.LargeTest; + +import androidx.test.runner.AndroidJUnit4; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; + + +import java.security.AccessController; +import java.security.PrivilegedAction; + +@RunWith(AndroidJUnit4.class) +@LargeTest +public class DoPrivilegedPerfTest { + @Rule public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter(); + + @Test + public void timeDirect() throws Exception { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + String lineSeparator = System.getProperty("line.separator"); + } + } + + @Test + public void timeFastAndSlow() throws Exception { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + String lineSeparator; + if (System.getSecurityManager() == null) { + lineSeparator = System.getProperty("line.separator"); + } else { + lineSeparator = AccessController.doPrivileged(new PrivilegedAction<String>() { + public String run() { + return System.getProperty("line.separator"); + } + }); + } + } + } + + @Test + public void timeNewAction() throws Exception { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + String lineSeparator = AccessController.doPrivileged(new PrivilegedAction<String>() { + public String run() { + return System.getProperty("line.separator"); + } + }); + } + } + + @Test + public void timeReusedAction() throws Exception { + final PrivilegedAction<String> action = new ReusableAction("line.separator"); + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + String lineSeparator = AccessController.doPrivileged(action); + } + } + + private static final class ReusableAction implements PrivilegedAction<String> { + private final String mPropertyName; + + ReusableAction(String propertyName) { + this.mPropertyName = propertyName; + } + + public String run() { + return System.getProperty(mPropertyName); + } + } +} diff --git a/apct-tests/perftests/core/src/android/libcore/regression/DoublePerfTest.java b/apct-tests/perftests/core/src/android/libcore/regression/DoublePerfTest.java new file mode 100644 index 000000000000..38904af7edd7 --- /dev/null +++ b/apct-tests/perftests/core/src/android/libcore/regression/DoublePerfTest.java @@ -0,0 +1,73 @@ +/* + * Copyright (C) 2022 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 android.libcore.regression; + +import android.perftests.utils.BenchmarkState; +import android.perftests.utils.PerfStatusReporter; +import android.test.suitebuilder.annotation.LargeTest; + +import androidx.test.runner.AndroidJUnit4; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; + +@RunWith(AndroidJUnit4.class) +@LargeTest +public class DoublePerfTest { + @Rule public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter(); + + private double mD = 1.2; + private long mL = 4608083138725491507L; + + @Test + public void timeDoubleToLongBits() { + long result = 123; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result = Double.doubleToLongBits(mD); + } + if (result != mL) { + throw new RuntimeException(Long.toString(result)); + } + } + + @Test + public void timeDoubleToRawLongBits() { + long result = 123; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result = Double.doubleToRawLongBits(mD); + } + if (result != mL) { + throw new RuntimeException(Long.toString(result)); + } + } + + @Test + public void timeLongBitsToDouble() { + double result = 123.0; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result = Double.longBitsToDouble(mL); + } + if (result != mD) { + throw new RuntimeException(Double.toString(result) + " " + + Double.doubleToRawLongBits(result)); + } + } +} diff --git a/apct-tests/perftests/core/src/android/libcore/regression/EqualsHashCodePerfTest.java b/apct-tests/perftests/core/src/android/libcore/regression/EqualsHashCodePerfTest.java new file mode 100644 index 000000000000..4ff3ba5b0aaa --- /dev/null +++ b/apct-tests/perftests/core/src/android/libcore/regression/EqualsHashCodePerfTest.java @@ -0,0 +1,114 @@ +/* + * Copyright (C) 2022 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 android.libcore.regression; + +import android.perftests.utils.BenchmarkState; +import android.perftests.utils.PerfStatusReporter; +import android.test.suitebuilder.annotation.LargeTest; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +import java.net.URI; +import java.net.URL; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +@RunWith(Parameterized.class) +@LargeTest +public final class EqualsHashCodePerfTest { + @Rule public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter(); + + private enum Type { + URI() { + @Override Object newInstance(String text) throws Exception { + return new URI(text); + } + }, + URL() { + @Override Object newInstance(String text) throws Exception { + return new URL(text); + } + }; + abstract Object newInstance(String text) throws Exception; + } + + private static final String QUERY = "%E0%AE%A8%E0%AE%BE%E0%AE%AE%E0%AF%8D+%E0%AE%AE%E0%AF%81%E0%AE%95%E0%AF%8D%E0%AE%95%E0%AE%BF%E0%AE%AF%E0%AE%AE%E0%AE%BE%E0%AE%A9%2C+%E0%AE%9A%E0%AF%81%E0%AE%B5%E0%AE%BE%E0%AE%B0%E0%AE%B8%E0%AF%8D%E0%AE%AF%E0%AE%AE%E0%AE%BE%E0%AE%A9+%E0%AE%87%E0%AE%B0%E0%AF%81%E0%AE%AA%E0%AF%8D%E0%AE%AA%E0%AF%87%E0%AE%BE%E0%AE%AE%E0%AF%8D%2C+%E0%AE%86%E0%AE%A9%E0%AE%BE%E0%AE%B2%E0%AF%8D+%E0%AE%9A%E0%AE%BF%E0%AE%B2+%E0%AE%A8%E0%AF%87%E0%AE%B0%E0%AE%99%E0%AF%8D%E0%AE%95%E0%AE%B3%E0%AE%BF%E0%AE%B2%E0%AF%8D+%E0%AE%9A%E0%AF%82%E0%AE%B4%E0%AF%8D%E0%AE%A8%E0%AE%BF%E0%AE%B2%E0%AF%88+%E0%AE%8F%E0%AE%B1%E0%AF%8D%E0%AE%AA%E0%AE%9F%E0%AF%81%E0%AE%AE%E0%AF%8D+%E0%AE%8E%E0%AE%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%A9%E0%AF%8D%E0%AE%AE%E0%AF%88%E0%AE%AF%E0%AE%BE%E0%AE%95+%E0%AE%AE%E0%AE%BE%E0%AE%B1%E0%AF%81%E0%AE%AE%E0%AF%8D"; + + @Parameterized.Parameters(name = "mType({0})") + public static Collection cases() { + final List<Object[]> params = new ArrayList<>(); + for (Type type : Type.values()) { + params.add(new Object[]{type}); + } + return params; + } + + @Parameterized.Parameter(0) + public Type mType; + + Object mA1; + Object mA2; + Object mB1; + Object mB2; + + Object mC1; + Object mC2; + + @Before + public void setUp() throws Exception { + mA1 = mType.newInstance("https://mail.google.com/mail/u/0/?shva=1#inbox"); + mA2 = mType.newInstance("https://mail.google.com/mail/u/0/?shva=1#inbox"); + mB1 = mType.newInstance("http://developer.android.com/reference/java/net/URI.html"); + mB2 = mType.newInstance("http://developer.android.com/reference/java/net/URI.html"); + + mC1 = mType.newInstance("http://developer.android.com/query?q=" + QUERY); + // Replace the very last char. + mC2 = mType.newInstance("http://developer.android.com/query?q=" + QUERY.substring(0, QUERY.length() - 3) + "%AF"); + } + + @Test + public void timeEquals() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + mA1.equals(mB1); + mA1.equals(mA2); + mB1.equals(mB2); + } + } + + @Test + public void timeHashCode() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + mA1.hashCode(); + mB1.hashCode(); + } + } + + @Test + public void timeEqualsWithHeavilyEscapedComponent() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + mC1.equals(mC2); + } + } +} diff --git a/apct-tests/perftests/core/src/android/libcore/regression/ExpensiveObjectsPerfTest.java b/apct-tests/perftests/core/src/android/libcore/regression/ExpensiveObjectsPerfTest.java new file mode 100644 index 000000000000..32117dcd1d9f --- /dev/null +++ b/apct-tests/perftests/core/src/android/libcore/regression/ExpensiveObjectsPerfTest.java @@ -0,0 +1,202 @@ +/* + * Copyright (C) 2022 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 android.libcore.regression; + +import android.perftests.utils.BenchmarkState; +import android.perftests.utils.PerfStatusReporter; +import android.test.suitebuilder.annotation.LargeTest; + +import androidx.test.runner.AndroidJUnit4; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; + +import java.text.Collator; +import java.text.DateFormat; +import java.text.DateFormatSymbols; +import java.text.DecimalFormatSymbols; +import java.text.NumberFormat; +import java.text.SimpleDateFormat; +import java.util.GregorianCalendar; +import java.util.Locale; + +/** + * Benchmarks creation and cloning various expensive objects. + */ +@RunWith(AndroidJUnit4.class) +@LargeTest +public class ExpensiveObjectsPerfTest { + @Rule public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter(); + + @Test + public void timeNewDateFormatTimeInstance() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + DateFormat df = DateFormat.getTimeInstance(DateFormat.SHORT); + df.format(System.currentTimeMillis()); + } + } + + @Test + public void timeClonedDateFormatTimeInstance() { + DateFormat df = DateFormat.getTimeInstance(DateFormat.SHORT); + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + ((DateFormat) df.clone()).format(System.currentTimeMillis()); + } + } + + @Test + public void timeReusedDateFormatTimeInstance() { + DateFormat df = DateFormat.getTimeInstance(DateFormat.SHORT); + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + synchronized (df) { + df.format(System.currentTimeMillis()); + } + } + } + + @Test + public void timeNewCollator() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + Collator.getInstance(Locale.US); + } + } + + @Test + public void timeClonedCollator() { + Collator c = Collator.getInstance(Locale.US); + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + c.clone(); + } + } + + @Test + public void timeNewDateFormatSymbols() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + new DateFormatSymbols(Locale.US); + } + } + + @Test + public void timeClonedDateFormatSymbols() { + DateFormatSymbols dfs = new DateFormatSymbols(Locale.US); + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + dfs.clone(); + } + } + + @Test + public void timeNewDecimalFormatSymbols() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + new DecimalFormatSymbols(Locale.US); + } + } + + @Test + public void timeClonedDecimalFormatSymbols() { + DecimalFormatSymbols dfs = new DecimalFormatSymbols(Locale.US); + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + dfs.clone(); + } + } + + @Test + public void timeNewNumberFormat() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + NumberFormat.getInstance(Locale.US); + } + } + + @Test + public void timeClonedNumberFormat() { + NumberFormat nf = NumberFormat.getInstance(Locale.US); + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + nf.clone(); + } + } + + @Test + public void timeNumberFormatTrivialFormatLong() { + NumberFormat nf = NumberFormat.getInstance(Locale.US); + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + nf.format(1024L); + } + } + + @Test + public void timeLongToString() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + Long.toString(1024L); + } + } + + @Test + public void timeNumberFormatTrivialFormatDouble() { + NumberFormat nf = NumberFormat.getInstance(Locale.US); + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + nf.format(1024.0); + } + } + + @Test + public void timeNewSimpleDateFormat() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + new SimpleDateFormat(); + } + } + + @Test + public void timeClonedSimpleDateFormat() { + SimpleDateFormat sdf = new SimpleDateFormat(); + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + sdf.clone(); + } + } + + @Test + public void timeNewGregorianCalendar() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + new GregorianCalendar(); + } + } + + @Test + public void timeClonedGregorianCalendar() { + GregorianCalendar gc = new GregorianCalendar(); + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + gc.clone(); + } + } +} diff --git a/apct-tests/perftests/core/src/android/libcore/regression/FilePerfTest.java b/apct-tests/perftests/core/src/android/libcore/regression/FilePerfTest.java new file mode 100644 index 000000000000..783136a5b3a2 --- /dev/null +++ b/apct-tests/perftests/core/src/android/libcore/regression/FilePerfTest.java @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2022 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 android.libcore.regression; + +import android.perftests.utils.BenchmarkState; +import android.perftests.utils.PerfStatusReporter; +import android.test.suitebuilder.annotation.LargeTest; + +import androidx.test.runner.AndroidJUnit4; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; + +import java.io.File; + +@RunWith(AndroidJUnit4.class) +@LargeTest +public final class FilePerfTest { + @Rule public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter(); + + @Test + public void timeFileCreationWithEmptyChild() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + new File("/foo", "/"); + } + } + + @Test + public void timeFileCreationWithNormalizationNecessary() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + new File("/foo//bar//baz//bag", "/baz/"); + } + } +} diff --git a/apct-tests/perftests/core/src/android/libcore/regression/FloatPerfTest.java b/apct-tests/perftests/core/src/android/libcore/regression/FloatPerfTest.java new file mode 100644 index 000000000000..a995f5caa183 --- /dev/null +++ b/apct-tests/perftests/core/src/android/libcore/regression/FloatPerfTest.java @@ -0,0 +1,73 @@ +/* + * Copyright (C) 2022 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 android.libcore.regression; + +import android.perftests.utils.BenchmarkState; +import android.perftests.utils.PerfStatusReporter; +import android.test.suitebuilder.annotation.LargeTest; + +import androidx.test.runner.AndroidJUnit4; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; + +@RunWith(AndroidJUnit4.class) +@LargeTest +public class FloatPerfTest { + @Rule public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter(); + + private float mFloat = 1.2f; + private int mInt = 1067030938; + + @Test + public void timeFloatToIntBits() { + int result = 123; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result = Float.floatToIntBits(mFloat); + } + if (result != mInt) { + throw new RuntimeException(Integer.toString(result)); + } + } + + @Test + public void timeFloatToRawIntBits() { + int result = 123; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result = Float.floatToRawIntBits(mFloat); + } + if (result != mInt) { + throw new RuntimeException(Integer.toString(result)); + } + } + + @Test + public void timeIntBitsToFloat() { + float result = 123.0f; + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + result = Float.intBitsToFloat(mInt); + } + if (result != mFloat) { + throw new RuntimeException(Float.toString(result) + " " + + Float.floatToRawIntBits(result)); + } + } +} diff --git a/apct-tests/perftests/core/src/android/libcore/regression/FormatterPerfTest.java b/apct-tests/perftests/core/src/android/libcore/regression/FormatterPerfTest.java new file mode 100644 index 000000000000..94c4f0807cbc --- /dev/null +++ b/apct-tests/perftests/core/src/android/libcore/regression/FormatterPerfTest.java @@ -0,0 +1,171 @@ +/* + * Copyright (C) 2022 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 android.libcore.regression; + +import android.perftests.utils.BenchmarkState; +import android.perftests.utils.PerfStatusReporter; +import android.test.suitebuilder.annotation.LargeTest; + +import androidx.test.runner.AndroidJUnit4; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; + +import java.util.Formatter; +import java.util.Locale; + +/** + * Compares Formatter against hand-written StringBuilder code. + */ +@RunWith(AndroidJUnit4.class) +@LargeTest +public class FormatterPerfTest { + @Rule public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter(); + + @Test + public void timeFormatter_NoFormatting() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + Formatter f = new Formatter(); + f.format("this is a reasonably short string that doesn't actually need any formatting"); + } + } + + @Test + public void timeStringBuilder_NoFormatting() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + StringBuilder sb = new StringBuilder(); + sb.append("this is a reasonably short string that doesn't actually need formatting"); + } + } + + @Test + public void timeFormatter_OneInt() { + Integer value = Integer.valueOf(1024); // We're not trying to benchmark boxing here. + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + Formatter f = new Formatter(); + f.format("this is a reasonably short string that has an int %d in it", value); + } + } + + @Test + public void timeFormatter_OneIntArabic() { + Locale arabic = new Locale("ar"); + Integer value = Integer.valueOf(1024); // We're not trying to benchmark boxing here. + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + Formatter f = new Formatter(); + f.format(arabic, "this is a reasonably short string that has an int %d in it", value); + } + } + + @Test + public void timeStringBuilder_OneInt() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + StringBuilder sb = new StringBuilder(); + sb.append("this is a reasonably short string that has an int "); + sb.append(1024); + sb.append(" in it"); + } + } + + @Test + public void timeFormatter_OneHexInt() { + Integer value = Integer.valueOf(1024); // We're not trying to benchmark boxing here. + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + Formatter f = new Formatter(); + f.format("this is a reasonably short string that has an int %x in it", value); + } + } + + @Test + public void timeStringBuilder_OneHexInt() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + StringBuilder sb = new StringBuilder(); + sb.append("this is a reasonably short string that has an int "); + sb.append(Integer.toHexString(1024)); + sb.append(" in it"); + } + } + + @Test + public void timeFormatter_OneFloat() { + Float value = Float.valueOf(10.24f); // We're not trying to benchmark boxing here. + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + Formatter f = new Formatter(); + f.format("this is a reasonably short string that has a float %f in it", value); + } + } + + @Test + public void timeFormatter_OneFloat_dot2f() { + Float value = Float.valueOf(10.24f); // We're not trying to benchmark boxing here. + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + Formatter f = new Formatter(); + f.format("this is a reasonably short string that has a float %.2f in it", value); + } + } + + @Test + public void timeFormatter_TwoFloats() { + Float value = Float.valueOf(10.24f); // We're not trying to benchmark boxing here. + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + Formatter f = new Formatter(); + f.format("this is a short string that has two floats %f and %f in it", value, value); + } + } + + @Test + public void timeStringBuilder_OneFloat() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + StringBuilder sb = new StringBuilder(); + sb.append("this is a reasonably short string that has a float "); + sb.append(10.24f); + sb.append(" in it"); + } + } + + @Test + public void timeFormatter_OneString() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + Formatter f = new Formatter(); + f.format("this is a reasonably short string that has a string %s in it", "hello"); + } + } + + @Test + public void timeStringBuilder_OneString() { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + StringBuilder sb = new StringBuilder(); + sb.append("this is a reasonably short string that has a string "); + sb.append("hello"); + sb.append(" in it"); + } + } +} diff --git a/apct-tests/perftests/core/src/android/libcore/regression/HostnameVerifierPerfTest.java b/apct-tests/perftests/core/src/android/libcore/regression/HostnameVerifierPerfTest.java new file mode 100644 index 000000000000..f2b7fdfdc1ca --- /dev/null +++ b/apct-tests/perftests/core/src/android/libcore/regression/HostnameVerifierPerfTest.java @@ -0,0 +1,192 @@ +/* + * Copyright (C) 2022 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 android.libcore.regression; + +import android.perftests.utils.BenchmarkState; +import android.perftests.utils.PerfStatusReporter; +import android.test.suitebuilder.annotation.LargeTest; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; + +import java.io.ByteArrayInputStream; +import java.net.URL; +import java.security.Principal; +import java.security.cert.Certificate; +import java.security.cert.CertificateFactory; +import java.util.Arrays; +import java.util.Collection; + +import javax.net.ssl.HostnameVerifier; +import javax.net.ssl.HttpsURLConnection; +import javax.net.ssl.SSLSession; +import javax.net.ssl.SSLSessionContext; + +/** + * This benchmark makes a real HTTP connection to a handful of hosts and + * captures the served certificates as a byte array. It then verifies each + * certificate in the benchmark loop, being careful to convert from the + * byte[] to the certificate each time. Otherwise the certificate class + * caches previous results which skews the results of the benchmark: In practice + * each certificate instance is verified once and then released. + */ +@RunWith(Parameterized.class) +@LargeTest +public final class HostnameVerifierPerfTest { + @Rule public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter(); + + @Parameters(name = "mHost({0})") + public static Collection<Object[]> data() { + return Arrays.asList( + new Object[][] { + {"m.google.com"}, + {"www.google.com"}, + {"www.amazon.com"}, + {"www.ubs.com"} + }); + } + + @Parameterized.Parameter(0) + public String mHost; + + + private String mHostname; + private HostnameVerifier mHostnameVerifier; + private byte[][] mEncodedCertificates; + + @Before + public void setUp() throws Exception { + URL url = new URL("https", mHost, "/"); + mHostnameVerifier = HttpsURLConnection.getDefaultHostnameVerifier(); + HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); + connection.setHostnameVerifier(new HostnameVerifier() { + public boolean verify(String mHostname, SSLSession sslSession) { + try { + mEncodedCertificates = certificatesToBytes(sslSession.getPeerCertificates()); + } catch (Exception e) { + throw new RuntimeException(e); + } + HostnameVerifierPerfTest.this.mHostname = mHostname; + return true; + } + }); + connection.getInputStream(); + connection.disconnect(); + } + + @Test + public void timeVerify() throws Exception { + BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); + while (state.keepRunning()) { + final Certificate[] certificates = bytesToCertificates(mEncodedCertificates); + FakeSSLSession sslSession = new FakeSSLSession() { + @Override public Certificate[] getPeerCertificates() { + return certificates; + } + }; + mHostnameVerifier.verify(mHostname, sslSession); + } + } + + private byte[][] certificatesToBytes(Certificate[] certificates) throws Exception { + byte[][] result = new byte[certificates.length][]; + for (int i = 0, certificatesLength = certificates.length; i < certificatesLength; i++) { + result[i] = certificates[i].getEncoded(); + } + return result; + } + + private Certificate[] bytesToCertificates(byte[][] encodedCertificates) throws Exception { + CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509"); + Certificate[] result = new Certificate[encodedCertificates.length]; + for (int i = 0; i < encodedCertificates.length; i++) { + result[i] = certificateFactory.generateCertificate( + new ByteArrayInputStream(encodedCertificates[i])); + } + return result; + } + + private static class FakeSSLSession implements SSLSession { + public int getApplicationBufferSize() { + throw new UnsupportedOperationException(); + } + public String getCipherSuite() { + throw new UnsupportedOperationException(); + } + public long getCreationTime() { + throw new UnsupportedOperationException(); + } + public byte[] getId() { + throw new UnsupportedOperationException(); + } + public long getLastAccessedTime() { + throw new UnsupportedOperationException(); + } + public Certificate[] getLocalCertificates() { + throw new UnsupportedOperationException(); + } + public Principal getLocalPrincipal() { + throw new UnsupportedOperationException(); + } + public int getPacketBufferSize() { + throw new UnsupportedOperationException(); + } + public javax.security.cert.X509Certificate[] getPeerCertificateChain() { + throw new UnsupportedOperationException(); + } + public Certificate[] getPeerCertificates() { + throw new UnsupportedOperationException(); + } + public String getPeerHost() { + throw new UnsupportedOperationException(); + } + public int getPeerPort() { + throw new UnsupportedOperationException(); + } + public Principal getPeerPrincipal() { + throw new UnsupportedOperationException(); + } + public String getProtocol() { + throw new UnsupportedOperationException(); + } + public SSLSessionContext getSessionContext() { + throw new UnsupportedOperationException(); + } + public Object getValue(String name) { + throw new UnsupportedOperationException(); + } + public String[] getValueNames() { + throw new UnsupportedOperationException(); + } + public void invalidate() { + throw new UnsupportedOperationException(); + } + public boolean isValid() { + throw new UnsupportedOperationException(); + } + public void putValue(String name, Object value) { + throw new UnsupportedOperationException(); + } + public void removeValue(String name) { + throw new UnsupportedOperationException(); + } + } +} |