Skip to content

Commit

Permalink
Interim commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua J. A. Harwood committed Feb 12, 2024
1 parent 1d60834 commit b83cd8e
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 34 deletions.
20 changes: 20 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@
<scope>import</scope>
<type>pom</type>
</dependency>
<dependency>
<groupId>com.pivovarit</groupId>
<artifactId>parallel-collectors</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<scope>compile</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
Expand All @@ -57,5 +67,15 @@
<version>24.1.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.pivovarit</groupId>
<artifactId>parallel-collectors</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.12</version>
</dependency>
</dependencies>
</project>

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.joshuaharwood.cifparser.processing;

import static com.pivovarit.collectors.ParallelCollectors.parallel;

import com.joshuaharwood.cifparser.parsing.model.CifRecord;
import com.joshuaharwood.cifparser.parsing.parser.CifLineParser;
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.stream.Stream;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class CifMultithreadedParser implements
CifProcessor<Path, CompletableFuture<Stream<CifRecord>>> {

private static final CifLineParser CIF_LINE_PARSER = new CifLineParser();
private final CifMultithreadedParserConfig config;

public CifMultithreadedParser() {
this(null);
}

public CifMultithreadedParser(@Nullable CifMultithreadedParserConfig config) {
this.config = config;
}

private static CompletableFuture<Stream<CifRecord>> usingDefaultExecutor(BufferedReader b) {
return b.lines().collect(parallel(CIF_LINE_PARSER::parseLine));
}

private static CompletableFuture<Stream<CifRecord>> usingCustomExecutor(BufferedReader b,
CifMultithreadedParserConfig config) {
return b.lines()
.collect(parallel(CIF_LINE_PARSER::parseLine, config.executor(), config.parallelism()));
}

public CompletableFuture<Stream<CifRecord>> parseCifRecords(Path path) throws IOException {
try (BufferedReader b = Files.newBufferedReader(path, StandardCharsets.US_ASCII)) {

if (this.config != null) {
return usingCustomExecutor(b, config);
} else {
return usingDefaultExecutor(b);
}
}
}

public record CifMultithreadedParserConfig(@NotNull Executor executor,
@NotNull Integer parallelism) {

public CifMultithreadedParserConfig {
Objects.requireNonNull(executor);
Objects.requireNonNull(parallelism);
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
import com.joshuaharwood.cifparser.parsing.model.CifRecord;
import java.util.List;

public interface CifProcessor<T> {
List<CifRecord> parseCifRecords(T input) throws Exception;
public interface CifProcessor<I, O> {
O parseCifRecords(I input) throws Exception;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,18 @@
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Stream;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

class CifFileParserTest {
class CifMultithreadedParserTest {

private CifFileParser cifFileParser;
private CifMultithreadedParser cifMultithreadedParser;

@BeforeEach
void setUp() {
cifFileParser = new CifFileParser();
cifMultithreadedParser = new CifMultithreadedParser();
}

@Test
Expand All @@ -28,12 +29,14 @@ void parseCifRecords() throws IOException {

assertThat(testCifPath).isNotNull();

List<CifRecord> records = cifFileParser.parseCifRecords(Path.of(testCifPath.getFile()));

List<CifRecord> records = cifMultithreadedParser.parseCifRecords(Path.of(testCifPath.getFile()))
.join()
.toList();

assertThat(records).hasSize(62);
}

@Disabled("Used for approximate manual benchmarking. Disabled by default")
// @Disabled("Used for approximate manual benchmarking. Disabled by default")
@Test
void parseEntireCifFullExtract() throws IOException {
/*
Expand All @@ -44,10 +47,14 @@ void parseEntireCifFullExtract() throws IOException {

assertThat(testCifPath).isNotNull();

Check failure on line 48 in src/test/java/com/joshuaharwood/cifparser/processing/CifMultithreadedParserTest.java

View workflow job for this annotation

GitHub Actions / JUnit Test Report

CifMultithreadedParserTest.parseEntireCifFullExtract

Expecting actual not to be null
Raw output
java.lang.AssertionError: 

Expecting actual not to be null
	at com.joshuaharwood.cifparser.processing.CifMultithreadedParserTest.parseEntireCifFullExtract(CifMultithreadedParserTest.java:48)
	at java.base/java.lang.reflect.Method.invoke(Method.java:580)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)

final LocalDateTime before = LocalDateTime.now();
cifFileParser.parseCifRecords(Path.of(testCifPath.getFile()));
final LocalDateTime after = LocalDateTime.now();
// final LocalDateTime before = LocalDateTime.now();

CompletableFuture<Stream<CifRecord>> future = cifMultithreadedParser.parseCifRecords(Path.of(
testCifPath.getFile()));

// assertThat(future).
// final LocalDateTime after = LocalDateTime.now();

System.out.printf("CifFileParser benchmark time elapsed: %s", Duration.between(before, after));
// System.out.printf("CifFileParser benchmark time elapsed: %s", Duration.between(before, after));
}
}

0 comments on commit b83cd8e

Please sign in to comment.