-
Notifications
You must be signed in to change notification settings - Fork 140
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(#2764): caching logic changed, added StHash, StHashTest, fix tests #2771
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,38 +29,40 @@ | |
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.time.ZonedDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
import org.eolang.maven.util.HmBase; | ||
import org.eolang.parser.StHash; | ||
import org.hamcrest.MatcherAssert; | ||
import org.hamcrest.Matchers; | ||
import org.hamcrest.io.FileMatchers; | ||
import org.junit.jupiter.api.Disabled; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.io.TempDir; | ||
import org.xembly.Directives; | ||
import org.xembly.ImpossibleModificationException; | ||
import org.xembly.Xembler; | ||
|
||
/** | ||
* Test case for {@link org.eolang.maven.optimization.OptCached}. | ||
* @since 0.28.12 | ||
*/ | ||
@SuppressWarnings("PMD.TooManyMethods") | ||
final class OptCachedTest { | ||
|
||
/** | ||
* Test case for XML program in cache. | ||
* | ||
* @param tmp Temp dir | ||
* @throws IOException if I/O fails | ||
* @todo #2422:60min returnsFromCacheIfXmlAlreadyInCache: this test is unstable. | ||
* We should resolve issues with unstable failures and only | ||
* then enable the test. | ||
* Also, see this <a href="https://github.com/objectionary/eo/issues/2727">issue</a>. | ||
*/ | ||
@Disabled | ||
@Test | ||
void returnsFromCacheIfXmlAlreadyInCache(@TempDir final Path tmp) throws IOException { | ||
final XML program = OptCachedTest.program(ZonedDateTime.now()); | ||
void returnsFromCacheIfXmlAlreadyInCache(@TempDir final Path tmp) | ||
throws IOException, ImpossibleModificationException, NoSuchAlgorithmException { | ||
final XML program = OptCachedTest.updatedProgram( | ||
OptCachedTest.program(ZonedDateTime.now()) | ||
); | ||
OptCachedTest.save(tmp, program); | ||
MatcherAssert.assertThat( | ||
"We expected that the program will be returned from the cache.", | ||
|
@@ -74,11 +76,12 @@ void returnsFromCacheIfXmlAlreadyInCache(@TempDir final Path tmp) throws IOExcep | |
); | ||
} | ||
|
||
@Disabled | ||
@Test | ||
void returnsFromCacheButTimesSaveAndExecuteDifferent(@TempDir final Path tmp) | ||
throws IOException { | ||
final XML program = OptCachedTest.program(ZonedDateTime.now().minusMinutes(2)); | ||
throws IOException, ImpossibleModificationException, NoSuchAlgorithmException { | ||
final XML program = OptCachedTest.updatedProgram( | ||
OptCachedTest.program(ZonedDateTime.now()) | ||
); | ||
OptCachedTest.save(tmp, program); | ||
MatcherAssert.assertThat( | ||
"We expected that the not immediately saved program will be returned from the cache.", | ||
|
@@ -92,13 +95,16 @@ void returnsFromCacheButTimesSaveAndExecuteDifferent(@TempDir final Path tmp) | |
); | ||
} | ||
|
||
@Disabled | ||
@Test | ||
void returnsFromCacheCorrectProgram(@TempDir final Path tmp) | ||
throws IOException { | ||
final XML prev = OptCachedTest.program(ZonedDateTime.now(), "first program"); | ||
throws IOException, ImpossibleModificationException, NoSuchAlgorithmException { | ||
final XML prev = OptCachedTest.updatedProgram( | ||
OptCachedTest.program(ZonedDateTime.now(), "first program") | ||
); | ||
OptCachedTest.save(tmp, prev); | ||
final XML current = OptCachedTest.program(ZonedDateTime.now(), "second program"); | ||
final XML current = OptCachedTest.updatedProgram( | ||
OptCachedTest.program(ZonedDateTime.now(), "second program") | ||
); | ||
MatcherAssert.assertThat( | ||
"Expecting current program to be compiled, but prev program was returned from cache.", | ||
new OptCached( | ||
|
@@ -199,9 +205,26 @@ private static XML program(final ZonedDateTime time, final String something) { | |
.add("program") | ||
.attr("name", "main") | ||
.attr("time", time.format(DateTimeFormatter.ISO_INSTANT)) | ||
.add("objects") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Yanich96 Why do you need this row? |
||
.attr("something", something) | ||
.up() | ||
).xmlQuietly() | ||
); | ||
} | ||
|
||
/** | ||
* Adds attribute "hash" in EO program for tests. | ||
* @param xml XML. | ||
* @return XML representation of program. | ||
*/ | ||
private static XML updatedProgram(final XML xml) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Yanich96 Maybe it's better to leave just |
||
throws NoSuchAlgorithmException, ImpossibleModificationException { | ||
final String hash = new StHash.Hash(xml).getHash(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Yanich96 Is it possible to inline this variable? |
||
return new XMLDocument( | ||
new Xembler( | ||
new Directives() | ||
.xpath("//program").attr("hash", hash) | ||
).apply(xml.node()) | ||
); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Yanich96 We use
OptCached
in order to avoid repeated unnecessary computations and increase the compilation speed. However the solution you provided might make the optimization even longer. Let's me explain.In order to read
/program/@hash
from thepath
you need to read the entire file. This operation is extremely long. I even bet that if you disable caching now, the compilation will be faster.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@volodya-lombrozo
Yes, but I have 3 points:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Yanich96, I conducted a simple benchmark to compare the time spent in
OptCache.apply
for thereturnsFromCacheIfXmlAlreadyInCache
test in the master branch (the old solution) and your solution. The results I obtained are as follows:As you can see, the "page cache" doesn't provide much improvement. Please note that in the test, we are using an extremely small program example (only a few lines). Real programs will be much larger, and the performance gap will likely be more significant.
Below is the test code, where I modified the test to include time measurement.
Master branch:
Your branch: