Meter Android API

UP TO DATE

You can include Meter Android API in your automated tests (UI Automator or Espresso for example) so that you can measure your application without modifying the code of your application.

Note : You can also include it in an android application directly if the goal is not to measure this application (Prefer not using this way)

API reference

init

public void init(Context context)

Init the probe with the Context.

  • context: Context of your Instumentation or Android's application.

init

public void init(Context context, String[] packageName, int[] lstPid, String appName, String version, String url, String privateToken, boolean online)

Sets up the probe with all the needed data.

  • context: Context of your Instumentation or Android's application.
  • packageName: Package name of the application to monitor (for example com.android.chrome). If it is blank (""), the probe does not monitor any particular process.
  • lstPid: In case of system applications, the probe can't get the PID by package name so you need to precise the PID list matching the package name. (Optional)
  • appName: GREENSPECTOR's application name.
  • version: GREENSPECTOR's application version.
  • url: GREENSPECTOR's server URL. For SaaS: https://api.greenspector.com/api. For on premises servers, replace api.greenspector.com by your URL.
  • privateToken: Your private token.
  • online: Set false to write the results on your device. Use this case if you launch your automated tests (UI Automator or Espresso for example) with the Test Runner or the Power Test Cloud/Bench.  Set true to send the measures immediately to your GREENSPECTOR server. Use this case if you launch your tests with Android Studio (i.e. you do not use the Test Runner or the Power Test Cloud / Bench).

setUpMeasure (Deprecated)

public void setUpMeasure(Context context, String[] packageName, int[] lstPid, String appName, String version, String url, String privateToken, int timeout, int interval, boolean online)

Sets up the probe with all the needed data. This method is deprecated.

  • context: Context of your Instumentation or Android's application.
  • packageName: Package name of the application to monitor (for example com.android.chrome). If it is blank (""), the probe does not monitor any particular process.
  • lstPid: In case of system applications, the probe can't get the PID by package name so you need to precise the PID list matching the package name. (Optional)
  • appName: GREENSPECTOR's application name.
  • version: GREENSPECTOR's application version.
  • url: GREENSPECTOR's server URL. For SaaS: https://api.greenspector.com/api. For on premises servers, replace api.greenspector.com by your URL.
  • privateToken: Your private token.
  • timeout: Test time in seconds. The measure stops when the measure time reaches this timeout. deprecated
  • interval: Measure interval in milliseconds. The interval must be greater than 100 ms or can be 0 if you do not want to set it. deprecated
  • online: Set false to write the results on your device. Use this case if you launch your automated tests (UI Automator or Espresso for example) with the Test Runner or the Power Test Cloud/Bench.  Set true to send the measures immediately to your GREENSPECTOR server. Use this case if you launch your tests with Android Studio (i.e. you do not use the Test Runner or the Power Test Cloud / Bench).

startMeasure

public boolean startMeasure()

Starts the measure.

Returns a boolean indicating whether the measure was successfully started or not.

stopMeasure

public boolean stopMeasure(String testName)

Stops the measure and writes the results on the device or sends them to your GREENSPECTOR Server.

  • testName: Name of the current test.

Returns a boolean indicating whether the measure was successfully stopped or not.

setProxy (optional)

public void setProxy(String url, int port)
public void setProxy(String url, int port, String login, String password

Sets the proxy URL and port.

If never set, no proxy will be used.

cpt_test

public int cpt_test

Use this counter to set the number of iterations (default: 1 iteration). This counter must be uptaded before calling the method stopMeasure() to be taken into account.

If you set a number of iterations for your test case, measures and data displayed on GREENSPECTOR Web Interface will refer to the mean value for one iteration.

Usage

Example of a Test Instrumentation

InstrumentationTestCase.java
package com.greenspector.sample.BasicExample;

import android.content.Context;
import android.test.InstrumentationTestCase;

import android.support.test.InstrumentationRegistry;

import com.greenspector.probe.android.interfaces.Api;

public class GreenspectorBasicExample extends InstrumentationTestCase {

	/** Package to measure **/
	private static final String BASIC_SAMPLE_PACKAGE = "com.android.chrome";

	/** Probe configuration **/
	private static final String APPLICATION = "TestApplication";
	private static final String VERSION = "1.0.2";
	private static final String URL = "https://my-instance.greenspector.com/api";
	private static final String PRIVATETOKEN = "123456789AZERTY";
	private static final boolean ONLINE = true;

	private Api gsptApi;
	
	public void testBasicExample() {
		gsptApi = new Api();
		gsptApi.init(getInstrumentation().getTargetContext(), new String[]{BASIC_SAMPLE_PACKAGE}, null, APPLICATION, VERSION, URL, PRIVATETOKEN, ONLINE);
		gsptApi.startMeasure();

		try {
			Thread.sleep(10000);
		} catch (InterruptedException e) {
			gsptApi.stopMeasure("test_Idle_Failed");
			return;
		}

		gsptApi.stopMeasure("test_Idle");
	}
}

Usage with Android Studio 

If you launch your automated tests (UI Automator or Espresso for example) with Android Studio, if you have set all parameters, especially online to true, the measure is sent to your instance. Use this method :

public void init(Context context, String[] packageName, int[] lstPid, String appName, String version, String url, String privateToken, boolean online)

Usage with GREENSPECTOR Test Runnner or Power Test Cloud/Bench

If you launch your automated tests (UI Automator or Espresso for example) with the Test Runner or the Power Test Cloud/Bench, the probe configuration is not important. All fields are not taken into account because they are directly handled by Test Runner Or Test Bench job parameters.

Use this method.

public void init(Context context)

or this method (all arguments except context are not taken into account). So you do not need to change your code if you launch with Test Runner or with Android Studio

public void init(Context context, String[] packageName, int[] lstPid, String appName, String version, String url, String privateToken, boolean online)


Related articles