add CharBufferJmh

This commit is contained in:
Looly
2024-09-30 19:05:52 +08:00
parent c7881c74fd
commit 00c01eedac
5 changed files with 52 additions and 7 deletions

View File

@@ -14,16 +14,15 @@
* limitations under the License.
*/
package org.dromara.hutool.core.io;
package org.dromara.hutool.core.io.buffer;
import java.nio.ByteBuffer;
import org.dromara.hutool.core.io.buffer.BufferUtil;
import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.core.util.CharsetUtil;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.nio.ByteBuffer;
/**
* BufferUtil单元测试
*

View File

@@ -0,0 +1,40 @@
package org.dromara.hutool.core.io.buffer;
import org.openjdk.jmh.annotations.*;
import java.util.concurrent.TimeUnit;
@BenchmarkMode(Mode.AverageTime)//每次执行平均花费时间
@Warmup(iterations = 1, time = 1, timeUnit = TimeUnit.SECONDS) //预热1次调用
@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) // 执行5此每次1秒
@Threads(1) //单线程
@Fork(1) //
@OutputTimeUnit(TimeUnit.NANOSECONDS) // 单位:纳秒
@State(Scope.Benchmark) // 共享域
public class CharBufferJmh {
private final int appendCount = 10000;
private String str;
@Setup
public void setup() {
str = "abc123你好";
}
@SuppressWarnings("MismatchedQueryAndUpdateOfStringBuilder")
@Benchmark
public void stringBuilderJmh() {
final StringBuilder stringBuilder = new StringBuilder(1024);
for (int i = 0; i < appendCount; i++) {
stringBuilder.append(str);
}
}
@Benchmark
public void fastCharBufferJmh() {
final FastCharBuffer fastCharBuffer = new FastCharBuffer(1024);
for (int i = 0; i < appendCount; i++) {
fastCharBuffer.append(str);
}
}
}