|
| 1 | +// |
| 2 | +// ======================================================================== |
| 3 | +// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others. |
| 4 | +// |
| 5 | +// This program and the accompanying materials are made available under the |
| 6 | +// terms of the Eclipse Public License v. 2.0 which is available at |
| 7 | +// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 |
| 8 | +// which is available at https://www.apache.org/licenses/LICENSE-2.0. |
| 9 | +// |
| 10 | +// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 |
| 11 | +// ======================================================================== |
| 12 | +// |
| 13 | + |
| 14 | +package org.eclipse.jetty.io.jmh; |
| 15 | + |
| 16 | +import java.nio.ByteBuffer; |
| 17 | +import java.nio.charset.StandardCharsets; |
| 18 | +import java.util.HashMap; |
| 19 | +import java.util.Map; |
| 20 | +import java.util.concurrent.TimeUnit; |
| 21 | + |
| 22 | +import org.openjdk.jmh.annotations.Benchmark; |
| 23 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 24 | +import org.openjdk.jmh.annotations.Fork; |
| 25 | +import org.openjdk.jmh.annotations.Measurement; |
| 26 | +import org.openjdk.jmh.annotations.Mode; |
| 27 | +import org.openjdk.jmh.annotations.Param; |
| 28 | +import org.openjdk.jmh.annotations.Scope; |
| 29 | +import org.openjdk.jmh.annotations.Setup; |
| 30 | +import org.openjdk.jmh.annotations.State; |
| 31 | +import org.openjdk.jmh.annotations.Threads; |
| 32 | +import org.openjdk.jmh.annotations.Warmup; |
| 33 | +import org.openjdk.jmh.profile.GCProfiler; |
| 34 | +import org.openjdk.jmh.runner.Runner; |
| 35 | +import org.openjdk.jmh.runner.RunnerException; |
| 36 | +import org.openjdk.jmh.runner.options.Options; |
| 37 | +import org.openjdk.jmh.runner.options.OptionsBuilder; |
| 38 | + |
| 39 | +@State(Scope.Benchmark) |
| 40 | +@Threads(1) |
| 41 | +@Fork(1) |
| 42 | +@Warmup(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS) |
| 43 | +@Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS) |
| 44 | +public class Utf8Benchmark |
| 45 | +{ |
| 46 | + // Each string is about 450 characters long. |
| 47 | + private static final Map<String, String> STRINGS_MAP = new HashMap<>() |
| 48 | + {{ |
| 49 | + put("ASCII", """ |
| 50 | + Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor |
| 51 | + incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud |
| 52 | + exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute |
| 53 | + irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat |
| 54 | + nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa |
| 55 | + qui officia deserunt mollit anim id est laborum."""); |
| 56 | + put("FR", """ |
| 57 | + J'ai su là-bas que, pour quelques emplettes, |
| 58 | + Éliante est sortie, et Célimène aussi ; |
| 59 | + Mais comme l'on m'a dit que vous étiez ici, |
| 60 | + J'ai monté pour vous dire, et d'un coeur véritable, |
| 61 | + Que j'ai conçu pour vous une estime incroyable, |
| 62 | + Et que, depuis longtemps, cette estime m'a mis |
| 63 | + Dans un ardent désir d'être de vos amis. |
| 64 | + Oui, mon coeur au mérite aime à rendre justice, |
| 65 | + Et je brûle qu'un noeud d'amitié nous unisse : |
| 66 | + Je crois qu'un ami chaud, et de ma qualité"""); |
| 67 | + put("JA", """ |
| 68 | + 参加希望の方は今すぐ登録してください。この会議では、グローバルなインタネット、Unicode、 |
| 69 | + ソフトウェアの国際化およびローカリゼーション、OSおよびアプリケーションでのUnicode |
| 70 | + のインプリメンテーション、フォント、テキスト表示、マルチ言語コンピューティングにおける業界の専門家が集まります。 |
| 71 | + 参加希望の方は今すぐ登録してください。この会議では、グローバルなインタネット、Unicode |
| 72 | + 、ソフトウェアの国際化およびローカリゼーション、OSおよびアプリケーションでのUnicode |
| 73 | + のインプリメンテーション、フォント、テキスト表示、マルチ言語コンピューティングにおける業界の専門家が集まります。 |
| 74 | + 参加希望の方は今すぐ登録してください。この会議では、グローバルなインタネット、Unicode |
| 75 | + 、ソフトウェアの国際化およびローカリゼーション、OSおよびアプリケーションでのUnicode |
| 76 | + のインプリメンテーション、フォント、テキスト表示、マルチ言語コンピューティングにおける業界の専門家が集まります。"""); |
| 77 | + }}; |
| 78 | + |
| 79 | + @Param({"ASCII", "FR", "JA"}) |
| 80 | + String locale; |
| 81 | + |
| 82 | + String utf8Content; |
| 83 | + |
| 84 | + @Setup |
| 85 | + public void setUp() |
| 86 | + { |
| 87 | + utf8Content = STRINGS_MAP.get(locale); |
| 88 | + } |
| 89 | + |
| 90 | + @Benchmark |
| 91 | + @BenchmarkMode({Mode.Throughput}) |
| 92 | + public Object testEncode() |
| 93 | + { |
| 94 | + return StandardCharsets.UTF_8.encode(utf8Content); |
| 95 | + } |
| 96 | + |
| 97 | + @Benchmark |
| 98 | + @BenchmarkMode({Mode.Throughput}) |
| 99 | + public Object testWrapGetBytes() |
| 100 | + { |
| 101 | + return ByteBuffer.wrap(utf8Content.getBytes(StandardCharsets.UTF_8)); |
| 102 | + } |
| 103 | + |
| 104 | + public static void main(String[] args) throws RunnerException |
| 105 | + { |
| 106 | + Options opt = new OptionsBuilder() |
| 107 | + .include(Utf8Benchmark.class.getSimpleName()) |
| 108 | + .addProfiler(GCProfiler.class) |
| 109 | + .build(); |
| 110 | + |
| 111 | + new Runner(opt).run(); |
| 112 | + } |
| 113 | +} |
0 commit comments