Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 11 Next »

UP TO DATE

API reference

setUpMeasure

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.

  • context: Context of your 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.
  • 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.
  • 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

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 int TIMEOUT = 180;
	private static final int INTERVAL = 200;
	private static final boolean ONLINE = true;

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

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

		gsptApi.stopMeasure("test_Idle");
	}
}

Usage with GREENSPECTOR Test Runnner or Power Test Cloud/Bench

The probe configuration is slightly different in the case you integrate GREENSPECTOR Meter Android API in your automated tests (UI Automator or Espresso for example) that you launch with the Test Runner or the Power Test Cloud/Bench.

  1. Some fields will be directly handled by the Test Runner (or the Power Test Cloud/Bench). You can leave empty string values for the following fields: application, version, url and privateToken.
  2. You have to set online to false because the Test Runner (or the Power Test Cloud/Bench) will read the results on the device.

As an example, the previous code would be updated in this way:

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

	/** Probe configuration **/
	private static final String APPLICATION = "";
	private static final String VERSION = "";
	private static final String URL = "";
	private static final String PRIVATETOKEN = "";
	private static final int TIMEOUT = 180;
	private static final int INTERVAL = 200;
	private static final boolean ONLINE = false;

	private Api gsptApi;
//...

Related articles


  • No labels