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

taosdata / TDengine / #4640

17 Jul 2025 10:48AM UTC coverage: 3.863%. Remained the same
#4640

push

travis-ci

web-flow
Merge pull request #31990 from taosdata/3.0

3.0

230 of 7082 branches covered (3.25%)

Branch coverage included in aggregate %.

0 of 62 new or added lines in 6 files covered. (0.0%)

1617 existing lines in 3 files now uncovered.

382 of 8759 relevant lines covered (4.36%)

1.22 hits per line

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

0.0
/tools/taos-tools/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 <stdio.h>
14
#include <stdint.h>
15
#include <stdlib.h>
16
#include <stdbool.h>
17
#include <string.h>
18

19

20
#include <bench.h>
21
#include "benchLog.h"
22
#include <math.h>
23
#include <benchData.h>
24
#include "decimal.h"
25

26

27
const char charset[] =
28
    "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
29

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

37
const char* locations_sml[] = {
38
    "California.SanFrancisco", "California.LosAngles",
39
    "California.SanDiego", "California.SanJose",
40
    "California.PaloAlto", "California.Campbell",
41
    "California.MountainView", "California.Sunnyvale",
42
    "California.SantaClara", "California.Cupertino"};
43

44
#ifdef WINDOWS
45
    // TODO: why define ssize_t in this way?
46
    #define ssize_t int
47
    // #if _MSC_VER >= 1910
48
    //     #include "benchLocations.h"
49
    // #else
50
    //     #include "benchLocationsWin.h"
51
    // #endif
52
    // NOTE: benchLocations.h is UTF-8 encoded, while benchLocationsWin.h is ANSI/GB18030 encoded.
53
    //       we don't want to use /utf-8 option in MSVC which will bring more considerations,
54
    //       so we use ANSI/GB18030 encoded file for the moment.
55
    #include "benchLocationsWin.h"
56
#else
57
    #include "benchLocations.h"
58
#endif
59

60
int32_t funCount(int32_t min, int32_t max, int32_t step, int32_t loop) {
×
61
    if(step == 0) {
×
62
        step = 1;
×
63
    }
64

65
    int32_t range = abs(max - min);
×
66
    int32_t maxCnt = range / step;
×
67
    int32_t val = min + (loop % maxCnt) * step ;
×
68

69
    return val;
×
70
}
71

72
int32_t funSaw(int32_t min, int32_t max, int32_t period, int32_t loop) {
×
73
    if(period == 0) {
×
74
        period = 1;
×
75
    }
76
    int32_t range = abs(max - min);
×
77
    int32_t step = range / period;
×
78
    int32_t val = min + (loop % period) * step ;
×
79
    return val;
×
80
}
81

82
int32_t funSquare(int32_t min, int32_t max, int32_t period, int32_t loop) {
×
83
    if(period == 0) {
×
84
        period = 1;
×
85
    }
86
    int32_t change = (loop/period) % 2;
×
87
    if (change)
×
88
       return min;
×
89
    else
90
       return max;
×
91
}
92

93
int32_t funTriAngle(int32_t min, int32_t max, int32_t period, int32_t loop) {
×
94
    if(period == 0) {
×
95
        period = 1;
×
96
    }
97
    int32_t range = abs(max - min);
×
98
    int32_t change = (loop/period) % 2;
×
99
    int32_t step = range/period;
×
100
    int32_t cnt = 0;    
×
101
    if(change)
×
102
       cnt = period - loop % period;
×
103
    else
104
       cnt = loop % period;
×
105

106
    return min + cnt * step;
×
107
}
108

109

110
// calc expression value like 10*sin(x) + 100
111
float funValueFloat(Field *field, int32_t angle, int32_t loop) {
×
112
    float radian = ATOR(angle);
×
113
    float funVal = 0;
×
114

115
    if (field->funType == FUNTYPE_SIN)
×
116
       funVal = sin(radian);
×
117
    else if (field->funType == FUNTYPE_COS)
×
118
       funVal = cos(radian);
×
119
    else if (field->funType == FUNTYPE_COUNT)
×
120
       funVal = (float)funCount(field->min, field->max, field->step, loop);
×
121
    else if (field->funType == FUNTYPE_SAW)
×
122
       funVal = (float)funSaw(field->min, field->max, field->period, loop + field->offset );
×
123
    else if (field->funType == FUNTYPE_SQUARE)
×
124
       funVal = (float)funSquare(field->min, field->max, field->period, loop + field->offset);
×
125
    else if (field->funType == FUNTYPE_TRI)
×
126
       funVal = (float)funTriAngle(field->min, field->max, field->period, loop + field->offset);
×
127

128
    if(field->multiple != 0)
×
129
       funVal *= field->multiple;
×
130
    
131
    if ( field->addend !=0 && field->random > 0 ) {
×
132
        float rate = taosRandom() % field->random;
×
133
        funVal += field->addend * (rate/100);
×
134
    } else if(field->addend !=0 ) {
×
135
        funVal += field->addend;
×
136
    }
137

138
    funVal += field->base;
×
139
    return funVal;
×
140
}
141

142
// calc expression value like 10*sin(x) + 100
143
int32_t funValueInt32(Field *field, int32_t angle, int32_t loop) {
×
144
    float radian = ATOR(angle);
×
145
    int32_t funVal = 0;
×
146

147
    if (field->funType == FUNTYPE_SIN)
×
148
       funVal = (int32_t)sin(radian);
×
149
    else if (field->funType == FUNTYPE_COS)
×
150
       funVal = (int32_t)cos(radian);
×
151
    else if (field->funType == FUNTYPE_COUNT)
×
152
       funVal = funCount(field->min, field->max, field->step, loop);
×
153
    else if (field->funType == FUNTYPE_SAW)
×
154
       funVal = funSaw(field->min, field->max, field->period, loop + field->offset );
×
155
    else if (field->funType == FUNTYPE_SQUARE)
×
156
       funVal = funSquare(field->min, field->max, field->period, loop + field->offset);
×
157
    else if (field->funType == FUNTYPE_TRI)
×
158
       funVal = funTriAngle(field->min, field->max, field->period, loop + field->offset);
×
159

160
    if(field->multiple != 0)
×
161
       funVal *= field->multiple;
×
162
    
163
    if ( field->addend !=0 && field->random > 0 ) {
×
164
        float rate = taosRandom() % field->random;
×
165
        funVal += field->addend * (rate/100);
×
166
    } else if(field->addend !=0 ) {
×
167
        funVal += field->addend;
×
168
    }
169

170
    funVal += field->base;
×
171

172
    return funVal;
×
173
}
174

175

176
static int usc2utf8(char *p, int unic) {
×
177
    int ret = 0;
×
178
    if (unic <= 0x0000007F) {
×
179
        *p = (unic & 0x7F);
×
180
        ret = 1;
×
181
    } else if (unic <= 0x000007FF) {
×
182
        *(p + 1) = (unic & 0x3F) | 0x80;
×
183
        *p = ((unic >> 6) & 0x1F) | 0xC0;
×
184
        ret = 2;
×
185
    } else if (unic <= 0x0000FFFF) {
×
186
        *(p + 2) = (unic & 0x3F) | 0x80;
×
187
        *(p + 1) = ((unic >> 6) & 0x3F) | 0x80;
×
188
        *p = ((unic >> 12) & 0x0F) | 0xE0;
×
189
        ret = 3;
×
190
    } else if (unic <= 0x001FFFFF) {
×
191
        *(p + 3) = (unic & 0x3F) | 0x80;
×
192
        *(p + 2) = ((unic >> 6) & 0x3F) | 0x80;
×
193
        *(p + 1) = ((unic >> 12) & 0x3F) | 0x80;
×
194
        *p = ((unic >> 18) & 0x07) | 0xF0;
×
195
        ret = 4;
×
196
    } else if (unic <= 0x03FFFFFF) {
×
197
        *(p + 4) = (unic & 0x3F) | 0x80;
×
198
        *(p + 3) = ((unic >> 6) & 0x3F) | 0x80;
×
199
        *(p + 2) = ((unic >> 12) & 0x3F) | 0x80;
×
200
        *(p + 1) = ((unic >> 18) & 0x3F) | 0x80;
×
201
        *p = ((unic >> 24) & 0x03) | 0xF8;
×
202
        ret = 5;
×
203
    // } else if (unic >= 0x04000000) {
204
    } else {
205
        *(p + 5) = (unic & 0x3F) | 0x80;
×
206
        *(p + 4) = ((unic >> 6) & 0x3F) | 0x80;
×
207
        *(p + 3) = ((unic >> 12) & 0x3F) | 0x80;
×
208
        *(p + 2) = ((unic >> 18) & 0x3F) | 0x80;
×
209
        *(p + 1) = ((unic >> 24) & 0x3F) | 0x80;
×
210
        *p = ((unic >> 30) & 0x01) | 0xFC;
×
211
        ret = 6;
×
212
    }
213

214
    return ret;
×
215
}
216

217
void rand_string(char *str, int size, bool chinese) {
×
218
    if (chinese) {
×
219
        char *pstr = str;
×
220
        while (size > 0) {
×
221
            // Chinese Character need 3 bytes space
222
            if (size < 3) {
×
223
                break;
×
224
            }
225
            // Basic Chinese Character's Unicode is from 0x4e00 to 0x9fa5
226
            int unic = 0x4e00 + taosRandom() % (0x9fa5 - 0x4e00);
×
227
            int move = usc2utf8(pstr, unic);
×
228
            pstr += move;
×
229
            size -= move;
×
230
        }
231
    } else {
232
        str[0] = 0;
×
233
        if (size > 0) {
×
234
            // --size;
235
            int n;
236
            for (n = 0; n < size; n++) {
×
237
                int key = taosRandom() % (unsigned int)(sizeof(charset) - 1);
×
238
                str[n] = charset[key];
×
239
            }
240
            str[n] = 0;
×
241
        }
242
    }
243
}
×
244

245
// generate prepare sql
246
char* genPrepareSql(SSuperTable *stbInfo, char* tagData, uint64_t tableSeq, char *db) {
×
247
    int   len = 0;
×
248
    char *prepare = benchCalloc(1, TSDB_MAX_ALLOWED_SQL_LEN, true);
×
249
    int n;
250
    char *tagQ = NULL;
×
251
    char *colQ = genQMark(stbInfo->cols->size);
×
252
    char *colNames = NULL;
×
253
    bool  tagQFree = false;
×
254

255
    if(tagData == NULL) {
×
256
        // if no tagData , replace with QMark
257
        tagQ = genQMark(stbInfo->tags->size);
×
258
        tagQFree = true;
×
259
    } else {
260
        tagQ = tagData + stbInfo->lenOfTags * tableSeq;
×
261
    }
262

263
    if (stbInfo->autoTblCreating) {
×
264
        char ttl[SMALL_BUFF_LEN] = "";
×
265
        if (stbInfo->ttl != 0) {
×
266
            snprintf(ttl, SMALL_BUFF_LEN, "TTL %d", stbInfo->ttl);
×
267
        }
268
        n = snprintf(prepare + len,
×
269
                       TSDB_MAX_ALLOWED_SQL_LEN - len,
×
270
                       "INSERT INTO ? USING `%s`.`%s` TAGS (%s) %s VALUES(?,%s)",
271
                       db, stbInfo->stbName, tagQ, ttl, colQ);
272
    } else {
273
        if (workingMode(g_arguments->connMode, g_arguments->dsn) == CONN_MODE_NATIVE) {
×
274
            // native
275
            n = snprintf(prepare + len, TSDB_MAX_ALLOWED_SQL_LEN - len,
×
276
                "INSERT INTO ? VALUES(?,%s)", colQ);
277
        } else {
278
            // websocket
279
            bool ntb = stbInfo->tags == NULL || stbInfo->tags->size == 0; // normal table
×
NEW
280
            colNames = genColNames(stbInfo->cols, !ntb, stbInfo->primaryKeyName);
×
281
            n = snprintf(prepare + len, TSDB_MAX_ALLOWED_SQL_LEN - len,
×
282
                "INSERT INTO `%s`.`%s`(%s) VALUES(%s,%s)", db, stbInfo->stbName, colNames,
283
                ntb ? "?" : "?,?", colQ);
284
        }
285
    }
286
    len += n;
×
287

288
    // free
289
    if (tagQFree) {
×
290
        tmfree(tagQ);
×
291
    }
292
    tmfree(colQ);
×
293
    tmfree(colNames);
×
294

295
    // check valid
296
    if (g_arguments->prepared_rand < g_arguments->reqPerReq) {
×
297
        infoPrint(
×
298
                  "in stmt mode, batch size(%u) can not larger than prepared "
299
                  "sample data size(%" PRId64
300
                  "), restart with larger prepared_rand or batch size will be "
301
                  "auto set to %" PRId64 "\n",
302
                  g_arguments->reqPerReq, g_arguments->prepared_rand,
303
                  g_arguments->prepared_rand);
304
        g_arguments->reqPerReq = g_arguments->prepared_rand;
×
305
    }
306

307
    return prepare;
×
308
}
309

310
int prepareStmt(TAOS_STMT *stmt, SSuperTable *stbInfo, char* tagData, uint64_t tableSeq, char *db) {
×
311
    char *prepare = genPrepareSql(stbInfo, tagData, tableSeq, db);
×
312
    if (taos_stmt_prepare(stmt, prepare, strlen(prepare))) {
×
313
        errorPrint("taos_stmt_prepare(%s) failed. errstr=%s\n", prepare, taos_stmt_errstr(stmt));
×
314
        tmfree(prepare);
×
315
        return -1;
×
316
    }
317
    debugPrint("succ call taos_stmt_prepare sql:%s\n", prepare);
×
318
    tmfree(prepare);
×
319
    return 0;
×
320
}
321

322
int prepareStmt2(TAOS_STMT2 *stmt2, SSuperTable *stbInfo, char* tagData, uint64_t tableSeq, char *db) {
×
323
    char *prepare = genPrepareSql(stbInfo, tagData, tableSeq, db);
×
324
    if (taos_stmt2_prepare(stmt2, prepare, strlen(prepare))) {
×
325
        errorPrint("taos_stmt2_prepare(%s) failed. errstr=%s\n", prepare, taos_stmt2_error(stmt2));
×
326
        tmfree(prepare);
×
327
        return -1;
×
328
    }
329
    debugPrint("succ call taos_stmt2_prepare. sql=%s\n", prepare);
×
330
    tmfree(prepare);
×
331
    return 0;
×
332
}
333

334

335
static bool getSampleFileNameByPattern(char *filePath,
×
336
                                       SSuperTable *stbInfo,
337
                                       int64_t child) {
338
    char *pos = strstr(stbInfo->childTblSample, "XXXX");
×
339
    snprintf(filePath, MAX_PATH_LEN, "%s", stbInfo->childTblSample);
×
340
    int64_t offset = (int64_t)pos - (int64_t)stbInfo->childTblSample;
×
341
    snprintf(filePath + offset,
×
342
             MAX_PATH_LEN - offset,
×
343
            "%s",
344
            stbInfo->childTblArray[child]->name);
×
345
    size_t len = strlen(stbInfo->childTblArray[child]->name);
×
346
    snprintf(filePath + offset + len,
×
347
            MAX_PATH_LEN - offset - len,
×
348
            "%s", pos +4);
349
    return true;
×
350
}
351

