Skip to content

Instantly share code, notes, and snippets.

@yangweigbh
yangweigbh / reverseindex.main.kts
Created August 17, 2020 15:19
simple reverse index application that parse wikipedia abstract https://dumps.wikimedia.org/enwiki/latest/enwiki-latest-abstract1.xml.gz
@file:DependsOn("com.github.ajalt:clikt:2.8.0")
@file:DependsOn("org.apache.opennlp:opennlp-tools:1.9.3")
import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.parameters.options.option
import com.github.ajalt.clikt.parameters.options.prompt
import opennlp.tools.stemmer.PorterStemmer
import org.xml.sax.Attributes
import org.xml.sax.helpers.DefaultHandler
import java.io.File
for (ApplicationExitInfo exitInfo: unprocessedExitInfos) {
if (exitInfo.getReason() == ApplicationExitInfo.REASON_ANR) {
UUID uuid = UUID.randomUUID();
String fileName = "anr_info_" + uuid.toString() + ".trace";
File outFile = new File(getFilesDir().getAbsolutePath() + "/" + fileName);
try (InputStream inputStream = exitInfo.getTraceInputStream()) {
copyStreamToFile(inputStream, outFile);
} catch (IOException e) {
Log.e(TAG, "copyStreamToFile: ", e);
for (ApplicationExitInfo exitInfo: unprocessedExitInfos) {
Log.e(TAG, String.format("Exit reason: %d, description: %s", exitInfo.getReason(), exitInfo.getDescription()));
}
long lastExitTimestamp = getPreferences(0).getLong(PREV_DETECT_TIME_KEY, 0);
List<ApplicationExitInfo> unprocessedExitInfos = new ArrayList<>();
for (ApplicationExitInfo exitInfo: applicationExitInfos) {
if (exitInfo.getTimestamp() > lastExitTimestamp) {
unprocessedExitInfos.add(exitInfo);
} else {
break;
}
}
List<ApplicationExitInfo> applicationExitInfos = am.getHistoricalProcessExitReasons(null, 0, 0);
ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
fun bumpCurrentThreadPriority() = synchronized(lock) {
val myTid = Process.myTid()
if (initialTid == myTid) {
// already bumped
return
}
// ensure we don't have multiple threads bumped at once
resetBumpedThread()
fun getState(): BenchmarkState {
// Note: this is an explicit method instead of an accessor to help convey it's only for Java
// Kotlin users should call the [measureRepeated] method.
if (!applied) {
throw IllegalStateException(
"Cannot get state before BenchmarkRule is applied to a test. Check that your " +
"BenchmarkRule is annotated correctly (@Rule in Java, @get:Rule in Kotlin)."
)
}
return internalState
@Test
public void myBenchmark() {
...
BenchmarkState state = benchmarkRule.getState();
while (state.keepRunning()) {
doSomeWork();
}
...
}
if (sustainedPerformanceModeInUse) {
// Keep at least one core busy. Together with a single threaded benchmark, this
// makes the process get multi-threaded setSustainedPerformanceMode.
//
// We want to keep to the relatively lower clocks of the multi-threaded benchmark
// mode to avoid any benchmarks running at higher clocks than any others.
//
// Note, thread names have 15 char max in Systrace
thread(name = "BenchSpinThread") {
Process.setThreadPriority(Process.THREAD_PRIORITY_LOWEST)