-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcreate-points-async.js
98 lines (86 loc) · 4.09 KB
/
create-points-async.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// import classes
const DataPointVO = Java.type('com.serotonin.m2m2.vo.DataPointVO');
const ModuleRegistry = Java.type('com.serotonin.m2m2.module.ModuleRegistry');
const VirtualDataSourceDefinition = Java.type('com.serotonin.m2m2.virtual.VirtualDataSourceDefinition');
const CompletableFuture = Java.type('java.util.concurrent.CompletableFuture');
const Common = Java.type('com.serotonin.m2m2.Common');
const PointValueDao = Java.type('com.serotonin.m2m2.db.dao.PointValueDao');
const BrownianPointValueGenerator = Java.type('com.infiniteautomation.mango.pointvalue.generator.BrownianPointValueGenerator');
const DataType = Java.type('com.serotonin.m2m2.DataType');
// import services
const dataPointService = services.dataPointService;
const dataSourceService = services.dataSourceService;
const pointValueDao = Common.getBean(PointValueDao.class);
// configuration parameters
const numDataSources = 3; // number of data sources to create
const updatePeriod = 5000; // update period in milliseconds
const pointsPerDataSource = 30000; // number of points per data source
const tagsPerPoint = 5; // actual number of tags added to each point
const possibleTagKeys = 10; // number of tag keys that are possible
const possibleTagValues = 30; // number of values per tag that are possible
// data generation for points
const generatePointValues = false; // generate point values for every point
const generateFrom = Date.parse('2021-01-01T00:00:00.000Z').valueOf(); // epoch ms
const generateTo = new Date().valueOf(); // epoch ms
const generateInterval = 5000; // ms
const tags = {};
for (let i = 0; i < possibleTagKeys; i++) {
const tagValues = [];
for (let j = 0; j < possibleTagValues; j++) {
tagValues.push(`key_${i}_value_${j}`);
}
tags[`key_${i}`] = tagValues;
}
const tagKeys = Object.keys(tags);
const futures = [];
const startTime = new Date();
const generator = new BrownianPointValueGenerator(generateFrom, generateTo, generateInterval);
const inserter = generator.createInserter(pointValueDao, 10000);
for (let dsCount = 0; dsCount < numDataSources; dsCount++) {
const dataSourceDef = ModuleRegistry.getDefinition(VirtualDataSourceDefinition.class);
const dataSource = dataSourceDef.baseCreateDataSourceVO();
dataSource.setName(`Performance test ${dsCount}`);
dataSource.setUpdatePeriodType(8); // MILLISECONDS
dataSource.setUpdatePeriods(updatePeriod);
dataSourceService.insert(dataSource);
const locator = dataSource.createPointLocator();
locator.setDataType(DataType.NUMERIC);
locator.setChangeTypeId(2); // BROWNIAN
locator.getBrownianChange().setStartValue('50');
locator.getBrownianChange().setMax(100);
locator.getBrownianChange().setMin(0);
locator.getBrownianChange().setMaxChange(0.1);
const dataPoint = new DataPointVO();
dataPoint.setDataSourceId(dataSource.getId());
dataPoint.setEnabled(true);
dataPoint.setLoggingType(2); // ALL
dataPoint.setPointLocator(locator);
dataPoint.setDeviceName(dataSource.getName());
// copy the template point, and save it
for (let i = 0; i < pointsPerDataSource; i++) {
const copy = dataPoint.copy();
copy.setId(-1);
copy.setSeriesId(-1);
copy.setXid(null);
copy.setName(`Point ${i}`);
// randomize the tag keys order, grab the first x
const randomKeys = tagKeys.sort(() => 0.5 - Math.random()).slice(0, tagsPerPoint);
const pointTags = {};
for (const key of randomKeys) {
const values = tags[key];
pointTags[key] = values[Math.round(Math.random() * (values.length - 1))];
}
copy.setTags(pointTags);
let future = dataPointService.insertAsync(copy);
if (generatePointValues) {
future = future.thenAccept(inserter);
}
futures.push(future);
}
}
// wait for all futures to complete
CompletableFuture.allOf(futures).get();
const time = (new Date() - startTime) / 1000;
const pointsCreated = numDataSources * pointsPerDataSource;
const pointsPerSec = pointsCreated / time;
log.info('Took {}s to create {} points. {} points/s', time, pointsCreated, pointsPerSec);