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

@ -41,6 +41,11 @@ import java.nio.charset.Charset;
*/
public class NioUtil {
/**
* 默认小缓存大小 1024
*/
public static final int DEFAULT_SMALL_BUFFER_SIZE = 2 << 9;
/**
* 默认缓存大小 8192
*/

View File

@ -41,7 +41,7 @@ public class FastByteBuffer extends FastBuffer {
* 构造
*/
public FastByteBuffer() {
this(IoUtil.DEFAULT_BUFFER_SIZE);
this(IoUtil.DEFAULT_SMALL_BUFFER_SIZE);
}
/**

View File

@ -21,7 +21,8 @@ import org.dromara.hutool.core.lang.Assert;
/**
* 代码移植自jetbrick<br>
* 快速字符缓冲将数据存放在缓冲集中取代以往的单一数组
* 快速字符缓冲将数据存放在缓冲集中取代以往的单一数组<br>
* 注意此缓存在大量重复append时性能比{@link StringBuilder}要好但是{@link #toArray()}性能很差
*
* @author jetbrick, looly
*/
@ -40,7 +41,7 @@ public class FastCharBuffer extends FastBuffer implements CharSequence, Appendab
* 构造
*/
public FastCharBuffer() {
this(IoUtil.DEFAULT_BUFFER_SIZE);
this(IoUtil.DEFAULT_SMALL_BUFFER_SIZE);
}
/**

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);
}
}
}