Skip to content

Commit

Permalink
The file module needs to add a few performance-related unit tests. #731
Browse files Browse the repository at this point in the history
… (#732)
  • Loading branch information
goodjava authored Sep 25, 2023
1 parent 16cb8ff commit f5a78cc
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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();
}
Expand Down
32 changes: 23 additions & 9 deletions jcommon/file/pom.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>run.mone</groupId>
<artifactId>jcommon</artifactId>
<version>1.4-jdk20-SNAPSHOT</version>
</parent>
<artifactId>file</artifactId>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>run.mone</groupId>
<artifactId>jcommon</artifactId>
<version>1.4-jdk20-SNAPSHOT</version>
</parent>
<artifactId>file</artifactId>


<dependencies>

<dependency>
<groupId>com.squareup.okio</groupId>
<artifactId>okio</artifactId>
<version>3.5.0</version>
<scope>test</scope>
</dependency>

</dependencies>

</project>
13 changes: 2 additions & 11 deletions jcommon/file/src/test/java/com/xiaomi/mone/file/LogFileTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,8 @@ class MyReadListener implements ReadListener {

@Override
public void onEvent(ReadEvent event) {
List<String> 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
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}

}

0 comments on commit f5a78cc

Please sign in to comment.