Code: Select all
@RestController
@RequestMapping("/log")
public class LogController {
private static final Logger logger = org.slf4j.LoggerFactory.getLogger(LogController.class);
private static final int LOOP_COUNT = 100;
@GetMapping("/system-out")
public String logSystemOut() {
long startTime = System.currentTimeMillis();
for (int i = 0; i < LOOP_COUNT; i++) {
System.out.println("hello world");
}
long endTime = System.currentTimeMillis();
return "Logged messages in " + (endTime - startTime) + " ms";
}
@GetMapping("/system-err")
public String logSystemErr() {
long startTime = System.currentTimeMillis();
for (int i = 0; i < LOOP_COUNT; i++) {
System.err.println("hello world");
}
long endTime = System.currentTimeMillis();
return "Logged messages in " + (endTime - startTime) + " ms";
}
@GetMapping("/info")
public String logInfo() {
long startTime = System.currentTimeMillis();
for (int i = 0; i < LOOP_COUNT; i++) {
logger.info("hello world");
}
long endTime = System.currentTimeMillis();
return "Logged messages in " + (endTime - startTime) + " ms";
}
}
< /code>
Mein logback.xml -Konfiguration ist unten: < /p>
logs/happyTest.log
logs/happyTest.%d{yyyy-MM-dd}.log
30
%msg%n
0
8192
Beachten Sie, dass jede verschiedene API einfach den Anforderungspfad zum Testen ändern. src = "https://i.static.net/nexwbrpn.png"/>
Basierend auf meinem Verständnis sollte die Leistung von System.out und System.err ähnlich sein und sein im Allgemeinen niedriger als logger.info Aufgrund von I konfigurieren Sie die ch.qos.logback.classic.asyncAppender in logback.xml.
Ja, es hat wie erwartet funktioniert. logger.info < /code> ist am besten. >
Hier ist System.err < /code> Leistung. signifikant langsamer. .out Leistung. schneller im Vergleich zu system.err . >
Nachdem ich den Quellcode von System.java überprüft habe, bemerkte ich, dass das unterschiedliche System von System.out und System.err nur der Enkriptor für Encodes und Datei ist. Beide sind gepuffertoutputStream , nicht wie einige KI behaupten, dass System.err unbufferd ist.
Code: Select all
// java.lang.System
// ... other code
public final static PrintStream out = null;
public final static PrintStream err = null;
// ... other code
private static void initializeSystemClass() {
// ... other code
FileInputStream fdIn = new FileInputStream(FileDescriptor.in);
FileOutputStream fdOut = new FileOutputStream(FileDescriptor.out);
FileOutputStream fdErr = new FileOutputStream(FileDescriptor.err);
setIn0(new BufferedInputStream(fdIn));
setOut0(newPrintStream(fdOut, props.getProperty("sun.stdout.encoding")));
setErr0(newPrintStream(fdErr, props.getProperty("sun.stderr.encoding")));
// ... other code
}
// ... other code
private static PrintStream newPrintStream(FileOutputStream fos, String enc) {
if (enc != null) {
try {
return new PrintStream(new BufferedOutputStream(fos, 128), true, enc);
} catch (UnsupportedEncodingException uee) {}
}
return new PrintStream(new BufferedOutputStream(fos, 128), true);
}
< /code>
Warum ist die Leistung von System.err < /code> erheblich niedriger als System.out < /code>?
Vielen Dank für Ihre Einblicke ! Ich werde den Lasttest in Ubuntu -Umgebung erneut einstellen. . > ./logs/happytest2.log 2> ./logs/happytest3.log &
Dann führen Sie den Testplan aus.
Code: Select all
System.out< /code> Testergebnis:
System.err< /code> Testergebnis:
Es wird angezeigt Die Leistung in Ubuntu, System.out