Skip to content

Instantly share code, notes, and snippets.

@yangweigbh
yangweigbh / NetworkEventProvider.java
Last active January 18, 2021 10:35
Class to listen to network change
public interface NetworkEventProvider {
void setListener(Listener listener);
interface Listener {
/**
* @param networkStatus {@link com.birbit.android.jobqueue.network.NetworkUtil.NetworkStatus}
*/
void onNetworkChange(@NetworkUtil.NetworkStatus int networkStatus);
}
}
@yangweigbh
yangweigbh / SkipList.kt
Created July 14, 2017 10:07
SkipList Implemented in Kotlin
import java.util.*
/**
* Created by yangwei-ms on 2017/7/14.
*/
class SkipList<T>(private val comparator: Comparator<T>) {
private var curHeight: Int = 1
private val head: Node<T> = Node(null, MAX_HEIGHT)
private val rnd: Random = Random(System.currentTimeMillis())
package com.github.yangweigbh.traceana;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.util.Log;
import java.util.concurrent.ConcurrentHashMap;
public class ThreadWatcher {
open class AndroidBenchmarkRunner : AndroidJUnitRunner() {
@CallSuper
override fun waitForActivitiesToComplete() {
// We don't call the super method here, since we have
// an activity we intend to persist between tests
// TODO: somehow wait for every activity but IsolationActivity
// Before/After each test, from the test thread, synchronously launch
// our IsolationActivity if it's not already resumed
val dest = "/data/local/tmp/lockClocks.sh"
val source = javaClass.classLoader.getResource("scripts/lockClocks.sh")
val tmpSource = Files.createTempFile("lockClocks.sh", null).toString()
Files.copy(
source.openStream(),
Paths.get(tmpSource),
StandardCopyOption.REPLACE_EXISTING
)
adb.execSync("push $tmpSource $dest")
if (!CpuInfo.locked && isSustainedPerformanceModeSupported()) {
sustainedPerformanceModeInUse = true
}
private val activityLifecycleCallbacks = object : Application.ActivityLifecycleCallbacks {
@SuppressLint("NewApi") // window API guarded by [isSustainedPerformanceModeSupported]
override fun onActivityCreated(activity: Activity, bundle: Bundle?) {
if (sustainedPerformanceModeInUse) {
activity.window.setSustainedPerformanceMode(true)
}
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)
@Test
public void myBenchmark() {
...
BenchmarkState state = benchmarkRule.getState();
while (state.keepRunning()) {
doSomeWork();
}
...
}
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
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()