Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit 5963342

Browse files
committed
Added cache for github json tests
1 parent 9436040 commit 5963342

File tree

2 files changed

+75
-18
lines changed

2 files changed

+75
-18
lines changed

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ script:
1414
cache:
1515
directories:
1616
- $HOME/.gradle
17+
- $HOME/.tests-cache
1718

1819
notifications:
1920
irc:
@@ -43,6 +44,6 @@ notifications:
4344
# http://www.gradle.org/docs/current/userguide/tutorial_this_and_that.html#sec:gradle_properties_and_system_properties
4445
env:
4546
global:
46-
- JAVA_OPTS="-XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled"
47+
- JAVA_OPTS="-XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled -DGitHubTests.testPath=$HOME/.tests-cache"
4748
- secure: "OlSe9DlT1D/b/ru3uO1m8nwevaDhH9XGmAfJ/2U69eBwRtg/aLEF9ZpULrMNTDR8XbNT6uuZsvvRby5HOKPRRkOqnWIY8He2hRpw0IYDONfRfKXIcr4WuJM3N98mQ9RYoNcV9LbHoXFQtfc7oUIp5o7WsCx5Pd/Ygyz4ZVNBc5g="
4849
- secure: "Y5L4DJXonAavfoUAMgM+RUTVYfyT5YkB8yBp8oUTK6RMCUrSTXB2Kpa8fvP8gvPXIXpIQgxa+bn/85wSrFAm8I9e4zXYO/1h4TPsbXrE1KB3aIXlg96wr1WRg+YyWed1VOtrCDZhO0K9l2fEG4ktysv+vtSaDxRVjtnFX+0Xymk="

ethereumj-core/src/test/java/org/ethereum/jsontestsuite/suite/JSONReader.java

Lines changed: 73 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
import java.nio.file.Paths;
3434
import java.util.ArrayList;
3535
import java.util.List;
36+
import java.util.Properties;
37+
import java.util.UUID;
3638
import java.util.concurrent.*;
3739

3840
import org.slf4j.Logger;
@@ -42,6 +44,11 @@ public class JSONReader {
4244

4345
private static Logger logger = LoggerFactory.getLogger("TCK-Test");
4446

47+
private static final String CACHE_DIR = SystemProperties.getDefault().githubTestsPath();
48+
private static final boolean USE_CACHE = SystemProperties.getDefault().githubTestsLoadLocal();
49+
private static final String CACHE_INDEX = "index.prop";
50+
private static final String CACHE_FILES_SUB_DIR = "files";
51+
4552
static ExecutorService threadPool;
4653

4754
private static int MAX_RETRIES = 3;
@@ -74,10 +81,9 @@ public static List<String> loadJSONsFromCommit(List<String> filenames, final Str
7481

7582
public static String loadJSONFromCommit(String filename, String shacommit) throws IOException {
7683
String json = "";
77-
if (!SystemProperties.getDefault().githubTestsLoadLocal())
78-
json = getFromUrl("https://raw.githubusercontent.com/ethereum/tests/" + shacommit + "/" + filename);
84+
json = getFromUrl("https://raw.githubusercontent.com/ethereum/tests/" + shacommit + "/" + filename);
7985
if (!json.isEmpty()) json = json.replaceAll("//", "data");
80-
return json.isEmpty() ? getFromLocal(filename) : json;
86+
return json;
8187
}
8288

8389
public static String getFromLocal(String filename) throws IOException {
@@ -94,7 +100,15 @@ public static String getFromUrl(String urlToRead) {
94100
String result = null;
95101
for (int i = 0; i < MAX_RETRIES; ++i) {
96102
try {
97-
result = getFromUrlImpl(urlToRead);
103+
if (USE_CACHE) {
104+
result = getFromCacheImpl(urlToRead);
105+
if (result == null) {
106+
result = getFromUrlImpl(urlToRead);
107+
recordCache(urlToRead, result);
108+
}
109+
} else {
110+
result = getFromUrlImpl(urlToRead);
111+
}
98112
break;
99113
} catch (Exception ex) {
100114
logger.debug(String.format("Failed to retrieve %s, retry %d/%d", urlToRead, (i + 1), MAX_RETRIES), ex);
@@ -138,26 +152,68 @@ private static String getFromUrlImpl(String urlToRead) throws Exception {
138152
return result.toString();
139153
}
140154

141-
public static List<String> listJsonBlobsForTreeSha(String sha, String testRoot) throws IOException {
155+
private static String getFromCacheImpl(String urlToRead) {
156+
String result = null;
157+
String filename = null;
158+
try (InputStream input = new FileInputStream(CACHE_DIR + System.getProperty("file.separator") + CACHE_INDEX)) {
159+
Properties prop = new Properties();
160+
prop.load(input);
161+
filename = prop.getProperty(urlToRead);
162+
} catch (Exception ex) {
163+
ex.printStackTrace();
164+
}
165+
166+
if (filename != null) {
167+
try {
168+
result = new String(Files.readAllBytes(new File(CACHE_DIR + System.getProperty("file.separator") +
169+
CACHE_FILES_SUB_DIR + System.getProperty("file.separator") + filename).toPath()));
170+
} catch (IOException ex) {
171+
ex.printStackTrace();
172+
}
173+
}
142174

143-
if (SystemProperties.getDefault().githubTestsLoadLocal()) {
175+
return result;
176+
}
144177

145-
String path = SystemProperties.getDefault().githubTestsPath() +
146-
System.getProperty("file.separator") + testRoot.replaceAll("/", "");
178+
private synchronized static void recordCache(String urlToRead, String data) {
179+
String filename = UUID.randomUUID().toString();
180+
File targetFile = new File(CACHE_DIR + System.getProperty("file.separator") +
181+
CACHE_FILES_SUB_DIR + System.getProperty("file.separator") + filename);
147182

148-
List<String> files = FileUtil.recursiveList(path);
183+
// Ensure we have directories created
184+
File parent = targetFile.getParentFile();
185+
if (!parent.exists() && !parent.mkdirs()) {
186+
throw new IllegalStateException("Couldn't create dir: " + parent);
187+
}
149188

150-
List<String> jsons = new ArrayList<>();
151-
for (String f : files) {
152-
if (f.endsWith(".json"))
153-
jsons.add(
154-
f.replace(path + System.getProperty("file.separator"), "")
155-
.replaceAll(System.getProperty("file.separator"), "/"));
156-
}
189+
// Load index
190+
Properties prop = new Properties();
191+
String propFile = CACHE_DIR + System.getProperty("file.separator") + CACHE_INDEX;
192+
try (InputStream input = new FileInputStream(propFile)) {
193+
prop.load(input);
194+
} catch (Exception ex) {
195+
ex.printStackTrace();
196+
}
157197

158-
return jsons;
198+
// Save with new entry
199+
prop.setProperty(urlToRead, filename);
200+
try (OutputStream output = new FileOutputStream(propFile)) {
201+
prop.store(output, null);
202+
} catch (Exception ex) {
203+
ex.printStackTrace();
204+
return;
159205
}
160206

207+
// Save data
208+
try (OutputStream output = new FileOutputStream(targetFile)) {
209+
output.write(data.getBytes());
210+
} catch (Exception ex) {
211+
ex.printStackTrace();
212+
}
213+
}
214+
215+
public static List<String> listJsonBlobsForTreeSha(String sha, String testRoot) throws IOException {
216+
161217
String result = getFromUrl("https://api.github.com/repos/ethereum/tests/git/trees/" + sha + "?recursive=1");
162218

163219
JSONParser parser = new JSONParser();

0 commit comments

Comments
 (0)