Search…
⌃K
Links

Data Generation Functions

There are several functions available inside the tests which allow you to generate data points for different purposes.

Simulate moving vehicles

To simulate a vehicle driving from one location to another is simply a matter of specifying the starting and end address. Use the following function in your template:
{
state['location'] = drive({start:'Munich,DE',end:'Berlin,DE',accuracy:5});
return JSON.stringify(state, null, 2);
}
The above template will generate driving coordinates starting from Munich to Berlin in real-time traffic conditions. Upon each iteration, the function will automatically output the updated GPS coordinates.
You should never call this function within a loop.
Following are the input parameters:
  • start, end : A starting and ending address where you would like to drive. It could be a city name, a street address or even a point of interest, such as:
{
drive({start:'Disneyland, CA',end:'Universal Studios, CA'})
}
  • accuracy: Since real-world GPS devices are not perfect, we will need to simulate the defects as well. Accuracy specifies how much maximum accuracy we should provide when simulating coordinates. In simple words, an accuracy of 5 meters specifies that the resulting coordinate could be anywhere within the 5-meter radius of the actual coordinate.
  • key: Provide your own Google maps direction key, if required.
The output format of the drive() function
The output of the drive() function is a JSON object, that’s why it is important to save this into a string with JSON.stringify(). Here is what the output looks like:
Response:
{
latitude: 34.123456, // decimal
longitude: 23.123455, // decimal
speed : 6, // meter per second
accuracy: 2 // meter radius
finished: // undefined, set to true for the last step
}
Once the drive() function has been finished, the output will contain an additional field called finished set to true to indicate that the current drive has been finished.
Use the finished field to calculate the next set of drive parameters or return.

Get GPS location

The location function converts an address to its GPS coordinates, with a given accuracy parameter.
state['location'] = location({address:'Delhi,IN',accuracy:5})
This will generate a GPS coordinate near New Delhi with an accuracy of 5 meters. Similar to the drive() function, you could also pass the actual address in the parameter and it will convert that to a set of GPS coordinates. The accuracy of this could be still controlled by the accuracy parameter. The output of the location() function is following:
Response:
{
latitude: 34.123456, // decimal
longitude: 23.123455 // decimal
}

Get the real-world weather

weather.temperature() and weather.humidity() functions will query weather API to get the current weather of a city. For example:
{
var temperature = weather.temperature({location: 'London, UK', unit: 'f'});
var humidity = weather.humidity({location: 'London, UK');
}
This will return the current temperature of London in degrees Fahrenheit and relative humidity in percentages.
If the weather can not be found or if the location is incorrect, an error message will be returned. By default, if no unit is specified, the output is in centigrade. To change the temperature to Fahrenheit, provide unit: 'f' as an argument.

Mocking CPU and Memory

cpu() and memory() functions will return mock CPU and memory usages of a running system in percentage points.
{
state['cpu'] = cpu();
state['memory'] = memory();
}

Simulating continuous variables

A volatile variable is useful while modelling a complex value such as stock prices or temperature. The value of the volatile variable is dependent upon its last value and can move up or down to max +/- delta steps from the last value at random.
{
state['temperature'] = volatile({min : -10, max: 100, delta: 4, key:"myvar"});
}
A volatile variable takes 3 parameters, min range, max range and delta value as an input.
The value of volatile will start from the middle of min and max, i.e. min+max/2 Upon each iteration, volatile will change by a minimum of 0 to a maximum of delta in either positive or negative direction from its last value.
Let's understand it with an example:
var chart = volatile({min : 10, max: 20, delta: 3, key:"myvar"})
In the above example, chart will be initialized to a value of 15 at iteration 0. (10+20)/2
At iteration 1, chart can be anywhere +/-3 of 15, i.e. min 12 and max 18. Let’s say it reaches 14.
At iteration 2, chart can be anywhere +/-3 of 14, i.e. min 11 and max 17 and so on.
Volatile is useful for modelling dependent values such as stock prices and temperature graphs, where the next value depends upon the last value.
Always provide a unique key for each volatile variable within an object, so that they do not collide in value.