• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

taosdata / taos-tools / 12946405359

24 Jan 2025 08:58AM UTC coverage: 75.05% (-0.2%) from 75.276%
12946405359

push

github

web-flow
Merge pull request #839 from taosdata/fix/TS-5846-3.0-A

FIX mixed query mode no need calc thread

414 of 553 new or added lines in 4 files covered. (74.86%)

93 existing lines in 7 files now uncovered.

12453 of 16593 relevant lines covered (75.05%)

328069.46 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

75.93
/src/benchData.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the MIT license as published by the Free Software
6
 * Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 */
12

13
#include <bench.h>
14
#include "benchLog.h"
15
#include <math.h>
16
#include <benchData.h>
17

18
const char charset[] =
19
    "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
20

21
const char* locations[] = {
22
    "California.SanFrancisco", "California.LosAngles",
23
    "California.SanDiego", "California.SanJose",
24
    "California.PaloAlto", "California.Campbell",
25
    "California.MountainView", "California.Sunnyvale",
26
    "California.SantaClara", "California.Cupertino"};
27

28
const char* locations_sml[] = {
29
    "California.SanFrancisco", "California.LosAngles",
30
    "California.SanDiego", "California.SanJose",
31
    "California.PaloAlto", "California.Campbell",
32
    "California.MountainView", "California.Sunnyvale",
33
    "California.SantaClara", "California.Cupertino"};
34

35
#ifdef WINDOWS
36
    #define ssize_t int
37
    #if _MSC_VER >= 1910
38
        #include "benchLocations.h"
39
    #else
40
        #include "benchLocationsWin.h"
41
    #endif
42
#else
43
    #include "benchLocations.h"
44
#endif
45

46
int32_t funCount(int32_t min, int32_t max, int32_t step, int32_t loop) {
×
47
    if(step == 0) {
×
48
        step = 1;
×
49
    }
50

51
    int32_t range = abs(max - min);
×
52
    int32_t maxCnt = range / step;
×
53
    int32_t val = min + (loop % maxCnt) * step ;
×
54

55
    return val;
×
56
}
57

58
int32_t funSaw(int32_t min, int32_t max, int32_t period, int32_t loop) {
1,062,814✔
59
    if(period == 0) {
1,062,814✔
60
        period = 1;
1,062,814✔
61
    }
62
    int32_t range = abs(max - min);
1,062,814✔
63
    int32_t step = range / period;
1,062,814✔
64
    int32_t val = min + (loop % period) * step ;
1,062,814✔
65
    return val;
1,062,814✔
66
}
67

68
int32_t funSquare(int32_t min, int32_t max, int32_t period, int32_t loop) {
1,082,814✔
69
    if(period == 0) {
1,082,814✔
70
        period = 1;
1,082,814✔
71
    }
72
    int32_t change = (loop/period) % 2;
1,082,814✔
73
    if (change)
1,082,814✔
74
       return min;
541,406✔
75
    else
76
       return max;
541,408✔
77
}
78

79
int32_t funTriAngle(int32_t min, int32_t max, int32_t period, int32_t loop) {
×
80
    if(period == 0) {
×
81
        period = 1;
×
82
    }
83
    int32_t range = abs(max - min);
×
84
    int32_t change = (loop/period) % 2;
×
85
    int32_t step = range/period;
×
86
    int32_t cnt = 0;    
×
87
    if(change)
×
88
       cnt = period - loop % period;
×
89
    else
90
       cnt = loop % period;
×
91

92
    return min + cnt * step;
×
93
}
94

95

96
// calc expression value like 10*sin(x) + 100
97
float funValueFloat(Field *field, int32_t angle, int32_t loop) {
2,185,632✔
98
    float radian = ATOR(angle);
2,185,632✔
99
    float funVal = 0;
2,185,632✔
100

101
    if (field->funType == FUNTYPE_SIN)
2,185,632✔
102
       funVal = sin(radian);
1,122,818✔
103
    else if (field->funType == FUNTYPE_COS)
1,062,814✔
104
       funVal = cos(radian);
×
105
    else if (field->funType == FUNTYPE_COUNT)
1,062,814✔
106
       funVal = (float)funCount(field->min, field->max, field->step, loop);
×
107
    else if (field->funType == FUNTYPE_SAW)
1,062,814✔
108
       funVal = (float)funSaw(field->min, field->max, field->period, loop + field->offset );
1,062,814✔
109
    else if (field->funType == FUNTYPE_SQUARE)
×
110
       funVal = (float)funSquare(field->min, field->max, field->period, loop + field->offset);
×
111
    else if (field->funType == FUNTYPE_TRI)
×
112
       funVal = (float)funTriAngle(field->min, field->max, field->period, loop + field->offset);
×
113

114
    if(field->multiple != 0)
2,185,632✔
115
       funVal *= field->multiple;
2,185,632✔
116
    
117
    if ( field->addend !=0 && field->random > 0 ) {
2,185,632✔
118
        float rate = taosRandom() % field->random;
2,185,632✔
119
        funVal += field->addend * (rate/100);
2,185,632✔
120
    } else if(field->addend !=0 ) {
×
121
        funVal += field->addend;
×
122
    }
123

124
    funVal += field->base;
2,185,632✔
125
    return funVal;
2,185,632✔
126
}
127

128
// calc expression value like 10*sin(x) + 100
129
int32_t funValueInt32(Field *field, int32_t angle, int32_t loop) {
1,082,814✔
130
    float radian = ATOR(angle);
1,082,814✔
131
    int32_t funVal = 0;
1,082,814✔
132

133
    if (field->funType == FUNTYPE_SIN)
1,082,814✔
134
       funVal = (int32_t)sin(radian);
×
135
    else if (field->funType == FUNTYPE_COS)
1,082,814✔
136
       funVal = (int32_t)cos(radian);
×
137
    else if (field->funType == FUNTYPE_COUNT)
1,082,814✔
138
       funVal = funCount(field->min, field->max, field->step, loop);
×
139
    else if (field->funType == FUNTYPE_SAW)
1,082,814✔
140
       funVal = funSaw(field->min, field->max, field->period, loop + field->offset );
×
141
    else if (field->funType == FUNTYPE_SQUARE)
1,082,814✔
142
       funVal = funSquare(field->min, field->max, field->period, loop + field->offset);
1,082,814✔
143
    else if (field->funType == FUNTYPE_TRI)
×
144
       funVal = funTriAngle(field->min, field->max, field->period, loop + field->offset);
×
145

146
    if(field->multiple != 0)
1,082,814✔
147
       funVal *= field->multiple;
1,082,814✔
148
    
149
    if ( field->addend !=0 && field->random > 0 ) {
1,082,814✔
150
        float rate = taosRandom() % field->random;
1,082,814✔
151
        funVal += field->addend * (rate/100);
1,082,814✔
152
    } else if(field->addend !=0 ) {
×
153
        funVal += field->addend;
×
154
    }
155

156
    funVal += field->base;
1,082,814✔
157

158
    return funVal;
1,082,814✔
159
}
160

161

162
static int usc2utf8(char *p, int unic) {
1,945✔
163
    int ret = 0;
1,945✔
164
    if (unic <= 0x0000007F) {
1,945✔
165
        *p = (unic & 0x7F);
×
166
        ret = 1;
×
167
    } else if (unic <= 0x000007FF) {
1,945✔
168
        *(p + 1) = (unic & 0x3F) | 0x80;
×
169
        *p = ((unic >> 6) & 0x1F) | 0xC0;
×
170
        ret = 2;
×
171
    } else if (unic <= 0x0000FFFF) {
1,945✔
172
        *(p + 2) = (unic & 0x3F) | 0x80;
1,945✔
173
        *(p + 1) = ((unic >> 6) & 0x3F) | 0x80;
1,945✔
174
        *p = ((unic >> 12) & 0x0F) | 0xE0;
1,945✔
175
        ret = 3;
1,945✔
176
    } else if (unic <= 0x001FFFFF) {
×
177
        *(p + 3) = (unic & 0x3F) | 0x80;
×
178
        *(p + 2) = ((unic >> 6) & 0x3F) | 0x80;
×
179
        *(p + 1) = ((unic >> 12) & 0x3F) | 0x80;
×
180
        *p = ((unic >> 18) & 0x07) | 0xF0;
×
181
        ret = 4;
×
182
    } else if (unic <= 0x03FFFFFF) {
×
183
        *(p + 4) = (unic & 0x3F) | 0x80;
×
184
        *(p + 3) = ((unic >> 6) & 0x3F) | 0x80;
×
185
        *(p + 2) = ((unic >> 12) & 0x3F) | 0x80;
×
186
        *(p + 1) = ((unic >> 18) & 0x3F) | 0x80;
×
187
        *p = ((unic >> 24) & 0x03) | 0xF8;
×
188
        ret = 5;
×
189
    // } else if (unic >= 0x04000000) {
190
    } else {
191
        *(p + 5) = (unic & 0x3F) | 0x80;
×
192
        *(p + 4) = ((unic >> 6) & 0x3F) | 0x80;
×
193
        *(p + 3) = ((unic >> 12) & 0x3F) | 0x80;
×
194
        *(p + 2) = ((unic >> 18) & 0x3F) | 0x80;
×
195
        *(p + 1) = ((unic >> 24) & 0x3F) | 0x80;
×
196
        *p = ((unic >> 30) & 0x01) | 0xFC;
×
197
        ret = 6;
×
198
    }
199

200
    return ret;
1,945✔
201
}
202

203
void rand_string(char *str, int size, bool chinese) {
4,645,334✔
204
    if (chinese) {
4,645,334✔
205
        char *pstr = str;
428✔
206
        while (size > 0) {
2,373✔
207
            // Chinese Character need 3 bytes space
208
            if (size < 3) {
2,210✔
209
                break;
265✔
210
            }
211
            // Basic Chinese Character's Unicode is from 0x4e00 to 0x9fa5
212
            int unic = 0x4e00 + taosRandom() % (0x9fa5 - 0x4e00);
1,945✔
213
            int move = usc2utf8(pstr, unic);
1,945✔
214
            pstr += move;
1,945✔
215
            size -= move;
1,945✔
216
        }
217
    } else {
218
        str[0] = 0;
4,644,906✔
219
        if (size > 0) {
4,644,906✔
220
            // --size;
221
            int n;
222
            for (n = 0; n < size; n++) {
223,600,988✔
223
                int key = taosRandom() % (unsigned int)(sizeof(charset) - 1);
220,508,918✔
224
                str[n] = charset[key];
220,506,904✔
225
            }
226
            str[n] = 0;
3,092,070✔
227
        }
228
    }
229
}
4,643,320✔
230

231
// generate prepare sql
232
char* genPrepareSql(SSuperTable *stbInfo, char* tagData, uint64_t tableSeq) {
115✔
233
    int   len = 0;
115✔
234
    char *prepare = benchCalloc(1, TSDB_MAX_ALLOWED_SQL_LEN, true);
115✔
235
    int n;
236
    char *tagQ = NULL;
115✔
237
    char *colQ = genQMark(stbInfo->cols->size);
115✔
238
    bool  tagQFree = false;
115✔
239

240
    if(tagData == NULL) {
115✔
241
        // if no tagData , replace with QMark
242
        tagQ = genQMark(stbInfo->tags->size);
94✔
243
        tagQFree = true;
94✔
244
    } else {
245
        tagQ = tagData + stbInfo->lenOfTags * tableSeq;
21✔
246
    }
247

248
    if (stbInfo->autoTblCreating) {
115✔
249
        char ttl[SMALL_BUFF_LEN] = "";
21✔
250
        if (stbInfo->ttl != 0) {
21✔
251
            snprintf(ttl, SMALL_BUFF_LEN, "TTL %d", stbInfo->ttl);
×
252
        }
253
        n = snprintf(prepare + len,
21✔
254
                       TSDB_MAX_ALLOWED_SQL_LEN - len,
21✔
255
                       "INSERT INTO ? USING `%s` TAGS (%s) %s VALUES(?,%s)",
256
                       stbInfo->stbName, tagQ, ttl, colQ);
257
    } else {
258
        n = snprintf(prepare + len, TSDB_MAX_ALLOWED_SQL_LEN - len,
94✔
259
                        "INSERT INTO ? VALUES(?,%s)", colQ);
260
    }
261
    len += n;
115✔
262

263
    // free from genQMark
264
    if(tagQFree) {
115✔
265
        tmfree(tagQ);
94✔
266
    }
267
    tmfree(colQ);
115✔
268

269
    // check valid
270
    if (g_arguments->prepared_rand < g_arguments->reqPerReq) {
115✔
271
        infoPrint(
×
272
                  "in stmt mode, batch size(%u) can not larger than prepared "
273
                  "sample data size(%" PRId64
274
                  "), restart with larger prepared_rand or batch size will be "
275
                  "auto set to %" PRId64 "\n",
276
                  g_arguments->reqPerReq, g_arguments->prepared_rand,
277
                  g_arguments->prepared_rand);
278
        g_arguments->reqPerReq = g_arguments->prepared_rand;
×
279
    }
280

281
    return prepare;
115✔
282
}
283

284
int prepareStmt(TAOS_STMT *stmt, SSuperTable *stbInfo, char* tagData, uint64_t tableSeq) {
97✔
285
    char *prepare = genPrepareSql(stbInfo, tagData, tableSeq);
97✔
286
    if (taos_stmt_prepare(stmt, prepare, strlen(prepare))) {
98✔
287
        errorPrint("taos_stmt_prepare(%s) failed. errstr=%s\n", prepare, taos_stmt_errstr(stmt));
×
288
        tmfree(prepare);
×
289
        return -1;
×
290
    }
291
    tmfree(prepare);
98✔
292
    return 0;
98✔
293
}
294

295
int prepareStmt2(TAOS_STMT2 *stmt2, SSuperTable *stbInfo, char* tagData, uint64_t tableSeq) {
17✔
296
    char *prepare = genPrepareSql(stbInfo, tagData, tableSeq);
17✔
297
    if (taos_stmt2_prepare(stmt2, prepare, strlen(prepare))) {
17✔
298
        errorPrint("taos_stmt2_prepare(%s) failed. errstr=%s\n", prepare, taos_stmt2_error(stmt2));
×
299
        tmfree(prepare);
×
300
        return -1;
×
301
    }
302
    debugPrint("succ call taos_stmt2_prepare. sql=%s\n", prepare);
17✔
303
    tmfree(prepare);
17✔
304
    return 0;
17✔
305
}
306

307

308
static bool getSampleFileNameByPattern(char *filePath,
16✔
309
                                       SSuperTable *stbInfo,
310
                                       int64_t child) {
311
    char *pos = strstr(stbInfo->childTblSample, "XXXX");
16✔
312
    snprintf(filePath, MAX_PATH_LEN, "%s", stbInfo->childTblSample);
16✔
313
    int64_t offset = (int64_t)pos - (int64_t)stbInfo->childTblSample;
16✔
314
    snprintf(filePath + offset,
16✔
315
             MAX_PATH_LEN - offset,
16✔
316
            "%s",
317
            stbInfo->childTblArray[child]->name);
16✔
318
    size_t len = strlen(stbInfo->childTblArray[child]->name);
16✔
319
    snprintf(filePath + offset + len,
16✔
320
            MAX_PATH_LEN - offset - len,
16✔
321
            "%s", pos +4);
322
    return true;
16✔
323
}
324

