From 25b7aa4f3642f1eb764ac4e214e27f03387718fe Mon Sep 17 00:00:00 2001 From: zhangzhiyong Date: Fri, 22 Sep 2023 10:13:13 +0800 Subject: [PATCH] The file module needs to add a few performance-related unit tests. #731 --- .../docean/plugin/log/test/LogWriterTest.java | 15 ++-- jcommon/file/pom.xml | 32 ++++++--- .../com/xiaomi/mone/file/LogFileTest.java | 13 +--- .../mone/file/RandomAccessFileTest.java | 70 +++++++++++++++++++ 4 files changed, 103 insertions(+), 27 deletions(-) create mode 100644 jcommon/file/src/test/java/com/xiaomi/mone/file/RandomAccessFileTest.java diff --git a/jcommon/docean-plugin/docean-plugin-log/src/test/java/com/xiaomi/youpin/docean/plugin/log/test/LogWriterTest.java b/jcommon/docean-plugin/docean-plugin-log/src/test/java/com/xiaomi/youpin/docean/plugin/log/test/LogWriterTest.java index 62b8abd4a..32f271662 100644 --- a/jcommon/docean-plugin/docean-plugin-log/src/test/java/com/xiaomi/youpin/docean/plugin/log/test/LogWriterTest.java +++ b/jcommon/docean-plugin/docean-plugin-log/src/test/java/com/xiaomi/youpin/docean/plugin/log/test/LogWriterTest.java @@ -16,6 +16,7 @@ package com.xiaomi.youpin.docean.plugin.log.test; +import com.google.common.base.Strings; import com.xiaomi.youpin.docean.plugin.log.LogWriter; import org.junit.Test; @@ -40,14 +41,14 @@ public class LogWriterTest { public void testWrite() { LogWriter logWriter = new LogWriter("/tmp/data"); logWriter.init(1024 * 1024 * 10); - IntStream.range(0, 1000).forEach(it -> { + IntStream.range(0, 1000000).forEach(it -> { System.out.println("run:" + new Date()); - logWriter.write(LocalDateTime.now(), "record:" + (new Date().toString()) + System.lineSeparator()); - try { - TimeUnit.SECONDS.sleep(1); - } catch (InterruptedException e) { - e.printStackTrace(); - } + logWriter.write(LocalDateTime.now(), "record:" + (new Date().toString()) + System.lineSeparator()+ Strings.repeat("abc",10)); +// try { +// TimeUnit.SECONDS.sleep(1); +// } catch (InterruptedException e) { +// e.printStackTrace(); +// } }); logWriter.force(); } diff --git a/jcommon/file/pom.xml b/jcommon/file/pom.xml index e35dca653..756373cdc 100644 --- a/jcommon/file/pom.xml +++ b/jcommon/file/pom.xml @@ -1,11 +1,25 @@ - - 4.0.0 - - run.mone - jcommon - 1.4-jdk20-SNAPSHOT - - file + + 4.0.0 + + run.mone + jcommon + 1.4-jdk20-SNAPSHOT + + file + + + + + + com.squareup.okio + okio + 3.5.0 + test + + + + diff --git a/jcommon/file/src/test/java/com/xiaomi/mone/file/LogFileTest.java b/jcommon/file/src/test/java/com/xiaomi/mone/file/LogFileTest.java index 8191fa27f..a424ee1cd 100644 --- a/jcommon/file/src/test/java/com/xiaomi/mone/file/LogFileTest.java +++ b/jcommon/file/src/test/java/com/xiaomi/mone/file/LogFileTest.java @@ -77,15 +77,8 @@ class MyReadListener implements ReadListener { @Override public void onEvent(ReadEvent event) { - List m = mLog.append(event.getReadResult().getLines().get(0)); - if (m.size() > 0) { - System.out.println("--->" + m); - } -// try { -// TimeUnit.MILLISECONDS.sleep(2); -// } catch (InterruptedException e) { -// e.printStackTrace(); -// } + String m = event.getReadResult().getLines().get(0); + System.out.println(m); } @Override @@ -99,13 +92,11 @@ public boolean isContinue(String line) { public void testLog2() throws IOException { LogFile log = new LogFile("/tmp/zzytest/zzytest/server.log", new MyReadListener()); log.readLine(); - System.in.read(); } @Test public void testReadFileCutting() throws IOException { - System.out.println("111111"); LogFile log = new LogFile("/home/work/log/hera-operator/server.log", new MyReadListener()); log.readLine(); System.in.read(); diff --git a/jcommon/file/src/test/java/com/xiaomi/mone/file/RandomAccessFileTest.java b/jcommon/file/src/test/java/com/xiaomi/mone/file/RandomAccessFileTest.java new file mode 100644 index 000000000..ee519d85d --- /dev/null +++ b/jcommon/file/src/test/java/com/xiaomi/mone/file/RandomAccessFileTest.java @@ -0,0 +1,70 @@ +package com.xiaomi.mone.file; + +import com.google.common.base.Stopwatch; +import okio.BufferedSource; +import okio.Okio; +import org.junit.Test; + +import java.io.File; +import java.io.IOException; +import java.io.RandomAccessFile; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; + +/** + * @author goodjava@qq.com + * @date 2023/9/21 15:25 + */ +public class RandomAccessFileTest { + + + @Test + public void test1() throws IOException { + AtomicInteger ai = new AtomicInteger(); + MoneRandomAccessFile mra = new MoneRandomAccessFile("/tmp/data", "r", 1024 * 4); + Stopwatch sw = Stopwatch.createStarted(); + while (true) { + String line = mra.getNextLine(); +// System.out.println(line); + ai.incrementAndGet(); + if (null == line) { + break; + } + } + System.out.println("use time:" + sw.elapsed(TimeUnit.MILLISECONDS)); + System.out.println(ai.get()); + } + + + @Test + public void test2() throws IOException { + RandomAccessFile mra = new RandomAccessFile("/tmp/data", "r"); + Stopwatch sw = Stopwatch.createStarted(); + while (true) { + String line = mra.readLine(); + System.out.println(line); + if (null == line) { + break; + } + } + System.out.println("use time:" + sw.elapsed(TimeUnit.MILLISECONDS)); + } + + @Test + public void test3() throws IOException { + AtomicInteger ai = new AtomicInteger(); + BufferedSource bufferedSource = Okio.buffer(Okio.source(new File("/tmp/data"))); + Stopwatch sw = Stopwatch.createStarted(); + while (true) { + String line = bufferedSource.readUtf8Line(); +// System.out.println(line); + ai.incrementAndGet(); + if (null == line) { + break; + } + } + System.out.println("use time:" + sw.elapsed(TimeUnit.MILLISECONDS)); + System.out.println(ai.get()); + } + +}