Skip to content

Commit

Permalink
Adapted Java Main to use Java System Logger.
Browse files Browse the repository at this point in the history
Changed benchmark to compare between JUL, log4j and logback.
  • Loading branch information
renatoathaydes committed Jan 15, 2022
1 parent 29019dd commit 948cc2e
Show file tree
Hide file tree
Showing 13 changed files with 52 additions and 7 deletions.
13 changes: 7 additions & 6 deletions benchmark.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ set -e
#

COMMANDS=(
"java -cp build/java Main" # Java 1
"java -cp build/java Main2" # Java 2
"sbcl --script src/lisp/main.lisp" # Common Lisp
"./phone_encoder" # Rust
"java -Djava.util.logging.config.file=log-config/jul.properties -cp build/java Main" # Java (JUL)
"java -Djava.util.logging.manager=org.apache.logging.log4j.jul.LogManager -cp build/java:log-config:log4j-libs/* Main" # Java (Log4j2)
"java -Djava.util.logging.config.file=log-config/jul-logback.properties -cp build/java:log-config:logback-libs/* Main" # Java (Logback)
)

echo "Compiling Java sources"
Expand All @@ -26,12 +25,13 @@ cd ../benchmark_runner && cargo build --release && cp target/release/benchmark_r
cd ../../..

echo "Generating inputs"
INPUTS=(phones_1000.txt phones_10_000.txt phones_50_000.txt phones_100_000_with_empty.txt)
INPUTS=(phones_1000.txt phones_10_000.txt phones_50_000.txt phones_100_000_with_empty.txt phones_200_000_with_empty.txt)
rm "${INPUTS[@]}" > /dev/null 2>&1 || true
java -cp "build/util" util.GeneratePhoneNumbers 1000 > phones_1000.txt
java -cp "build/util" util.GeneratePhoneNumbers 10000 > phones_10_000.txt
java -cp "build/util" util.GeneratePhoneNumbers 50000 > phones_50_000.txt
java -cp "build/util" util.GeneratePhoneNumbers 100000 "true" > phones_100_000_with_empty.txt
java -cp "build/util" util.GeneratePhoneNumbers 200000 "true" > phones_200_000_with_empty.txt

CHECK_FILE="proc_out.txt"
DEFAULT_INPUT="input.txt"
Expand All @@ -42,9 +42,10 @@ echo "Checking all programs for correctness"
for CMD in "${COMMANDS[@]}"
do
echo "Checking: $CMD"
$CMD $DICTIONARY $DEFAULT_INPUT > $CHECK_FILE
$CMD $DICTIONARY $DEFAULT_INPUT
diff -q <(sort $CHECK_FILE) <(sort $DEFAULT_OUTPUT)
echo "OK"
rm $CHECK_FILE
done

echo "Benchmarking..."
Expand Down
6 changes: 6 additions & 0 deletions log-config/jul-logback.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
handlers = org.slf4j.bridge.SLF4JBridgeHandler
.level= INFO
java.util.logging.FileHandler.level = INFO
java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter
java.util.logging.FileHandler.pattern = proc_out.txt
java.util.logging.SimpleFormatter.format = %5$s%n
6 changes: 6 additions & 0 deletions log-config/jul.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
handlers= java.util.logging.FileHandler
.level= INFO
java.util.logging.FileHandler.level = INFO
java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter
java.util.logging.FileHandler.pattern = proc_out.txt
java.util.logging.SimpleFormatter.format = %5$s%n
13 changes: 13 additions & 0 deletions log-config/log4j2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<File name="File" bufferedIO="true" fileName="proc_out.txt">
<PatternLayout pattern="%msg%n"/>
</File>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="File"/>
</Root>
</Loggers>
</Configuration>
13 changes: 13 additions & 0 deletions log-config/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<configuration>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>proc_out.txt</file>
<immediateFlush>true</immediateFlush>
<encoder>
<pattern>%msg%n</pattern>
</encoder>
</appender>

<root level="info">
<appender-ref ref="FILE" />
</root>
</configuration>
Binary file added log4j-libs/log4j-api-2.17.1.jar
Binary file not shown.
Binary file added log4j-libs/log4j-core-2.17.1.jar
Binary file not shown.
Binary file added log4j-libs/log4j-jul-2.17.1.jar
Binary file not shown.
Binary file added logback-libs/jul-to-slf4j-1.7.33.jar
Binary file not shown.
Binary file added logback-libs/logback-classic-1.2.10.jar
Binary file not shown.
Binary file added logback-libs/logback-core-1.2.10.jar
Binary file not shown.
Binary file added logback-libs/slf4j-api-1.7.33.jar
Binary file not shown.
8 changes: 7 additions & 1 deletion src/java/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.lang.System.Logger.Level.INFO;
import static java.nio.charset.StandardCharsets.US_ASCII;

/**
Expand All @@ -25,10 +26,15 @@ public static void main( String[] args ) throws IOException {
.parse( new File( args.length > 0 ? args[ 0 ] : "tests/words.txt" ) );

var encoder = new PhoneNumberEncoder( words );
var logger = System.getLogger( "Main" );

new InputParser( PhoneNumberCleaner::clean )
.parse( new File( args.length > 1 ? args[ 1 ] : "tests/numbers.txt" ) )
.forEach( phone -> encoder.encode( phone, System.out::println ) );
.forEach( phone ->
encoder.encode( phone, item -> {
logger.log( INFO, "{0}: {1}", item.original(), item.result() );
} ) );

}

}
Expand Down

0 comments on commit 948cc2e

Please sign in to comment.