325
static int generateSampleFromCsv(char *buffer, char* file, FILE* fp, int32_t length, int64_t size) {
24✔
326
    size_t  n = 0;
24✔
327
    char *  line = NULL;
24✔
328
    int     getRows = 0;
24✔
329
    bool    needClose = false;
24✔
330

331
    if (file != NULL && fp == NULL) {
24✔
332
        fp = fopen(file, "r");
12✔
333
        if (fp == NULL) {
12✔
334
            errorPrint("open csv file failed. file=%s\n", file);
×
335
            return -1;
×
336
        }
337
        needClose = true;
12✔
338
    }
339

340
    while (1) {
59,020✔
341
        ssize_t readLen = 0;
59,044✔
342
#if defined(WIN32) || defined(WIN64)
343
        toolsGetLineFile(&line, &n, fp);
344
        readLen = n;
345
        if (0 == readLen) {
346
#else
347
        readLen = getline(&line, &n, fp);
59,044✔
348
        if (-1 == readLen) {
59,044✔
349
#endif
350
            if (0 != fseek(fp, 0, SEEK_SET)) {
7,754✔
351
                errorPrint("Failed to fseek , reason:%s\n", strerror(errno));
×
352
                if(needClose)
×
353
                    fclose(fp);
×
354
                return -1;
×
355
            }
356
            continue;
7,754✔
357
        }
358

359
        int32_t pos = (int32_t)(readLen - 1);
51,290✔
360
        if(pos > 0) {
51,290✔
361
            if (('\r' == line[pos]) || ('\n' == line[pos])) {
51,290✔
362
                line[pos] = 0;
44,090✔
363
            }
364
        }
365
        pos = (int32_t)(readLen - 2);
51,290✔
366
        if(pos > 0) {
51,290✔
367
            if (('\r' == line[pos]) || ('\n' == line[pos])) {
51,282✔
368
                line[pos] = 0;
9,800✔
369
            }
370
        }
371

372
        if (readLen == 0) {
51,290✔
373
            continue;
×
374
        }
375

376
        int64_t offset = ((int64_t)getRows) * length;
51,290✔
377
        memcpy(buffer + offset, line, readLen + 1);
51,290✔
378
        getRows++;
51,290✔
379

380
        if (getRows == size) {
51,290✔
381
            break;
24✔
382
        }
383
    }
384

385
    if(needClose) {
24✔
386
        fclose(fp);
12✔
387
    }
388

389
    tmfree(line);
24✔
390
    infoPrint("read data from csv file %s, read rows=%d\n", file, getRows);
24✔
391
    return 0;
24✔
392
}
393

394
static int getAndSetRowsFromCsvFile(char *sampleFile, uint64_t *insertRows) {
10✔
395
    FILE *  fp = fopen(sampleFile, "r");
10✔
396
    if (NULL == fp) {
10✔
397
        errorPrint("Failed to open sample file: %s, reason:%s\n",
×
398
                   sampleFile, strerror(errno));
399
        return -1;
×
400
    }
401

402
    int     line_count = 0;
10✔
403
    char *  buf = NULL;
10✔
404

405
    buf = benchCalloc(1, TSDB_MAX_ALLOWED_SQL_LEN, false);
10✔
406
    if (NULL == buf) {
10✔
407
        errorPrint("%s() failed to allocate memory!\n", __func__);
×
408
        fclose(fp);
×
409
        return -1;
×
410
    }
411

412
    while (fgets(buf, TSDB_MAX_ALLOWED_SQL_LEN, fp)) {
96✔
413
        line_count++;
86✔
414
    }
415
    *insertRows = line_count;
10✔
416
    fclose(fp);
10✔
417
    tmfree(buf);
10✔
418
    return 0;
10✔
419
}
420

421
uint32_t accumulateRowLen(BArray *fields, int iface) {
533✔
422
    uint32_t len = 0;
533✔
423
    for (int i = 0; i < fields->size; ++i) {
3,513✔
424
        Field *field = benchArrayGet(fields, i);
2,982✔
425
        switch (field->type) {
2,982✔
426
            case TSDB_DATA_TYPE_BINARY:
747✔
427
            case TSDB_DATA_TYPE_VARBINARY:
428
            case TSDB_DATA_TYPE_GEOMETRY:
429
            case TSDB_DATA_TYPE_NCHAR:
430
                len += field->length + 3;
747✔
431
                break;
747✔
432
            case TSDB_DATA_TYPE_INT:
502✔
433
            case TSDB_DATA_TYPE_UINT:
434
                len += INT_BUFF_LEN;
502✔
435
                break;
502✔
436

437
            case TSDB_DATA_TYPE_BIGINT:
260✔
438
            case TSDB_DATA_TYPE_UBIGINT:
439
                len += BIGINT_BUFF_LEN;
260✔
440
                break;
260✔
441

442
            case TSDB_DATA_TYPE_SMALLINT:
260✔
443
            case TSDB_DATA_TYPE_USMALLINT:
444
                len += SMALLINT_BUFF_LEN;
260✔
445
                break;
260✔
446

447
            case TSDB_DATA_TYPE_TINYINT:
343✔
448
            case TSDB_DATA_TYPE_UTINYINT:
449
                len += TINYINT_BUFF_LEN;
343✔
450
                break;
343✔
451

452
            case TSDB_DATA_TYPE_BOOL:
183✔
453
                len += BOOL_BUFF_LEN;
183✔
454
                break;
183✔
455

456
            case TSDB_DATA_TYPE_FLOAT:
334✔
457
                len += FLOAT_BUFF_LEN;
334✔
458
                break;
334✔
459

460
            case TSDB_DATA_TYPE_DOUBLE:
306✔
461
                len += DOUBLE_BUFF_LEN;
306✔
462
                break;
306✔
463

464
            case TSDB_DATA_TYPE_TIMESTAMP:
45✔
465
                len += TIMESTAMP_BUFF_LEN;
45✔
466
                break;
45✔
467
            case TSDB_DATA_TYPE_JSON:
2✔
468
                len += field->length * fields->size;
2✔
469
                return len;
2✔
470
        }
471
        len += 1;
2,980✔
472
        if (iface == SML_REST_IFACE || iface == SML_IFACE) {
2,980✔
473
            len += SML_LINE_SQL_SYNTAX_OFFSET + strlen(field->name);
1,419✔
474
        }
475
    }
476
    if (iface == SML_IFACE || iface == SML_REST_IFACE) {
531✔
477
        len += 2 * TSDB_TABLE_NAME_LEN * 2 + SML_LINE_SQL_SYNTAX_OFFSET;
219✔
478
    }
479
    len += TIMESTAMP_BUFF_LEN;
531✔
480
    return len;
531✔
481
}
482

483

484
int tmpStr(char *tmp, int iface, Field *field, int64_t k) {
5,648,114✔
485
    if (field->values) {
5,648,114✔
486
        int arraySize = tools_cJSON_GetArraySize(field->values);
8,750✔
487
        if (arraySize) {
8,749✔
488
            tools_cJSON *buf = tools_cJSON_GetArrayItem(
8,750✔
489
                    field->values,
8,750✔
490
                    taosRandom() % arraySize);
8,749✔
491
            snprintf(tmp, field->length,
8,749✔
492
                     "%s", buf->valuestring);
493
        } else {
494
            errorPrint("%s() cannot read correct value "
×
495
                       "from json file. array size: %d\n",
496
                       __func__, arraySize);
497
            return -1;
×
498
        }
499
    } else if (g_arguments->demo_mode) {
5,639,364✔
500
        unsigned int tmpRand = taosRandom();
18,328✔
501
        if (g_arguments->chinese) {
18,328✔
502
            snprintf(tmp, field->length, "%s",
×
503
                     locations_chinese[tmpRand % 10]);
×
504
        } else if (SML_IFACE == iface) {
18,328✔
505
            snprintf(tmp, field->length, "%s",
25✔
506
                     locations_sml[tmpRand % 10]);
25✔
507
        } else {
508
            snprintf(tmp, field->length, "%s",
18,303✔
509
                     locations[tmpRand % 10]);
18,303✔
510
        }
511
    } else {
512
        if(field->gen == GEN_ORDER) {
5,621,036✔
513
            snprintf(tmp, field->length, "%"PRId64, k);
979,728✔
514
        } else {
515
            rand_string(tmp, taosRandom() % field->length, g_arguments->chinese);
4,641,308✔
516
        }
517
    }
518
    return 0;
5,649,620✔
519
}
520

521
int tmpGeometry(char *tmp, int iface, Field *field, int64_t k) {
×
522
    // values
523
    if (field->values) {
×
524
        int arraySize = tools_cJSON_GetArraySize(field->values);
×
525
        if (arraySize) {
×
526
            tools_cJSON *buf = tools_cJSON_GetArrayItem(
×
527
                    field->values,
×
528
                    taosRandom() % arraySize);
×
529
            snprintf(tmp, field->length,
×
530
                     "%s", buf->valuestring);
531
        } else {
532
            errorPrint("%s() cannot read correct value "
×
533
                       "from json file. array size: %d\n",
534
                       __func__, arraySize);
535
            return -1;
×
536
        }
537
        return 0;
×
538
    }
539

540
    // gen point count
541
    int32_t cnt = field->length / 24;
×
542
    if(cnt < 2) {
×
543
        snprintf(tmp, field->length, "POINT(%d %d)", tmpUint16(field), tmpUint16(field));
×
544
        return 0;
×
545
    }
546
    
547
    int32_t pos = snprintf(tmp, field->length, "LINESTRING(");
×
548
    char * format = "%d %d,";
×
549
    for(int32_t i = 0; i < cnt; i++) {
×
550
        if (i == cnt - 1) {
×
551
            format = "%d %d";
×
552
        } 
553
        pos += snprintf(tmp + pos, field->length - pos, format, tmpUint16(field), tmpUint16(field));
×
554
    }
555
    strcat(tmp, ")");
×
556

557
    return 0;
×
558
}
559

560
bool tmpBool(Field *field) {
1,219,763✔
561
    bool boolTmp;
562
    if (field->min == field->max) {
1,219,763✔
563
        boolTmp = (field->min)?1:0;
623✔
564
    } else {
565
        boolTmp = (taosRandom() % 2)&1;
1,219,140✔
566
    }
567
    return boolTmp;
1,220,470✔
568
}
569

570
int8_t tmpInt8Impl(Field *field, int64_t k) {
1,289,878✔
571
    int8_t tinyint = field->min;
1,289,878✔
572
    if (field->min != field->max) {
1,289,878✔
573
        tinyint += COL_GEN % (field->max - field->min);
1,289,439✔
574
    }
575
    return tinyint;
1,290,265✔
576
}
577

578
uint8_t tmpUint8Impl(Field *field, int64_t k) {
1,208,568✔
579
    uint8_t utinyint = field->min;
1,208,568✔
580
    if (field->min != field->max) {
1,208,568✔
581
        utinyint += (COL_GEN % (field->max - field->min));
1,207,989✔
582
    }
583
    return utinyint;
1,208,568✔
584
}
585

586
int16_t tmpInt16Impl(Field *field, int64_t k) {
1,208,847✔
587
    int16_t smallint = field->min;
1,208,847✔
588
    if (field->min != field->max) {
1,208,847✔
589
        smallint += (COL_GEN % (field->max - field->min));
1,208,530✔
590
    }
591
    return smallint;
1,209,363✔
592
}
593

594
uint16_t tmpUint16Impl(Field *field, int64_t k) {
1,208,302✔
595
    uint16_t usmallintTmp = field->min;
1,208,302✔
596
    if (field->max != field->min) {
1,208,302✔
597
        usmallintTmp += (COL_GEN % (field->max - field->min));
1,207,760✔
598
    }
599
    return usmallintTmp;
1,208,306✔
600
}
601

602
int tmpInt32Impl(Field *field, int i, int angle, int32_t k) {
4,332,787✔
603
    int intTmp;
604
    if (field->funType != FUNTYPE_NONE) {
4,332,787✔
605
        // calc from function
606
        intTmp = funValueInt32(field, angle, k);
1,082,814✔
607
    } else if ((g_arguments->demo_mode) && (i == 0)) {
3,249,973✔
608
        unsigned int tmpRand = taosRandom();
18,302✔
609
        intTmp = tmpRand % 10 + 1;
18,302✔
610
    } else if ((g_arguments->demo_mode) && (i == 1)) {
3,231,671✔
611
        intTmp = 105 + taosRandom() % 10;
×
612
    } else {
613
        if (field->min < (-1 * (RAND_MAX >> 1))) {
3,231,671✔
614
            field->min = -1 * (RAND_MAX >> 1);
×
615
        }
616
        if (field->max > (RAND_MAX >> 1)) {
3,231,671✔
617
            field->max = RAND_MAX >> 1;
×
618
        }
619
        intTmp = field->min;
3,231,671✔
620
        if (field->max != field->min) {
3,231,671✔
621
            intTmp += (COL_GEN % (field->max - field->min));
3,230,914✔
622
        }
623
    }
624
    return intTmp;
4,331,536✔
625
}
626

627
int tmpInt32ImplTag(Field *field, int i, int k) {
66✔
628
    int intTmp;
629

630
    if (field->min < (-1 * (RAND_MAX >> 1))) {
66✔
631
        field->min = -1 * (RAND_MAX >> 1);
×
632
    }
633
    if (field->max > (RAND_MAX >> 1)) {
66✔
634
        field->max = RAND_MAX >> 1;
×
635
    }
636
    intTmp = field->min;
66✔
637
    if (field->max != field->min) {
66✔
638
        intTmp += (COL_GEN % (field->max - field->min));
66✔
639
    }
640
    return intTmp;
66✔
641
}
642

643

644
uint32_t tmpUint32Impl(Field *field, int i, int angle, int64_t k) {
1,211,994✔
645
    uint32_t intTmp;
646
    if (field->funType != FUNTYPE_NONE) {
1,211,994✔
647
        // calc from function
648
        intTmp = funValueInt32(field, angle, k);
×
649
    } else if ((g_arguments->demo_mode) && (i == 0)) {
1,211,994✔
650
        unsigned int tmpRand = taosRandom();
×
651
        intTmp = tmpRand % 10 + 1;
×
652
    } else if ((g_arguments->demo_mode) && (i == 1)) {
1,211,994✔
653
        intTmp = 105 + taosRandom() % 10;
×
654
    } else {
655
        intTmp = field->min;
1,211,994✔
656
        if (field->max != field->min) {
1,211,994✔
657
            intTmp += (COL_GEN % (field->max - field->min));
1,211,218✔
658
        }
659
    }
660
    return intTmp;
1,211,996✔
661
}
662

663
int64_t tmpInt64Impl(Field *field, int32_t angle, int32_t k) {
1,208,984✔
664
    int64_t bigintTmp = field->min;
1,208,984✔
665
    if(field->funType != FUNTYPE_NONE) {
1,208,984✔
666
        bigintTmp = funValueInt32(field, angle, k);
×
667
    } else if (field->min != field->max) {
1,208,984✔
668
        bigintTmp += (COL_GEN % (field->max - field->min));
1,208,468✔
669
    }
670
    return bigintTmp;
1,208,991✔
671
}
672

673
uint64_t tmpUint64Impl(Field *field, int32_t angle, int64_t k) {
1,237,747✔
674
    uint64_t bigintTmp = field->min;
1,237,747✔
675
    if(field->funType != FUNTYPE_NONE) {
1,237,747✔
676
        bigintTmp = funValueInt32(field, angle, k);
×
677
    } else if (field->min != field->max) {
1,237,747✔
678
        bigintTmp += (COL_GEN % (field->max - field->min));
1,237,387✔
679
    }
680
    return bigintTmp;
1,237,758✔
681
}
682

683
float tmpFloatImpl(Field *field, int i, int32_t angle, int32_t k) {
3,827,582✔
684
    float floatTmp = (float)field->min;
3,827,582✔
685
    if(field->funType != FUNTYPE_NONE) {
3,827,582✔
686
        floatTmp = funValueFloat(field, angle, k);
2,185,632✔
687
    } else {
688
        if (field->max != field->min) {
1,641,950✔
689
            if (field->gen == GEN_ORDER) {
1,641,598✔
690
                floatTmp += (k % (field->max - field->min));
970,000✔
691
            } else {
692
                floatTmp += ((taosRandom() %
671,598✔
693
                        (field->max - field->min))
671,648✔
694
                    + (taosRandom() % 1000) / 1000.0);
671,648✔
695
            }
696
        }
697
        if (g_arguments->demo_mode && i == 0) {
1,642,001✔
698
            floatTmp = (float)(9.8 + 0.04 * (taosRandom() % 10)
×
699
                + floatTmp / 1000000000);
×
700
        } else if (g_arguments->demo_mode && i == 2) {
1,642,001✔
701
            floatTmp = (float)((105 + taosRandom() % 10
×
702
                + floatTmp / 1000000000) / 360);
×
703
        }
704
    }
705

706
    if (field->scalingFactor > 0) {
3,827,825✔
707
        if (field->scalingFactor > 1)
1,500,059✔
708
            floatTmp = floatTmp / field->scalingFactor;
209,970✔
709

710
        if (floatTmp > field->maxInDbl)
1,500,059✔
711
            floatTmp = field->maxInDbl;
×
712
        else if (floatTmp < field->minInDbl)
1,500,059✔
713
            floatTmp = field->minInDbl;
×
714
    }
715

716
    return floatTmp;
3,827,825✔
717
}
718

719
double tmpDoubleImpl(Field *field, int32_t angle, int32_t k) {
3,609,841✔
720
    double doubleTmp = (double)(field->min);
3,609,841✔
721
    if(field->funType != FUNTYPE_NONE) {
3,609,841✔
722
        doubleTmp = funValueFloat(field, angle, k);
×
723
    } else if (field->max != field->min) {
3,609,841✔
724
        if(field->gen == GEN_ORDER) {
3,609,255✔
725
            doubleTmp += k % (field->max - field->min);
969,322✔
726
        } else {
727
            doubleTmp += ((taosRandom() %
2,639,933✔
728
                (field->max - field->min)) +
5,279,802✔
729
                taosRandom() % 1000000 / 1000000.0);
2,639,945✔
730
        }
731
    }
732

733
    if (field->scalingFactor > 0) {
3,609,765✔
734
        if (field->scalingFactor > 1)
3,468,975✔
735
            doubleTmp = doubleTmp / field->scalingFactor;
89,967✔
736

737
        if (doubleTmp > field->maxInDbl)
3,468,975✔
738
            doubleTmp = field->maxInDbl;
×
739
        else if (doubleTmp < field->minInDbl)
3,468,975✔
740
            doubleTmp = field->minInDbl;
×
741
    }
742

743
    return doubleTmp;
3,609,765✔
744
}
745

746
static int tmpJson(char *sampleDataBuf,
900✔
747
                   int bufLen, int64_t pos,
748
                   int fieldsSize, Field *field) {
749
    int n = snprintf(sampleDataBuf + pos, bufLen - pos, "'{");
900✔
750
    if (n < 0 || n >= bufLen - pos) {
900✔
751
        errorPrint("%s() LN%d snprintf overflow\n",
×
752
                   __func__, __LINE__);
753
        return -1;
×
754
    }
755
    for (int j = 0; j < fieldsSize; ++j) {
1,800✔
756
        // key
757
        n += snprintf(sampleDataBuf + pos + n, bufLen - pos - n,
900✔
758
                        "\"k%d\":", j);
759
        if (n < 0 || n >= bufLen - pos) {
900✔
760
            errorPrint("%s() LN%d snprintf overflow\n",
×
761
                       __func__, __LINE__);
762
            return -1;
×
763
        }
764
        // value
765
        char *buf = benchCalloc(1, field->length + 1, false);
900✔
766
        rand_string(buf, 12, g_arguments->chinese);
900✔
767
        n += snprintf(sampleDataBuf + pos + n, bufLen - pos - n,
900✔
768
                        "\"%s\",", buf);
769
        if (n < 0 || n >= bufLen - pos) {
900✔
770
            errorPrint("%s() LN%d snprintf overflow\n",
×
771
                       __func__, __LINE__);
772
            tmfree(buf);
×
773
            return -1;
×
774
        }
775
        tmfree(buf);
900✔
776
    }
777
    n += snprintf(sampleDataBuf + pos + n - 1,
900✔
778
                    bufLen - pos - n, "}'");
900✔
779
    if (n < 0 || n >= bufLen - pos) {
900✔
780
        errorPrint("%s() LN%d snprintf overflow\n",
×
781
                   __func__, __LINE__);
782
        return -1;
×
783
    }
784

785
    return n;
900✔
786
}
787

788
static int generateRandDataSQL(SSuperTable *stbInfo, char *sampleDataBuf,
742✔
789
                     int64_t bufLen,
790
                      int lenOfOneRow, BArray * fields, int64_t loop,
791
                      bool tag) {
792

793
    int angle = stbInfo->startTimestamp % 360; // 0 ~ 360
742✔
794
    for (int64_t k = 0; k < loop; ++k) {
2,046,524✔
795
        int64_t pos = k * lenOfOneRow;
2,045,884✔
796
        int fieldsSize = fields->size;
2,045,884✔
797
        for (int i = 0; i < fieldsSize; ++i) {
12,379,478✔
798
            Field * field = benchArrayGet(fields, i);
10,334,596✔
799
            if (field->none) {
10,334,508✔
800
                continue;
21,940✔
801
            }
802

803
            int n = 0;
10,312,568✔
804
            if (field->null) {
10,312,568✔
805
                n = snprintf(sampleDataBuf + pos, bufLen - pos, "null,");
20✔
806
                if (n < 0 || n >= bufLen - pos) {
20✔
807
                    errorPrint("%s() LN%d snprintf overflow\n",
×
808
                               __func__, __LINE__);
809
                    return -1;
×
810
                } else {
811
                    pos += n;
20✔
812
                    continue;
20✔
813
                }
814
            }
815
            if (field->type == TSDB_DATA_TYPE_TIMESTAMP && !tag) {
10,312,548✔
816
                n = snprintf(sampleDataBuf + pos, bufLen - pos, "now,");
163,100✔
817
                if (n < 0 || n >= bufLen - pos) {
163,100✔
818
                    errorPrint("%s() LN%d snprintf overflow\n",
×
819
                               __func__, __LINE__);
820
                    return -1;
×
821
                } else {
822
                    pos += n;
163,100✔
823
                    continue;
163,100✔
824
                }
825
            }
826
            switch (field->type) {
10,149,448✔
827
                case TSDB_DATA_TYPE_BOOL: {
226,762✔
828
                    bool boolTmp = tmpBool(field);
226,762✔
829
                    n = snprintf(sampleDataBuf + pos, bufLen - pos,
226,764✔
830
                                    "%s,", boolTmp ? "true" : "false");
831
                    break;
226,764✔
832
                }
833
                case TSDB_DATA_TYPE_TINYINT: {
291,864✔
834
                    int8_t tinyint = tmpInt8Impl(field, k);
291,864✔
835
                    n = snprintf(sampleDataBuf + pos, bufLen - pos,
291,862✔
836
                                    "%d,", tinyint);
837
                    break;
291,862✔
838
                }
839
                case TSDB_DATA_TYPE_UTINYINT: {
216,349✔
840
                    uint8_t utinyint = tmpUint8Impl(field, k);
216,349✔
841
                    n = snprintf(sampleDataBuf + pos,
216,349✔
842
                                    bufLen - pos, "%u,", utinyint);
216,349✔
843
                    break;
216,349✔
844
                }
845
                case TSDB_DATA_TYPE_SMALLINT: {
216,549✔
846
                    int16_t smallint = tmpInt16Impl(field, k);
216,549✔
847
                    n = snprintf(sampleDataBuf + pos, bufLen - pos,
216,549✔
848
                                        "%d,", smallint);
849
                    break;
216,549✔
850
                }
851
                case TSDB_DATA_TYPE_USMALLINT: {
216,350✔
852
                    uint16_t usmallint = tmpUint16Impl(field, k);
216,350✔
853
                    n = snprintf(sampleDataBuf + pos, bufLen - pos,
216,350✔
854
                                        "%u,", usmallint);
855
                    break;
216,350✔
856
                }
857
                case TSDB_DATA_TYPE_INT: {
1,739,762✔
858
                    int32_t intTmp = tmpInt32Impl(field, i, angle, k);
1,739,762✔
859
                    n = snprintf(sampleDataBuf + pos, bufLen - pos,
1,739,764✔
860
                                        "%d,", intTmp);
861
                    break;
1,739,764✔
862
                }
863
                case TSDB_DATA_TYPE_BIGINT: {
216,570✔
864
                    int64_t bigintTmp = tmpInt64Impl(field, angle, k);
216,570✔
865
                    n = snprintf(sampleDataBuf + pos,
216,569✔
866
                                 bufLen - pos, "%"PRId64",", bigintTmp);
216,569✔
867
                    break;
216,569✔
868
                }
869
                case TSDB_DATA_TYPE_UINT: {
220,400✔
870
                    uint32_t uintTmp = tmpUint32Impl(field, i, angle, k);
220,400✔
871
                    n = snprintf(sampleDataBuf + pos,
220,400✔
872
                                 bufLen - pos, "%u,", uintTmp);
220,400✔
873
                    break;
220,400✔
874
                }
875
                case TSDB_DATA_TYPE_UBIGINT:
221,647✔
876
                case TSDB_DATA_TYPE_TIMESTAMP: {
877
                    uint64_t ubigintTmp = tmpUint64Impl(field, angle, k);
221,647✔
878
                    n = snprintf(sampleDataBuf + pos,
221,649✔
879
                                 bufLen - pos, "%"PRIu64",", ubigintTmp);
221,649✔
880
                    break;
221,649✔
881
                }
882
                case TSDB_DATA_TYPE_FLOAT: {
1,838,980✔
883
                    float floatTmp = tmpFloatImpl(field, i, angle, k);
1,838,980✔
884
                    n = snprintf(sampleDataBuf + pos, bufLen - pos,
1,838,980✔
885
                                        "%f,", floatTmp);
886
                    break;
1,838,980✔
887
                }
888
                case TSDB_DATA_TYPE_DOUBLE: {
1,817,360✔
889
                    double double_ =  tmpDoubleImpl(field, angle, k);
1,817,360✔
890
                    n = snprintf(sampleDataBuf + pos, bufLen - pos,
1,817,358✔
891
                                        "%f,", double_);
892
                    break;
1,817,358✔
893
                }
894
                case TSDB_DATA_TYPE_BINARY:
2,926,108✔
895
                case TSDB_DATA_TYPE_VARBINARY:
896
                case TSDB_DATA_TYPE_NCHAR: {
897
                    char *tmp = benchCalloc(1, field->length + 1, false);
2,926,108✔
898
                    if (0 != tmpStr(tmp, stbInfo->iface, field, k)) {
2,926,107✔
899
                        free(tmp);
×
900
                        return -1;
×
901
                    }
902
                    n = snprintf(sampleDataBuf + pos, bufLen - pos,
2,926,108✔
903
                                        "'%s',", tmp);
904
                    tmfree(tmp);
2,926,108✔
905
                    break;
2,926,108✔
906
                }
907
                case TSDB_DATA_TYPE_GEOMETRY: {
×
908
                    int   bufferSize = field->length + 1;
×
909
                    char *tmp = benchCalloc(1, bufferSize, false);
×
910
                    if (0 != tmpGeometry(tmp, stbInfo->iface, field, i)) {
×
911
                        free(tmp);
×
912
                        return -1;
×
913
                    }
914
                    n = snprintf(sampleDataBuf + pos, bufLen - pos, "'%s',", tmp);
×
915
                    tmfree(tmp);
×
916
                    break;
×
917
                }
918
                case TSDB_DATA_TYPE_JSON: {
900✔
919
                    n = tmpJson(sampleDataBuf, bufLen, pos, fieldsSize, field);
900✔
920
                    if (n == -1) {
900✔
921
                        return -1;
×
922
                    }
923
                    pos += n;
900✔
924
                    goto skip_sql;
900✔
925
                }
926
            }
927
            if (TSDB_DATA_TYPE_JSON != field->type) {
10,148,549✔
928
                if (n < 0 || n >= bufLen - pos) {
10,148,665✔
929
                    errorPrint("%s() LN%d snprintf overflow\n",
15✔
930
                               __func__, __LINE__);
931
                    return -1;
×
932
                } else {
933
                    pos += n;
10,148,650✔
934
                }
935
            }
936
        }
937
skip_sql:
2,044,882✔
938
        *(sampleDataBuf + pos - 1) = 0;
2,045,782✔
939
        angle += stbInfo->timestamp_step/stbInfo->angle_step;
2,045,782✔
940
        if (angle > 360) {
2,045,782✔
941
            angle -= 360;
171,078✔
942
        }
943
}
944

945
    return 0;
640✔
946
}
947

948
static int fillStmt(
120✔
949
    SSuperTable *stbInfo,
950
    char *sampleDataBuf,
951
    int64_t bufLen,
952
    int lenOfOneRow, BArray *fields,
953
    int64_t loop, bool tag, BArray *childCols) {
954
    int angle = stbInfo->startTimestamp % 360; // 0 ~ 360
120✔
955
    debugPrint("fillStml stbname=%s loop=%"PRId64" istag=%d  fieldsSize=%d\n", stbInfo->stbName, loop, tag, (int32_t)fields->size);
120✔
956
    for (int64_t k = 0; k < loop; ++k) {
304,451✔
957
        int64_t pos = k * lenOfOneRow;
304,329✔
958
        char* line = sampleDataBuf + pos;
304,329✔
959
        int fieldsSize = fields->size;
304,329✔
960
        for (int i = 0; i < fieldsSize; ++i) {
2,828,928✔
961
            Field * field = benchArrayGet(fields, i);
2,524,635✔
962
            ChildField *childCol = NULL;
2,524,575✔
963
            if (childCols) {
2,524,575✔
964
                childCol = benchArrayGet(childCols, i);
8,442✔
965
            }
966
            int64_t n = 0;
2,524,569✔
967

968
            // 
969
            if (childCol) {
2,524,569✔
970
                childCol->stmtData.is_null[k] = 0;
8,442✔
971
                childCol->stmtData.lengths[k] = field->length;
8,442✔
972
            } else {
973
                field->stmtData.is_null[k] = 0;
2,516,127✔
974
                field->stmtData.lengths[k] = field->length;
2,516,127✔
975
            }
976

977
            switch (field->type) {
2,524,569✔
978
                case TSDB_DATA_TYPE_BOOL: {
22,518✔
979
                    bool boolTmp = tmpBool(field);
22,518✔
980
                    if (childCol) {
22,518✔
981
                        ((bool *)childCol->stmtData.data)[k] = boolTmp;
×
982
                    } else {
983
                        ((bool *)field->stmtData.data)[k] = boolTmp;
22,518✔
984
                    }
985
                    n = snprintf(sampleDataBuf + pos, bufLen - pos,
22,518✔
986
                                 "%d,", boolTmp);
987
                    break;
22,518✔
988
                }
989
                case TSDB_DATA_TYPE_TINYINT: {
27,718✔
990
                    int8_t tinyintTmp = tmpInt8Impl(field, k);
27,718✔
991
                    if (childCol) {
27,719✔
992
                        ((int8_t *)childCol->stmtData.data)[k] = tinyintTmp;
×
993
                    } else {
994
                        ((int8_t *)field->stmtData.data)[k] = tinyintTmp;
27,719✔
995
                    }
996
                    n = snprintf(sampleDataBuf + pos, bufLen - pos,
27,719✔
997
                                 "%d,", tinyintTmp);
998
                    break;
27,719✔
999
                }
1000
                case TSDB_DATA_TYPE_UTINYINT: {
22,519✔
1001
                    uint8_t utinyintTmp = tmpUint8Impl(field, k);
22,519✔
1002
                    if (childCol) {
22,516✔
1003
                        ((uint8_t *)childCol->stmtData.data)[k] = utinyintTmp;
×
1004
                    } else {
1005
                        ((uint8_t *)field->stmtData.data)[k] = utinyintTmp;
22,516✔
1006
                    }
1007
                    n = snprintf(sampleDataBuf + pos,
22,516✔
1008
                                 bufLen - pos, "%u,", utinyintTmp);
22,516✔
1009
                    break;
22,516✔
1010
                }
1011
                case TSDB_DATA_TYPE_SMALLINT: {
22,518✔
1012
                    int16_t smallintTmp = tmpInt16Impl(field, k);
22,518✔
1013
                    if (childCol) {
22,519✔
1014
                        ((int16_t *)childCol->stmtData.data)[k] = smallintTmp;
×
1015
                    } else {
1016
                        ((int16_t *)field->stmtData.data)[k] = smallintTmp;
22,519✔
1017
                    }
1018
                    n = snprintf(sampleDataBuf + pos, bufLen - pos,
22,519✔
1019
                                        "%d,", smallintTmp);
1020
                    break;
22,519✔
1021
                }
1022
                case TSDB_DATA_TYPE_USMALLINT: {
22,515✔
1023
                    uint16_t usmallintTmp = tmpUint16Impl(field, k);
22,515✔
1024
                    if (childCol) {
22,516✔
1025
                        ((uint16_t *)childCol->stmtData.data)[k] = usmallintTmp;
×
1026
                    } else {
1027
                        ((uint16_t *)field->stmtData.data)[k] = usmallintTmp;
22,516✔
1028
                    }
1029
                    n = snprintf(sampleDataBuf + pos, bufLen - pos,
22,516✔
1030
                                        "%u,", usmallintTmp);
1031
                    break;
22,516✔
1032
                }
1033
                case TSDB_DATA_TYPE_INT: {
289,706✔
1034
                    int32_t intTmp = tmpInt32Impl(field, i, angle, k);
289,706✔
1035
                    if (childCol) {
289,725✔
1036
                        ((int32_t *)childCol->stmtData.data)[k] = intTmp;
2,814✔
1037
                    } else {
1038
                        ((int32_t *)field->stmtData.data)[k] = intTmp;
286,911✔
1039
                    }
1040
                    n = snprintf(sampleDataBuf + pos, bufLen - pos,
289,725✔
1041
                                        "%d,", intTmp);
1042
                    break;
289,725✔
1043
                }
1044
                case TSDB_DATA_TYPE_BIGINT: {
22,520✔
1045
                    int64_t bigintTmp = tmpInt64Impl(field, angle, k);
22,520✔
1046
                    if (childCol) {
22,520✔
1047
                        ((int64_t *)childCol->stmtData.data)[k] = bigintTmp;
×
1048
                    } else {
1049
                        ((int64_t *)field->stmtData.data)[k] = bigintTmp;
22,520✔
1050
                    }
1051
                    n = snprintf(sampleDataBuf + pos,
22,520✔
1052
                                 bufLen - pos, "%"PRId64",", bigintTmp);
22,520✔
1053
                    break;
22,520✔
1054
                }
1055
                case TSDB_DATA_TYPE_UINT: {
22,618✔
1056
                    uint32_t uintTmp = tmpUint32Impl(field, i, angle, k);
22,618✔
1057
                    if (childCol) {
22,620✔
1058
                        ((uint32_t *)childCol->stmtData.data)[k] = uintTmp;
×
1059
                    } else {
1060
                        ((uint32_t *)field->stmtData.data)[k] = uintTmp;
22,620✔
1061
                    }
1062
                    n = snprintf(sampleDataBuf + pos,
22,620✔
1063
                                 bufLen - pos, "%u,", uintTmp);
22,620✔
1064
                    break;
22,620✔
1065
                }
1066
                case TSDB_DATA_TYPE_UBIGINT:
46,818✔
1067
                case TSDB_DATA_TYPE_TIMESTAMP: {
1068
                    uint64_t ubigintTmp = tmpUint64Impl(field, angle, k);
46,818✔
1069
                    if (childCol) {
46,822✔
1070
                        ((uint64_t *)childCol->stmtData.data)[k] = ubigintTmp;
×
1071
                    } else {
1072
                        ((uint64_t *)field->stmtData.data)[k] = ubigintTmp;
46,822✔
1073
                    }
1074
                    n = snprintf(sampleDataBuf + pos,
46,822✔
1075
                                 bufLen - pos, "%"PRIu64",", ubigintTmp);
46,822✔
1076
                    break;
46,822✔
1077
                }
1078
                case TSDB_DATA_TYPE_FLOAT: {
418,148✔
1079
                    float floatTmp = tmpFloatImpl(field, i, angle, k);
418,148✔
1080
                    if (childCol) {
418,147✔
1081
                        ((float *)childCol->stmtData.data)[k] = floatTmp;
5,628✔
1082
                    } else {
1083
                        ((float *)field->stmtData.data)[k] = floatTmp;
412,519✔
1084
                    }
1085
                    n = snprintf(sampleDataBuf + pos, bufLen - pos,
418,147✔
1086
                                        "%f,", floatTmp);
1087
                    break;
418,147✔
1088
                }
1089
                case TSDB_DATA_TYPE_DOUBLE: {
822,515✔
1090
                    double doubleTmp = tmpDoubleImpl(field, angle, k);
822,515✔
1091
                    if (childCol) {
822,520✔
1092
                        ((double *)childCol->stmtData.data)[k] = doubleTmp;
×
1093
                    } else {
1094
                        ((double *)field->stmtData.data)[k] = doubleTmp;
822,520✔
1095
                    }
1096
                    n = snprintf(sampleDataBuf + pos, bufLen - pos,
822,520✔
1097
                                        "%f,", doubleTmp);
1098
                    break;
822,520✔
1099
                }
1100
                case TSDB_DATA_TYPE_BINARY:
784,602✔
1101
                case TSDB_DATA_TYPE_VARBINARY:
1102
                case TSDB_DATA_TYPE_NCHAR: {
1103
                    char *tmp = benchCalloc(1, field->length + 1, false);
784,602✔
1104
                    if (0 != tmpStr(tmp, stbInfo->iface, field, k)) {
784,611✔
1105
                        free(tmp);
×
1106
                        return -1;
×
1107
                    }
1108
                    if (childCol) {
784,619✔
1109
                        snprintf((char *)childCol->stmtData.data
×
1110
                                    + k * field->length,
×
1111
                                 field->length,
×
1112
                                "%s", tmp);
1113
                    } else {
1114
                        snprintf((char *)field->stmtData.data
784,619✔
1115
                                    + k * field->length,
784,619✔
1116
                                 field->length,
784,619✔
1117
                                "%s", tmp);
1118
                    }
1119
                    n = snprintf(sampleDataBuf + pos, bufLen - pos,
784,619✔
1120
                                        "'%s',", tmp);
1121
                    tmfree(tmp);
784,619✔
1122
                    break;
784,619✔
1123
                }
1124
                case TSDB_DATA_TYPE_GEOMETRY: {
×
1125
                    char *tmp = benchCalloc(1, field->length + 1, false);
×
1126
                    if (0 != tmpGeometry(tmp, stbInfo->iface, field, k)) {
×
1127
                        tmfree(tmp);
×
1128
                        return -1;
×
1129
                    }
1130
                    if (childCol) {
×
1131
                        snprintf((char *)childCol->stmtData.data
×
1132
                                    + k * field->length,
×
1133
                                 field->length,
×
1134
                                "%s", tmp);
1135
                    } else {
1136
                        snprintf((char *)field->stmtData.data
×
1137
                                    + k * field->length,
×
1138
                                 field->length,
×
1139
                                "%s", tmp);
1140
                    }
1141
                    n = snprintf(sampleDataBuf + pos, bufLen - pos,
×
1142
                                        "'%s',", tmp);
1143
                    tmfree(tmp);
×
1144
                    break;
×
1145
                }
1146
                case TSDB_DATA_TYPE_JSON: {
×
1147
                    n = tmpJson(sampleDataBuf, bufLen, pos, fieldsSize, field);
×
1148
                    if (n == -1) {
×
1149
                        return -1;
×
1150
                    }
1151
                    pos += n;
×
1152
                    goto skip_stmt;
×
1153
                }
1154
            }
1155
            if (TSDB_DATA_TYPE_JSON != field->type) {
2,524,615✔
1156
                if (n < 0 || n >= bufLen - pos) {
2,524,714✔
1157
                    errorPrint("%s() LN%d snprintf overflow\n",
16✔
1158
                               __func__, __LINE__);
1159
                    return -1;
×
1160
                } else {
1161
                    pos += n;
2,524,698✔
1162
                }
1163
            }
1164
        }
1165
        debugPrint(" k=%" PRId64 " pos=%" PRId64 " line=%s\n", k, pos, line);
304,293✔
1166

1167
skip_stmt:
304,293✔
1168
        if (pos > 0)
304,331✔
1169
            *(sampleDataBuf + pos - 1) = 0;
304,331✔
1170
        angle += stbInfo->timestamp_step/stbInfo->angle_step;
304,331✔
1171
        if (angle > 360) {
304,331✔
1172
            angle -= 360;
68,900✔
1173
        }
1174

1175
    }
1176
    return 0;
122✔
1177
}
1178

1179
static int generateRandDataStmtForChildTable(
6✔
1180
    SSuperTable *stbInfo,
1181
    char *sampleDataBuf,
1182
    int64_t bufLen,
1183
    int lenOfOneRow, BArray *fields,
1184
    int64_t loop, BArray *childCols) {
1185
    //  generateRandDataStmtForChildTable()
1186
    for (int i = 0; i < fields->size; ++i) {
24✔
1187
        Field *field = benchArrayGet(fields, i);
18✔
1188
        ChildField *childField = benchArrayGet(childCols, i);
18✔
1189
        if (field->type == TSDB_DATA_TYPE_BINARY
18✔
1190
                || field->type == TSDB_DATA_TYPE_NCHAR) {
18✔
1191
            childField->stmtData.data = benchCalloc(
×
1192
                        1, loop * (field->length + 1), true);
×
1193
        } else {
1194
            childField->stmtData.data = benchCalloc(
18✔
1195
                    1, loop * field->length, true);
18✔
1196
        }
1197

1198
        // is_null
1199
        childField->stmtData.is_null = benchCalloc(sizeof(char), loop, true);
18✔
1200
        // lengths
1201
        childField->stmtData.lengths = benchCalloc(sizeof(int32_t), loop, true);
18✔
1202

1203
        // log
1204
        debugPrint("i=%d generateRandDataStmtForChildTable fields=%p %s malloc stmtData.data=%p\n", i, fields, field->name ,field->stmtData.data);
18✔
1205

1206
    }
1207
    return fillStmt(
6✔
1208
        stbInfo,
1209
        sampleDataBuf,
1210
        bufLen,
1211
        lenOfOneRow, fields,
1212
        loop, false, childCols);
1213
}
1214

1215
static int generateRandDataStmt(
114✔
1216
    SSuperTable *stbInfo,
1217
    char *sampleDataBuf,
1218
    int64_t bufLen,
1219
    int lenOfOneRow, BArray *fields,
1220
    int64_t loop, bool tag) {
1221
    // generateRandDataStmt()
1222
    for (int i = 0; i < fields->size; ++i) {
881✔
1223
        Field *field = benchArrayGet(fields, i);
767✔
1224
        if (field->stmtData.data == NULL) {
767✔
1225
            // data
1226
            if (field->type == TSDB_DATA_TYPE_BINARY
276✔
1227
                    || field->type == TSDB_DATA_TYPE_NCHAR) {
196✔
1228
                field->stmtData.data = benchCalloc(1, loop * (field->length + 1), true);
86✔
1229
            } else {
1230
                field->stmtData.data = benchCalloc(1, loop * field->length, true);
190✔
1231
            }
1232

1233
            // is_null
1234
            field->stmtData.is_null = benchCalloc(sizeof(char), loop, true);
276✔
1235
            // lengths
1236
            field->stmtData.lengths = benchCalloc(sizeof(int32_t), loop, true);
276✔
1237

1238
            // log
1239
            debugPrint("i=%d generateRandDataStmt tag=%d fields=%p %s malloc stmtData.data=%p\n", i, tag, fields, field->name ,field->stmtData.data);
276✔
1240
        }
1241
    }
1242

1243
    return fillStmt(
114✔
1244
        stbInfo,
1245
        sampleDataBuf,
1246
        bufLen,
1247
        lenOfOneRow, fields,
1248
        loop, tag, NULL);
1249
}
1250

1251
static int generateRandDataSmlTelnet(SSuperTable *stbInfo, char *sampleDataBuf,
168✔
1252
                     int bufLen,
1253
                      int lenOfOneRow, BArray * fields, int64_t loop,
1254
                      bool tag) {
1255
    int angle = stbInfo->startTimestamp % 360; // 0 ~ 360
168✔
1256
    for (int64_t k = 0; k < loop; ++k) {
40,505✔
1257
        int64_t pos = k * lenOfOneRow;
40,337✔
1258
        int fieldsSize = fields->size;
40,337✔
1259
        if (!tag) {
40,337✔
1260
            fieldsSize = 1;
40,190✔
1261
        }
1262
        for (int i = 0; i < fieldsSize; ++i) {
82,414✔
1263
            Field * field = benchArrayGet(fields, i);
42,077✔
1264
            int n = 0;
42,077✔
1265
            switch (field->type) {
42,077✔
1266
                case TSDB_DATA_TYPE_BOOL: {
155✔
1267
                    bool boolTmp = tmpBool(field);
155✔
1268
                    if (tag) {
155✔
1269
                        n = snprintf(sampleDataBuf + pos, bufLen - pos,
145✔
1270
                                     "%s=%s ", field->name,
145✔
1271
                                        boolTmp ? "true" : "false");
1272
                    } else {
1273
                        n = snprintf(sampleDataBuf + pos, bufLen - pos,
10✔
1274
                                        "%s ", boolTmp ? "true" : "false");
1275
                    }
1276
                    break;
155✔
1277
                }
1278
                case TSDB_DATA_TYPE_TINYINT: {
155✔
1279
                    int8_t tinyint = tmpInt8Impl(field, k);
155✔
1280
                    if (tag) {
155✔
1281
                        n = snprintf(sampleDataBuf + pos, bufLen - pos,
145✔
1282
                                        "%s=%di8 ", field->name, tinyint);
145✔
1283
                    } else {
1284
                        n = snprintf(sampleDataBuf + pos,
10✔
1285
                                     bufLen - pos, "%di8 ", tinyint);
10✔
1286
                    }
1287
                    break;
155✔
1288
                }
1289
                case TSDB_DATA_TYPE_UTINYINT: {
155✔
1290
                    uint8_t utinyint = tmpUint8Impl(field, k);
155✔
1291
                    if (tag) {
155✔
1292
                        n = snprintf(sampleDataBuf + pos, bufLen - pos,
145✔
1293
                                        "%s=%uu8 ", field->name, utinyint);
145✔
1294
                    } else {
1295
                        n = snprintf(sampleDataBuf + pos,
10✔
1296
                                        bufLen - pos, "%uu8 ", utinyint);
10✔
1297
                    }
1298
                    break;
155✔
1299
                }
1300
                case TSDB_DATA_TYPE_SMALLINT: {
155✔
1301
                    int16_t smallint = tmpInt16Impl(field, k);
155✔
1302
                    if (tag) {
155✔
1303
                        n = snprintf(sampleDataBuf + pos, bufLen - pos,
145✔
1304
                                        "%s=%di16 ", field->name, smallint);
145✔
1305
                    } else {
1306
                        n = snprintf(sampleDataBuf + pos, bufLen - pos,
10✔
1307
                                        "%di16 ", smallint);
1308
                    }
1309
                    break;
155✔
1310
                }
1311
                case TSDB_DATA_TYPE_USMALLINT: {
155✔
1312
                    uint16_t usmallint = tmpUint16Impl(field, k);
155✔
1313
                    if (tag) {
155✔
1314
                        n = snprintf(sampleDataBuf + pos, bufLen - pos,
145✔
1315
                                        "%s=%uu16 ", field->name, usmallint);
145✔
1316
                    } else {
1317
                        n = snprintf(sampleDataBuf + pos, bufLen - pos,
10✔
1318
                                        "%uu16 ", usmallint);
1319
                    }
1320
                    break;
155✔
1321
                }
1322
                case TSDB_DATA_TYPE_INT: {
217✔
1323
                    int32_t intTmp = tmpInt32Impl(field, i, angle, k);
217✔
1324
                    if (tag) {
217✔
1325
                        n = snprintf(sampleDataBuf + pos, bufLen - pos,
147✔
1326
                                        "%s=%di32 ",
1327
                                        field->name, intTmp);
147✔
1328
                    } else {
1329
                        n = snprintf(sampleDataBuf + pos,
70✔
1330
                                        bufLen - pos,
70✔
1331
                                        "%di32 ", intTmp);
1332
                    }
1333
                    break;
217✔
1334
                }
1335
                case TSDB_DATA_TYPE_BIGINT: {
155✔
1336
                    int64_t bigintTmp = tmpInt64Impl(field, angle, k);
155✔
1337
                    if (tag) {
155✔
1338
                        n = snprintf(sampleDataBuf + pos, bufLen - pos,
145✔
1339
                                     "%s=%"PRId64"i64 ",
1340
                                     field->name, bigintTmp);
145✔
1341
                    } else {
1342
                        n = snprintf(sampleDataBuf + pos,
10✔
1343
                                     bufLen - pos, "%"PRId64"i64 ", bigintTmp);
10✔
1344
                    }
1345
                    break;
155✔
1346
                }
1347
                case TSDB_DATA_TYPE_UINT: {
155✔
1348
                    uint32_t uintTmp = tmpUint32Impl(field, i, angle, k);
155✔
1349
                    if (tag) {
155✔
1350
                        n = snprintf(sampleDataBuf + pos,
145✔
1351
                                        bufLen - pos,
145✔
1352
                                        "%s=%uu32 ",
1353
                                        field->name, uintTmp);
145✔
1354
                    } else {
1355
                        n = snprintf(sampleDataBuf + pos,
10✔
1356
                                        bufLen - pos,
10✔
1357
                                        "%uu32 ", uintTmp);
1358
                    }
1359
                    break;
155✔
1360
                }
1361
                case TSDB_DATA_TYPE_UBIGINT:
155✔
1362
                case TSDB_DATA_TYPE_TIMESTAMP: {
1363
                    uint64_t ubigintTmp = tmpUint64Impl(field, angle, k);
155✔
1364
                    if (tag) {
155✔
1365
                        n = snprintf(sampleDataBuf + pos, bufLen - pos,
145✔
1366
                                     "%s=%"PRIu64"u64 ",
1367
                                     field->name, ubigintTmp);
145✔
1368
                    } else {
1369
                        n = snprintf(sampleDataBuf + pos,
10✔
1370
                                     bufLen - pos, "%"PRIu64"u64 ", ubigintTmp);
10✔
1371
                    }
1372
                    break;
155✔
1373
                }
1374
                case TSDB_DATA_TYPE_FLOAT: {
40,155✔
1375
                    float floatTmp = tmpFloatImpl(field, i, angle, k);
40,155✔
1376
                    if (tag) {
40,155✔
1377
                        n = snprintf(sampleDataBuf + pos, bufLen - pos,
145✔
1378
                                        "%s=%ff32 ", field->name, floatTmp);
145✔
1379
                    } else {
1380
                        n = snprintf(sampleDataBuf + pos,
40,010✔
1381
                                     bufLen - pos, "%ff32 ", floatTmp);
40,010✔
1382
                    }
1383
                    break;
40,155✔
1384
                }
1385
                case TSDB_DATA_TYPE_DOUBLE: {
155✔
1386
                    double double_ = tmpDoubleImpl(field, angle, k);
155✔
1387
                    if (tag) {
155✔
1388
                        n = snprintf(sampleDataBuf + pos, bufLen - pos,
145✔
1389
                                        "%s=%ff64 ", field->name, double_);
145✔
1390
                    } else {
1391
                        n = snprintf(sampleDataBuf + pos, bufLen - pos,
10✔
1392
                                     "%ff64 ", double_);
1393
                    }
1394
                    break;
155✔
1395
                }
1396
                case TSDB_DATA_TYPE_BINARY:
310✔
1397
                case TSDB_DATA_TYPE_VARBINARY:
1398
                case TSDB_DATA_TYPE_NCHAR: {
1399
                    char *tmp = benchCalloc(1, field->length + 1, false);
310✔
1400
                    if (0 != tmpStr(tmp, stbInfo->iface, field, k)) {
310✔
1401
                        tmfree(tmp);
×
1402
                        return -1;
×
1403
                    }
1404
                    if (field->type == TSDB_DATA_TYPE_BINARY || field->type == TSDB_DATA_TYPE_VARBINARY) {
310✔
1405
                        if (tag) {
156✔
1406
                            n = snprintf(sampleDataBuf + pos, bufLen - pos,
146✔
1407
                                            "%s=L\"%s\" ",
1408
                                           field->name, tmp);
146✔
1409
                        } else {
1410
                            n = snprintf(sampleDataBuf + pos, bufLen - pos,
10✔
1411
                                            "\"%s\" ", tmp);
1412
                        }
1413
                        if (n < 0 || n >= bufLen - pos) {
156✔
1414
                            errorPrint("%s() LN%d snprintf overflow\n",
×
1415
                                       __func__, __LINE__);
1416
                            tmfree(tmp);
×
1417
                            return -1;
×
1418
                        } else {
1419
                            pos += n;
156✔
1420
                        }
1421
                    } else if (field->type == TSDB_DATA_TYPE_NCHAR) {
154✔
1422
                        if (tag) {
154✔
1423
                            n = snprintf(sampleDataBuf + pos, bufLen - pos,
144✔
1424
                                            "%s=L\"%s\" ",
1425
                                           field->name, tmp);
144✔
1426
                        } else {
1427
                            n = snprintf(sampleDataBuf + pos, bufLen - pos,
10✔
1428
                                         "L\"%s\" ", tmp);
1429
                        }
1430
                        if (n < 0 || n >= bufLen - pos) {
154✔
1431
                            errorPrint("%s() LN%d snprintf overflow\n",
×
1432
                                       __func__, __LINE__);
1433
                            tmfree(tmp);
×
1434
                            return -1;
×
1435
                        } else {
1436
                            pos += n;
154✔
1437
                        }
1438
                    }
1439
                    tmfree(tmp);
310✔
1440
                    break;
310✔
1441
                }
1442
                case TSDB_DATA_TYPE_GEOMETRY: {
×
1443
                    char *tmp = benchCalloc(1, field->length + 1, false);
×
1444
                    if (0 != tmpGeometry(tmp, stbInfo->iface, field, k)) {
×
1445
                        tmfree(tmp);
×
1446
                        return -1;
×
1447
                    }
1448
                    if (tag) {
×
1449
                        n = snprintf(sampleDataBuf + pos, bufLen - pos,
×
1450
                                        "%s=L\"%s\" ",
1451
                                        field->name, tmp);
×
1452
                    } else {
1453
                        n = snprintf(sampleDataBuf + pos, bufLen - pos,
×
1454
                                        "\"%s\" ", tmp);
1455
                    }
1456
                    if (n < 0 || n >= bufLen - pos) {
×
1457
                        errorPrint("%s() LN%d snprintf overflow\n",
×
1458
                                    __func__, __LINE__);
1459
                        tmfree(tmp);
×
1460
                        return -1;
×
1461
                    } else {
1462
                        pos += n;
×
1463
                    }
1464
                    tmfree(tmp);
×
1465
                    break;
×
1466
                }
1467
                case TSDB_DATA_TYPE_JSON: {
×
1468
                    n = tmpJson(sampleDataBuf, bufLen, pos, fieldsSize, field);
×
1469
                    if (n == -1) {
×
1470
                        return -1;
×
1471
                    }
1472
                    pos += n;
×
1473
                    goto skip_telnet;
×
1474
                }
1475
            }
1476
            if (TSDB_DATA_TYPE_JSON != field->type) {
42,077✔
1477
                if (n < 0 || n >= bufLen - pos) {
42,077✔
1478
                    errorPrint("%s() LN%d snprintf overflow\n",
×
1479
                               __func__, __LINE__);
1480
                    return -1;
×
1481
                } else {
1482
                    pos += n;
42,077✔
1483
                }
1484
            }
1485
        }
1486
skip_telnet:
40,337✔
1487
        *(sampleDataBuf + pos - 1) = 0;
40,337✔
1488
        angle += stbInfo->timestamp_step/stbInfo->angle_step;
40,337✔
1489
        if (angle > 360) {
40,337✔
1490
            angle -= 360;
130✔
1491
        }
1492

1493
    }
1494

1495
    return 0;
168✔
1496
}
1497

1498
static int generateRandDataSmlJson(SSuperTable *stbInfo, char *sampleDataBuf,
36✔
1499
                     int bufLen,
1500
                      int lenOfOneRow, BArray * fields, int64_t loop,
1501
                      bool tag) {
1502
    int angle = stbInfo->startTimestamp % 360; // 0 ~ 360
36✔
1503
    for (int64_t k = 0; k < loop; ++k) {
80,356✔
1504
        int64_t pos = k * lenOfOneRow;
80,320✔
1505
        int fieldsSize = fields->size;
80,320✔
1506
        for (int i = 0; i < fieldsSize; ++i) {
320,640✔
1507
            Field * field = benchArrayGet(fields, i);
240,320✔
1508
            int n = 0;
240,320✔
1509
            switch (field->type) {
240,320✔
1510
                case TSDB_DATA_TYPE_BOOL: {
30✔
1511
                    bool boolTmp = tmpBool(field);
30✔
1512
                    n = snprintf(sampleDataBuf + pos, bufLen - pos,
30✔
1513
                                    "%s,", boolTmp ? "true" : "false");
1514
                    break;
30✔
1515
                }
1516
                case TSDB_DATA_TYPE_TINYINT: {
30✔
1517
                    int8_t tinyint = tmpInt8Impl(field, k);
30✔
1518
                    n = snprintf(sampleDataBuf + pos, bufLen - pos,
30✔
1519
                                        "%d,", tinyint);
1520
                    break;
30✔
1521
                }
1522
                case TSDB_DATA_TYPE_UTINYINT: {
×
1523
                    uint8_t utinyint = tmpUint8Impl(field, k);
×
1524
                    n = snprintf(sampleDataBuf + pos,
×
1525
                                        bufLen - pos, "%u,", utinyint);
×
1526
                    break;
×
1527
                }
1528
                case TSDB_DATA_TYPE_SMALLINT: {
30✔
1529
                    int16_t smallint = tmpInt16Impl(field, k);
30✔
1530
                    n = snprintf(sampleDataBuf + pos, bufLen - pos,
30✔
1531
                                        "%d,", smallint);
1532
                    break;
30✔
1533
                }
1534
                case TSDB_DATA_TYPE_USMALLINT: {
×
1535
                    uint16_t usmallint = tmpUint16Impl(field, k);
×
1536
                    n = snprintf(sampleDataBuf + pos, bufLen - pos,
×
1537
                                        "%u,", usmallint);
1538
                    break;
×
1539
                }
1540
                case TSDB_DATA_TYPE_INT: {
80,070✔
1541
                    int32_t intTmp = tmpInt32Impl(field, i, angle, k);
80,070✔
1542
                    n = snprintf(sampleDataBuf + pos,
80,070✔
1543
                                 bufLen - pos, "%d,", intTmp);
80,070✔
1544
                    break;
80,070✔
1545
                }
1546
                case TSDB_DATA_TYPE_BIGINT: {
30✔
1547
                    int64_t bigintTmp = tmpInt64Impl(field, angle, k);
30✔
1548
                    n = snprintf(sampleDataBuf + pos,
30✔
1549
                                 bufLen - pos, "%"PRId64",", bigintTmp);
30✔
1550
                    break;
30✔
1551
                }
1552
                case TSDB_DATA_TYPE_UINT: {
×
1553
                    uint32_t uintTmp = tmpUint32Impl(field, i, angle, k);
×
1554
                    n = snprintf(sampleDataBuf + pos,
×
1555
                                 bufLen - pos, "%u,", uintTmp);
×
1556
                    break;
×
1557
                }
1558
                case TSDB_DATA_TYPE_UBIGINT:
×
1559
                case TSDB_DATA_TYPE_TIMESTAMP: {
1560
                    uint64_t ubigintTmp = tmpUint64Impl(field, angle, k);
×
1561
                    n = snprintf(sampleDataBuf + pos,
×
1562
                                 bufLen - pos, "%"PRIu64",", ubigintTmp);
×
1563
                    break;
×
1564
                }
1565
                case TSDB_DATA_TYPE_FLOAT: {
160,030✔
1566
                    float floatTmp = tmpFloatImpl(field, i, angle, k);
160,030✔
1567
                    n = snprintf(sampleDataBuf + pos, bufLen - pos,
160,030✔
1568
                                        "%f,", floatTmp);
1569
                    break;
160,030✔
1570
                }
1571
                case TSDB_DATA_TYPE_DOUBLE: {
30✔
1572
                    double double_ = tmpDoubleImpl(field, angle, k);
30✔
1573
                    n = snprintf(sampleDataBuf + pos, bufLen - pos,
30✔
1574
                                        "%f,", double_);
1575
                    break;
30✔
1576
                }
1577
                case TSDB_DATA_TYPE_BINARY:
70✔
1578
                case TSDB_DATA_TYPE_VARBINARY:
1579
                case TSDB_DATA_TYPE_NCHAR: {
1580
                    char *tmp = benchCalloc(1, field->length + 1, false);
70✔
1581
                    if (0 != tmpStr(tmp, stbInfo->iface, field, k)) {
70✔
1582
                        free(tmp);
×
1583
                        return -1;
×
1584
                    }
1585
                    n = snprintf(sampleDataBuf + pos, bufLen - pos,
70✔
1586
                                        "'%s',", tmp);
1587
                    tmfree(tmp);
70✔
1588
                    break;
70✔
1589
                }
1590
                case TSDB_DATA_TYPE_JSON: {
×
1591
                    n = tmpJson(sampleDataBuf, bufLen, pos, fieldsSize, field);
×
1592
                    if (n == -1) {
×
1593
                        return -1;
×
1594
                    }
1595
                    pos += n;
×
1596
                    goto skip_json;
×
1597
                }
1598
            }
1599
            if (TSDB_DATA_TYPE_JSON != field->type) {
240,320✔
1600
                if (n < 0 || n >= bufLen - pos) {
240,320✔
1601
                    errorPrint("%s() LN%d snprintf overflow\n",
×
1602
                               __func__, __LINE__);
1603
                    return -1;
×
1604
                } else {
1605
                    pos += n;
240,320✔
1606
                }
1607
            }
1608
        }
1609
skip_json:
80,320✔
1610
        *(sampleDataBuf + pos - 1) = 0;
80,320✔
1611
        angle += stbInfo->timestamp_step/stbInfo->angle_step;
80,320✔
1612
        if (angle > 360) {
80,320✔
1613
            angle -= 360;
224✔
1614
        }
1615
    }
1616

1617
    return 0;
36✔
1618
}
1619

1620
static int generateRandDataSmlLine(SSuperTable *stbInfo, char *sampleDataBuf,
81✔
1621
                     int bufLen,
1622
                      int lenOfOneRow, BArray * fields, int64_t loop,
1623
                      bool tag) {
1624
    int angle = stbInfo->startTimestamp % 360; // 0 ~ 360                    
81✔
1625
    for (int64_t k = 0; k < loop; ++k) {
200,377✔
1626
        int64_t pos = k * lenOfOneRow;
200,296✔
1627
        int n = 0;
200,296✔
1628
        if (tag) {
200,296✔
1629
            n = snprintf(sampleDataBuf + pos,
66✔
1630
                           bufLen - pos,
66✔
1631
                           "%s,", stbInfo->stbName);
1632
            if (n < 0 || n >= bufLen - pos) {
66✔
1633
                errorPrint("%s() LN%d snprintf overflow\n",
×
1634
                           __func__, __LINE__);
1635
                return -1;
×
1636
            } else {
1637
                pos += n;
66✔
1638
            }
1639
        }
1640

1641
        int fieldsSize = fields->size;
200,296✔
1642
        for (int i = 0; i < fieldsSize; ++i) {
803,858✔
1643
            Field * field = benchArrayGet(fields, i);
603,562✔
1644
            switch (field->type) {
603,562✔
1645
                case TSDB_DATA_TYPE_BOOL: {
270✔
1646
                    bool boolTmp = tmpBool(field);
270✔
1647
                    n = snprintf(sampleDataBuf + pos, bufLen - pos, "%s=%s,",
270✔
1648
                                 field->name, boolTmp ? "true" : "false");
270✔
1649
                    break;
270✔
1650
                }
1651
                case TSDB_DATA_TYPE_TINYINT: {
270✔
1652
                    int8_t tinyint = tmpInt8Impl(field, k);
270✔
1653
                    n = snprintf(sampleDataBuf + pos, bufLen - pos,
270✔
1654
                                    "%s=%di8,", field->name, tinyint);
270✔
1655
                    break;
270✔
1656
                }
1657
                case TSDB_DATA_TYPE_UTINYINT: {
270✔
1658
                    uint8_t utinyint = tmpUint8Impl(field, k);
270✔
1659
                    n = snprintf(sampleDataBuf + pos, bufLen - pos,
270✔
1660
                                    "%s=%uu8,", field->name, utinyint);
270✔
1661
                    break;
270✔
1662
                }
1663
                case TSDB_DATA_TYPE_SMALLINT: {
270✔
1664
                    int16_t smallint = tmpInt16Impl(field, k);
270✔
1665
                    n = snprintf(sampleDataBuf + pos, bufLen - pos,
270✔
1666
                                    "%s=%di16,", field->name, smallint);
270✔
1667
                    break;
270✔
1668
                }
1669
                case TSDB_DATA_TYPE_USMALLINT: {
270✔
1670
                    uint16_t usmallint = tmpUint16Impl(field, k);
270✔
1671
                    n = snprintf(sampleDataBuf + pos, bufLen - pos,
270✔
1672
                                    "%s=%uu16,",
1673
                                    field->name, usmallint);
270✔
1674
                    break;
270✔
1675
                }
1676
                case TSDB_DATA_TYPE_INT: {
200,296✔
1677
                    int32_t intTmp;
1678
                    if (tag) {
200,296✔
1679
                        intTmp = tmpInt32ImplTag(field, i, k);
66✔
1680
                    } else {
1681
                        intTmp = tmpInt32Impl(field, i, angle, k);
200,230✔
1682
                    }                    
1683
                    n = snprintf(sampleDataBuf + pos, bufLen - pos,
200,296✔
1684
                                    "%s=%di32,",
1685
                                    field->name, intTmp);
200,296✔
1686
                    break;
200,296✔
1687
                }
1688
                case TSDB_DATA_TYPE_BIGINT: {
270✔
1689
                    int64_t bigintTmp = tmpInt64Impl(field, angle, k);
270✔
1690
                    n = snprintf(sampleDataBuf + pos, bufLen - pos,
270✔
1691
                                 "%s=%"PRId64"i64,", field->name, bigintTmp);
270✔
1692
                    break;
270✔
1693
                }
1694
                case TSDB_DATA_TYPE_UINT: {
270✔
1695
                    uint32_t uintTmp = tmpUint32Impl(field, i, angle, k);
270✔
1696
                    n = snprintf(sampleDataBuf + pos, bufLen - pos,
270✔
1697
                                    "%s=%uu32,", field->name, uintTmp);
270✔
1698
                    break;
270✔
1699
                }
1700
                case TSDB_DATA_TYPE_UBIGINT:
270✔
1701
                case TSDB_DATA_TYPE_TIMESTAMP: {
1702
                    uint64_t ubigintTmp = tmpUint64Impl(field, angle, k);
270✔
1703
                    n = snprintf(sampleDataBuf + pos, bufLen - pos,
270✔
1704
                                 "%s=%"PRIu64"u64,", field->name, ubigintTmp);
270✔
1705
                    break;
270✔
1706
                }
1707
                case TSDB_DATA_TYPE_FLOAT: {
400,270✔
1708
                    float floatTmp = tmpFloatImpl(field, i, angle, k);
400,270✔
1709
                    n = snprintf(sampleDataBuf + pos,
400,270✔
1710
                                    bufLen - pos, "%s=%ff32,",
400,270✔
1711
                                    field->name, floatTmp);
400,270✔
1712
                    break;
400,270✔
1713
                }
1714
                case TSDB_DATA_TYPE_DOUBLE: {
270✔
1715
                    double doubleTmp = tmpDoubleImpl(field, angle, k);
270✔
1716
                    n = snprintf(sampleDataBuf + pos, bufLen - pos,
270✔
1717
                                 "%s=%ff64,", field->name, doubleTmp);
270✔
1718
                    break;
270✔
1719
                }
1720
                case TSDB_DATA_TYPE_BINARY:
566✔
1721
                case TSDB_DATA_TYPE_VARBINARY:
1722
                case TSDB_DATA_TYPE_NCHAR: {
1723
                    char *tmp = benchCalloc(1, field->length + 1, false);
566✔
1724
                    if (0 != tmpStr(tmp, stbInfo->iface, field, k)) {
566✔
1725
                        free(tmp);
×
1726
                        return -1;
×
1727
                    }
1728
                    if (field->type == TSDB_DATA_TYPE_BINARY) {
566✔
1729
                        n = snprintf(sampleDataBuf + pos, bufLen - pos,
296✔
1730
                                        "%s=\"%s\",",
1731
                                       field->name, tmp);
296✔
1732
                    } else if (field->type == TSDB_DATA_TYPE_NCHAR) {
270✔
1733
                        n = snprintf(sampleDataBuf + pos, bufLen - pos,
270✔
1734
                                        "%s=L\"%s\",",
1735
                                       field->name, tmp);
270✔
1736
                    }
1737
                    tmfree(tmp);
566✔
1738
                    break;
566✔
1739
                }
1740
                case TSDB_DATA_TYPE_GEOMETRY: {
×
1741
                    char *tmp = benchCalloc(1, field->length + 1, false);
×
1742
                    if (0 != tmpGeometry(tmp, stbInfo->iface, field, k)) {
×
1743
                        tmfree(tmp);
×
1744
                        return -1;
×
1745
                    }
1746
                    n = snprintf(sampleDataBuf + pos, bufLen - pos,
×
1747
                                    "%s=\"%s\",",
1748
                                    field->name, tmp);
×
1749
                    tmfree(tmp);
×
1750
                    break;
×
1751
                }
1752
                case TSDB_DATA_TYPE_JSON: {
×
1753
                    n = tmpJson(sampleDataBuf, bufLen, pos,
×
1754
                                   fieldsSize, field);
1755
                    if (n < 0 || n >= bufLen) {
×
1756
                        errorPrint("%s() LN%d snprintf overflow\n",
×
1757
                                   __func__, __LINE__);
1758
                        return -1;
×
1759
                    } else {
1760
                        pos += n;
×
1761
                    }
1762
                    goto skip_line;
×
1763
                }
1764
            }
1765
            if (TSDB_DATA_TYPE_JSON != field->type) {
603,562✔
1766
                if (n < 0 || n >= bufLen - pos) {
603,562✔
1767
                    errorPrint("%s() LN%d snprintf overflow\n",
×
1768
                               __func__, __LINE__);
1769
                    return -1;
×
1770
                } else {
1771
                    pos += n;
603,562✔
1772
                }
1773
            }
1774
        }
1775
skip_line:
200,296✔
1776
        *(sampleDataBuf + pos - 1) = 0;
200,296✔
1777
        angle += stbInfo->timestamp_step/stbInfo->angle_step;
200,296✔
1778
        if (angle > 360) {
200,296✔
1779
            angle -= 360;
560✔
1780
        }
1781

1782
    }
1783

1784
    return 0;
81✔
1785
}
1786

1787
static int generateRandDataSml(SSuperTable *stbInfo, char *sampleDataBuf,
285✔
1788
                     int64_t bufLen,
1789
                      int lenOfOneRow, BArray * fields, int64_t loop,
1790
                      bool tag) {
1791
    int     protocol = stbInfo->lineProtocol;
285✔
1792

1793
    switch (protocol) {
285✔
1794
        case TSDB_SML_LINE_PROTOCOL:
81✔
1795
            return generateRandDataSmlLine(stbInfo, sampleDataBuf,
81✔
1796
                                    bufLen, lenOfOneRow, fields, loop, tag);
1797
        case TSDB_SML_TELNET_PROTOCOL:
168✔
1798
            return generateRandDataSmlTelnet(stbInfo, sampleDataBuf,
168✔
1799
                                    bufLen, lenOfOneRow, fields, loop, tag);
1800
        default:
36✔
1801
            return generateRandDataSmlJson(stbInfo, sampleDataBuf,
36✔
1802
                                    bufLen, lenOfOneRow, fields, loop, tag);
1803
    }
1804

1805
    return -1;
1806
}
1807

1808
int generateRandData(SSuperTable *stbInfo, char *sampleDataBuf,
1,147✔
1809
                     int64_t bufLen,
1810
                     int lenOfOneRow, BArray *fields,
1811
                     int64_t loop,
1812
                     bool tag, BArray *childCols) {
1813
    int     iface = stbInfo->iface;
1,147✔
1814
    switch (iface) {
1,147✔
1815
        case TAOSC_IFACE:
742✔
1816
        case REST_IFACE:
1817
            return generateRandDataSQL(stbInfo, sampleDataBuf,
742✔
1818
                                    bufLen, lenOfOneRow, fields, loop, tag);
1819
        case STMT_IFACE:
120✔
1820
        case STMT2_IFACE:
1821
            if (childCols) {
120✔
1822
                return generateRandDataStmtForChildTable(stbInfo,
6✔
1823
                                                         sampleDataBuf,
1824
                                    bufLen, lenOfOneRow, fields, loop,
1825
                                                         childCols);
1826
            } else {
1827
                return generateRandDataStmt(stbInfo, sampleDataBuf,
114✔
1828
                                    bufLen, lenOfOneRow, fields, loop, tag);
1829
            }
1830
        case SML_IFACE:
285✔
1831
        case SML_REST_IFACE:
1832
            return generateRandDataSml(stbInfo, sampleDataBuf,
285✔
1833
                                    bufLen, lenOfOneRow, fields, loop, tag);
1834
        default:
×
1835
            errorPrint("Unknown iface: %d\n", iface);
×
1836
            break;
×
1837
    }
1838

1839
    return -1;
×
1840
}
1841

1842
static BArray *initChildCols(int colsSize) {
8✔
1843
    BArray *childCols = benchArrayInit(colsSize,
8✔
1844
                                       sizeof(ChildField));
1845
    for (int col = 0; col < colsSize; col++) {
28✔
1846
        ChildField *childCol = benchCalloc(
20✔
1847
                1, sizeof(ChildField), true);
1848
        benchArrayPush(childCols, childCol);
20✔
1849
    }
1850
    return childCols;
8✔
1851
}
1852

1853
int prepareSampleData(SDataBase* database, SSuperTable* stbInfo) {
229✔
1854
    stbInfo->lenOfCols = accumulateRowLen(stbInfo->cols, stbInfo->iface);
229✔
1855
    stbInfo->lenOfTags = accumulateRowLen(stbInfo->tags, stbInfo->iface);
229✔
1856
    if (stbInfo->partialColNum != 0
229✔
1857
            && ((stbInfo->iface == TAOSC_IFACE
11✔
1858
                || stbInfo->iface == REST_IFACE))) {
1✔
1859
        // check valid
1860
        if(stbInfo->partialColFrom >= stbInfo->cols->size) {
10✔
1861
            stbInfo->partialColFrom = 0;
×
1862
            infoPrint("stbInfo->partialColFrom(%d) is large than stbInfo->cols->size(%"PRIu64") \n ",stbInfo->partialColFrom,stbInfo->cols->size);
×
1863
        }
1864

1865
        if (stbInfo->partialColFrom + stbInfo->partialColNum > stbInfo->cols->size) {
10✔
1866
            stbInfo->partialColNum = stbInfo->cols->size - stbInfo->partialColFrom ;
4✔
1867
        }
1868

1869
        if(stbInfo->partialColNum < stbInfo->cols->size) {
10✔
1870
            stbInfo->partialColNameBuf =
6✔
1871
                    benchCalloc(1, TSDB_MAX_ALLOWED_SQL_LEN, true);
6✔
1872
            int pos = 0;
6✔
1873
            int n;
1874
            n = snprintf(stbInfo->partialColNameBuf + pos,
6✔
1875
                            TSDB_MAX_ALLOWED_SQL_LEN - pos,
6✔
1876
                            TS_COL_NAME);
1877
            if (n < 0 || n > TSDB_MAX_ALLOWED_SQL_LEN - pos) {
6✔
1878
                errorPrint("%s() LN%d snprintf overflow\n",
×
1879
                           __func__, __LINE__);
1880
            } else {
1881
                pos += n;
6✔
1882
            }
1883
            for (int i = stbInfo->partialColFrom; i < stbInfo->partialColFrom + stbInfo->partialColNum; ++i) {
26✔
1884
                Field * col = benchArrayGet(stbInfo->cols, i);
20✔
1885
                n = snprintf(stbInfo->partialColNameBuf+pos,
20✔
1886
                                TSDB_MAX_ALLOWED_SQL_LEN - pos,
20✔
1887
                               ",%s", col->name);
20✔
1888
                if (n < 0 || n > TSDB_MAX_ALLOWED_SQL_LEN - pos) {
20✔
1889
                    errorPrint("%s() LN%d snprintf overflow at %d\n",
×
1890
                               __func__, __LINE__, i);
1891
                } else {
1892
                    pos += n;
20✔
1893
                }
1894
            }
1895
            
1896
            // first part set noen
1897
            for (uint32_t i = 0; i < stbInfo->partialColFrom; ++i) {
6✔
1898
                Field * col = benchArrayGet(stbInfo->cols, i);
×
1899
                col->none = true;
×
1900
            }
1901
            // last part set none
1902
            for (uint32_t i = stbInfo->partialColFrom + stbInfo->partialColNum; i < stbInfo->cols->size; ++i) {
57✔
1903
                Field * col = benchArrayGet(stbInfo->cols, i);
51✔
1904
                col->none = true;
51✔
1905
            }
1906
            debugPrint("partialColNameBuf: %s\n",
6✔
1907
                       stbInfo->partialColNameBuf);
1908
        }
1909
    } else {
1910
        stbInfo->partialColNum = stbInfo->cols->size;
219✔
1911
    }
1912
    stbInfo->sampleDataBuf =
229✔
1913
            benchCalloc(
229✔
1914
                1, stbInfo->lenOfCols*g_arguments->prepared_rand, true);
229✔
1915
    infoPrint(
229✔
1916
              "generate stable<%s> columns data with lenOfCols<%u> * "
1917
              "prepared_rand<%" PRIu64 ">\n",
1918
              stbInfo->stbName, stbInfo->lenOfCols, g_arguments->prepared_rand);
1919
    if (stbInfo->random_data_source) {
229✔
1920
        if (g_arguments->mistMode) {
221✔
1921
            infoPrint("Each child table using different random prepare data pattern. need "
3✔
1922
            "all memory(%d M) = childs(%"PRId64") * prepared_rand(%"PRId64") * lenOfCols(%d) \n",
1923
            (int32_t)(stbInfo->childTblCount*g_arguments->prepared_rand*stbInfo->lenOfCols/1024/1024),
1924
            stbInfo->childTblCount, g_arguments->prepared_rand, stbInfo->lenOfCols);
1925
            for (int64_t child = 0; child < stbInfo->childTblCount; child++) {
11✔
1926
                SChildTable *childTbl = stbInfo->childTblArray[child];
8✔
1927
                if (STMT_IFACE == stbInfo->iface || STMT2_IFACE == stbInfo->iface) {
8✔
1928
                    childTbl->childCols = initChildCols(stbInfo->cols->size);
6✔
1929
                }
1930
                childTbl->sampleDataBuf =
8✔
1931
                    benchCalloc(
8✔
1932
                        1, stbInfo->lenOfCols*g_arguments->prepared_rand, true);
8✔
1933
                if (generateRandData(stbInfo, childTbl->sampleDataBuf,
8✔
1934
                             stbInfo->lenOfCols*g_arguments->prepared_rand,
8✔
1935
                             stbInfo->lenOfCols,
8✔
1936
                             stbInfo->cols,
1937
                             g_arguments->prepared_rand,
8✔
1938
                             false, childTbl->childCols)) {
1939
                    errorPrint("Failed to generate data for table %s\n",
×
1940
                               childTbl->name);
1941
                    return -1;
×
1942
                }
1943
                childTbl->useOwnSample = true;
8✔
1944
            }
1945
        } else {
1946
            if (generateRandData(stbInfo, stbInfo->sampleDataBuf,
218✔
1947
                             stbInfo->lenOfCols*g_arguments->prepared_rand,
218✔
1948
                             stbInfo->lenOfCols,
218✔
1949
                             stbInfo->cols,
1950
                             g_arguments->prepared_rand,
218✔
1951
                             false, NULL)) {
1952
                return -1;
×
1953
            }
1954
        }
1955
        debugPrint("sampleDataBuf: %s\n", stbInfo->sampleDataBuf);
221✔
1956
    } else {
1957
        if (stbInfo->useSampleTs) {
8✔
1958
            if (getAndSetRowsFromCsvFile(
6✔
1959
                    stbInfo->sampleFile, &stbInfo->insertRows)) {
6✔
1960
                return -1;
×
1961
            }
1962
        }
1963
        if (generateSampleFromCsv(stbInfo->sampleDataBuf,
8✔
1964
                                        stbInfo->sampleFile, NULL, stbInfo->lenOfCols,
8✔
1965
                                        g_arguments->prepared_rand)) {
8✔
1966
            errorPrint("Failed to generate sample from csv file %s\n",
×
1967
                    stbInfo->sampleFile);
1968
            return -1;
×
1969
        }
1970

1971
        debugPrint("sampleDataBuf: %s\n", stbInfo->sampleDataBuf);
8✔
1972
        if (stbInfo->childTblSample) {
8✔
1973
            if (NULL == strstr(stbInfo->childTblSample, "XXXX")) {
2✔
1974
                errorPrint("Child table sample file pattern has no %s\n",
×
1975
                   "XXXX");
1976
                return -1;
×
1977
            }
1978
            for (int64_t child = 0; child < stbInfo->childTblCount; child++) {
18✔
1979
                char sampleFilePath[MAX_PATH_LEN] = {0};
16✔
1980
                getSampleFileNameByPattern(sampleFilePath, stbInfo, child);
16✔
1981
                if (0 != access(sampleFilePath, F_OK)) {
16✔
1982
                    continue;
12✔
1983
                }
1984
                SChildTable *childTbl = stbInfo->childTblArray[child];
4✔
1985
                infoPrint("Found specified sample file for table %s\n",
4✔
1986
                          childTbl->name);
1987
                if (getAndSetRowsFromCsvFile(sampleFilePath,
4✔
1988
                                             &(childTbl->insertRows))) {
1989
                    errorPrint("Failed to get sample data rows for table %s\n",
×
1990
                          childTbl->name);
1991
                    return -1;
×
1992
                }
1993

1994
                childTbl->sampleDataBuf =
4✔
1995
                    benchCalloc(
4✔
1996
                        1, stbInfo->lenOfCols*g_arguments->prepared_rand, true);
4✔
1997
                if (generateSampleFromCsv(
4✔
1998
                            childTbl->sampleDataBuf,
1999
                            sampleFilePath,
2000
                            NULL,
2001
                            stbInfo->lenOfCols,
4✔
2002
                            g_arguments->prepared_rand)) {
4✔
2003
                    errorPrint("Failed to generate sample from file "
×
2004
                                   "for child table %"PRId64"\n",
2005
                                    child);
2006
                    return -1;
×
2007
                }
2008
                if (STMT_IFACE == stbInfo->iface || STMT2_IFACE == stbInfo->iface) {
4✔
2009
                    childTbl->childCols = initChildCols(stbInfo->cols->size);
2✔
2010
                }
2011
                childTbl->useOwnSample = true;
4✔
2012
                debugPrint("sampleDataBuf: %s\n", childTbl->sampleDataBuf);
4✔
2013
            }
2014
        }
2015
    }
2016

2017
    if (0 != convertServAddr(
229✔
2018
            stbInfo->iface,
229✔
2019
            stbInfo->tcpTransfer,
229✔
2020
            stbInfo->lineProtocol)) {
229✔
2021
        return -1;
×
2022
    }
2023
    return 0;
229✔
2024
}
2025

2026
int64_t getTSRandTail(int64_t timeStampStep, int32_t seq, int disorderRatio,
20✔
2027
                      int disorderRange) {
2028
    int64_t randTail = timeStampStep * seq;
20✔
2029
    if (disorderRatio > 0) {
20✔
2030
        int rand_num = taosRandom() % 100;
20✔
2031
        if (rand_num < disorderRatio) {
20✔
2032
            randTail = (randTail + (taosRandom() % disorderRange + 1)) * (-1);
10✔
2033
        }
2034
    }
2035
    return randTail;
20✔
2036
}
2037

2038
uint32_t bindParamBatch(threadInfo *pThreadInfo,
33,813✔
2039
                        uint32_t batch, int64_t startTime, int64_t pos,
2040
                        SChildTable *childTbl, int32_t *pkCur, int32_t *pkCnt, int32_t *n, int64_t *delay2, int64_t *delay3) {
2041
    TAOS_STMT   *stmt = pThreadInfo->conn->stmt;
33,813✔
2042
    SSuperTable *stbInfo = pThreadInfo->stbInfo;
33,813✔
2043
    uint32_t     columnCount = stbInfo->cols->size;
33,813✔
2044

2045
    //if (!pThreadInfo->stmtBind || stbInfo->interlaceRows > 0 ) {
2046
    {
2047
        pThreadInfo->stmtBind = true;
33,813✔
2048
        memset(pThreadInfo->bindParams, 0,
33,813✔
2049
            (sizeof(TAOS_MULTI_BIND) * (columnCount + 1)));
33,813✔
2050
        memset(pThreadInfo->is_null, 0, batch);
33,813✔
2051

2052
        for (int c = 0; c <= columnCount; c++) {
169,067✔
2053
            TAOS_MULTI_BIND *param =
135,579✔
2054
                (TAOS_MULTI_BIND *)(pThreadInfo->bindParams +
135,579✔
2055
                                    sizeof(TAOS_MULTI_BIND) * c);
135,579✔
2056
            char data_type;
2057
            if (c == 0) {
135,579✔
2058
                data_type = TSDB_DATA_TYPE_TIMESTAMP;
33,834✔
2059
                param->buffer_length = sizeof(int64_t);
33,834✔
2060
                if (stbInfo->useSampleTs) {
33,834✔
2061
                    param->buffer = pThreadInfo->bind_ts_array + pos;
66✔
2062
                } else {
2063
                    param->buffer = pThreadInfo->bind_ts_array;
33,768✔
2064
                }
2065
            } else {
2066
                Field *col = benchArrayGet(stbInfo->cols, c - 1);
101,745✔
2067
                data_type = col->type;
101,686✔
2068
                if (childTbl->useOwnSample) {
101,686✔
2069
                    ChildField *childCol = benchArrayGet(childTbl->childCols, c-1);
×
2070
                    param->buffer = (char *)childCol->stmtData.data + pos * col->length;
14✔
2071
                    param->is_null = childCol->stmtData.is_null + pos;
14✔
2072
                } else {
2073
                    param->buffer = (char *)col->stmtData.data + pos * col->length;
101,907✔
2074
                    param->is_null = col->stmtData.is_null + pos;
101,907✔
2075
                }
2076
                param->buffer_length = col->length;
101,921✔
2077
                debugPrint("col[%d]: type: %s, len: %d\n", c,
101,921✔
2078
                        convertDatatypeToString(data_type),
2079
                        col->length);
2080
            }
2081
            param->buffer_type = data_type;
135,254✔
2082
            param->length = pThreadInfo->lengths[c];
135,254✔
2083

2084
            for (int b = 0; b < batch; b++) {
638,872✔
2085
                param->length[b] = (int32_t)param->buffer_length;
503,618✔
2086
            }
2087
            param->num = batch;
135,254✔
2088
        }
2089
    }
2090

2091
    if (!stbInfo->useSampleTs) {
33,488✔
2092
        // set first column ts array values
2093
        for (uint32_t k = 0; k < batch; k++) {
98,757✔
2094
            /* columnCount + 1 (ts) */
2095
            if (stbInfo->disorderRatio) {
64,979✔
2096
                *(pThreadInfo->bind_ts_array + k) =
20✔
2097
                    startTime + getTSRandTail(stbInfo->timestamp_step, *n,
20✔
2098
                                            stbInfo->disorderRatio,
2099
                                            stbInfo->disorderRange);
2100
            } else {
2101
                *(pThreadInfo->bind_ts_array + k) = startTime + stbInfo->timestamp_step * (*n);
64,959✔
2102
            }
2103

2104
            // check n need add
2105
            if (!stbInfo->primary_key || needChangeTs(stbInfo, pkCur, pkCnt)) {
64,979✔
2106
                *n = *n + 1;
64,947✔
2107
            }
2108
        }
2109
    }
2110

2111
    /*
2112
      1. The last batch size may be smaller than the previous batch size.
2113
      2. When inserting another table, the batch size reset again(bigger than lastBatchSize)
2114
    */        
2115
    int lastBatchSize = ((TAOS_MULTI_BIND *) pThreadInfo->bindParams)->num;
33,488✔
2116
    if (batch != lastBatchSize) {
33,488✔
2117
        for (int c = 0; c < columnCount + 1; c++) {
×
2118
            TAOS_MULTI_BIND *param =
×
2119
                    (TAOS_MULTI_BIND *) (pThreadInfo->bindParams +
×
2120
                    sizeof(TAOS_MULTI_BIND) * c);
×
2121
            param->num = batch;
×
2122
        }
2123
    }
2124

2125
    int64_t start = toolsGetTimestampUs();
33,488✔
2126
    if (taos_stmt_bind_param_batch(
33,967✔
2127
            stmt, (TAOS_MULTI_BIND *)pThreadInfo->bindParams)) {
33,834✔
2128
        errorPrint("taos_stmt_bind_param_batch() failed! reason: %s\n",
×
2129
                   taos_stmt_errstr(stmt));
2130
        return 0;
×
2131
    }
2132
    *delay2 += toolsGetTimestampUs() - start;
33,967✔
2133

2134
    if(stbInfo->autoTblCreating) {
33,856✔
2135
        start = toolsGetTimestampUs();
31✔
2136
        if (taos_stmt_add_batch(pThreadInfo->conn->stmt) != 0) {
32✔
2137
            errorPrint("taos_stmt_add_batch() failed! reason: %s\n",
×
2138
                    taos_stmt_errstr(pThreadInfo->conn->stmt));
2139
            return 0;
×
2140
        }
UNCOV
2141
        if(delay3) {
×
2142
            *delay3 += toolsGetTimestampUs() - start;
31✔
2143
        }
2144
    }
2145
    return batch;
33,794✔
2146
}
2147

2148
void generateSmlJsonTags(tools_cJSON *tagsList,
171✔
2149
                         char **sml_tags_json_array,
2150
                         SSuperTable *stbInfo,
2151
                            uint64_t start_table_from, int tbSeq) {
2152
    tools_cJSON * tags = tools_cJSON_CreateObject();
171✔
2153
    char *  tbName = benchCalloc(1, TSDB_TABLE_NAME_LEN, true);
171✔
2154
    snprintf(tbName, TSDB_TABLE_NAME_LEN, "%s%" PRIu64 "",
171✔
2155
             stbInfo->childTblPrefix, start_table_from + tbSeq);
2156
    char *tagName = benchCalloc(1, TSDB_MAX_TAGS, true);
171✔
2157
    for (int i = 0; i < stbInfo->tags->size; i++) {
1,570✔
2158
        Field * tag = benchArrayGet(stbInfo->tags, i);
1,399✔
2159
        snprintf(tagName, TSDB_MAX_TAGS, "t%d", i);
1,399✔
2160
        switch (tag->type) {
1,399✔
2161
            case TSDB_DATA_TYPE_BOOL: {
153✔
2162
                bool boolTmp = tmpBool(tag);
153✔
2163
                tools_cJSON_AddNumberToObject(tags, tagName, boolTmp);
153✔
2164
                break;
153✔
2165
            }
2166
            case TSDB_DATA_TYPE_FLOAT: {
153✔
2167
                float floatTmp = tmpFloat(tag);
153✔
2168
                tools_cJSON_AddNumberToObject(tags, tagName, floatTmp);
153✔
2169
                break;
153✔
2170
            }
2171
            case TSDB_DATA_TYPE_DOUBLE: {
153✔
2172
                double doubleTmp = tmpDouble(tag);
153✔
2173
                tools_cJSON_AddNumberToObject(tags, tagName, doubleTmp);
153✔
2174
                break;
153✔
2175
            }
2176

2177
            case TSDB_DATA_TYPE_BINARY:
306✔
2178
            case TSDB_DATA_TYPE_VARBINARY:
2179
            case TSDB_DATA_TYPE_NCHAR: {
2180
                char *buf = (char *)benchCalloc(tag->length + 1, 1, false);
306✔
2181
                rand_string(buf, tag->length, g_arguments->chinese);
306✔
2182
                if (tag->type == TSDB_DATA_TYPE_BINARY || tag->type == TSDB_DATA_TYPE_VARBINARY) {
306✔
2183
                    tools_cJSON_AddStringToObject(tags, tagName, buf);
154✔
2184
                } else {
2185
                    tools_cJSON_AddStringToObject(tags, tagName, buf);
152✔
2186
                }
2187
                tmfree(buf);
306✔
2188
                break;
306✔
2189
            }
2190
            default: {
634✔
2191
                int tagTmp = tag->min;
634✔
2192
                if (tag->max != tag->min) {
634✔
2193
                    tagTmp += (taosRandom() % (tag->max - tag->min));
626✔
2194
                }
2195
                tools_cJSON_AddNumberToObject(
634✔
2196
                        tags, tagName, tagTmp);
2197
                break;
634✔
2198
            }
2199
        }
2200
    }
2201
    tools_cJSON_AddItemToArray(tagsList, tags);
171✔
2202
    debugPrintJsonNoTime(tags);
171✔
2203
    char *tags_text = tools_cJSON_PrintUnformatted(tags);
171✔
2204
    debugPrintNoTimestamp("%s() LN%d, No.%"PRIu64" table's tags text: %s\n",
171✔
2205
                          __func__, __LINE__,
2206
                          start_table_from + tbSeq, tags_text);
2207
    sml_tags_json_array[tbSeq] = tags_text;
171✔
2208
    tmfree(tagName);
171✔
2209
    tmfree(tbName);
171✔
2210
}
171✔
2211

2212
void generateSmlTaosJsonTags(tools_cJSON *tagsList, SSuperTable *stbInfo,
75✔
2213
                            uint64_t start_table_from, int tbSeq) {
2214
    tools_cJSON * tags = tools_cJSON_CreateObject();
75✔
2215
    char *  tbName = benchCalloc(1, TSDB_TABLE_NAME_LEN, true);
75✔
2216
    snprintf(tbName, TSDB_TABLE_NAME_LEN, "%s%" PRIu64 "",
75✔
2217
             stbInfo->childTblPrefix, tbSeq + start_table_from);
2218
    tools_cJSON_AddStringToObject(tags, "id", tbName);
75✔
2219
    char *tagName = benchCalloc(1, TSDB_MAX_TAGS, true);
75✔
2220
    for (int i = 0; i < stbInfo->tags->size; i++) {
736✔
2221
        Field * tag = benchArrayGet(stbInfo->tags, i);
661✔
2222
        tools_cJSON *tagObj = tools_cJSON_CreateObject();
661✔
2223
        snprintf(tagName, TSDB_MAX_TAGS, "t%d", i);
661✔
2224
        switch (tag->type) {
661✔
2225
            case TSDB_DATA_TYPE_BOOL: {
73✔
2226
                bool boolTmp = tmpBool(tag);
73✔
2227
                tools_cJSON_AddBoolToObject(tagObj, "value", boolTmp);
73✔
2228
                tools_cJSON_AddStringToObject(tagObj, "type", "bool");
73✔
2229
                break;
73✔
2230
            }
2231
            case TSDB_DATA_TYPE_FLOAT: {
73✔
2232
                float floatTmp = tmpFloat(tag);
73✔
2233
                tools_cJSON_AddNumberToObject(tagObj, "value", floatTmp);
73✔
2234
                tools_cJSON_AddStringToObject(tagObj, "type", "float");
73✔
2235
                break;
73✔
2236
            }
2237
            case TSDB_DATA_TYPE_DOUBLE: {
73✔
2238
                double doubleTmp = tmpDouble(tag);
73✔
2239
                tools_cJSON_AddNumberToObject(tagObj, "value", doubleTmp);
73✔
2240
                tools_cJSON_AddStringToObject(tagObj, "type", "double");
73✔
2241
                break;
73✔
2242
            }
2243

2244
            case TSDB_DATA_TYPE_BINARY:
148✔
2245
            case TSDB_DATA_TYPE_VARBINARY:
2246
            case TSDB_DATA_TYPE_NCHAR: {
2247
                char *buf = (char *)benchCalloc(tag->length + 1, 1, false);
148✔
2248
                rand_string(buf, tag->length, g_arguments->chinese);
148✔
2249
                if (tag->type == TSDB_DATA_TYPE_BINARY || tag->type == TSDB_DATA_TYPE_VARBINARY) {
148✔
2250
                    tools_cJSON_AddStringToObject(tagObj, "value", buf);
75✔
2251
                    tools_cJSON_AddStringToObject(tagObj, "type", "binary");
75✔
2252
                } else {
2253
                    tools_cJSON_AddStringToObject(tagObj, "value", buf);
73✔
2254
                    tools_cJSON_AddStringToObject(tagObj, "type", "nchar");
73✔
2255
                }
2256
                tmfree(buf);
148✔
2257
                break;
148✔
2258
            }
2259
            default: {
294✔
2260
                int64_t tagTmp = tag->min;
294✔
2261
                if (tag->max != tag->min) {
294✔
2262
                    tagTmp += (taosRandom() % (tag->max - tag->min));
290✔
2263
                }
2264
                tools_cJSON_AddNumberToObject(tagObj, "value", tagTmp);
294✔
2265
                        tools_cJSON_AddStringToObject(tagObj, "type",
294✔
2266
                                        convertDatatypeToString(tag->type));
2267
                break;
294✔
2268
            }
2269
        }
2270
        tools_cJSON_AddItemToObject(tags, tagName, tagObj);
661✔
2271
    }
2272
    tools_cJSON_AddItemToArray(tagsList, tags);
75✔
2273
    tmfree(tagName);
75✔
2274
    tmfree(tbName);
75✔
2275
}
75✔
2276

2277
void generateSmlJsonValues(
147✔
2278
        char **sml_json_value_array, SSuperTable *stbInfo, int tableSeq) {
2279
    char *value_buf = NULL;
147✔
2280
    Field* col = benchArrayGet(stbInfo->cols, 0);
147✔
2281
    int len_key = strlen("\"value\":,");
147✔
2282
    switch (col->type) {
147✔
2283
        case TSDB_DATA_TYPE_BOOL: {
8✔
2284
            bool boolTmp = tmpBool(col);
8✔
2285
            value_buf = benchCalloc(len_key + 6, 1, true);
8✔
2286
            snprintf(value_buf, len_key + 6,
8✔
2287
                     "\"value\":%s,", boolTmp?"true":"false");
2288
            break;
8✔
2289
        }
2290
        case TSDB_DATA_TYPE_FLOAT: {
18✔
2291
            value_buf = benchCalloc(len_key + 20, 1, true);
18✔
2292
            float floatTmp = tmpFloat(col);
18✔
2293
            snprintf(value_buf, len_key + 20,
18✔
2294
                     "\"value\":%f,", floatTmp);
2295
            break;
18✔
2296
        }
2297
        case TSDB_DATA_TYPE_DOUBLE: {
16✔
2298
            value_buf = benchCalloc(len_key + 40, 1, true);
16✔
2299
            double doubleTmp = tmpDouble(col);
16✔
2300
            snprintf(
16✔
2301
                value_buf, len_key + 40, "\"value\":%f,", doubleTmp);
16✔
2302
            break;
16✔
2303
        }
2304
        case TSDB_DATA_TYPE_BINARY:
32✔
2305
        case TSDB_DATA_TYPE_VARBINARY:
2306
        case TSDB_DATA_TYPE_NCHAR: {
2307
            char *buf = (char *)benchCalloc(col->length + 1, 1, false);
32✔
2308
            rand_string(buf, col->length, g_arguments->chinese);
32✔
2309
            value_buf = benchCalloc(len_key + col->length + 3, 1, true);
32✔
2310
            snprintf(value_buf, len_key + col->length + 3,
32✔
2311
                     "\"value\":\"%s\",", buf);
2312
            tmfree(buf);
32✔
2313
            break;
32✔
2314
        }
2315
        case TSDB_DATA_TYPE_GEOMETRY: {
×
2316
            char *buf = (char *)benchCalloc(col->length + 1, 1, false);
×
2317
            tmpGeometry(buf, stbInfo->iface, col, 0);
×
2318
            value_buf = benchCalloc(len_key + col->length + 3, 1, true);
×
2319
            snprintf(value_buf, len_key + col->length + 3,
×
2320
                     "\"value\":\"%s\",", buf);
2321
            tmfree(buf);
×
2322
            break;
×
2323
        }
2324
        default: {
73✔
2325
            value_buf = benchCalloc(len_key + 20, 1, true);
73✔
2326
            double doubleTmp = tmpDouble(col);
73✔
2327
            snprintf(value_buf, len_key + 20, "\"value\":%f,", doubleTmp);
73✔
2328
            break;
73✔
2329
        }
2330
    }
2331
    sml_json_value_array[tableSeq] = value_buf;
147✔
2332
}
147✔
2333

2334
void generateSmlJsonCols(tools_cJSON *array, tools_cJSON *tag,
480✔
2335
                         SSuperTable *stbInfo,
2336
                            uint32_t time_precision, int64_t timestamp) {
2337
    tools_cJSON * record = tools_cJSON_CreateObject();
480✔
2338
    tools_cJSON_AddNumberToObject(record, "timestamp", (double)timestamp);
480✔
2339
    Field* col = benchArrayGet(stbInfo->cols, 0);
480✔
2340
    switch (col->type) {
480✔
2341
        case TSDB_DATA_TYPE_BOOL: {
160✔
2342
            bool boolTmp = tmpBool(col);
160✔
2343
            tools_cJSON_AddBoolToObject(record, "value", boolTmp);
160✔
2344
            break;
160✔
2345
        }
2346
        case TSDB_DATA_TYPE_FLOAT: {
×
2347
            float floatTmp = tmpFloat(col);
×
2348
            tools_cJSON_AddNumberToObject(record, "value", floatTmp);
×
2349
            break;
×
2350
        }
2351
        case TSDB_DATA_TYPE_DOUBLE: {
×
2352
            double doubleTmp = tmpDouble(col);
×
2353
            tools_cJSON_AddNumberToObject(record, "value", doubleTmp);
×
2354
            break;
×
2355
        }
2356
        case TSDB_DATA_TYPE_BINARY:
×
2357
        case TSDB_DATA_TYPE_VARBINARY:
2358
        case TSDB_DATA_TYPE_NCHAR: {
2359
            char *buf = (char *)benchCalloc(col->length + 1, 1, false);
×
2360
            rand_string(buf, col->length, g_arguments->chinese);
×
2361
            if (col->type == TSDB_DATA_TYPE_BINARY) {
×
2362
                tools_cJSON_AddStringToObject(record, "value", buf);
×
2363
            } else {
2364
                tools_cJSON_AddStringToObject(record, "value", buf);
×
2365
            }
2366
            tmfree(buf);
×
2367
            break;
×
2368
        }
2369
        case TSDB_DATA_TYPE_GEOMETRY: {
×
2370
            char *buf = (char *)benchCalloc(col->length + 1, 1, false);
×
2371
            tmpGeometry(buf, stbInfo->iface, col, 0);
×
2372
            tools_cJSON_AddStringToObject(record, "value", buf);
×
2373
            tmfree(buf);
×
2374
            break;
×
2375
        }
2376
        default: {
320✔
2377
            double doubleTmp = tmpDouble(col);
320✔
2378
            tools_cJSON_AddNumberToObject(record, "value", doubleTmp);
320✔
2379
            break;
320✔
2380
        }
2381
    }
2382
    tools_cJSON_AddItemToObject(record, "tags", tag);
480✔
2383
    tools_cJSON_AddStringToObject(record, "metric", stbInfo->stbName);
480✔
2384
    tools_cJSON_AddItemToArray(array, record);
480✔
2385
}
480✔
2386

2387
void generateSmlTaosJsonCols(tools_cJSON *array, tools_cJSON *tag,
1,452✔
2388
                         SSuperTable *stbInfo,
2389
                            uint32_t time_precision, int64_t timestamp) {
2390
    tools_cJSON * record = tools_cJSON_CreateObject();
1,452✔
2391
    tools_cJSON * ts = tools_cJSON_CreateObject();
1,450✔
2392
    tools_cJSON_AddNumberToObject(ts, "value", (double)timestamp);
1,450✔
2393
    if (time_precision == TSDB_SML_TIMESTAMP_MILLI_SECONDS) {
1,451✔
2394
        tools_cJSON_AddStringToObject(ts, "type", "ms");
1,451✔
2395
    } else if (time_precision == TSDB_SML_TIMESTAMP_MICRO_SECONDS) {
×
2396
        tools_cJSON_AddStringToObject(ts, "type", "us");
×
2397
    } else if (time_precision == TSDB_SML_TIMESTAMP_NANO_SECONDS) {
×
2398
        tools_cJSON_AddStringToObject(ts, "type", "ns");
×
2399
    }
2400
    tools_cJSON *value = tools_cJSON_CreateObject();
1,452✔
2401
    Field* col = benchArrayGet(stbInfo->cols, 0);
1,452✔
2402
    switch (col->type) {
1,451✔
2403
        case TSDB_DATA_TYPE_BOOL: {
160✔
2404
            bool boolTmp = tmpBool(col);
160✔
2405
            tools_cJSON_AddBoolToObject(value, "value", boolTmp);
160✔
2406
            tools_cJSON_AddStringToObject(value, "type", "bool");
160✔
2407
            break;
160✔
2408
        }
2409
        case TSDB_DATA_TYPE_FLOAT: {
162✔
2410
            float floatTmp = tmpFloat(col);
162✔
2411
            tools_cJSON_AddNumberToObject(value, "value", floatTmp);
162✔
2412
            tools_cJSON_AddStringToObject(value, "type", "float");
162✔
2413
            break;
161✔
2414
        }
2415
        case TSDB_DATA_TYPE_DOUBLE: {
160✔
2416
            double dblTmp = tmpDouble(col);
160✔
2417
            tools_cJSON_AddNumberToObject(value, "value", dblTmp);
160✔
2418
            tools_cJSON_AddStringToObject(value, "type", "double");
160✔
2419
            break;
160✔
2420
        }
2421
        case TSDB_DATA_TYPE_BINARY:
330✔
2422
        case TSDB_DATA_TYPE_VARBINARY:
2423
        case TSDB_DATA_TYPE_NCHAR: {
2424
            char *buf = (char *)benchCalloc(col->length + 1, 1, false);
330✔
2425
            rand_string(buf, col->length, g_arguments->chinese);
330✔
2426
            if (col->type == TSDB_DATA_TYPE_BINARY || col->type == TSDB_DATA_TYPE_VARBINARY) {
330✔
2427
                tools_cJSON_AddStringToObject(value, "value", buf);
160✔
2428
                tools_cJSON_AddStringToObject(value, "type", "binary");
160✔
2429
            } else {
2430
                tools_cJSON_AddStringToObject(value, "value", buf);
170✔
2431
                tools_cJSON_AddStringToObject(value, "type", "nchar");
170✔
2432
            }
2433
            tmfree(buf);
330✔
2434
            break;
329✔
2435
        }
2436
        case TSDB_DATA_TYPE_GEOMETRY: {
×
2437
            char *buf = (char *)benchCalloc(col->length + 1, 1, false);
×
2438
            tmpGeometry(buf, stbInfo->iface, col, 0);
×
2439
            tools_cJSON_AddStringToObject(value, "value", buf);
×
2440
            tools_cJSON_AddStringToObject(value, "type", "geometry");
×
2441
            tmfree(buf);
×
2442
        }
2443
        default: {
640✔
2444
            double dblTmp = (double)col->min;
640✔
2445
            if (col->max != col->min) {
640✔
2446
                dblTmp += (double)((taosRandom() % (col->max - col->min)));
640✔
2447
            }
2448
            tools_cJSON_AddNumberToObject(value, "value", dblTmp);
640✔
2449
            tools_cJSON_AddStringToObject(
640✔
2450
                    value, "type", convertDatatypeToString(col->type));
2451
            break;
640✔
2452
        }
2453
    }
2454
    tools_cJSON_AddItemToObject(record, "timestamp", ts);
1,450✔
2455
    tools_cJSON_AddItemToObject(record, "value", value);
1,451✔
2456
    tools_cJSON_AddItemToObject(record, "tags", tag);
1,452✔
2457
    tools_cJSON_AddStringToObject(record, "metric", stbInfo->stbName);
1,452✔
2458
    tools_cJSON_AddItemToArray(array, record);
1,452✔
2459
}
1,452✔
2460

2461
// generateTag data from random or csv file
2462
bool generateTagData(SSuperTable *stbInfo, char *buf, int64_t cnt, FILE* csv, BArray* tagsStmt) {
720✔
2463
    if(csv) {
720✔
2464
        if (generateSampleFromCsv(
12✔
2465
                buf, NULL, csv,
2466
                stbInfo->lenOfTags,
12✔
2467
                cnt)) {
2468
            return false;
×
2469
        }
2470
    } else {
2471
        if (generateRandData(stbInfo,
1,416✔
2472
                            buf,
2473
                            cnt * stbInfo->lenOfTags,
708✔
2474
                            stbInfo->lenOfTags,
708✔
2475
                            tagsStmt ? tagsStmt : stbInfo->tags,
2476
                            cnt, true, NULL)) {
2477
            errorPrint("Generate Tag Rand Data Failed. stb=%s\n", stbInfo->stbName);
×
2478
            return false;
×
2479
        }
2480
    }
2481

2482
    return true;
720✔
2483
}
2484

2485
// open tag from csv file
2486
FILE* openTagCsv(SSuperTable* stbInfo) {
586✔
2487
    FILE* csvFile = NULL;
586✔
2488
    if (stbInfo->tagsFile[0] != 0) {
586✔
2489
        csvFile = fopen(stbInfo->tagsFile, "r");
15✔
2490
        if (csvFile == NULL) {
15✔
2491
            errorPrint("Failed to open tag sample file: %s, reason:%s\n", stbInfo->tagsFile, strerror(errno));
×
2492
            return NULL;
×
2493
        }
2494
        infoPrint("open tag csv file :%s \n", stbInfo->tagsFile);
15✔
2495
    }
2496
    return csvFile;
586✔
2497
}
2498

2499
//
2500
// STMT2 bind cols param progressive
2501
//
2502
uint32_t bindVColsProgressive(TAOS_STMT2_BINDV *bindv, int32_t tbIndex,
133✔
2503
                 threadInfo *pThreadInfo,
2504
                 uint32_t batch, int64_t startTime, int64_t pos,
2505
                 SChildTable *childTbl, int32_t *pkCur, int32_t *pkCnt, int32_t *n) {
2506
    
2507
    SSuperTable *stbInfo = pThreadInfo->stbInfo;
133✔
2508
    uint32_t     columnCount = stbInfo->cols->size;
133✔
2509

2510
    // clear
2511
    memset(pThreadInfo->bindParams, 0, sizeof(TAOS_STMT2_BIND) * (columnCount + 1));
133✔
2512
    memset(pThreadInfo->is_null, 0, batch);
133✔
2513
    debugPrint("stmt2 bindVColsProgressive child=%s batch=%d pos=%" PRId64 "\n", childTbl->name, batch, pos);
133✔
2514
    // loop cols
2515
    for (int c = 0; c <= columnCount; c++) {
661✔
2516
        // des
2517
        TAOS_STMT2_BIND *param = (TAOS_STMT2_BIND *)(pThreadInfo->bindParams + sizeof(TAOS_STMT2_BIND) * c);
532✔
2518
        char data_type;
2519
        int32_t length = 0;
532✔
2520
        if (c == 0) {
532✔
2521
            data_type = TSDB_DATA_TYPE_TIMESTAMP;
133✔
2522
            if (stbInfo->useSampleTs) {
133✔
2523
                param->buffer = pThreadInfo->bind_ts_array + pos;
×
2524
            } else {
2525
                param->buffer = pThreadInfo->bind_ts_array;
133✔
2526
            }
2527
            length = sizeof(int64_t);
133✔
2528
        } else {
2529
            Field *col = benchArrayGet(stbInfo->cols, c - 1);
399✔
2530
            data_type = col->type;
398✔
2531
            length    = col->length;
398✔
2532
            if (childTbl->useOwnSample) {
398✔
2533
                ChildField *childCol = benchArrayGet(childTbl->childCols, c-1);
23✔
2534
                param->buffer = (char *)childCol->stmtData.data + pos * col->length;
24✔
2535
                param->is_null = childCol->stmtData.is_null + pos;
24✔
2536
            } else {
2537
                param->buffer = (char *)col->stmtData.data + pos * col->length;
375✔
2538
                param->is_null = col->stmtData.is_null + pos;
375✔
2539
            }
2540
            debugPrint("col[%d]: type: %s, len: %d\n", c,
399✔
2541
                    convertDatatypeToString(data_type),
2542
                    col->length);
2543
        }
2544
        param->buffer_type = data_type;
528✔
2545
        param->length = pThreadInfo->lengths[c];
528✔
2546

2547
        for (int b = 0; b < batch; b++) {
73,033✔
2548
            param->length[b] = length;
72,505✔
2549
        }
2550
        param->num = batch;
528✔
2551
    }
2552
    
2553
    // ts key
2554
    if (!stbInfo->useSampleTs) {
129✔
2555
        // set first column ts array values
2556
        for (uint32_t k = 0; k < batch; k++) {
18,846✔
2557
            /* columnCount + 1 (ts) */
2558
            if (stbInfo->disorderRatio) {
18,713✔
2559
                *(pThreadInfo->bind_ts_array + k) =
×
2560
                    startTime + getTSRandTail(stbInfo->timestamp_step, *n,
×
2561
                                            stbInfo->disorderRatio,
2562
                                            stbInfo->disorderRange);
2563
            } else {
2564
                *(pThreadInfo->bind_ts_array + k) = startTime + stbInfo->timestamp_step * (*n);
18,713✔
2565
            }
2566

2567
            // check n need add
2568
            if (!stbInfo->primary_key || needChangeTs(stbInfo, pkCur, pkCnt)) {
18,713✔
2569
                *n = *n + 1;
18,661✔
2570
            }
2571
        }
2572
    }
2573

2574
    // set to bindv (only one table, so always is 0 index table)
2575
    bindv->bind_cols[tbIndex] = (TAOS_STMT2_BIND *)pThreadInfo->bindParams;
129✔
2576
    return batch;
129✔
2577
}
2578

2579

2580
//
2581
// STMT2 bind tags param progressive
2582
//
2583
uint32_t bindVTags(TAOS_STMT2_BINDV *bindv, int32_t tbIndex, int32_t w, BArray* fields) {
×
2584

2585
    TAOS_STMT2_BIND *tagsTb = bindv->tags[tbIndex];
×
2586

2587
    // loop 
2588
    for (int32_t i = 0; i < fields->size; i++) {
×
2589
        Field* field = benchArrayGet(fields, i);
×
2590

2591
        // covert field data to bind struct
2592
        tagsTb[i].buffer      = (char *)(field->stmtData.data) + field->length * w ;
×
2593
        tagsTb[i].buffer_type = field->type;
×
2594
        tagsTb[i].is_null     = field->stmtData.is_null;
×
2595
        if (IS_VAR_DATA_TYPE(field->type)) {
×
2596
            // only var set length
2597
            tagsTb[i].length  = field->stmtData.lengths;
×
2598
        }
2599

2600
        // tag always one line
2601
        tagsTb[i].num = 1;
×
2602
    }
2603
    
2604
    return 1;
×
2605
}
2606

2607
//
2608
// STMT2 bind cols param progressive
2609
//
2610
uint32_t bindVColsInterlace(TAOS_STMT2_BINDV *bindv, int32_t tbIndex,
×
2611
                 threadInfo *pThreadInfo,
2612
                 uint32_t batch, int64_t startTime, int64_t pos,
2613
                 SChildTable *childTbl, int32_t *pkCur, int32_t *pkCnt, int32_t *n) {
2614
    // count
2615
    bindv->count += 1;
×
2616
    // info
2617
    SSuperTable *stbInfo    = pThreadInfo->stbInfo;
×
2618
    TAOS_STMT2_BIND *colsTb = bindv->bind_cols[tbIndex];
×
2619
    BArray* fields          = stbInfo->cols;
×
2620

2621

2622
    // loop 
2623
    for (int32_t i = 0; i < fields->size + 1; i++) {
×
2624
        // col bind
2625
        if (i == 0) {
×
2626
            // ts 
2627
            colsTb[i].buffer_type = TSDB_DATA_TYPE_TIMESTAMP;
×
2628
            colsTb[i].length      = pThreadInfo->lengths[0];
×
2629
            for (int32_t j = 0; j < batch; j++) {
×
2630
                colsTb[i].length[j] = sizeof(int64_t); 
×
2631
            }
2632
            if (stbInfo->useSampleTs) {
×
2633
                colsTb[i].buffer = pThreadInfo->bind_ts_array + pos;
×
2634
            } else {
2635
                colsTb[i].buffer = pThreadInfo->bind_ts_array;
×
2636
            }
2637
            // no need set is_null for main key
2638
        } else {
2639
            Field* field = benchArrayGet(fields, i - 1);
×
2640
            colsTb[i].buffer_type = field->type;
×
2641

2642
            if (childTbl->useOwnSample) {
×
2643
                ChildField *childCol = benchArrayGet(childTbl->childCols, i - 1);
×
2644
                colsTb[i].buffer  = (char *)childCol->stmtData.data + pos * field->length;
×
2645
                colsTb[i].is_null = childCol->stmtData.is_null + pos;
×
2646
                colsTb[i].length  = childCol->stmtData.lengths + pos;
×
2647
            } else {
2648
                colsTb[i].buffer  = (char *)field->stmtData.data + pos * field->length;
×
2649
                colsTb[i].is_null = field->stmtData.is_null + pos;
×
2650
                colsTb[i].length  = field->stmtData.lengths + pos;
×
2651
            }
2652
        }
2653

2654
        // set batch
2655
        colsTb[i].num = batch;
×
2656
    }
2657

2658
    // ts key
2659
    if (!stbInfo->useSampleTs) {
×
2660
        // set first column ts array values
2661
        for (uint32_t k = 0; k < batch; k++) {
×
2662
            /* columnCount + 1 (ts) */
2663
            if (stbInfo->disorderRatio) {
×
2664
                *(pThreadInfo->bind_ts_array + k) =
×
2665
                    startTime + getTSRandTail(stbInfo->timestamp_step, *n,
×
2666
                                            stbInfo->disorderRatio,
2667
                                            stbInfo->disorderRange);
2668
            } else {
2669
                *(pThreadInfo->bind_ts_array + k) = startTime + stbInfo->timestamp_step * (*n);
×
2670
            }
2671

2672
            // check n need add
2673
            if (!stbInfo->primary_key || needChangeTs(stbInfo, pkCur, pkCnt)) {
×
2674
                *n = *n + 1;
×
2675
            }
2676
        }
2677
    }    
2678
    
2679
    return batch;
×
2680
}
2681

2682
// early malloc tags for stmt
2683
void prepareTagsStmt(SSuperTable* stbInfo) {
3✔
2684
    BArray *fields = stbInfo->tags;
3✔
2685
    int32_t loop   = TAG_BATCH_COUNT;
3✔
2686
    for (int i = 0; i < fields->size; ++i) {
34✔
2687
        Field *field = benchArrayGet(fields, i);
31✔
2688
        if (field->stmtData.data == NULL) {
31✔
2689
            // data
2690
            if (field->type == TSDB_DATA_TYPE_BINARY
31✔
2691
                    || field->type == TSDB_DATA_TYPE_NCHAR) {
28✔
2692
                field->stmtData.data = benchCalloc(1, loop * (field->length + 1), true);
5✔
2693
            } else {
2694
                field->stmtData.data = benchCalloc(1, loop * field->length, true);
26✔
2695
            }
2696

2697
            // is_null
2698
            field->stmtData.is_null = benchCalloc(sizeof(char), loop, true);
31✔
2699
            // lengths
2700
            field->stmtData.lengths = benchCalloc(sizeof(int32_t), loop, true);
31✔
2701

2702
            // log
2703
            debugPrint("i=%d prepareTags fields=%p %s malloc stmtData.data=%p\n", i, fields, field->name ,field->stmtData.data);
31✔
2704
        }
2705
    }
2706
}
3✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc