|
| 1 | +package com.aquib.androidperflab.sdk |
| 2 | + |
| 3 | +import android.app.ActivityManager |
| 4 | +import android.content.Context |
| 5 | +import android.os.Debug |
| 6 | +import android.os.Process |
| 7 | +import android.util.Log |
| 8 | + |
| 9 | +object FakePerformanceMonitorSdk { |
| 10 | + |
| 11 | + // Simulates: snapshotting baseline memory stats, reading /proc/self/status for CPU |
| 12 | + // accounting, and installing a frame-timing callback — all synchronously on the main |
| 13 | + // thread before the first frame is drawn. |
| 14 | + fun init(context: Context) { |
| 15 | + // Simulate: collecting baseline memory snapshot |
| 16 | + val runtime = Runtime.getRuntime() |
| 17 | + val totalMemory = runtime.totalMemory() |
| 18 | + val freeMemory = runtime.freeMemory() |
| 19 | + val nativeHeap = Debug.getNativeHeapSize() |
| 20 | + |
| 21 | + // Simulate: reading ActivityManager memory info |
| 22 | + val activityManager = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager |
| 23 | + val memInfo = ActivityManager.MemoryInfo() |
| 24 | + activityManager.getMemoryInfo(memInfo) |
| 25 | + |
| 26 | + // Simulate: reading per-process CPU stats from procfs (string parsing) |
| 27 | + val pid = Process.myPid() |
| 28 | + val procStatLines = buildString { |
| 29 | + repeat(80) { i -> append("cpu_stat_field_${i}=${pid + i}\n") } |
| 30 | + }.lines() |
| 31 | + |
| 32 | + // Simulate: building baseline metric registry (100 named counters) |
| 33 | + val metrics = HashMap<String, Long>(128) |
| 34 | + repeat(100) { i -> |
| 35 | + metrics["metric_$i"] = System.nanoTime() + i |
| 36 | + } |
| 37 | + |
| 38 | + // Simulate: GC pause measurement — forces a GC and measures its cost |
| 39 | + val gcBefore = Debug.getRuntimeStat("art.gc.gc-count") |
| 40 | + System.gc() |
| 41 | + val gcAfter = Debug.getRuntimeStat("art.gc.gc-count") |
| 42 | + |
| 43 | + // Simulate: installing frame-timing infrastructure (method tracing warmup) |
| 44 | + Thread.sleep(100L) |
| 45 | + |
| 46 | + Log.d( |
| 47 | + "FakePerfMonitorSdk", |
| 48 | + "init complete — heap=${totalMemory / 1024}KB free=${freeMemory / 1024}KB " + |
| 49 | + "nativeHeap=${nativeHeap / 1024}KB availMem=${memInfo.availMem / 1024}KB " + |
| 50 | + "procStatLines=${procStatLines.size} metrics=${metrics.size} gc=$gcBefore→$gcAfter", |
| 51 | + ) |
| 52 | + } |
| 53 | +} |
0 commit comments