Visitar URL original
test path & PrintStream · zacscoding/java_example@bfeda0b · GitHub
Skip to content

Commit bfeda0b

Browse files
committed
test path & PrintStream
1 parent bdf0528 commit bfeda0b

3 files changed

Lines changed: 137 additions & 0 deletions

File tree

‎javademo/pom.xml‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,15 @@
164164
<version>5.3.0.201903130848-r</version>
165165
</dependency>
166166

167+
<!-- https://mvnrepository.com/artifact/com.jayway.awaitility/awaitility -->
168+
<dependency>
169+
<groupId>com.jayway.awaitility</groupId>
170+
<artifactId>awaitility</artifactId>
171+
<version>1.7.0</version>
172+
<scope>test</scope>
173+
</dependency>
174+
175+
167176
<!-- https://mvnrepository.com/artifact/org.mockito/mockito-core -->
168177
<dependency>
169178
<groupId>org.mockito</groupId>
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package io;
2+
3+
import java.io.File;
4+
import java.io.FileOutputStream;
5+
import java.io.PrintStream;
6+
import java.util.UUID;
7+
import lombok.AllArgsConstructor;
8+
import org.junit.Test;
9+
10+
/**
11+
* @GitHub : https://github.com/zacscoding
12+
*/
13+
public class PrintstreamTest {
14+
15+
@Test
16+
public void compare() throws Exception {
17+
//PrintType printType = PrintType.PRINTLN_APPEND;
18+
PrintType printType = PrintType.PRINTLN_BUILDER_INIT_CAPACITY;
19+
// PrintType printType = PrintType.PRINTF;
20+
int repeat = 10000;
21+
int threadCount = 5;
22+
File targetFile = new File("src/test/resources/io.txt");
23+
if (targetFile.exists()) {
24+
targetFile.delete();
25+
}
26+
27+
PrintStream ps = new PrintStream(new FileOutputStream(targetFile));
28+
String[] messages = new String[repeat];
29+
30+
for (int i = 0; i < repeat; i++) {
31+
messages[i] = UUID.randomUUID().toString() + UUID.randomUUID().toString() + UUID.randomUUID().toString();
32+
}
33+
34+
Task[] tasks = new Task[threadCount];
35+
for (int i = 0; i < threadCount; i++) {
36+
tasks[i] = new Task(messages, ps, printType);
37+
}
38+
39+
long start = System.currentTimeMillis();
40+
for (int i = 0; i < threadCount; i++) {
41+
tasks[i].start();
42+
}
43+
44+
for (int i = 0; i < threadCount; i++) {
45+
tasks[i].join();
46+
}
47+
long elapsed = System.currentTimeMillis() - start;
48+
49+
System.out.printf("## print type : %s >> %d [MS]\n", printType, elapsed);
50+
51+
// Output
52+
// ## print type : PRINTLN_APPEND >> 336 [MS]
53+
// ## print type : PRINTLN_BUILDER >> 378 [MS]
54+
// ## print type : PRINTF >> 459 [MS]
55+
}
56+
57+
@AllArgsConstructor
58+
public static class Task extends Thread {
59+
60+
private String[] message;
61+
private PrintStream ps;
62+
private PrintType printType;
63+
64+
@Override
65+
public void run() {
66+
int repeat = message.length - 1;
67+
for (int i = 0; i < repeat; i++) {
68+
switch (printType) {
69+
case PRINTLN_APPEND:
70+
ps.println(message[i] + message[i + 1]);
71+
break;
72+
case PRINTLN_BUILDER_INIT_CAPACITY:
73+
ps.println(
74+
new StringBuilder(message[i].length() + message[i + 1].length() + 50)
75+
.append(message[i])
76+
.append(message[i + 1])
77+
.toString()
78+
);
79+
break;
80+
case PRINTF:
81+
ps.printf("%s%s\n", message[i], message[i + 1]);
82+
83+
}
84+
}
85+
}
86+
}
87+
88+
private enum PrintType {
89+
PRINTLN_APPEND,
90+
PRINTLN_BUILDER_INIT_CAPACITY,
91+
PRINTF;
92+
}
93+
}
94+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package paths;
2+
3+
import static org.junit.Assert.assertTrue;
4+
5+
import java.io.File;
6+
import java.nio.file.Path;
7+
import java.nio.file.Paths;
8+
import org.junit.Test;
9+
10+
/**
11+
* @GitHub : https://github.com/zacscoding
12+
*/
13+
public class WindowPathTest {
14+
15+
16+
@Test
17+
public void testPathsWithSlash() throws Exception {
18+
File configDir = new File("src/test/resources/config");
19+
String absolutePath = configDir.getAbsolutePath();
20+
System.out.println("AbsolutePath :: " + absolutePath);
21+
22+
Path configDirPath = Paths.get(absolutePath);
23+
System.out.println("Path :: " + configDirPath.toString());
24+
25+
assertTrue(configDirPath.toFile().exists());
26+
27+
String configurerPathValue = absolutePath + "/configurer.json";
28+
Path configurerPath = Paths.get(configurerPathValue);
29+
System.out.println("Configurer path :: " + configurerPath);
30+
31+
assertTrue(configurerPath.toFile().exists());
32+
}
33+
34+
}

0 commit comments

Comments
 (0)