352
static int generateSampleFromCsv(char *buffer, char* file, FILE* fp, int32_t length, int64_t size) {
×
353
    size_t  n = 0;
×
354
    char *  line = NULL;
×
355
    int     getRows = 0;
×
356
    bool    needClose = false;
×
357

358
    if (file != NULL && fp == NULL) {
×
359
        fp = fopen(file, "r");
×
360
        if (fp == NULL) {
×
361
            errorPrint("open csv file failed. file=%s\n", file);
×
362
            return -1;
×
363
        }
364
        needClose = true;
×
365
    }
366

367
    while (1) {
×
368
        ssize_t readLen = 0;
×
369
#if defined(WIN32) || defined(WIN64)
370
        toolsGetLineFile(&line, &n, fp);
371
        readLen = n;
372
        if (0 == readLen) {
373
#else
374
        readLen = getline(&line, &n, fp);
×
375
        if (-1 == readLen) {
×
376
#endif
377
            if (0 != fseek(fp, 0, SEEK_SET)) {
×
378
                errorPrint("Failed to fseek , reason:%s\n", strerror(errno));
×
379
                if(needClose)
×
380
                    fclose(fp);
×
381
                return -1;
×
382
            }
383
            continue;
×
384
        }
385

386
        int32_t pos = (int32_t)(readLen - 1);
×
387
        if(pos > 0) {
×
388
            if (('\r' == line[pos]) || ('\n' == line[pos])) {
×
389
                line[pos] = 0;
×
390
            }
391
        }
392
        pos = (int32_t)(readLen - 2);
×
393
        if(pos > 0) {
×
394
            if (('\r' == line[pos]) || ('\n' == line[pos])) {
×
395
                line[pos] = 0;
×
396
            }
397
        }
398

399
        if (readLen == 0) {
×
400
            continue;
×
401
        }
402

403
        int64_t offset = ((int64_t)getRows) * length;
×
404
        memcpy(buffer + offset, line, readLen + 1);
×
405
        getRows++;
×
406

407
        if (getRows == size) {
×
408
            break;
×
409
        }
410
    }
411

412
    if(needClose) {
×
413
        fclose(fp);
×
414
    }
415

416
    tmfree(line);
×
417
    infoPrint("read data from csv file %s, read rows=%d\n", file, getRows);
×
418
    return 0;
×
419
}
420

421
static int getAndSetRowsFromCsvFile(char *sampleFile, uint64_t *insertRows) {
×
422
    FILE *  fp = fopen(sampleFile, "r");
×
423
    if (NULL == fp) {
×
424
        errorPrint("Failed to open sample file: %s, reason:%s\n",
×
425
                   sampleFile, strerror(errno));
426
        return -1;
×
427
    }
428

429
    int     line_count = 0;
×
430
    char *  buf = NULL;
×
431

432
    buf = benchCalloc(1, TSDB_MAX_ALLOWED_SQL_LEN, false);
×
433
    if (NULL == buf) {
×
434
        errorPrint("%s() failed to allocate memory!\n", __func__);
×
435
        fclose(fp);
×
436
        return -1;
×
437
    }
438

439
    while (fgets(buf, TSDB_MAX_ALLOWED_SQL_LEN, fp)) {
×
440
        line_count++;
×
441
    }
442
    *insertRows = line_count;
×
443
    fclose(fp);
×
444
    tmfree(buf);
×
445
    return 0;
×
446
}
447

448
uint32_t accumulateRowLen(BArray *fields, int iface) {
×
449
    uint32_t len = 0;
×
450
    for (int i = 0; i < fields->size; ++i) {
×
451
        Field *field = benchArrayGet(fields, i);
×
452
        switch (field->type) {
×
453
            case TSDB_DATA_TYPE_BINARY:
×
454
            case TSDB_DATA_TYPE_VARBINARY:
455
            case TSDB_DATA_TYPE_GEOMETRY:
456
            case TSDB_DATA_TYPE_NCHAR:
457
                len += field->length + 3;
×
458
                break;
×
459
            case TSDB_DATA_TYPE_INT:
×
460
            case TSDB_DATA_TYPE_UINT:
461
                len += INT_BUFF_LEN;
×
462
                break;
×
463

464
            case TSDB_DATA_TYPE_BIGINT:
×
465
            case TSDB_DATA_TYPE_UBIGINT:
466
                len += BIGINT_BUFF_LEN;
×
467
                break;
×
468

469
            case TSDB_DATA_TYPE_SMALLINT:
×
470
            case TSDB_DATA_TYPE_USMALLINT:
471
                len += SMALLINT_BUFF_LEN;
×
472
                break;
×
473

474
            case TSDB_DATA_TYPE_TINYINT:
×
475
            case TSDB_DATA_TYPE_UTINYINT:
476
                len += TINYINT_BUFF_LEN;
×
477
                break;
×
478

479
            case TSDB_DATA_TYPE_BOOL:
×
480
                len += BOOL_BUFF_LEN;
×
481
                break;
×
482

483
            case TSDB_DATA_TYPE_FLOAT:
×
484
                len += FLOAT_BUFF_LEN;
×
485
                break;
×
486

487
            case TSDB_DATA_TYPE_DOUBLE:
×
488
                len += DOUBLE_BUFF_LEN;
×
489
                break;
×
490

491
            case TSDB_DATA_TYPE_DECIMAL:
×
492
                len += DECIMAL_BUFF_LEN;
×
493
                break;
×
494

495
            case TSDB_DATA_TYPE_DECIMAL64:
×
496
                len += DECIMAL64_BUFF_LEN;
×
497
                break;
×
498

499
            case TSDB_DATA_TYPE_TIMESTAMP:
×
500
                len += TIMESTAMP_BUFF_LEN;
×
501
                break;
×
502
            case TSDB_DATA_TYPE_JSON:
×
503
                len += field->length * fields->size;
×
504
                return len;
×
505
        }
506
        len += 1;
×
507
        if (iface == SML_REST_IFACE || iface == SML_IFACE) {
×
508
            len += SML_LINE_SQL_SYNTAX_OFFSET + strlen(field->name);
×
509
        }
510
    }
511
    if (iface == SML_IFACE || iface == SML_REST_IFACE) {
×
512
        len += 2 * TSDB_TABLE_NAME_LEN * 2 + SML_LINE_SQL_SYNTAX_OFFSET;
×
513
    }
514
    len += TIMESTAMP_BUFF_LEN;
×
515
    return len;
×
516
}
517

518

519
int tmpStr(char *tmp, int iface, Field *field, int64_t k) {
×
520
    if (field->values) {
×
521
        int arraySize = tools_cJSON_GetArraySize(field->values);
×
522
        if (arraySize) {
×
523
            tools_cJSON *buf = tools_cJSON_GetArrayItem(
×
524
                    field->values,
×
525
                    taosRandom() % arraySize);
×
526
            snprintf(tmp, field->length,
×
527
                     "%s", buf->valuestring);
528
        } else {
529
            errorPrint("%s() cannot read correct value "
×
530
                       "from json file. array size: %d\n",
531
                       __func__, arraySize);
532
            return -1;
×
533
        }
534
    } else if (g_arguments->demo_mode) {
×
535
        unsigned int tmpRand = taosRandom();
×
536
        if (g_arguments->chinese) {
×
537
            snprintf(tmp, field->length, "%s",
×
538
                     locations_chinese[tmpRand % 10]);
×
539
        } else if (SML_IFACE == iface) {
×
540
            snprintf(tmp, field->length, "%s",
×
541
                     locations_sml[tmpRand % 10]);
×
542
        } else {
543
            snprintf(tmp, field->length, "%s",
×
544
                     locations[tmpRand % 10]);
×
545
        }
546
    } else {
547
        if(field->gen == GEN_ORDER) {
×
548
            snprintf(tmp, field->length, "%"PRId64, k);
×
549
        } else {
550
            rand_string(tmp, taosRandom() % field->length, g_arguments->chinese);
×
551
        }
552
    }
553
    return 0;
×
554
}
555

556
int tmpGeometry(char *tmp, int iface, Field *field, int64_t k) {
×
557
    // values
558
    if (field->values) {
×
559
        int arraySize = tools_cJSON_GetArraySize(field->values);
×
560
        if (arraySize) {
×
561
            tools_cJSON *buf = tools_cJSON_GetArrayItem(
×
562
                    field->values,
×
563
                    taosRandom() % arraySize);
×
564
            snprintf(tmp, field->length,
×
565
                     "%s", buf->valuestring);
566
        } else {
567
            errorPrint("%s() cannot read correct value "
×
568
                       "from json file. array size: %d\n",
569
                       __func__, arraySize);
570
            return -1;
×
571
        }
572
        return 0;
×
573
    }
574

575
    // gen point count
576
    int32_t cnt = field->length / 24;
×
577
    if(cnt < 2) {
×
578
        snprintf(tmp, field->length, "POINT(%d %d)", tmpUint16(field), tmpUint16(field));
×
579
        return 0;
×
580
    }
581
    
582
    int32_t pos = snprintf(tmp, field->length, "LINESTRING(");
×
583
    char * format = "%d %d,";
×
584
    for(int32_t i = 0; i < cnt; i++) {
×
585
        if (i == cnt - 1) {
×
586
            format = "%d %d";
×
587
        } 
588
        pos += snprintf(tmp + pos, field->length - pos, format, tmpUint16(field), tmpUint16(field));
×
589
    }
590
    strcat(tmp, ")");
×
591

592
    return 0;
×
593
}
594

595
bool tmpBool(Field *field) {
×
596
    bool boolTmp;
597
    if (field->min == field->max) {
×
598
        boolTmp = (field->min)?1:0;
×
599
    } else {
600
        boolTmp = (taosRandom() % 2)&1;
×
601
    }
602
    return boolTmp;
×
603
}
604

605
int8_t tmpInt8Impl(Field *field, int64_t k) {
×
606
    int8_t tinyint = field->min;
×
607
    if (field->min != field->max) {
×
608
        tinyint += COL_GEN % (field->max - field->min);
×
609
    }
610
    return tinyint;
×
611
}
612

613
uint8_t tmpUint8Impl(Field *field, int64_t k) {
×
614
    uint8_t utinyint = field->min;
×
615
    if (field->min != field->max) {
×
616
        utinyint += (COL_GEN % (field->max - field->min));
×
617
    }
618
    return utinyint;
×
619
}
620

621
int16_t tmpInt16Impl(Field *field, int64_t k) {
×
622
    int16_t smallint = field->min;
×
623
    if (field->min != field->max) {
×
624
        smallint += (COL_GEN % (field->max - field->min));
×
625
    }
626
    return smallint;
×
627
}
628

629
uint16_t tmpUint16Impl(Field *field, int64_t k) {
×
630
    uint16_t usmallintTmp = field->min;
×
631
    if (field->max != field->min) {
×
632
        usmallintTmp += (COL_GEN % (field->max - field->min));
×
633
    }
634
    return usmallintTmp;
×
635
}
636

637
int tmpInt32Impl(Field *field, int i, int angle, int32_t k) {
×
638
    int intTmp;
639
    if (field->funType != FUNTYPE_NONE) {
×
640
        // calc from function
641
        intTmp = funValueInt32(field, angle, k);
×
642
    } else if ((g_arguments->demo_mode) && (i == 0)) {
×
643
        unsigned int tmpRand = taosRandom();
×
644
        intTmp = tmpRand % 10 + 1;
×
645
    } else if ((g_arguments->demo_mode) && (i == 1)) {
×
646
        intTmp = 105 + taosRandom() % 10;
×
647
    } else {
648
        if (field->min < (-1 * (RAND_MAX >> 1))) {
×
649
            field->min = -1 * (RAND_MAX >> 1);
×
650
        }
651
        if (field->max > (RAND_MAX >> 1)) {
×
652
            field->max = RAND_MAX >> 1;
×
653
        }
654
        intTmp = field->min;
×
655
        if (field->max != field->min) {
×
656
            intTmp += (COL_GEN % (field->max - field->min));
×
657
        }
658
    }
659
    return intTmp;
×
660
}
661

662
int tmpInt32ImplTag(Field *field, int i, int k) {
×
663
    int intTmp;
664

665
    if (field->min < (-1 * (RAND_MAX >> 1))) {
×
666
        field->min = -1 * (RAND_MAX >> 1);
×
667
    }
668
    if (field->max > (RAND_MAX >> 1)) {
×
669
        field->max = RAND_MAX >> 1;
×
670
    }
671
    intTmp = field->min;
×
672
    if (field->max != field->min) {
×
673
        intTmp += (COL_GEN % (field->max - field->min));
×
674
    }
675
    return intTmp;
×
676
}
677

678

679
uint32_t tmpUint32Impl(Field *field, int i, int angle, int64_t k) {
×
680
    uint32_t intTmp;
681
    if (field->funType != FUNTYPE_NONE) {
×
682
        // calc from function
683
        intTmp = funValueInt32(field, angle, k);
×
684
    } else if ((g_arguments->demo_mode) && (i == 0)) {
×
685
        unsigned int tmpRand = taosRandom();
×
686
        intTmp = tmpRand % 10 + 1;
×
687
    } else if ((g_arguments->demo_mode) && (i == 1)) {
×
688
        intTmp = 105 + taosRandom() % 10;
×
689
    } else {
690
        intTmp = field->min;
×
691
        if (field->max != field->min) {
×
692
            intTmp += (COL_GEN % (field->max - field->min));
×
693
        }
694
    }
695
    return intTmp;
×
696
}
697

698
int64_t tmpInt64Impl(Field *field, int32_t angle, int32_t k) {
×
699
    int64_t bigintTmp = field->min;
×
700
    if(field->funType != FUNTYPE_NONE) {
×
701
        bigintTmp = funValueInt32(field, angle, k);
×
702
    } else if (field->min != field->max) {
×
703
        bigintTmp += (COL_GEN % (field->max - field->min));
×
704
    }
705
    return bigintTmp;
×
706
}
707

708
uint64_t tmpUint64Impl(Field *field, int32_t angle, int64_t k) {
×
709
    uint64_t bigintTmp = field->min;
×
710
    if(field->funType != FUNTYPE_NONE) {
×
711
        bigintTmp = funValueInt32(field, angle, k);
×
712
    } else if (field->min != field->max) {
×
713
        bigintTmp += (COL_GEN % (field->max - field->min));
×
714
    }
715
    return bigintTmp;
×
716
}
717

718
float tmpFloatImpl(Field *field, int i, int32_t angle, int32_t k) {
×
719
    float floatTmp = (float)field->min;
×
720
    if(field->funType != FUNTYPE_NONE) {
×
721
        floatTmp = funValueFloat(field, angle, k);
×
722
    } else {
723
        if (field->max != field->min) {
×
724
            if (field->gen == GEN_ORDER) {
×
725
                floatTmp += (k % (field->max - field->min));
×
726
            } else {
727
                floatTmp += ((taosRandom() %
×
728
                        (field->max - field->min))
×
729
                    + (taosRandom() % 1000) / 1000.0);
×
730
            }
731
        }
732
        if (g_arguments->demo_mode && i == 0) {
×
733
            floatTmp = (float)(9.8 + 0.04 * (taosRandom() % 10)
×
734
                + floatTmp / 1000000000);
×
735
        } else if (g_arguments->demo_mode && i == 2) {
×
736
            floatTmp = (float)((105 + taosRandom() % 10
×
737
                + floatTmp / 1000000000) / 360);
×
738
        }
739
    }
740

741
    if (field->scalingFactor > 0) {
×
742
        if (field->scalingFactor > 1)
×
743
            floatTmp = floatTmp / field->scalingFactor;
×
744

745
        if (floatTmp > field->maxInDbl)
×
746
            floatTmp = field->maxInDbl;
×
747
        else if (floatTmp < field->minInDbl)
×
748
            floatTmp = field->minInDbl;
×
749
    }
750

751
    return floatTmp;
×
752
}
753

754
double tmpDoubleImpl(Field *field, int32_t angle, int32_t k) {
×
755
    double doubleTmp = (double)(field->min);
×
756
    if(field->funType != FUNTYPE_NONE) {
×
757
        doubleTmp = funValueFloat(field, angle, k);
×
758
    } else if (field->max != field->min) {
×
759
        if(field->gen == GEN_ORDER) {
×
760
            doubleTmp += k % (field->max - field->min);
×
761
        } else {
762
            doubleTmp += ((taosRandom() %
×
763
                (field->max - field->min)) +
×
764
                taosRandom() % 1000000 / 1000000.0);
×
765
        }
766
    }
767

768
    if (field->scalingFactor > 0) {
×
769
        if (field->scalingFactor > 1) {
×
770
            doubleTmp /= field->scalingFactor;
×
771
        }
772

773
        if (doubleTmp > field->maxInDbl)
×
774
            doubleTmp = field->maxInDbl;
×
775
        else if (doubleTmp < field->minInDbl)
×
776
            doubleTmp = field->minInDbl;
×
777
    }
778

779
    return doubleTmp;
×
780
}
781

782
static int tmpJson(char *sampleDataBuf,
×
783
                   int bufLen, int64_t pos,
784
                   int fieldsSize, Field *field) {
785
    int n = snprintf(sampleDataBuf + pos, bufLen - pos, "'{");
×
786
    if (n < 0 || n >= bufLen - pos) {
×
787
        errorPrint("%s() LN%d snprintf overflow\n",
×
788
                   __func__, __LINE__);
789
        return -1;
×
790
    }
791
    for (int j = 0; j < fieldsSize; ++j) {
×
792
        // key
793
        n += snprintf(sampleDataBuf + pos + n, bufLen - pos - n,
×
794
                        "\"k%d\":", j);
795
        if (n < 0 || n >= bufLen - pos) {
×
796
            errorPrint("%s() LN%d snprintf overflow\n",
×
797
                       __func__, __LINE__);
798
            return -1;
×
799
        }
800
        // value
801
        char *buf = benchCalloc(1, field->length + 1, false);
×
802
        rand_string(buf, 12, g_arguments->chinese);
×
803
        n += snprintf(sampleDataBuf + pos + n, bufLen - pos - n,
×
804
                        "\"%s\",", buf);
805
        if (n < 0 || n >= bufLen - pos) {
×
806
            errorPrint("%s() LN%d snprintf overflow\n",
×
807
                       __func__, __LINE__);
808
            tmfree(buf);
×
809
            return -1;
×
810
        }
811
        tmfree(buf);
×
812
    }
813
    n += snprintf(sampleDataBuf + pos + n - 1,
×
814
                    bufLen - pos - n, "}'");
×
815
    if (n < 0 || n >= bufLen - pos) {
×
816
        errorPrint("%s() LN%d snprintf overflow\n",
×
817
                   __func__, __LINE__);
818
        return -1;
×
819
    }
820

821
    return n;
×
822
}
823

824

825
static uint64_t generateRandomUint64(uint64_t range) {
×
826
    uint64_t randomValue;
827

828
    if (range <= (uint64_t)RAND_MAX) {
×
829
        randomValue = (uint64_t)rand() % range;
×
830
    } else {
831
        int bitsPerRand = 0;
×
832
        for (uint64_t r = RAND_MAX; r > 0; r >>= 1) {
×
833
            bitsPerRand++;
×
834
        }
835

836
        uint64_t result;
837
        uint64_t threshold;
838

839
        do {
840
            result = 0;
×
841
            int bitsAccumulated = 0;
×
842

843
            while (bitsAccumulated < 64) {
×
844
                uint64_t part = (uint64_t)rand();
×
845
                int bits = (64 - bitsAccumulated) < bitsPerRand ? (64 - bitsAccumulated) : bitsPerRand;
×
846
                part &= (1ULL << bits) - 1;
×
847
                result |= part << bitsAccumulated;
×
848
                bitsAccumulated += bits;
×
849
            }
850

851
            // rejecting sample
852
            threshold = (UINT64_MAX / range) * range;
×
853
            threshold = (threshold == 0) ? 0 : threshold - 1;
×
854

855
        } while (result > threshold);
×
856

857
        randomValue = result % range;
×
858
    }
859

860
    return randomValue;
×
861
}
862

863

864
static uint64_t randUint64(uint64_t min, uint64_t max) {
×
865
    if (min >= max || (max - min) == UINT64_MAX) {
×
866
        return min;
×
867
    }
868

869
    uint64_t range = max - min + 1;
×
870
    return min + generateRandomUint64(range);
×
871
}
872

873

874
static int64_t randInt64(int64_t min, int64_t max) {
×
875
    if (min >= max || ((uint64_t)max - (uint64_t)min) == UINT64_MAX) {
×
876
        return min;
×
877
    }
878

879
    uint64_t range = (uint64_t)max - (uint64_t)min + 1;
×
880
    return (int64_t)(min + generateRandomUint64(range));
×
881
}
882

883

884
static void decimal64Rand(Decimal64* result, const Decimal64* min, const Decimal64* max) {
×
885
    int64_t temp = 0;
×
886
    
887
    do {
888
        temp = randInt64(DECIMAL64_GET_VALUE(min), DECIMAL64_GET_VALUE(max));
×
889
    } while (temp < DECIMAL64_GET_VALUE(min) || temp > DECIMAL64_GET_VALUE(max));
×
890

891
    DECIMAL64_SET_VALUE(result, temp);
×
892
}
×
893

894

895
static void decimal128Rand(Decimal128* result, const Decimal128* min, const Decimal128* max) {
×
896
    int64_t  high   = 0;
×
897
    uint64_t low    = 0;
×
898
    Decimal128 temp = {0};
×
899

900
    int64_t minHigh = DECIMAL128_HIGH_WORD(min);
×
901
    int64_t maxHigh = DECIMAL128_HIGH_WORD(max);
×
902
    uint64_t minLow = DECIMAL128_LOW_WORD(min);
×
903
    uint64_t maxLow = DECIMAL128_LOW_WORD(max);
×
904

905
    do {
906
        // high byte
907
        high = randInt64(minHigh, maxHigh);
×
908

909
        // low byte
910
        if (high == minHigh && high == maxHigh) {
×
911
            low = randUint64(minLow, maxLow);   
×
912
        } else if (high == minHigh) {
×
913
            low = randUint64(minLow, UINT64_MAX);
×
914
        } else if (high == maxHigh) {
×
915
            low = randUint64(0, maxLow);
×
916
        } else {
917
            low = randUint64(0, UINT64_MAX);
×
918
        }
919

920
        DECIMAL128_SET_HIGH_WORD(&temp, high);
×
921
        DECIMAL128_SET_LOW_WORD(&temp, low);
×
922

923
    } while (decimal128BCompare(&temp, min) < 0 || decimal128BCompare(&temp, max) > 0);
×
924

925
    *result = temp;
×
926
}
×
927

928

929
Decimal64 tmpDecimal64Impl(Field* field, int32_t angle, int32_t k) {
×
930
    (void)angle;
931
    (void)k;
932

933
    Decimal64 result = {0};
×
934
    decimal64Rand(&result, &field->decMin.dec64, &field->decMax.dec64);
×
935
    return result;
×
936
}
937

938

939
Decimal128 tmpDecimal128Impl(Field* field, int32_t angle, int32_t k) {
×
940
    (void)angle;
941
    (void)k;
942

943
    Decimal128 result = {0};
×
944
    decimal128Rand(&result, &field->decMin.dec128, &field->decMax.dec128);
×
945
    return result;
×
946
}
947

948

949
static int generateRandDataSQL(SSuperTable *stbInfo, char *sampleDataBuf,
×
950
                     int64_t bufLen,
951
                      int lenOfOneRow, BArray * fields, int64_t loop,
952
                      bool tag, int64_t loopBegin) {
953
                        
954
    int64_t index = loopBegin;
×
955
    int angle = stbInfo->startTimestamp % 360; // 0 ~ 360
×
956
    for (int64_t k = 0; k < loop; ++k, ++index) {
×
957
        int64_t pos = k * lenOfOneRow;
×
958
        int fieldsSize = fields->size;
×
959
        for (int i = 0; i < fieldsSize; ++i) {
×
960
            Field * field = benchArrayGet(fields, i);
×
961
            if (field->none) {
×
962
                continue;
×
963
            }
964

965
            int n = 0;
×
966
            if (field->null) {
×
967
                n = snprintf(sampleDataBuf + pos, bufLen - pos, "null,");
×
968
                if (n < 0 || n >= bufLen - pos) {
×
969
                    errorPrint("%s() LN%d snprintf overflow\n",
×
970
                               __func__, __LINE__);
971
                    return -1;
×
972
                } else {
973
                    pos += n;
×
974
                    continue;
×
975
                }
976
            }
977
            switch (field->type) {
×
978
                case TSDB_DATA_TYPE_BOOL: {
×
979
                    bool boolTmp = tmpBool(field);
×
980
                    n = snprintf(sampleDataBuf + pos, bufLen - pos,
×
981
                                    "%s,", boolTmp ? "true" : "false");
982
                    break;
×
983
                }
984
                case TSDB_DATA_TYPE_TINYINT: {
×
985
                    int8_t tinyint = tmpInt8Impl(field, index);
×
986
                    n = snprintf(sampleDataBuf + pos, bufLen - pos,
×
987
                                    "%d,", tinyint);
988
                    break;
×
989
                }
990
                case TSDB_DATA_TYPE_UTINYINT: {
×
991
                    uint8_t utinyint = tmpUint8Impl(field, index);
×
992
                    n = snprintf(sampleDataBuf + pos,
×
993
                                    bufLen - pos, "%u,", utinyint);
×
994
                    break;
×
995
                }
996
                case TSDB_DATA_TYPE_SMALLINT: {
×
997
                    int16_t smallint = tmpInt16Impl(field, index);
×
998
                    n = snprintf(sampleDataBuf + pos, bufLen - pos,
×
999
                                        "%d,", smallint);
1000
                    break;
×
1001
                }
1002
                case TSDB_DATA_TYPE_USMALLINT: {
×
1003
                    uint16_t usmallint = tmpUint16Impl(field, index);
×
1004
                    n = snprintf(sampleDataBuf + pos, bufLen - pos,
×
1005
                                        "%u,", usmallint);
1006
                    break;
×
1007
                }
1008
                case TSDB_DATA_TYPE_INT: {
×
1009
                    int32_t intTmp = tmpInt32Impl(field, i, angle, index);
×
1010
                    n = snprintf(sampleDataBuf + pos, bufLen - pos,
×
1011
                                        "%d,", intTmp);
1012
                    break;
×
1013
                }
1014
                case TSDB_DATA_TYPE_BIGINT: {
×
1015
                    int64_t bigintTmp = tmpInt64Impl(field, angle, index);
×
1016
                    n = snprintf(sampleDataBuf + pos,
×
1017
                                 bufLen - pos, "%"PRId64",", bigintTmp);
×
1018
                    break;
×
1019
                }
1020
                case TSDB_DATA_TYPE_UINT: {
×
1021
                    uint32_t uintTmp = tmpUint32Impl(field, i, angle, index);
×
1022
                    n = snprintf(sampleDataBuf + pos,
×
1023
                                 bufLen - pos, "%u,", uintTmp);
×
1024
                    break;
×
1025
                }
1026
                case TSDB_DATA_TYPE_UBIGINT:
×
1027
                case TSDB_DATA_TYPE_TIMESTAMP: {
1028
                    uint64_t ubigintTmp = tmpUint64Impl(field, angle, index);
×
1029
                    n = snprintf(sampleDataBuf + pos,
×
1030
                                 bufLen - pos, "%"PRIu64",", ubigintTmp);
×
1031
                    break;
×
1032
                }
1033
                case TSDB_DATA_TYPE_FLOAT: {
×
1034
                    float floatTmp = tmpFloatImpl(field, i, angle, index);
×
1035
                    n = snprintf(sampleDataBuf + pos, bufLen - pos,
×
1036
                                        "%f,", floatTmp);
1037
                    break;
×
1038
                }
1039
                case TSDB_DATA_TYPE_DOUBLE: {
×
1040
                    double double_ =  tmpDoubleImpl(field, angle, index);
×
1041
                    n = snprintf(sampleDataBuf + pos, bufLen - pos,
×
1042
                                        "%f,", double_);
1043
                    break;
×
1044
                }
1045
                case TSDB_DATA_TYPE_DECIMAL: {
×
1046
                    Decimal128 dec = tmpDecimal128Impl(field, angle, index);
×
1047
                    int ret = decimal128ToString(&dec, field->precision, field->scale, sampleDataBuf + pos, bufLen - pos);
×
1048
                    if (ret != 0) {
×
1049
                        errorPrint("%s() LN%d precision: %d, scale: %d, high: %" PRId64 ", low: %" PRIu64 "\n",
×
1050
                                __func__, __LINE__, field->precision, field->scale, DECIMAL128_HIGH_WORD(&dec), DECIMAL128_LOW_WORD(&dec));
1051
                        return -1;
×
1052
                    }
1053
                    size_t decLen = strlen(sampleDataBuf + pos);
×
1054
                    n = snprintf(sampleDataBuf + pos + decLen, bufLen - pos - decLen, ",");
×
1055
                    n += decLen;
×
1056
                    break;
×
1057
                }
1058
                case TSDB_DATA_TYPE_DECIMAL64: {
×
1059
                    Decimal64 dec = tmpDecimal64Impl(field, angle, index);
×
1060
                    int ret = decimal64ToString(&dec, field->precision, field->scale, sampleDataBuf + pos, bufLen - pos);
×
1061
                    if (ret != 0) {
×
1062
                        errorPrint("%s() LN%d precision: %d, scale: %d, value: %" PRId64 "\n",
×
1063
                                __func__, __LINE__, field->precision, field->scale, DECIMAL64_GET_VALUE(&dec));
1064
                        return -1;
×
1065
                    }
1066
                    size_t decLen = strlen(sampleDataBuf + pos);
×
1067
                    n = snprintf(sampleDataBuf + pos + decLen, bufLen - pos - decLen, ",");
×
1068
                    n += decLen;
×
1069
                    break;
×
1070
                }
1071
                case TSDB_DATA_TYPE_BINARY:
×
1072
                case TSDB_DATA_TYPE_VARBINARY:
1073
                case TSDB_DATA_TYPE_NCHAR: {
1074
                    char *tmp = benchCalloc(1, field->length + 1, false);
×
1075
                    if (0 != tmpStr(tmp, stbInfo->iface, field, index)) {
×
1076
                        free(tmp);
×
1077
                        return -1;
×
1078
                    }
1079
                    n = snprintf(sampleDataBuf + pos, bufLen - pos,
×
1080
                                        "'%s',", tmp);
1081
                    tmfree(tmp);
×
1082
                    break;
×
1083
                }
1084
                case TSDB_DATA_TYPE_GEOMETRY: {
×
1085
                    int   bufferSize = field->length + 1;
×
1086
                    char *tmp = benchCalloc(1, bufferSize, false);
×
1087
                    if (0 != tmpGeometry(tmp, stbInfo->iface, field, i)) {
×
1088
                        free(tmp);
×
1089
                        return -1;
×
1090
                    }
1091
                    n = snprintf(sampleDataBuf + pos, bufLen - pos, "'%s',", tmp);
×
1092
                    tmfree(tmp);
×
1093
                    break;
×
1094
                }
1095
                case TSDB_DATA_TYPE_JSON: {
×
1096
                    n = tmpJson(sampleDataBuf, bufLen, pos, fieldsSize, field);
×
1097
                    if (n == -1) {
×
1098
                        return -1;
×
1099
                    }
1100
                    pos += n;
×
1101
                    goto skip_sql;
×
1102
                }
1103
            }
1104
            if (TSDB_DATA_TYPE_JSON != field->type) {
×
1105
                if (n < 0 || n >= bufLen - pos) {
×
1106
                    errorPrint("%s() LN%d snprintf overflow\n",
×
1107
                               __func__, __LINE__);
1108
                    return -1;
×
1109
                } else {
1110
                    pos += n;
×
1111
                }
1112
            }
1113
        }
1114
skip_sql:
×
1115
        *(sampleDataBuf + pos - 1) = 0;
×
1116
        angle += stbInfo->timestamp_step/stbInfo->angle_step;
×
1117
        if (angle > 360) {
×
1118
            angle -= 360;
×
1119
        }
1120
}
1121

1122
    return 0;
×
1123
}
1124

1125
static int fillStmt(
×
1126
    SSuperTable *stbInfo,
1127
    char *sampleDataBuf,
1128
    int64_t bufLen,
1129
    int lenOfOneRow, BArray *fields,
1130
    int64_t loop, bool tag, BArray *childCols, int64_t loopBegin) {
1131
    int angle = stbInfo->startTimestamp % 360; // 0 ~ 360
×
1132
    debugPrint("fillStml stbname=%s loop=%"PRId64" istag=%d  fieldsSize=%d\n", stbInfo->stbName, loop, tag, (int32_t)fields->size);
×
1133
    int64_t index = loopBegin;
×
1134
    for (int64_t k = 0; k < loop; ++k, ++index) {
×
1135
        int64_t pos = k * lenOfOneRow;
×
1136
        char* line = sampleDataBuf + pos;
×
1137
        int fieldsSize = fields->size;
×
1138
        for (int i = 0; i < fieldsSize; ++i) {
×
1139
            Field * field = benchArrayGet(fields, i);
×
1140
            ChildField *childCol = NULL;
×
1141
            if (childCols) {
×
1142
                childCol = benchArrayGet(childCols, i);
×
1143
            }
1144
            int64_t n = 0;
×
1145

1146
            // 
1147
            if (childCol) {
×
1148
                childCol->stmtData.is_null[k] = 0;
×
1149
                childCol->stmtData.lengths[k] = field->length;
×
1150
            } else {
1151
                field->stmtData.is_null[k] = 0;
×
1152
                field->stmtData.lengths[k] = field->length;
×
1153
            }
1154

1155
            switch (field->type) {
×
1156
                case TSDB_DATA_TYPE_BOOL: {
×
1157
                    bool boolTmp = tmpBool(field);
×
1158
                    if (childCol) {
×
1159
                        ((bool *)childCol->stmtData.data)[k] = boolTmp;
×
1160
                    } else {
1161
                        ((bool *)field->stmtData.data)[k] = boolTmp;
×
1162
                    }
1163
                    n = snprintf(sampleDataBuf + pos, bufLen - pos,
×
1164
                                 "%d,", boolTmp);
1165
                    break;
×
1166
                }
1167
                case TSDB_DATA_TYPE_TINYINT: {
×
1168
                    int8_t tinyintTmp = tmpInt8Impl(field, index);
×
1169
                    if (childCol) {
×
1170
                        ((int8_t *)childCol->stmtData.data)[k] = tinyintTmp;
×
1171
                    } else {
1172
                        ((int8_t *)field->stmtData.data)[k] = tinyintTmp;
×
1173
                    }
1174
                    n = snprintf(sampleDataBuf + pos, bufLen - pos,
×
1175
                                 "%d,", tinyintTmp);
1176
                    break;
×
1177
                }
1178
                case TSDB_DATA_TYPE_UTINYINT: {
×
1179
                    uint8_t utinyintTmp = tmpUint8Impl(field, index);
×
1180
                    if (childCol) {
×
1181
                        ((uint8_t *)childCol->stmtData.data)[k] = utinyintTmp;
×
1182
                    } else {
1183
                        ((uint8_t *)field->stmtData.data)[k] = utinyintTmp;
×
1184
                    }
1185
                    n = snprintf(sampleDataBuf + pos,
×
1186
                                 bufLen - pos, "%u,", utinyintTmp);
×
1187
                    break;
×
1188
                }
1189
                case TSDB_DATA_TYPE_SMALLINT: {
×
1190
                    int16_t smallintTmp = tmpInt16Impl(field, index);
×
1191
                    if (childCol) {
×
1192
                        ((int16_t *)childCol->stmtData.data)[k] = smallintTmp;
×
1193
                    } else {
1194
                        ((int16_t *)field->stmtData.data)[k] = smallintTmp;
×
1195
                    }
1196
                    n = snprintf(sampleDataBuf + pos, bufLen - pos,
×
1197
                                        "%d,", smallintTmp);
1198
                    break;
×
1199
                }
1200
                case TSDB_DATA_TYPE_USMALLINT: {
×
1201
                    uint16_t usmallintTmp = tmpUint16Impl(field, index);
×
1202
                    if (childCol) {
×
1203
                        ((uint16_t *)childCol->stmtData.data)[k] = usmallintTmp;
×
1204
                    } else {
1205
                        ((uint16_t *)field->stmtData.data)[k] = usmallintTmp;
×
1206
                    }
1207
                    n = snprintf(sampleDataBuf + pos, bufLen - pos,
×
1208
                                        "%u,", usmallintTmp);
1209
                    break;
×
1210
                }
1211
                case TSDB_DATA_TYPE_INT: {
×
1212
                    int32_t intTmp = tmpInt32Impl(field, i, angle, index);
×
1213
                    if (childCol) {
×
1214
                        ((int32_t *)childCol->stmtData.data)[k] = intTmp;
×
1215
                    } else {
1216
                        ((int32_t *)field->stmtData.data)[k] = intTmp;
×
1217
                    }
1218
                    n = snprintf(sampleDataBuf + pos, bufLen - pos,
×
1219
                                        "%d,", intTmp);
1220
                    break;
×
1221
                }
1222
                case TSDB_DATA_TYPE_BIGINT: {
×
1223
                    int64_t bigintTmp = tmpInt64Impl(field, angle, index);
×
1224
                    if (childCol) {
×
1225
                        ((int64_t *)childCol->stmtData.data)[k] = bigintTmp;
×
1226
                    } else {
1227
                        ((int64_t *)field->stmtData.data)[k] = bigintTmp;
×
1228
                    }
1229
                    n = snprintf(sampleDataBuf + pos,
×
1230
                                 bufLen - pos, "%"PRId64",", bigintTmp);
×
1231
                    break;
×
1232
                }
1233
                case TSDB_DATA_TYPE_UINT: {
×
1234
                    uint32_t uintTmp = tmpUint32Impl(field, i, angle, index);
×
1235
                    if (childCol) {
×
1236
                        ((uint32_t *)childCol->stmtData.data)[k] = uintTmp;
×
1237
                    } else {
1238
                        ((uint32_t *)field->stmtData.data)[k] = uintTmp;
×
1239
                    }
1240
                    n = snprintf(sampleDataBuf + pos,
×
1241
                                 bufLen - pos, "%u,", uintTmp);
×
1242
                    break;
×
1243
                }
1244
                case TSDB_DATA_TYPE_UBIGINT:
×
1245
                case TSDB_DATA_TYPE_TIMESTAMP: {
1246
                    uint64_t ubigintTmp = tmpUint64Impl(field, angle, index);
×
1247
                    if (childCol) {
×
1248
                        ((uint64_t *)childCol->stmtData.data)[k] = ubigintTmp;
×
1249
                    } else {
1250
                        ((uint64_t *)field->stmtData.data)[k] = ubigintTmp;
×
1251
                    }
1252
                    n = snprintf(sampleDataBuf + pos,
×
1253
                                 bufLen - pos, "%"PRIu64",", ubigintTmp);
×
1254
                    break;
×
1255
                }
1256
                case TSDB_DATA_TYPE_FLOAT: {
×
1257
                    float floatTmp = tmpFloatImpl(field, i, angle, index);
×
1258
                    if (childCol) {
×
1259
                        ((float *)childCol->stmtData.data)[k] = floatTmp;
×
1260
                    } else {
1261
                        ((float *)field->stmtData.data)[k] = floatTmp;
×
1262
                    }
1263
                    n = snprintf(sampleDataBuf + pos, bufLen - pos,
×
1264
                                        "%f,", floatTmp);
1265
                    break;
×
1266
                }
1267
                case TSDB_DATA_TYPE_DOUBLE: {
×
1268
                    double doubleTmp = tmpDoubleImpl(field, angle, index);
×
1269
                    if (childCol) {
×
1270
                        ((double *)childCol->stmtData.data)[k] = doubleTmp;
×
1271
                    } else {
1272
                        ((double *)field->stmtData.data)[k] = doubleTmp;
×
1273
                    }
1274
                    n = snprintf(sampleDataBuf + pos, bufLen - pos,
×
1275
                                        "%f,", doubleTmp);
1276
                    break;
×
1277
                }
1278
                case TSDB_DATA_TYPE_BINARY:
×
1279
                case TSDB_DATA_TYPE_VARBINARY:
1280
                case TSDB_DATA_TYPE_NCHAR: {
1281
                    char *tmp = benchCalloc(1, field->length + 1, false);
×
1282
                    if (0 != tmpStr(tmp, stbInfo->iface, field, k)) {
×
1283
                        free(tmp);
×
1284
                        return -1;
×
1285
                    }
1286
                    if (childCol) {
×
1287
                        snprintf((char *)childCol->stmtData.data
×
1288
                                    + k * field->length,
×
1289
                                 field->length,
×
1290
                                "%s", tmp);
1291
                    } else {
1292
                        snprintf((char *)field->stmtData.data
×
1293
                                    + k * field->length,
×
1294
                                 field->length,
×
1295
                                "%s", tmp);
1296
                    }
1297
                    n = snprintf(sampleDataBuf + pos, bufLen - pos,
×
1298
                                        "'%s',", tmp);
1299
                    tmfree(tmp);
×
1300
                    break;
×
1301
                }
1302
                case TSDB_DATA_TYPE_GEOMETRY: {
×
1303
                    char *tmp = benchCalloc(1, field->length + 1, false);
×
1304
                    if (0 != tmpGeometry(tmp, stbInfo->iface, field, k)) {
×
1305
                        tmfree(tmp);
×
1306
                        return -1;
×
1307
                    }
1308
                    if (childCol) {
×
1309
                        snprintf((char *)childCol->stmtData.data
×
1310
                                    + k * field->length,
×
1311
                                 field->length,
×
1312
                                "%s", tmp);
1313
                    } else {
1314
                        snprintf((char *)field->stmtData.data
×
1315
                                    + k * field->length,
×
1316
                                 field->length,
×
1317
                                "%s", tmp);
1318
                    }
1319
                    n = snprintf(sampleDataBuf + pos, bufLen - pos,
×
1320
                                        "'%s',", tmp);
1321
                    tmfree(tmp);
×
1322
                    break;
×
1323
                }
1324
                case TSDB_DATA_TYPE_JSON: {
×
1325
                    n = tmpJson(sampleDataBuf, bufLen, pos, fieldsSize, field);
×
1326
                    if (n == -1) {
×
1327
                        return -1;
×
1328
                    }
1329
                    pos += n;
×
1330
                    goto skip_stmt;
×
1331
                }
1332
            }
1333
            if (TSDB_DATA_TYPE_JSON != field->type) {
×
1334
                if (n < 0 || n >= bufLen - pos) {
×
1335
                    errorPrint("%s() LN%d snprintf overflow\n",
×
1336
                               __func__, __LINE__);
1337
                    return -1;
×
1338
                } else {
1339
                    pos += n;
×
1340
                }
1341
            }
1342
        }
1343
        debugPrint(" k=%" PRId64 " pos=%" PRId64 " line=%s\n", k, pos, line);
×
1344

1345
skip_stmt:
×
1346
        if (pos > 0)
×
1347
            *(sampleDataBuf + pos - 1) = 0;
×
1348
        angle += stbInfo->timestamp_step/stbInfo->angle_step;
×
1349
        if (angle > 360) {
×
1350
            angle -= 360;
×
1351
        }
1352

1353
    }
1354
    return 0;
×
1355
}
1356

1357
static int generateRandDataStmtForChildTable(
×
1358
    SSuperTable *stbInfo,
1359
    char *sampleDataBuf,
1360
    int64_t bufLen,
1361
    int lenOfOneRow, BArray *fields,
1362
    int64_t loop, BArray *childCols, int64_t loopBegin) {
1363
    //  generateRandDataStmtForChildTable()
1364
    for (int i = 0; i < fields->size; ++i) {
×
1365
        Field *field = benchArrayGet(fields, i);
×
1366
        ChildField *childField = benchArrayGet(childCols, i);
×
1367
        if (field->type == TSDB_DATA_TYPE_BINARY
×
1368
                || field->type == TSDB_DATA_TYPE_NCHAR) {
×
1369
            childField->stmtData.data = benchCalloc(
×
1370
                        1, loop * (field->length + 1), true);
×
1371
        } else {
1372
            childField->stmtData.data = benchCalloc(
×
1373
                    1, loop * field->length, true);
×
1374
        }
1375

1376
        // is_null
1377
        childField->stmtData.is_null = benchCalloc(sizeof(char), loop, true);
×
1378
        // lengths
1379
        childField->stmtData.lengths = benchCalloc(sizeof(int32_t), loop, true);
×
1380

1381
        // log
1382
        debugPrint("i=%d generateRandDataStmtForChildTable fields=%p %s malloc stmtData.data=%p\n", i, fields, field->name ,field->stmtData.data);
×
1383

1384
    }
1385
    return fillStmt(
×
1386
        stbInfo,
1387
        sampleDataBuf,
1388
        bufLen,
1389
        lenOfOneRow, fields,
1390
        loop, false, childCols, loopBegin);
1391
}
1392

1393
static int generateRandDataStmt(
×
1394
    SSuperTable *stbInfo,
1395
    char *sampleDataBuf,
1396
    int64_t bufLen,
1397
    int lenOfOneRow, BArray *fields,
1398
    int64_t loop, bool tag, int64_t loopBegin) {
1399
    // generateRandDataStmt()
1400
    for (int i = 0; i < fields->size; ++i) {
×
1401
        Field *field = benchArrayGet(fields, i);
×
1402
        if (field->stmtData.data == NULL) {
×
1403
            // data
1404
            if (field->type == TSDB_DATA_TYPE_BINARY
×
1405
                    || field->type == TSDB_DATA_TYPE_NCHAR) {
×
1406
                field->stmtData.data = benchCalloc(1, loop * (field->length + 1), true);
×
1407
            } else {
1408
                field->stmtData.data = benchCalloc(1, loop * field->length, true);
×
1409
            }
1410

1411
            // is_null
1412
            field->stmtData.is_null = benchCalloc(sizeof(char), loop, true);
×
1413
            // lengths
1414
            field->stmtData.lengths = benchCalloc(sizeof(int32_t), loop, true);
×
1415

1416
            // log
1417
            debugPrint("i=%d generateRandDataStmt tag=%d fields=%p %s malloc stmtData.data=%p\n", i, tag, fields, field->name ,field->stmtData.data);
×
1418
        }
1419
    }
1420

1421
    return fillStmt(
×
1422
        stbInfo,
1423
        sampleDataBuf,
1424
        bufLen,
1425
        lenOfOneRow, fields,
1426
        loop, tag, NULL, loopBegin);
1427
}
1428

1429
static int generateRandDataSmlTelnet(SSuperTable *stbInfo, char *sampleDataBuf,
×
1430
                     int bufLen,
1431
                      int lenOfOneRow, BArray * fields, int64_t loop,
1432
                      bool tag, int64_t loopBegin) {
1433
    int64_t index = loopBegin;
×
1434
    int angle = stbInfo->startTimestamp % 360; // 0 ~ 360
×
1435
    for (int64_t k = 0; k < loop; ++k, ++index) {
×
1436
        int64_t pos = k * lenOfOneRow;
×
1437
        int fieldsSize = fields->size;
×
1438
        if (!tag) {
×
1439
            fieldsSize = 1;
×
1440
        }
1441
        for (int i = 0; i < fieldsSize; ++i) {
×
1442
            Field * field = benchArrayGet(fields, i);
×
1443
            int n = 0;
×
1444
            switch (field->type) {
×
1445
                case TSDB_DATA_TYPE_BOOL: {
×
1446
                    bool boolTmp = tmpBool(field);
×
1447
                    if (tag) {
×
1448
                        n = snprintf(sampleDataBuf + pos, bufLen - pos,
×
1449
                                     "%s=%s ", field->name,
×
1450
                                        boolTmp ? "true" : "false");
1451
                    } else {
1452
                        n = snprintf(sampleDataBuf + pos, bufLen - pos,
×
1453
                                        "%s ", boolTmp ? "true" : "false");
1454
                    }
1455
                    break;
×
1456
                }
1457
                case TSDB_DATA_TYPE_TINYINT: {
×
1458
                    int8_t tinyint = tmpInt8Impl(field, index);
×
1459
                    if (tag) {
×
1460
                        n = snprintf(sampleDataBuf + pos, bufLen - pos,
×
1461
                                        "%s=%di8 ", field->name, tinyint);
×
1462
                    } else {
1463
                        n = snprintf(sampleDataBuf + pos,
×
1464
                                     bufLen - pos, "%di8 ", tinyint);
×
1465
                    }
1466
                    break;
×
1467
                }
1468
                case TSDB_DATA_TYPE_UTINYINT: {
×
1469
                    uint8_t utinyint = tmpUint8Impl(field, index);
×
1470
                    if (tag) {
×
1471
                        n = snprintf(sampleDataBuf + pos, bufLen - pos,
×
1472
                                        "%s=%uu8 ", field->name, utinyint);
×
1473
                    } else {
1474
                        n = snprintf(sampleDataBuf + pos,
×
1475
                                        bufLen - pos, "%uu8 ", utinyint);
×
1476
                    }
1477
                    break;
×
1478
                }
1479
                case TSDB_DATA_TYPE_SMALLINT: {
×
1480
                    int16_t smallint = tmpInt16Impl(field, index);
×
1481
                    if (tag) {
×
1482
                        n = snprintf(sampleDataBuf + pos, bufLen - pos,
×
1483
                                        "%s=%di16 ", field->name, smallint);
×
1484
                    } else {
1485
                        n = snprintf(sampleDataBuf + pos, bufLen - pos,
×
1486
                                        "%di16 ", smallint);
1487
                    }
1488
                    break;
×
1489
                }
1490
                case TSDB_DATA_TYPE_USMALLINT: {
×
1491
                    uint16_t usmallint = tmpUint16Impl(field, index);
×
1492
                    if (tag) {
×
1493
                        n = snprintf(sampleDataBuf + pos, bufLen - pos,
×
1494
                                        "%s=%uu16 ", field->name, usmallint);
×
1495
                    } else {
1496
                        n = snprintf(sampleDataBuf + pos, bufLen - pos,
×
1497
                                        "%uu16 ", usmallint);
1498
                    }
1499
                    break;
×
1500
                }
1501
                case TSDB_DATA_TYPE_INT: {
×
1502
                    int32_t intTmp = tmpInt32Impl(field, i, angle, index);
×
1503
                    if (tag) {
×
1504
                        n = snprintf(sampleDataBuf + pos, bufLen - pos,
×
1505
                                        "%s=%di32 ",
1506
                                        field->name, intTmp);
×
1507
                    } else {
1508
                        n = snprintf(sampleDataBuf + pos,
×
1509
                                        bufLen - pos,
×
1510
                                        "%di32 ", intTmp);
1511
                    }
1512
                    break;
×
1513
                }
1514
                case TSDB_DATA_TYPE_BIGINT: {
×
1515
                    int64_t bigintTmp = tmpInt64Impl(field, angle, index);
×
1516
                    if (tag) {
×
1517
                        n = snprintf(sampleDataBuf + pos, bufLen - pos,
×
1518
                                     "%s=%"PRId64"i64 ",
1519
                                     field->name, bigintTmp);
×
1520
                    } else {
1521
                        n = snprintf(sampleDataBuf + pos,
×
1522
                                     bufLen - pos, "%"PRId64"i64 ", bigintTmp);
×
1523
                    }
1524
                    break;
×
1525
                }
1526
                case TSDB_DATA_TYPE_UINT: {
×
1527
                    uint32_t uintTmp = tmpUint32Impl(field, i, angle, index);
×
1528
                    if (tag) {
×
1529
                        n = snprintf(sampleDataBuf + pos,
×
1530
                                        bufLen - pos,
×
1531
                                        "%s=%uu32 ",
1532
                                        field->name, uintTmp);
×
1533
                    } else {
1534
                        n = snprintf(sampleDataBuf + pos,
×
1535
                                        bufLen - pos,
×
1536
                                        "%uu32 ", uintTmp);
1537
                    }
1538
                    break;
×
1539
                }
1540
                case TSDB_DATA_TYPE_UBIGINT:
×
1541
                case TSDB_DATA_TYPE_TIMESTAMP: {
1542
                    uint64_t ubigintTmp = tmpUint64Impl(field, angle, index);
×
1543
                    if (tag) {
×
1544
                        n = snprintf(sampleDataBuf + pos, bufLen - pos,
×
1545
                                     "%s=%"PRIu64"u64 ",
1546
                                     field->name, ubigintTmp);
×
1547
                    } else {
1548
                        n = snprintf(sampleDataBuf + pos,
×
1549
                                     bufLen - pos, "%"PRIu64"u64 ", ubigintTmp);
×
1550
                    }
1551
                    break;
×
1552
                }
1553
                case TSDB_DATA_TYPE_FLOAT: {
×
1554
                    float floatTmp = tmpFloatImpl(field, i, angle, index);
×
1555
                    if (tag) {
×
1556
                        n = snprintf(sampleDataBuf + pos, bufLen - pos,
×
1557
                                        "%s=%ff32 ", field->name, floatTmp);
×
1558
                    } else {
1559
                        n = snprintf(sampleDataBuf + pos,
×
1560
                                     bufLen - pos, "%ff32 ", floatTmp);
×
1561
                    }
1562
                    break;
×
1563
                }
1564
                case TSDB_DATA_TYPE_DOUBLE: {
×
1565
                    double double_ = tmpDoubleImpl(field, angle, index);
×
1566
                    if (tag) {
×
1567
                        n = snprintf(sampleDataBuf + pos, bufLen - pos,
×
1568
                                        "%s=%ff64 ", field->name, double_);
×
1569
                    } else {
1570
                        n = snprintf(sampleDataBuf + pos, bufLen - pos,
×
1571
                                     "%ff64 ", double_);
1572
                    }
1573
                    break;
×
1574
                }
1575
                case TSDB_DATA_TYPE_BINARY:
×
1576
                case TSDB_DATA_TYPE_VARBINARY:
1577
                case TSDB_DATA_TYPE_NCHAR: {
1578
                    char *tmp = benchCalloc(1, field->length + 1, false);
×
1579
                    if (0 != tmpStr(tmp, stbInfo->iface, field, index)) {
×
1580
                        tmfree(tmp);
×
1581
                        return -1;
×
1582
                    }
1583
                    if (field->type == TSDB_DATA_TYPE_BINARY || field->type == TSDB_DATA_TYPE_VARBINARY) {
×
1584
                        if (tag) {
×
1585
                            n = snprintf(sampleDataBuf + pos, bufLen - pos,
×
1586
                                            "%s=L\"%s\" ",
1587
                                           field->name, tmp);
×
1588
                        } else {
1589
                            n = snprintf(sampleDataBuf + pos, bufLen - pos,
×
1590
                                            "\"%s\" ", tmp);
1591
                        }
1592
                        if (n < 0 || n >= bufLen - pos) {
×
1593
                            errorPrint("%s() LN%d snprintf overflow\n",
×
1594
                                       __func__, __LINE__);
1595
                            tmfree(tmp);
×
1596
                            return -1;
×
1597
                        } else {
1598
                            pos += n;
×
1599
                        }
1600
                    } else if (field->type == TSDB_DATA_TYPE_NCHAR) {
×
1601
                        if (tag) {
×
1602
                            n = snprintf(sampleDataBuf + pos, bufLen - pos,
×
1603
                                            "%s=L\"%s\" ",
1604
                                           field->name, tmp);
×
1605
                        } else {
1606
                            n = snprintf(sampleDataBuf + pos, bufLen - pos,
×
1607
                                         "L\"%s\" ", tmp);
1608
                        }
1609
                        if (n < 0 || n >= bufLen - pos) {
×
1610
                            errorPrint("%s() LN%d snprintf overflow\n",
×
1611
                                       __func__, __LINE__);
1612
                            tmfree(tmp);
×
1613
                            return -1;
×
1614
                        } else {
1615
                            pos += n;
×
1616
                        }
1617
                    }
1618
                    tmfree(tmp);
×
1619
                    break;
×
1620
                }
1621
                case TSDB_DATA_TYPE_GEOMETRY: {
×
1622
                    char *tmp = benchCalloc(1, field->length + 1, false);
×
1623
                    if (0 != tmpGeometry(tmp, stbInfo->iface, field, index)) {
×
1624
                        tmfree(tmp);
×
1625
                        return -1;
×
1626
                    }
1627
                    if (tag) {
×
1628
                        n = snprintf(sampleDataBuf + pos, bufLen - pos,
×
1629
                                        "%s=L\"%s\" ",
1630
                                        field->name, tmp);
×
1631
                    } else {
1632
                        n = snprintf(sampleDataBuf + pos, bufLen - pos,
×
1633
                                        "\"%s\" ", tmp);
1634
                    }
1635
                    if (n < 0 || n >= bufLen - pos) {
×
1636
                        errorPrint("%s() LN%d snprintf overflow\n",
×
1637
                                    __func__, __LINE__);
1638
                        tmfree(tmp);
×
1639
                        return -1;
×
1640
                    } else {
1641
                        pos += n;
×
1642
                    }
1643
                    tmfree(tmp);
×
1644
                    break;
×
1645
                }
1646
                case TSDB_DATA_TYPE_JSON: {
×
1647
                    n = tmpJson(sampleDataBuf, bufLen, pos, fieldsSize, field);
×
1648
                    if (n == -1) {
×
1649
                        return -1;
×
1650
                    }
1651
                    pos += n;
×
1652
                    goto skip_telnet;
×
1653
                }
1654
            }
1655
            if (TSDB_DATA_TYPE_JSON != field->type) {
×
1656
                if (n < 0 || n >= bufLen - pos) {
×
1657
                    errorPrint("%s() LN%d snprintf overflow\n",
×
1658
                               __func__, __LINE__);
1659
                    return -1;
×
1660
                } else {
1661
                    pos += n;
×
1662
                }
1663
            }
1664
        }
1665
skip_telnet:
×
1666
        *(sampleDataBuf + pos - 1) = 0;
×
1667
        angle += stbInfo->timestamp_step/stbInfo->angle_step;
×
1668
        if (angle > 360) {
×
1669
            angle -= 360;
×
1670
        }
1671

1672
    }
1673

1674
    return 0;
×
1675
}
1676

1677
static int generateRandDataSmlJson(SSuperTable *stbInfo, char *sampleDataBuf,
×
1678
                     int bufLen,
1679
                      int lenOfOneRow, BArray * fields, int64_t loop,
1680
                      bool tag, int64_t loopBegin) {
1681
    int64_t index = loopBegin;
×
1682
    int angle = stbInfo->startTimestamp % 360; // 0 ~ 360
×
1683
    for (int64_t k = 0; k < loop; ++k, ++index) {
×
1684
        int64_t pos = k * lenOfOneRow;
×
1685
        int fieldsSize = fields->size;
×
1686
        for (int i = 0; i < fieldsSize; ++i) {
×
1687
            Field * field = benchArrayGet(fields, i);
×
1688
            int n = 0;
×
1689
            switch (field->type) {
×
1690
                case TSDB_DATA_TYPE_BOOL: {
×
1691
                    bool boolTmp = tmpBool(field);
×
1692
                    n = snprintf(sampleDataBuf + pos, bufLen - pos,
×
1693
                                    "%s,", boolTmp ? "true" : "false");
1694
                    break;
×
1695
                }
1696
                case TSDB_DATA_TYPE_TINYINT: {
×
1697
                    int8_t tinyint = tmpInt8Impl(field, index);
×
1698
                    n = snprintf(sampleDataBuf + pos, bufLen - pos,
×
1699
                                        "%d,", tinyint);
1700
                    break;
×
1701
                }
1702
                case TSDB_DATA_TYPE_UTINYINT: {
×
1703
                    uint8_t utinyint = tmpUint8Impl(field, index);
×
1704
                    n = snprintf(sampleDataBuf + pos,
×
1705
                                        bufLen - pos, "%u,", utinyint);
×
1706
                    break;
×
1707
                }
1708
                case TSDB_DATA_TYPE_SMALLINT: {
×
1709
                    int16_t smallint = tmpInt16Impl(field, index);
×
1710
                    n = snprintf(sampleDataBuf + pos, bufLen - pos,
×
1711
                                        "%d,", smallint);
1712
                    break;
×
1713
                }
1714
                case TSDB_DATA_TYPE_USMALLINT: {
×
1715
                    uint16_t usmallint = tmpUint16Impl(field, index);
×
1716
                    n = snprintf(sampleDataBuf + pos, bufLen - pos,
×
1717
                                        "%u,", usmallint);
1718
                    break;
×
1719
                }
1720
                case TSDB_DATA_TYPE_INT: {
×
1721
                    int32_t intTmp = tmpInt32Impl(field, i, angle, index);
×
1722
                    n = snprintf(sampleDataBuf + pos,
×
1723
                                 bufLen - pos, "%d,", intTmp);
×
1724
                    break;
×
1725
                }
1726
                case TSDB_DATA_TYPE_BIGINT: {
×
1727
                    int64_t bigintTmp = tmpInt64Impl(field, angle, index);
×
1728
                    n = snprintf(sampleDataBuf + pos,
×
1729
                                 bufLen - pos, "%"PRId64",", bigintTmp);
×
1730
                    break;
×
1731
                }
1732
                case TSDB_DATA_TYPE_UINT: {
×
1733
                    uint32_t uintTmp = tmpUint32Impl(field, i, angle, index);
×
1734
                    n = snprintf(sampleDataBuf + pos,
×
1735
                                 bufLen - pos, "%u,", uintTmp);
×
1736
                    break;
×
1737
                }
1738
                case TSDB_DATA_TYPE_UBIGINT:
×
1739
                case TSDB_DATA_TYPE_TIMESTAMP: {
1740
                    uint64_t ubigintTmp = tmpUint64Impl(field, angle, index);
×
1741
                    n = snprintf(sampleDataBuf + pos,
×
1742
                                 bufLen - pos, "%"PRIu64",", ubigintTmp);
×
1743
                    break;
×
1744
                }
1745
                case TSDB_DATA_TYPE_FLOAT: {
×
1746
                    float floatTmp = tmpFloatImpl(field, i, angle, index);
×
1747
                    n = snprintf(sampleDataBuf + pos, bufLen - pos,
×
1748
                                        "%f,", floatTmp);
1749
                    break;
×
1750
                }
1751
                case TSDB_DATA_TYPE_DOUBLE: {
×
1752
                    double double_ = tmpDoubleImpl(field, angle, index);
×
1753
                    n = snprintf(sampleDataBuf + pos, bufLen - pos,
×
1754
                                        "%f,", double_);
1755
                    break;
×
1756
                }
1757
                case TSDB_DATA_TYPE_BINARY:
×
1758
                case TSDB_DATA_TYPE_VARBINARY:
1759
                case TSDB_DATA_TYPE_NCHAR: {
1760
                    char *tmp = benchCalloc(1, field->length + 1, false);
×
1761
                    if (0 != tmpStr(tmp, stbInfo->iface, field, k)) {
×
1762
                        free(tmp);
×
1763
                        return -1;
×
1764
                    }
1765
                    n = snprintf(sampleDataBuf + pos, bufLen - pos,
×
1766
                                        "'%s',", tmp);
1767
                    tmfree(tmp);
×
1768
                    break;
×
1769
                }
1770
                case TSDB_DATA_TYPE_JSON: {
×
1771
                    n = tmpJson(sampleDataBuf, bufLen, pos, fieldsSize, field);
×
1772
                    if (n == -1) {
×
1773
                        return -1;
×
1774
                    }
1775
                    pos += n;
×
1776
                    goto skip_json;
×
1777
                }
1778
            }
1779
            if (TSDB_DATA_TYPE_JSON != field->type) {
×
1780
                if (n < 0 || n >= bufLen - pos) {
×
1781
                    errorPrint("%s() LN%d snprintf overflow\n",
×
1782
                               __func__, __LINE__);
1783
                    return -1;
×
1784
                } else {
1785
                    pos += n;
×
1786
                }
1787
            }
1788
        }
1789
skip_json:
×
1790
        *(sampleDataBuf + pos - 1) = 0;
×
1791
        angle += stbInfo->timestamp_step/stbInfo->angle_step;
×
1792
        if (angle > 360) {
×
1793
            angle -= 360;
×
1794
        }
1795
    }
1796

1797
    return 0;
×
1798
}
1799

1800
static int generateRandDataSmlLine(SSuperTable *stbInfo, char *sampleDataBuf,
×
1801
                     int bufLen,
1802
                      int lenOfOneRow, BArray * fields, int64_t loop,
1803
                      bool tag, int64_t loopBegin) {
1804
    int64_t index = loopBegin;
×
1805
    int angle = stbInfo->startTimestamp % 360; // 0 ~ 360                    
×
1806
    for (int64_t k = 0; k < loop; ++k, ++index) {
×
1807
        int64_t pos = k * lenOfOneRow;
×
1808
        int n = 0;
×
1809
        if (tag) {
×
1810
            n = snprintf(sampleDataBuf + pos,
×
1811
                           bufLen - pos,
×
1812
                           "%s,", stbInfo->stbName);
1813
            if (n < 0 || n >= bufLen - pos) {
×
1814
                errorPrint("%s() LN%d snprintf overflow\n",
×
1815
                           __func__, __LINE__);
1816
                return -1;
×
1817
            } else {
1818
                pos += n;
×
1819
            }
1820
        }
1821

1822
        int fieldsSize = fields->size;
×
1823
        for (int i = 0; i < fieldsSize; ++i) {
×
1824
            Field * field = benchArrayGet(fields, i);
×
1825
            switch (field->type) {
×
1826
                case TSDB_DATA_TYPE_BOOL: {
×
1827
                    bool boolTmp = tmpBool(field);
×
1828
                    n = snprintf(sampleDataBuf + pos, bufLen - pos, "%s=%s,",
×
1829
                                 field->name, boolTmp ? "true" : "false");
×
1830
                    break;
×
1831
                }
1832
                case TSDB_DATA_TYPE_TINYINT: {
×
1833
                    int8_t tinyint = tmpInt8Impl(field, index);
×
1834
                    n = snprintf(sampleDataBuf + pos, bufLen - pos,
×
1835
                                    "%s=%di8,", field->name, tinyint);
×
1836
                    break;
×
1837
                }
1838
                case TSDB_DATA_TYPE_UTINYINT: {
×
1839
                    uint8_t utinyint = tmpUint8Impl(field, index);
×
1840
                    n = snprintf(sampleDataBuf + pos, bufLen - pos,
×
1841
                                    "%s=%uu8,", field->name, utinyint);
×
1842
                    break;
×
1843
                }
1844
                case TSDB_DATA_TYPE_SMALLINT: {
×
1845
                    int16_t smallint = tmpInt16Impl(field, index);
×
1846
                    n = snprintf(sampleDataBuf + pos, bufLen - pos,
×
1847
                                    "%s=%di16,", field->name, smallint);
×
1848
                    break;
×
1849
                }
1850
                case TSDB_DATA_TYPE_USMALLINT: {
×
1851
                    uint16_t usmallint = tmpUint16Impl(field, index);
×
1852
                    n = snprintf(sampleDataBuf + pos, bufLen - pos,
×
1853
                                    "%s=%uu16,",
1854
                                    field->name, usmallint);
×
1855
                    break;
×
1856
                }
1857
                case TSDB_DATA_TYPE_INT: {
×
1858
                    int32_t intTmp;
1859
                    if (tag) {
×
1860
                        intTmp = tmpInt32ImplTag(field, i, index);
×
1861
                    } else {
1862
                        intTmp = tmpInt32Impl(field, i, angle, index);
×
1863
                    }                    
1864
                    n = snprintf(sampleDataBuf + pos, bufLen - pos,
×
1865
                                    "%s=%di32,",
1866
                                    field->name, intTmp);
×
1867
                    break;
×
1868
                }
1869
                case TSDB_DATA_TYPE_BIGINT: {
×
1870
                    int64_t bigintTmp = tmpInt64Impl(field, angle, index);
×
1871
                    n = snprintf(sampleDataBuf + pos, bufLen - pos,
×
1872
                                 "%s=%"PRId64"i64,", field->name, bigintTmp);
×
1873
                    break;
×
1874
                }
1875
                case TSDB_DATA_TYPE_UINT: {
×
1876
                    uint32_t uintTmp = tmpUint32Impl(field, i, angle, index);
×
1877
                    n = snprintf(sampleDataBuf + pos, bufLen - pos,
×
1878
                                    "%s=%uu32,", field->name, uintTmp);
×
1879
                    break;
×
1880
                }
1881
                case TSDB_DATA_TYPE_UBIGINT:
×
1882
                case TSDB_DATA_TYPE_TIMESTAMP: {
1883
                    uint64_t ubigintTmp = tmpUint64Impl(field, angle, index);
×
1884
                    n = snprintf(sampleDataBuf + pos, bufLen - pos,
×
1885
                                 "%s=%"PRIu64"u64,", field->name, ubigintTmp);
×
1886
                    break;
×
1887
                }
1888
                case TSDB_DATA_TYPE_FLOAT: {
×
1889
                    float floatTmp = tmpFloatImpl(field, i, angle, index);
×
1890
                    n = snprintf(sampleDataBuf + pos,
×
1891
                                    bufLen - pos, "%s=%ff32,",
×
1892
                                    field->name, floatTmp);
×
1893
                    break;
×
1894
                }
1895
                case TSDB_DATA_TYPE_DOUBLE: {
×
1896
                    double doubleTmp = tmpDoubleImpl(field, angle, index);
×
1897
                    n = snprintf(sampleDataBuf + pos, bufLen - pos,
×
1898
                                 "%s=%ff64,", field->name, doubleTmp);
×
1899
                    break;
×
1900
                }
1901
                case TSDB_DATA_TYPE_BINARY:
×
1902
                case TSDB_DATA_TYPE_VARBINARY:
1903
                case TSDB_DATA_TYPE_NCHAR: {
1904
                    char *tmp = benchCalloc(1, field->length + 1, false);
×
1905
                    if (0 != tmpStr(tmp, stbInfo->iface, field, k)) {
×
1906
                        free(tmp);
×
1907
                        return -1;
×
1908
                    }
1909
                    if (field->type == TSDB_DATA_TYPE_BINARY) {
×
1910
                        n = snprintf(sampleDataBuf + pos, bufLen - pos,
×
1911
                                        "%s=\"%s\",",
1912
                                       field->name, tmp);
×
1913
                    } else if (field->type == TSDB_DATA_TYPE_NCHAR) {
×
1914
                        n = snprintf(sampleDataBuf + pos, bufLen - pos,
×
1915
                                        "%s=L\"%s\",",
1916
                                       field->name, tmp);
×
1917
                    }
1918
                    tmfree(tmp);
×
1919
                    break;
×
1920
                }
1921
                case TSDB_DATA_TYPE_GEOMETRY: {
×
1922
                    char *tmp = benchCalloc(1, field->length + 1, false);
×
1923
                    if (0 != tmpGeometry(tmp, stbInfo->iface, field, index)) {
×
1924
                        tmfree(tmp);
×
1925
                        return -1;
×
1926
                    }
1927
                    n = snprintf(sampleDataBuf + pos, bufLen - pos,
×
1928
                                    "%s=\"%s\",",
1929
                                    field->name, tmp);
×
1930
                    tmfree(tmp);
×
1931
                    break;
×
1932
                }
1933
                case TSDB_DATA_TYPE_JSON: {
×
1934
                    n = tmpJson(sampleDataBuf, bufLen, pos,
×
1935
                                   fieldsSize, field);
1936
                    if (n < 0 || n >= bufLen) {
×
1937
                        errorPrint("%s() LN%d snprintf overflow\n",
×
1938
                                   __func__, __LINE__);
1939
                        return -1;
×
1940
                    } else {
1941
                        pos += n;
×
1942
                    }
1943
                    goto skip_line;
×
1944
                }
1945
            }
1946
            if (TSDB_DATA_TYPE_JSON != field->type) {
×
1947
                if (n < 0 || n >= bufLen - pos) {
×
1948
                    errorPrint("%s() LN%d snprintf overflow\n",
×
1949
                               __func__, __LINE__);
1950
                    return -1;
×
1951
                } else {
1952
                    pos += n;
×
1953
                }
1954
            }
1955
        }
1956
skip_line:
×
1957
        *(sampleDataBuf + pos - 1) = 0;
×
1958
        angle += stbInfo->timestamp_step/stbInfo->angle_step;
×
1959
        if (angle > 360) {
×
1960
            angle -= 360;
×
1961
        }
1962

1963
    }
1964

1965
    return 0;
×
1966
}
1967

1968
static int generateRandDataSml(SSuperTable *stbInfo, char *sampleDataBuf,
×
1969
                     int64_t bufLen,
1970
                      int lenOfOneRow, BArray * fields, int64_t loop,
1971
                      bool tag, int64_t loopBegin) {
1972
    int     protocol = stbInfo->lineProtocol;
×
1973

1974
    switch (protocol) {
×
1975
        case TSDB_SML_LINE_PROTOCOL:
×
1976
            return generateRandDataSmlLine(stbInfo, sampleDataBuf,
×
1977
                                    bufLen, lenOfOneRow, fields, loop, tag, loopBegin);
1978
        case TSDB_SML_TELNET_PROTOCOL:
×
1979
            return generateRandDataSmlTelnet(stbInfo, sampleDataBuf,
×
1980
                                    bufLen, lenOfOneRow, fields, loop, tag, loopBegin);
1981
        default:
×
1982
            return generateRandDataSmlJson(stbInfo, sampleDataBuf,
×
1983
                                    bufLen, lenOfOneRow, fields, loop, tag, loopBegin);
1984
    }
1985

1986
    return -1;
1987
}
1988

1989
int generateRandData(SSuperTable *stbInfo, char *sampleDataBuf,
×
1990
                     int64_t bufLen,
1991
                     int lenOfOneRow, BArray *fields,
1992
                     int64_t loop,
1993
                     bool tag, BArray *childCols, int64_t loopBegin) {
1994
    int     iface = stbInfo->iface;
×
1995
    switch (iface) {
×
1996
        case TAOSC_IFACE:
×
1997
            return generateRandDataSQL(stbInfo, sampleDataBuf,
×
1998
                                    bufLen, lenOfOneRow, fields, loop, tag, loopBegin);
1999
        // REST
2000
        case REST_IFACE:
×
2001
            return generateRandDataSQL(stbInfo, sampleDataBuf,
×
2002
                                    bufLen, lenOfOneRow, fields, loop, tag, loopBegin);
2003
        case STMT_IFACE:
×
2004
        case STMT2_IFACE:
2005
            if (childCols) {
×
2006
                return generateRandDataStmtForChildTable(stbInfo,
×
2007
                                                         sampleDataBuf,
2008
                                    bufLen, lenOfOneRow, fields, loop,
2009
                                                         childCols, loopBegin);
2010
            } else {
2011
                return generateRandDataStmt(stbInfo, sampleDataBuf,
×
2012
                                    bufLen, lenOfOneRow, fields, loop, tag, loopBegin);
2013
            }
2014
        case SML_IFACE:
×
2015
        case SML_REST_IFACE: // REST
2016
            return generateRandDataSml(stbInfo, sampleDataBuf,
×
2017
                                    bufLen, lenOfOneRow, fields, loop, tag, loopBegin);
2018
        default:
×
2019
            errorPrint("Unknown iface: %d\n", iface);
×
2020
            break;
×
2021
    }
2022

2023
    return -1;
×
2024
}
2025

2026
static BArray *initChildCols(int colsSize) {
×
2027
    BArray *childCols = benchArrayInit(colsSize,
×
2028
                                       sizeof(ChildField));
2029
    for (int col = 0; col < colsSize; col++) {
×
2030
        ChildField *childCol = benchCalloc(
×
2031
                1, sizeof(ChildField), true);
2032
        benchArrayPush(childCols, childCol);
×
2033
    }
2034
    return childCols;
×
2035
}
2036

2037
int prepareSampleData(SDataBase* database, SSuperTable* stbInfo) {
×
2038
    stbInfo->lenOfCols = accumulateRowLen(stbInfo->cols, stbInfo->iface);
×
2039
    stbInfo->lenOfTags = accumulateRowLen(stbInfo->tags, stbInfo->iface);
×
2040
    if (stbInfo->partialColNum != 0
×
2041
            && ((stbInfo->iface == TAOSC_IFACE
×
2042
                || stbInfo->iface == REST_IFACE))) {
×
2043
        // check valid
2044
        if(stbInfo->partialColFrom >= stbInfo->cols->size) {
×
2045
            stbInfo->partialColFrom = 0;
×
2046
            infoPrint("stbInfo->partialColFrom(%d) is large than stbInfo->cols->size(%zd) \n ",stbInfo->partialColFrom,stbInfo->cols->size);
×
2047
        }
2048

2049
        if (stbInfo->partialColFrom + stbInfo->partialColNum > stbInfo->cols->size) {
×
2050
            stbInfo->partialColNum = stbInfo->cols->size - stbInfo->partialColFrom ;
×
2051
        }
2052

2053
        if(stbInfo->partialColNum < stbInfo->cols->size) {
×
2054
            stbInfo->partialColNameBuf =
×
2055
                    benchCalloc(1, TSDB_MAX_ALLOWED_SQL_LEN, true);
×
2056
            int pos = 0;
×
2057
            int n;
2058
            n = snprintf(stbInfo->partialColNameBuf + pos,
×
NEW
2059
                            TSDB_MAX_ALLOWED_SQL_LEN - pos, "%s",
×
NEW
2060
                            stbInfo->primaryKeyName);
×
2061
            if (n < 0 || n > TSDB_MAX_ALLOWED_SQL_LEN - pos) {
×
2062
                errorPrint("%s() LN%d snprintf overflow\n",
×
2063
                           __func__, __LINE__);
2064
            } else {
2065
                pos += n;
×
2066
            }
2067
            for (int i = stbInfo->partialColFrom; i < stbInfo->partialColFrom + stbInfo->partialColNum; ++i) {
×
2068
                Field * col = benchArrayGet(stbInfo->cols, i);
×
2069
                n = snprintf(stbInfo->partialColNameBuf+pos,
×
2070
                                TSDB_MAX_ALLOWED_SQL_LEN - pos,
×
2071
                               ",%s", col->name);
×
2072
                if (n < 0 || n > TSDB_MAX_ALLOWED_SQL_LEN - pos) {
×
2073
                    errorPrint("%s() LN%d snprintf overflow at %d\n",
×
2074
                               __func__, __LINE__, i);
2075
                } else {
2076
                    pos += n;
×
2077
                }
2078
            }
2079
            
2080
            // first part set noen
2081
            for (uint32_t i = 0; i < stbInfo->partialColFrom; ++i) {
×
2082
                Field * col = benchArrayGet(stbInfo->cols, i);
×
2083
                col->none = true;
×
2084
            }
2085
            // last part set none
2086
            for (uint32_t i = stbInfo->partialColFrom + stbInfo->partialColNum; i < stbInfo->cols->size; ++i) {
×
2087
                Field * col = benchArrayGet(stbInfo->cols, i);
×
2088
                col->none = true;
×
2089
            }
2090
            debugPrint("partialColNameBuf: %s\n",
×
2091
                       stbInfo->partialColNameBuf);
2092
        }
2093
    } else {
2094
        stbInfo->partialColNum = stbInfo->cols->size;
×
2095
    }
2096
    stbInfo->sampleDataBuf =
×
2097
            benchCalloc(
×
2098
                1, stbInfo->lenOfCols*g_arguments->prepared_rand, true);
×
2099
    infoPrint(
×
2100
              "generate stable<%s> columns data with lenOfCols<%u> * "
2101
              "prepared_rand<%" PRIu64 ">\n",
2102
              stbInfo->stbName, stbInfo->lenOfCols, g_arguments->prepared_rand);
2103
    if (stbInfo->random_data_source) {
×
2104
        if (g_arguments->mistMode) {
×
2105
            infoPrint("Each child table using different random prepare data pattern. need "
×
2106
            "all memory(%d M) = childs(%"PRId64") * prepared_rand(%"PRId64") * lenOfCols(%d) \n",
2107
            (int32_t)(stbInfo->childTblCount*g_arguments->prepared_rand*stbInfo->lenOfCols/1024/1024),
2108
            stbInfo->childTblCount, g_arguments->prepared_rand, stbInfo->lenOfCols);
2109
            for (int64_t child = 0; child < stbInfo->childTblCount; child++) {
×
2110
                SChildTable *childTbl = stbInfo->childTblArray[child];
×
2111
                if (STMT_IFACE == stbInfo->iface || STMT2_IFACE == stbInfo->iface) {
×
2112
                    childTbl->childCols = initChildCols(stbInfo->cols->size);
×
2113
                }
2114
                childTbl->sampleDataBuf =
×
2115
                    benchCalloc(
×
2116
                        1, stbInfo->lenOfCols*g_arguments->prepared_rand, true);
×
2117
                if (generateRandData(stbInfo, childTbl->sampleDataBuf,
×
2118
                             stbInfo->lenOfCols*g_arguments->prepared_rand,
×
2119
                             stbInfo->lenOfCols,
×
2120
                             stbInfo->cols,
2121
                             g_arguments->prepared_rand,
×
2122
                             false, childTbl->childCols, 0)) {
2123
                    errorPrint("Failed to generate data for table %s\n",
×
2124
                               childTbl->name);
2125
                    return -1;
×
2126
                }
2127
                childTbl->useOwnSample = true;
×
2128
            }
2129
        } else {
2130
            if (generateRandData(stbInfo, stbInfo->sampleDataBuf,
×
2131
                             stbInfo->lenOfCols*g_arguments->prepared_rand,
×
2132
                             stbInfo->lenOfCols,
×
2133
                             stbInfo->cols,
2134
                             g_arguments->prepared_rand,
×
2135
                             false, NULL, 0)) {
2136
                return -1;
×
2137
            }
2138
        }
2139
        debugPrint("sampleDataBuf: %s\n", stbInfo->sampleDataBuf);
×
2140
    } else {
2141
        if (stbInfo->useSampleTs) {
×
2142
            if (getAndSetRowsFromCsvFile(
×
2143
                    stbInfo->sampleFile, &stbInfo->insertRows)) {
×
2144
                return -1;
×
2145
            }
2146
        }
2147
        if (generateSampleFromCsv(stbInfo->sampleDataBuf,
×
2148
                                        stbInfo->sampleFile, NULL, stbInfo->lenOfCols,
×
2149
                                        g_arguments->prepared_rand)) {
×
2150
            errorPrint("Failed to generate sample from csv file %s\n",
×
2151
                    stbInfo->sampleFile);
2152
            return -1;
×
2153
        }
2154

2155
        debugPrint("sampleDataBuf: %s\n", stbInfo->sampleDataBuf);
×
2156
        if (stbInfo->childTblSample) {
×
2157
            if (NULL == strstr(stbInfo->childTblSample, "XXXX")) {
×
2158
                errorPrint("Child table sample file pattern has no %s\n",
×
2159
                   "XXXX");
2160
                return -1;
×
2161
            }
2162
            for (int64_t child = 0; child < stbInfo->childTblCount; child++) {
×
2163
                char sampleFilePath[MAX_PATH_LEN] = {0};
×
2164
                getSampleFileNameByPattern(sampleFilePath, stbInfo, child);
×
2165
                if (0 != access(sampleFilePath, F_OK)) {
×
2166
                    continue;
×
2167
                }
2168
                SChildTable *childTbl = stbInfo->childTblArray[child];
×
2169
                infoPrint("Found specified sample file for table %s\n",
×
2170
                          childTbl->name);
2171
                if (getAndSetRowsFromCsvFile(sampleFilePath,
×
2172
                                             &(childTbl->insertRows))) {
2173
                    errorPrint("Failed to get sample data rows for table %s\n",
×
2174
                          childTbl->name);
2175
                    return -1;
×
2176
                }
2177

2178
                childTbl->sampleDataBuf =
×
2179
                    benchCalloc(
×
2180
                        1, stbInfo->lenOfCols*g_arguments->prepared_rand, true);
×
2181
                if (generateSampleFromCsv(
×
2182
                            childTbl->sampleDataBuf,
2183
                            sampleFilePath,
2184
                            NULL,
2185
                            stbInfo->lenOfCols,
×
2186
                            g_arguments->prepared_rand)) {
×
2187
                    errorPrint("Failed to generate sample from file "
×
2188
                                   "for child table %"PRId64"\n",
2189
                                    child);
2190
                    return -1;
×
2191
                }
2192
                if (STMT_IFACE == stbInfo->iface || STMT2_IFACE == stbInfo->iface) {
×
2193
                    childTbl->childCols = initChildCols(stbInfo->cols->size);
×
2194
                }
2195
                childTbl->useOwnSample = true;
×
2196
                debugPrint("sampleDataBuf: %s\n", childTbl->sampleDataBuf);
×
2197
            }
2198
        }
2199
    }
2200

2201
    if (0 != convertServAddr(
×
2202
            stbInfo->iface,
×
2203
            stbInfo->tcpTransfer,
×
2204
            stbInfo->lineProtocol)) {
×
2205
        return -1;
×
2206
    }    
2207
    return 0;
×
2208
}
2209

2210
int64_t getTSRandTail(int64_t timeStampStep, int32_t seq, int disorderRatio,
×
2211
                      int disorderRange) {
2212
    int64_t randTail = timeStampStep * seq;
×
2213
    if (disorderRatio > 0) {
×
2214
        int rand_num = taosRandom() % 100;
×
2215
        if (rand_num < disorderRatio) {
×
2216
            randTail = (randTail + (taosRandom() % disorderRange + 1)) * (-1);
×
2217
        }
2218
    }
2219
    return randTail;
×
2220
}
2221

2222
uint32_t bindParamBatch(threadInfo *pThreadInfo,
×
2223
                        uint32_t batch, int64_t startTime, int64_t pos,
2224
                        SChildTable *childTbl, int32_t *pkCur, int32_t *pkCnt, int32_t *n, int64_t *delay2, int64_t *delay3) {
2225
    TAOS_STMT   *stmt = pThreadInfo->conn->stmt;
×
2226
    SSuperTable *stbInfo = pThreadInfo->stbInfo;
×
2227
    uint32_t     columnCount = stbInfo->cols->size;
×
2228

2229
    //if (!pThreadInfo->stmtBind || stbInfo->interlaceRows > 0 ) {
2230
    {
2231
        pThreadInfo->stmtBind = true;
×
2232
        memset(pThreadInfo->bindParams, 0,
×
2233
            (sizeof(TAOS_MULTI_BIND) * (columnCount + 1)));
×
2234

2235
        for (int c = 0; c <= columnCount; c++) {
×
2236
            TAOS_MULTI_BIND *param =
×
2237
                (TAOS_MULTI_BIND *)(pThreadInfo->bindParams +
×
2238
                                    sizeof(TAOS_MULTI_BIND) * c);
×
2239
            char data_type;
2240
            if (c == 0) {
×
2241
                data_type = TSDB_DATA_TYPE_TIMESTAMP;
×
2242
                param->buffer_length = sizeof(int64_t);
×
2243
                if (stbInfo->useSampleTs) {
×
2244
                    param->buffer = pThreadInfo->bind_ts_array + pos;
×
2245
                } else {
2246
                    param->buffer = pThreadInfo->bind_ts_array;
×
2247
                }
2248
            } else {
2249
                Field *col = benchArrayGet(stbInfo->cols, c - 1);
×
2250
                data_type = col->type;
×
2251
                if (childTbl->useOwnSample) {
×
2252
                    ChildField *childCol = benchArrayGet(childTbl->childCols, c-1);
×
2253
                    param->buffer = (char *)childCol->stmtData.data + pos * col->length;
×
2254
                    param->is_null = childCol->stmtData.is_null + pos;
×
2255
                } else {
2256
                    param->buffer = (char *)col->stmtData.data + pos * col->length;
×
2257
                    param->is_null = col->stmtData.is_null + pos;
×
2258
                }
2259
                param->buffer_length = col->length;
×
2260
                debugPrint("col[%d]: type: %s, len: %d\n", c,
×
2261
                        convertDatatypeToString(data_type),
2262
                        col->length);
2263
            }
2264
            param->buffer_type = data_type;
×
2265
            param->length = pThreadInfo->lengths[c];
×
2266

2267
            for (int b = 0; b < batch; b++) {
×
2268
                param->length[b] = (int32_t)param->buffer_length;
×
2269
            }
2270
            param->num = batch;
×
2271
        }
2272
    }
2273

2274
    if (!stbInfo->useSampleTs) {
×
2275
        // set first column ts array values
2276
        for (uint32_t k = 0; k < batch; k++) {
×
2277
            /* columnCount + 1 (ts) */
2278
            if (stbInfo->disorderRatio) {
×
2279
                *(pThreadInfo->bind_ts_array + k) =
×
2280
                    startTime + getTSRandTail(stbInfo->timestamp_step, *n,
×
2281
                                            stbInfo->disorderRatio,
2282
                                            stbInfo->disorderRange);
2283
            } else {
2284
                *(pThreadInfo->bind_ts_array + k) = startTime + stbInfo->timestamp_step * (*n);
×
2285
            }
2286

2287
            // check n need add
2288
            if (!stbInfo->primary_key || needChangeTs(stbInfo, pkCur, pkCnt)) {
×
2289
                *n = *n + 1;
×
2290
            }
2291
        }
2292
    }
2293

2294
    /*
2295
      1. The last batch size may be smaller than the previous batch size.
2296
      2. When inserting another table, the batch size reset again(bigger than lastBatchSize)
2297
    */        
2298
    int lastBatchSize = ((TAOS_MULTI_BIND *) pThreadInfo->bindParams)->num;
×
2299
    if (batch != lastBatchSize) {
×
2300
        for (int c = 0; c < columnCount + 1; c++) {
×
2301
            TAOS_MULTI_BIND *param =
×
2302
                    (TAOS_MULTI_BIND *) (pThreadInfo->bindParams +
×
2303
                    sizeof(TAOS_MULTI_BIND) * c);
×
2304
            param->num = batch;
×
2305
        }
2306
    }
2307

2308
    int64_t start = toolsGetTimestampUs();
×
2309
    if (taos_stmt_bind_param_batch(
×
2310
            stmt, (TAOS_MULTI_BIND *)pThreadInfo->bindParams)) {
×
2311
        errorPrint("taos_stmt_bind_param_batch() failed! reason: %s\n",
×
2312
                   taos_stmt_errstr(stmt));
2313
        return 0;
×
2314
    }
2315
    *delay2 += toolsGetTimestampUs() - start;
×
2316

2317
    if(stbInfo->autoTblCreating) {
×
2318
        start = toolsGetTimestampUs();
×
2319
        if (taos_stmt_add_batch(pThreadInfo->conn->stmt) != 0) {
×
2320
            errorPrint("taos_stmt_add_batch() failed! reason: %s\n",
×
2321
                    taos_stmt_errstr(pThreadInfo->conn->stmt));
2322
            return 0;
×
2323
        }
2324
        if(delay3) {
×
2325
            *delay3 += toolsGetTimestampUs() - start;
×
2326
        }
2327
    }
2328
    return batch;
×
2329
}
2330

2331
void generateSmlJsonTags(tools_cJSON *tagsList,
×
2332
                         char **sml_tags_json_array,
2333
                         SSuperTable *stbInfo,
2334
                            uint64_t start_table_from, int tbSeq) {
2335
    tools_cJSON * tags = tools_cJSON_CreateObject();
×
2336
    char *  tbName = benchCalloc(1, TSDB_TABLE_NAME_LEN, true);
×
2337
    snprintf(tbName, TSDB_TABLE_NAME_LEN, "%s%" PRIu64,
×
2338
             stbInfo->childTblPrefix, start_table_from + tbSeq);
2339
    char *tagName = benchCalloc(1, TSDB_MAX_TAGS, true);
×
2340
    for (int i = 0; i < stbInfo->tags->size; i++) {
×
2341
        Field * tag = benchArrayGet(stbInfo->tags, i);
×
2342
        snprintf(tagName, TSDB_MAX_TAGS, "t%d", i);
×
2343
        switch (tag->type) {
×
2344
            case TSDB_DATA_TYPE_BOOL: {
×
2345
                bool boolTmp = tmpBool(tag);
×
2346
                tools_cJSON_AddNumberToObject(tags, tagName, boolTmp);
×
2347
                break;
×
2348
            }
2349
            case TSDB_DATA_TYPE_FLOAT: {
×
2350
                float floatTmp = tmpFloat(tag);
×
2351
                tools_cJSON_AddNumberToObject(tags, tagName, floatTmp);
×
2352
                break;
×
2353
            }
2354
            case TSDB_DATA_TYPE_DOUBLE: {
×
2355
                double doubleTmp = tmpDouble(tag);
×
2356
                tools_cJSON_AddNumberToObject(tags, tagName, doubleTmp);
×
2357
                break;
×
2358
            }
2359

2360
            case TSDB_DATA_TYPE_BINARY:
×
2361
            case TSDB_DATA_TYPE_VARBINARY:
2362
            case TSDB_DATA_TYPE_NCHAR: {
2363
                char *buf = (char *)benchCalloc(tag->length + 1, 1, false);
×
2364
                rand_string(buf, tag->length, g_arguments->chinese);
×
2365
                if (tag->type == TSDB_DATA_TYPE_BINARY || tag->type == TSDB_DATA_TYPE_VARBINARY) {
×
2366
                    tools_cJSON_AddStringToObject(tags, tagName, buf);
×
2367
                } else {
2368
                    tools_cJSON_AddStringToObject(tags, tagName, buf);
×
2369
                }
2370
                tmfree(buf);
×
2371
                break;
×
2372
            }
2373
            default: {
×
2374
                int tagTmp = tag->min;
×
2375
                if (tag->max != tag->min) {
×
2376
                    tagTmp += (taosRandom() % (tag->max - tag->min));
×
2377
                }
2378
                tools_cJSON_AddNumberToObject(
×
2379
                        tags, tagName, tagTmp);
2380
                break;
×
2381
            }
2382
        }
2383
    }
2384
    tools_cJSON_AddItemToArray(tagsList, tags);
×
2385
    debugPrintJsonNoTime(tags);
×
2386
    char *tags_text = tools_cJSON_PrintUnformatted(tags);
×
2387
    debugPrintNoTimestamp("%s() LN%d, No.%"PRIu64" table's tags text: %s\n",
×
2388
                          __func__, __LINE__,
2389
                          start_table_from + tbSeq, tags_text);
2390
    sml_tags_json_array[tbSeq] = tags_text;
×
2391
    tmfree(tagName);
×
2392
    tmfree(tbName);
×
2393
}
×
2394

2395
void generateSmlTaosJsonTags(tools_cJSON *tagsList, SSuperTable *stbInfo,
×
2396
                            uint64_t start_table_from, int tbSeq) {
2397
    tools_cJSON * tags = tools_cJSON_CreateObject();
×
2398
    char *  tbName = benchCalloc(1, TSDB_TABLE_NAME_LEN, true);
×
2399
    snprintf(tbName, TSDB_TABLE_NAME_LEN, "%s%" PRIu64,
×
2400
             stbInfo->childTblPrefix, tbSeq + start_table_from);
2401
    tools_cJSON_AddStringToObject(tags, "id", tbName);
×
2402
    char *tagName = benchCalloc(1, TSDB_MAX_TAGS, true);
×
2403
    for (int i = 0; i < stbInfo->tags->size; i++) {
×
2404
        Field * tag = benchArrayGet(stbInfo->tags, i);
×
2405
        tools_cJSON *tagObj = tools_cJSON_CreateObject();
×
2406
        snprintf(tagName, TSDB_MAX_TAGS, "t%d", i);
×
2407
        switch (tag->type) {
×
2408
            case TSDB_DATA_TYPE_BOOL: {
×
2409
                bool boolTmp = tmpBool(tag);
×
2410
                tools_cJSON_AddBoolToObject(tagObj, "value", boolTmp);
×
2411
                tools_cJSON_AddStringToObject(tagObj, "type", "bool");
×
2412
                break;
×
2413
            }
2414
            case TSDB_DATA_TYPE_FLOAT: {
×
2415
                float floatTmp = tmpFloat(tag);
×
2416
                tools_cJSON_AddNumberToObject(tagObj, "value", floatTmp);
×
2417
                tools_cJSON_AddStringToObject(tagObj, "type", "float");
×
2418
                break;
×
2419
            }
2420
            case TSDB_DATA_TYPE_DOUBLE: {
×
2421
                double doubleTmp = tmpDouble(tag);
×
2422
                tools_cJSON_AddNumberToObject(tagObj, "value", doubleTmp);
×
2423
                tools_cJSON_AddStringToObject(tagObj, "type", "double");
×
2424
                break;
×
2425
            }
2426

2427
            case TSDB_DATA_TYPE_BINARY:
×
2428
            case TSDB_DATA_TYPE_VARBINARY:
2429
            case TSDB_DATA_TYPE_NCHAR: {
2430
                char *buf = (char *)benchCalloc(tag->length + 1, 1, false);
×
2431
                rand_string(buf, tag->length, g_arguments->chinese);
×
2432
                if (tag->type == TSDB_DATA_TYPE_BINARY || tag->type == TSDB_DATA_TYPE_VARBINARY) {
×
2433
                    tools_cJSON_AddStringToObject(tagObj, "value", buf);
×
2434
                    tools_cJSON_AddStringToObject(tagObj, "type", "binary");
×
2435
                } else {
2436
                    tools_cJSON_AddStringToObject(tagObj, "value", buf);
×
2437
                    tools_cJSON_AddStringToObject(tagObj, "type", "nchar");
×
2438
                }
2439
                tmfree(buf);
×
2440
                break;
×
2441
            }
2442
            default: {
×
2443
                int64_t tagTmp = tag->min;
×
2444
                if (tag->max != tag->min) {
×
2445
                    tagTmp += (taosRandom() % (tag->max - tag->min));
×
2446
                }
2447
                tools_cJSON_AddNumberToObject(tagObj, "value", tagTmp);
×
2448
                        tools_cJSON_AddStringToObject(tagObj, "type",
×
2449
                                        convertDatatypeToString(tag->type));
2450
                break;
×
2451
            }
2452
        }
2453
        tools_cJSON_AddItemToObject(tags, tagName, tagObj);
×
2454
    }
2455
    tools_cJSON_AddItemToArray(tagsList, tags);
×
2456
    tmfree(tagName);
×
2457
    tmfree(tbName);
×
2458
}
×
2459

2460
void generateSmlJsonValues(
×
2461
        char **sml_json_value_array, SSuperTable *stbInfo, int tableSeq) {
2462
    char *value_buf = NULL;
×
2463
    Field* col = benchArrayGet(stbInfo->cols, 0);
×
2464
    int len_key = strlen("\"value\":,");
×
2465
    switch (col->type) {
×
2466
        case TSDB_DATA_TYPE_BOOL: {
×
2467
            bool boolTmp = tmpBool(col);
×
2468
            value_buf = benchCalloc(len_key + 6, 1, true);
×
2469
            snprintf(value_buf, len_key + 6,
×
2470
                     "\"value\":%s,", boolTmp?"true":"false");
2471
            break;
×
2472
        }
2473
        case TSDB_DATA_TYPE_FLOAT: {
×
2474
            value_buf = benchCalloc(len_key + 20, 1, true);
×
2475
            float floatTmp = tmpFloat(col);
×
2476
            snprintf(value_buf, len_key + 20,
×
2477
                     "\"value\":%f,", floatTmp);
2478
            break;
×
2479
        }
2480
        case TSDB_DATA_TYPE_DOUBLE: {
×
2481
            value_buf = benchCalloc(len_key + 40, 1, true);
×
2482
            double doubleTmp = tmpDouble(col);
×
2483
            snprintf(
×
2484
                value_buf, len_key + 40, "\"value\":%f,", doubleTmp);
×
2485
            break;
×
2486
        }
2487
        case TSDB_DATA_TYPE_BINARY:
×
2488
        case TSDB_DATA_TYPE_VARBINARY:
2489
        case TSDB_DATA_TYPE_NCHAR: {
2490
            char *buf = (char *)benchCalloc(col->length + 1, 1, false);
×
2491
            rand_string(buf, col->length, g_arguments->chinese);
×
2492
            value_buf = benchCalloc(len_key + col->length + 3, 1, true);
×
2493
            snprintf(value_buf, len_key + col->length + 3,
×
2494
                     "\"value\":\"%s\",", buf);
2495
            tmfree(buf);
×
2496
            break;
×
2497
        }
2498
        case TSDB_DATA_TYPE_GEOMETRY: {
×
2499
            char *buf = (char *)benchCalloc(col->length + 1, 1, false);
×
2500
            tmpGeometry(buf, stbInfo->iface, col, 0);
×
2501
            value_buf = benchCalloc(len_key + col->length + 3, 1, true);
×
2502
            snprintf(value_buf, len_key + col->length + 3,
×
2503
                     "\"value\":\"%s\",", buf);
2504
            tmfree(buf);
×
2505
            break;
×
2506
        }
2507
        default: {
×
2508
            value_buf = benchCalloc(len_key + 20, 1, true);
×
2509
            double doubleTmp = tmpDouble(col);
×
2510
            snprintf(value_buf, len_key + 20, "\"value\":%f,", doubleTmp);
×
2511
            break;
×
2512
        }
2513
    }
2514
    sml_json_value_array[tableSeq] = value_buf;
×
2515
}
×
2516

2517
void generateSmlJsonCols(tools_cJSON *array, tools_cJSON *tag,
×
2518
                         SSuperTable *stbInfo,
2519
                            uint32_t time_precision, int64_t timestamp) {
2520
    tools_cJSON * record = tools_cJSON_CreateObject();
×
2521
    tools_cJSON_AddNumberToObject(record, "timestamp", (double)timestamp);
×
2522
    Field* col = benchArrayGet(stbInfo->cols, 0);
×
2523
    switch (col->type) {
×
2524
        case TSDB_DATA_TYPE_BOOL: {
×
2525
            bool boolTmp = tmpBool(col);
×
2526
            tools_cJSON_AddBoolToObject(record, "value", boolTmp);
×
2527
            break;
×
2528
        }
2529
        case TSDB_DATA_TYPE_FLOAT: {
×
2530
            float floatTmp = tmpFloat(col);
×
2531
            tools_cJSON_AddNumberToObject(record, "value", floatTmp);
×
2532
            break;
×
2533
        }
2534
        case TSDB_DATA_TYPE_DOUBLE: {
×
2535
            double doubleTmp = tmpDouble(col);
×
2536
            tools_cJSON_AddNumberToObject(record, "value", doubleTmp);
×
2537
            break;
×
2538
        }
2539
        case TSDB_DATA_TYPE_BINARY:
×
2540
        case TSDB_DATA_TYPE_VARBINARY:
2541
        case TSDB_DATA_TYPE_NCHAR: {
2542
            char *buf = (char *)benchCalloc(col->length + 1, 1, false);
×
2543
            rand_string(buf, col->length, g_arguments->chinese);
×
2544
            if (col->type == TSDB_DATA_TYPE_BINARY) {
×
2545
                tools_cJSON_AddStringToObject(record, "value", buf);
×
2546
            } else {
2547
                tools_cJSON_AddStringToObject(record, "value", buf);
×
2548
            }
2549
            tmfree(buf);
×
2550
            break;
×
2551
        }
2552
        case TSDB_DATA_TYPE_GEOMETRY: {
×
2553
            char *buf = (char *)benchCalloc(col->length + 1, 1, false);
×
2554
            tmpGeometry(buf, stbInfo->iface, col, 0);
×
2555
            tools_cJSON_AddStringToObject(record, "value", buf);
×
2556
            tmfree(buf);
×
2557
            break;
×
2558
        }
2559
        default: {
×
2560
            double doubleTmp = tmpDouble(col);
×
2561
            tools_cJSON_AddNumberToObject(record, "value", doubleTmp);
×
2562
            break;
×
2563
        }
2564
    }
2565
    tools_cJSON_AddItemToObject(record, "tags", tag);
×
2566
    tools_cJSON_AddStringToObject(record, "metric", stbInfo->stbName);
×
2567
    tools_cJSON_AddItemToArray(array, record);
×
2568
}
×
2569

2570
void generateSmlTaosJsonCols(tools_cJSON *array, tools_cJSON *tag,
×
2571
                         SSuperTable *stbInfo,
2572
                            uint32_t time_precision, int64_t timestamp) {
2573
    tools_cJSON * record = tools_cJSON_CreateObject();
×
2574
    tools_cJSON * ts = tools_cJSON_CreateObject();
×
2575
    tools_cJSON_AddNumberToObject(ts, "value", (double)timestamp);
×
2576
    if (time_precision == TSDB_SML_TIMESTAMP_MILLI_SECONDS) {
×
2577
        tools_cJSON_AddStringToObject(ts, "type", "ms");
×
2578
    } else if (time_precision == TSDB_SML_TIMESTAMP_MICRO_SECONDS) {
×
2579
        tools_cJSON_AddStringToObject(ts, "type", "us");
×
2580
    } else if (time_precision == TSDB_SML_TIMESTAMP_NANO_SECONDS) {
×
2581
        tools_cJSON_AddStringToObject(ts, "type", "ns");
×
2582
    }
2583
    tools_cJSON *value = tools_cJSON_CreateObject();
×
2584
    Field* col = benchArrayGet(stbInfo->cols, 0);
×
2585
    switch (col->type) {
×
2586
        case TSDB_DATA_TYPE_BOOL: {
×
2587
            bool boolTmp = tmpBool(col);
×
2588
            tools_cJSON_AddBoolToObject(value, "value", boolTmp);
×
2589
            tools_cJSON_AddStringToObject(value, "type", "bool");
×
2590
            break;
×
2591
        }
2592
        case TSDB_DATA_TYPE_FLOAT: {
×
2593
            float floatTmp = tmpFloat(col);
×
2594
            tools_cJSON_AddNumberToObject(value, "value", floatTmp);
×
2595
            tools_cJSON_AddStringToObject(value, "type", "float");
×
2596
            break;
×
2597
        }
2598
        case TSDB_DATA_TYPE_DOUBLE: {
×
2599
            double dblTmp = tmpDouble(col);
×
2600
            tools_cJSON_AddNumberToObject(value, "value", dblTmp);
×
2601
            tools_cJSON_AddStringToObject(value, "type", "double");
×
2602
            break;
×
2603
        }
2604
        case TSDB_DATA_TYPE_BINARY:
×
2605
        case TSDB_DATA_TYPE_VARBINARY:
2606
        case TSDB_DATA_TYPE_NCHAR: {
2607
            char *buf = (char *)benchCalloc(col->length + 1, 1, false);
×
2608
            rand_string(buf, col->length, g_arguments->chinese);
×
2609
            if (col->type == TSDB_DATA_TYPE_BINARY || col->type == TSDB_DATA_TYPE_VARBINARY) {
×
2610
                tools_cJSON_AddStringToObject(value, "value", buf);
×
2611
                tools_cJSON_AddStringToObject(value, "type", "binary");
×
2612
            } else {
2613
                tools_cJSON_AddStringToObject(value, "value", buf);
×
2614
                tools_cJSON_AddStringToObject(value, "type", "nchar");
×
2615
            }
2616
            tmfree(buf);
×
2617
            break;
×
2618
        }
2619
        case TSDB_DATA_TYPE_GEOMETRY: {
×
2620
            char *buf = (char *)benchCalloc(col->length + 1, 1, false);
×
2621
            tmpGeometry(buf, stbInfo->iface, col, 0);
×
2622
            tools_cJSON_AddStringToObject(value, "value", buf);
×
2623
            tools_cJSON_AddStringToObject(value, "type", "geometry");
×
2624
            tmfree(buf);
×
2625
        }
2626
        default: {
×
2627
            double dblTmp = (double)col->min;
×
2628
            if (col->max != col->min) {
×
2629
                dblTmp += (double)((taosRandom() % (col->max - col->min)));
×
2630
            }
2631
            tools_cJSON_AddNumberToObject(value, "value", dblTmp);
×
2632
            tools_cJSON_AddStringToObject(
×
2633
                    value, "type", convertDatatypeToString(col->type));
2634
            break;
×
2635
        }
2636
    }
2637
    tools_cJSON_AddItemToObject(record, "timestamp", ts);
×
2638
    tools_cJSON_AddItemToObject(record, "value", value);
×
2639
    tools_cJSON_AddItemToObject(record, "tags", tag);
×
2640
    tools_cJSON_AddStringToObject(record, "metric", stbInfo->stbName);
×
2641
    tools_cJSON_AddItemToArray(array, record);
×
2642
}
×
2643

2644
// generateTag data from random or csv file
2645
bool generateTagData(SSuperTable *stbInfo, char *buf, int64_t cnt, FILE* csv, BArray* tagsStmt, int64_t loopBegin) {
×
2646
    if(csv) {
×
2647
        if (generateSampleFromCsv(
×
2648
                buf, NULL, csv,
2649
                stbInfo->lenOfTags,
×
2650
                cnt)) {
2651
            return false;
×
2652
        }
2653
    } else {
2654
        if (generateRandData(stbInfo,
×
2655
                            buf,
2656
                            cnt * stbInfo->lenOfTags,
×
2657
                            stbInfo->lenOfTags,
×
2658
                            tagsStmt ? tagsStmt : stbInfo->tags,
2659
                            cnt, true, NULL, loopBegin)) {
2660
            errorPrint("Generate Tag Rand Data Failed. stb=%s\n", stbInfo->stbName);
×
2661
            return false;
×
2662
        }
2663
    }
2664

2665
    return true;
×
2666
}
2667

2668
static int seekFromCsv(FILE* fp, int64_t seek) {
×
2669
    size_t  n = 0;
×
2670
    char *  line = NULL;
×
2671

2672
    if (seek > 0) {
×
2673
        for (size_t i = 0; i < seek; i++){
×
2674
            ssize_t readLen = 0;
×
2675
#if defined(WIN32) || defined(WIN64)
2676
            toolsGetLineFile(&line, &n, fp);
2677
            readLen = n;
2678
            if (0 == readLen) {
2679
#else
2680
            readLen = getline(&line, &n, fp);
×
2681
            if (-1 == readLen) {
×
2682
#endif
2683
                if (0 != fseek(fp, 0, SEEK_SET)) {
×
2684
                    return -1;
×
2685
                }
2686
                continue;
×
2687
            }           
2688
        }
2689
    }
2690

2691
    tmfree(line);
×
2692
    infoPrint("seek data from csv file, seek rows=%" PRId64 "\n", seek);
×
2693
    return 0;
×
2694
}
2695

2696
// open tag from csv file
2697
FILE* openTagCsv(SSuperTable* stbInfo, uint64_t seek) {
×
2698
    FILE* csvFile = NULL;
×
2699
    if (stbInfo->tagsFile[0] != 0) {
×
2700
        csvFile = fopen(stbInfo->tagsFile, "r");
×
2701
        if (csvFile == NULL) {
×
2702
            errorPrint("Failed to open tag sample file: %s, reason:%s\n", stbInfo->tagsFile, strerror(errno));
×
2703
            return NULL;
×
2704
        }
2705
        
2706
        if (seekFromCsv(csvFile, seek)) {
×
2707
            fclose(csvFile);
×
2708
            errorPrint("Failed to seek csv file: %s, reason:%s\n", stbInfo->tagsFile, strerror(errno));
×
2709
            return NULL;
×
2710

2711
        }
2712
        infoPrint("open tag csv file :%s \n", stbInfo->tagsFile);
×
2713
        
2714
    }
2715
    return csvFile;
×
2716
}
2717

2718
//
2719
// STMT2 bind cols param progressive
2720
//
2721
uint32_t bindVColsProgressive(TAOS_STMT2_BINDV *bindv, int32_t tbIndex,
×
2722
                 threadInfo *pThreadInfo,
2723
                 uint32_t batch, int64_t startTime, int64_t pos,
2724
                 SChildTable *childTbl, int32_t *pkCur, int32_t *pkCnt, int32_t *n) {
2725
    
2726
    SSuperTable *stbInfo = pThreadInfo->stbInfo;
×
2727
    uint32_t     columnCount = stbInfo->cols->size;
×
2728

2729
    // clear
2730
    memset(pThreadInfo->bindParams, 0, sizeof(TAOS_STMT2_BIND) * (columnCount + 1));
×
2731
    debugPrint("stmt2 bindVColsProgressive child=%s batch=%d pos=%" PRId64 "\n", childTbl->name, batch, pos);
×
2732
    // loop cols
2733
    for (int c = 0; c <= columnCount; c++) {
×
2734
        // des
2735
        TAOS_STMT2_BIND *param = (TAOS_STMT2_BIND *)(pThreadInfo->bindParams + sizeof(TAOS_STMT2_BIND) * c);
×
2736
        char data_type;
2737
        int32_t length = 0;
×
2738
        if (c == 0) {
×
2739
            data_type = TSDB_DATA_TYPE_TIMESTAMP;
×
2740
            if (stbInfo->useSampleTs) {
×
2741
                param->buffer = pThreadInfo->bind_ts_array + pos;
×
2742
            } else {
2743
                param->buffer = pThreadInfo->bind_ts_array;
×
2744
            }
2745
            length = sizeof(int64_t);
×
2746
        } else {
2747
            Field *col = benchArrayGet(stbInfo->cols, c - 1);
×
2748
            data_type = col->type;
×
2749
            length    = col->length;
×
2750
            if (childTbl->useOwnSample) {
×
2751
                ChildField *childCol = benchArrayGet(childTbl->childCols, c-1);
×
2752
                param->buffer = (char *)childCol->stmtData.data + pos * col->length;
×
2753
                param->is_null = childCol->stmtData.is_null + pos;
×
2754
            } else {
2755
                param->buffer = (char *)col->stmtData.data + pos * col->length;
×
2756
                param->is_null = col->stmtData.is_null + pos;
×
2757
            }
2758
            debugPrint("col[%d]: type: %s, len: %d\n", c,
×
2759
                    convertDatatypeToString(data_type),
2760
                    col->length);
2761
        }
2762
        param->buffer_type = data_type;
×
2763
        param->length = pThreadInfo->lengths[c];
×
2764

2765
        for (int b = 0; b < batch; b++) {
×
2766
            param->length[b] = length;
×
2767
        }
2768
        param->num = batch;
×
2769
    }
2770
    
2771
    // ts key
2772
    if (!stbInfo->useSampleTs) {
×
2773
        // set first column ts array values
2774
        for (uint32_t k = 0; k < batch; k++) {
×
2775
            /* columnCount + 1 (ts) */
2776
            if (stbInfo->disorderRatio) {
×
2777
                *(pThreadInfo->bind_ts_array + k) =
×
2778
                    startTime + getTSRandTail(stbInfo->timestamp_step, *n,
×
2779
                                            stbInfo->disorderRatio,
2780
                                            stbInfo->disorderRange);
2781
            } else {
2782
                *(pThreadInfo->bind_ts_array + k) = startTime + stbInfo->timestamp_step * (*n);
×
2783
            }
2784

2785
            // check n need add
2786
            if (!stbInfo->primary_key || needChangeTs(stbInfo, pkCur, pkCnt)) {
×
2787
                *n = *n + 1;
×
2788
            }
2789
        }
2790
    }
2791

2792
    // set to bindv (only one table, so always is 0 index table)
2793
    bindv->bind_cols[tbIndex] = (TAOS_STMT2_BIND *)pThreadInfo->bindParams;
×
2794
    return batch;
×
2795
}
2796

2797

2798
//
2799
// STMT2 bind tags param progressive
2800
//
2801
uint32_t bindVTags(TAOS_STMT2_BINDV *bindv, int32_t tbIndex, int32_t w, BArray* fields) {
×
2802

2803
    TAOS_STMT2_BIND *tagsTb = bindv->tags[tbIndex];
×
2804

2805
    // loop 
2806
    for (int32_t i = 0; i < fields->size; i++) {
×
2807
        Field* field = benchArrayGet(fields, i);
×
2808

2809
        // covert field data to bind struct
2810
        tagsTb[i].buffer      = (char *)(field->stmtData.data) + field->length * w ;
×
2811
        tagsTb[i].buffer_type = field->type;
×
2812
        tagsTb[i].is_null     = field->stmtData.is_null;
×
2813
        if (IS_VAR_DATA_TYPE(field->type)) {
×
2814
            // only var set length
2815
            tagsTb[i].length  = field->stmtData.lengths;
×
2816
        }
2817

2818
        // tag always one line
2819
        tagsTb[i].num = 1;
×
2820
    }
2821
    
2822
    return 1;
×
2823
}
2824

2825
//
2826
// STMT2 bind cols param progressive
2827
//
2828
uint32_t bindVColsInterlace(TAOS_STMT2_BINDV *bindv, int32_t tbIndex,
×
2829
                 threadInfo *pThreadInfo,
2830
                 uint32_t batch, int64_t startTime, int64_t pos,
2831
                 SChildTable *childTbl, int32_t *pkCur, int32_t *pkCnt, int32_t *n) {
2832
    // count
2833
    bindv->count += 1;
×
2834
    // info
2835
    SSuperTable *stbInfo    = pThreadInfo->stbInfo;
×
2836
    TAOS_STMT2_BIND *colsTb = bindv->bind_cols[tbIndex];
×
2837
    BArray* fields          = stbInfo->cols;
×
2838

2839

2840
    // loop 
2841
    for (int32_t i = 0; i < fields->size + 1; i++) {
×
2842
        // col bind
2843
        if (i == 0) {
×
2844
            // ts 
2845
            colsTb[i].buffer_type = TSDB_DATA_TYPE_TIMESTAMP;
×
2846
            colsTb[i].length      = pThreadInfo->lengths[0];
×
2847
            for (int32_t j = 0; j < batch; j++) {
×
2848
                colsTb[i].length[j] = sizeof(int64_t); 
×
2849
            }
2850
            if (stbInfo->useSampleTs) {
×
2851
                colsTb[i].buffer = pThreadInfo->bind_ts_array + pos;
×
2852
            } else {
2853
                colsTb[i].buffer = pThreadInfo->bind_ts_array;
×
2854
            }
2855
            // no need set is_null for main key
2856
        } else {
2857
            Field* field = benchArrayGet(fields, i - 1);
×
2858
            colsTb[i].buffer_type = field->type;
×
2859

2860
            if (childTbl->useOwnSample) {
×
2861
                ChildField *childCol = benchArrayGet(childTbl->childCols, i - 1);
×
2862
                colsTb[i].buffer  = (char *)childCol->stmtData.data + pos * field->length;
×
2863
                colsTb[i].is_null = childCol->stmtData.is_null + pos;
×
2864
                colsTb[i].length  = childCol->stmtData.lengths + pos;
×
2865
            } else {
2866
                colsTb[i].buffer  = (char *)field->stmtData.data + pos * field->length;
×
2867
                colsTb[i].is_null = field->stmtData.is_null + pos;
×
2868
                colsTb[i].length  = field->stmtData.lengths + pos;
×
2869
            }
2870
        }
2871

2872
        // set batch
2873
        colsTb[i].num = batch;
×
2874
    }
2875

2876
    // ts key
2877
    if (!stbInfo->useSampleTs) {
×
2878
        // set first column ts array values
2879
        for (uint32_t k = 0; k < batch; k++) {
×
2880
            /* columnCount + 1 (ts) */
2881
            if (stbInfo->disorderRatio) {
×
2882
                *(pThreadInfo->bind_ts_array + k) =
×
2883
                    startTime + getTSRandTail(stbInfo->timestamp_step, *n,
×
2884
                                            stbInfo->disorderRatio,
2885
                                            stbInfo->disorderRange);
2886
            } else {
2887
                *(pThreadInfo->bind_ts_array + k) = startTime + stbInfo->timestamp_step * (*n);
×
2888
            }
2889

2890
            // check n need add
2891
            if (!stbInfo->primary_key || needChangeTs(stbInfo, pkCur, pkCnt)) {
×
2892
                *n = *n + 1;
×
2893
            }
2894
        }
2895
    }    
2896
    
2897
    return batch;
×
2898
}
2899

2900
// early malloc tags for stmt
2901
void prepareTagsStmt(SSuperTable* stbInfo) {
×
2902
    BArray *fields = stbInfo->tags;
×
2903
    int32_t loop   = TAG_BATCH_COUNT;
×
2904
    for (int i = 0; i < fields->size; ++i) {
×
2905
        Field *field = benchArrayGet(fields, i);
×
2906
        if (field->stmtData.data == NULL) {
×
2907
            // data
2908
            if (field->type == TSDB_DATA_TYPE_BINARY
×
2909
                    || field->type == TSDB_DATA_TYPE_NCHAR) {
×
2910
                field->stmtData.data = benchCalloc(1, loop * (field->length + 1), true);
×
2911
            } else {
2912
                field->stmtData.data = benchCalloc(1, loop * field->length, true);
×
2913
            }
2914

2915
            // is_null
2916
            field->stmtData.is_null = benchCalloc(sizeof(char), loop, true);
×
2917
            // lengths
2918
            field->stmtData.lengths = benchCalloc(sizeof(int32_t), loop, true);
×
2919

2920
            // log
2921
            debugPrint("i=%d prepareTags fields=%p %s malloc stmtData.data=%p\n", i, fields, field->name ,field->stmtData.data);
×
2922
        }
2923
    }
2924
}
×
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

© 2025 Coveralls, Inc