Note: The table schema is based on the blog [(In Chinese) Data Transfer, Storage, Presentation, EMQX + TDengine Build MQTT IoT Data Visualization Platform](https://www.taosdata.com/blog/2020/08/04/1722.html) as an example. Subsequent operations are carried out with this blog scenario too. Please modify it according to your actual application scenario.
Note: The table schema is based on the blog [(In Chinese) Data Transfer, Storage, Presentation, EMQX + TDengine Build MQTT IoT Data Visualization Platform](https://www.taosdata.com/blog/2020/08/04/1722.html) as an example. Subsequent operations are carried out with this blog scenario too. Please modify it according to your actual application scenario.
...
@@ -81,21 +81,21 @@ Edit the resource configuration to add the key/value pairing for Authorization.
...
@@ -81,21 +81,21 @@ Edit the resource configuration to add the key/value pairing for Authorization.
## Compose program to mock data
## Compose program to mock data
```javascript
```javascript
// mock.js
// mock.js
constmqtt=require('mqtt')
constmqtt=require('mqtt')
constMock=require('mockjs')
constMock=require('mockjs')
constEMQX_SERVER='mqtt://localhost:1883'
constEMQX_SERVER='mqtt://localhost:1883'
constCLIENT_NUM=10
constCLIENT_NUM=10
constSTEP=5000// Data interval in ms
constSTEP=5000// Data interval in ms
constAWAIT=5000// Sleep time after data be written once to avoid data writing too fast
constAWAIT=5000// Sleep time after data be written once to avoid data writing too fast
constCLIENT_POOL=[]
constCLIENT_POOL=[]
startMock()
startMock()
functionsleep(timer=100){
functionsleep(timer=100){
returnnewPromise(resolve=>{
returnnewPromise(resolve=>{
setTimeout(resolve,timer)
setTimeout(resolve,timer)
})
})
}
}
asyncfunctionstartMock(){
asyncfunctionstartMock(){
constnow=Date.now()
constnow=Date.now()
for(leti=0;i<CLIENT_NUM;i++){
for(leti=0;i<CLIENT_NUM;i++){
constclient=awaitcreateClient(`mock_client_${i}`)
constclient=awaitcreateClient(`mock_client_${i}`)
...
@@ -119,12 +119,12 @@ Edit the resource configuration to add the key/value pairing for Authorization.
...
@@ -119,12 +119,12 @@ Edit the resource configuration to add the key/value pairing for Authorization.
awaitsleep(AWAIT)
awaitsleep(AWAIT)
}
}
console.log(`Done, use ${(Date.now()-now)/1000}s`)
console.log(`Done, use ${(Date.now()-now)/1000}s`)
}
}
/**
/**
* Init a virtual mqtt client
* Init a virtual mqtt client
* @param {string} clientId ClientID
* @param {string} clientId ClientID
*/
*/
functioncreateClient(clientId){
functioncreateClient(clientId){
returnnewPromise((resolve,reject)=>{
returnnewPromise((resolve,reject)=>{
constclient=mqtt.connect(EMQX_SERVER,{
constclient=mqtt.connect(EMQX_SERVER,{
clientId,
clientId,
...
@@ -141,11 +141,11 @@ Edit the resource configuration to add the key/value pairing for Authorization.
...
@@ -141,11 +141,11 @@ Edit the resource configuration to add the key/value pairing for Authorization.
@@ -158,7 +158,7 @@ Edit the resource configuration to add the key/value pairing for Authorization.
...
@@ -158,7 +158,7 @@ Edit the resource configuration to add the key/value pairing for Authorization.
"area":Mock.Random.integer(0,20),
"area":Mock.Random.integer(0,20),
"ts":1596157444170,
"ts":1596157444170,
}
}
}
}
```
```
Note: `CLIENT_NUM` in the code can be set to a smaller value at the beginning of the test to avoid hardware performance be not capable to handle a more significant number of concurrent clients.
Note: `CLIENT_NUM` in the code can be set to a smaller value at the beginning of the test to avoid hardware performance be not capable to handle a more significant number of concurrent clients.