zhangmeng
2023-02-03 35a85de7b7495878ae4dcb73449f18c28131496c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/time.h>
#include "shmparser/shmparser.h"
 
static char* timestamp = "2023-1-31 10:05:56.998";
static char* cid = "cameraid";
static char* cname = "cameraname";
 
static int printlog = 1;
static int savepic = 0;
 
static struct stimg* pack_image(const int w, const int h,
                                const unsigned char* data, const uint64_t size)
{
    int width = w, height = h;
    uint64_t id = 1223997600787778;
 
    return make_image_ref(id, data, size, width, height,
        timestamp, strlen(timestamp), cid, strlen(cid), cname, strlen(cname));
}
 
static void print_image(void* shm, const char* file){
    struct stimg* img = shm2image(shm);
 
    if (printlog){
        char fmt[128] = {0};
        sprintf(fmt, "imag2 id [%%lu] data size [%%lu] width [%%d] height [%%d] "
            "time [%%%d.%ds] cid [%%%d.%ds] cname [%%%d.%ds]",
            img->timestamp_size, img->timestamp_size,
            img->camera_id_size, img->camera_id_size,
            img->camera_name_size, img->camera_name_size);
 
        printf(fmt, img->id, img->data_size, img->width, img->height, img->timestamp, img->camera_id, img->camera_name);
        printf("\n");
    }
 
    if (savepic){
        FILE* fp = fopen(file, "wb");
        long pos = 0;
        while (pos != img->data_size) {
            pos += fwrite(img->data+pos, 1, img->data_size-pos, fp);
        }
        fclose(fp);
    }
 
 
    free_stimg(img);
}
 
static void readfile(const char* file, unsigned char** data, size_t* size){
    FILE* fp = fopen(file, "rb");
    if (!fp) {
        printf("file %s error %s\n", file, strerror(errno));
        return;
    }
 
    fseek(fp, 0, SEEK_END);
    *size = ftell(fp);
    fseek(fp, 0, SEEK_SET);
 
    unsigned char* d = (unsigned char*)malloc(*size);
    long s = *size;
    long pos = 0;
    while (pos != *size) {
        pos += fread(d+pos, 1, s-pos, fp);
    }
 
    *data = d;
 
    fclose(fp);
}
 
static void test_image(void* shm, const int order){
 
    const char* pics[] = {
        "../opic/720x576.jpg",
        "../opic/720x576.bgr",
        "../opic/1920x1080.jpg",
        "../opic/1920x1080.bgr",
    };
 
    unsigned char* d = NULL;
    size_t s = 0;
 
    if (order == 0)
        for(int i = 0; i < sizeof(pics)/sizeof(pics[0]); i++){
            readfile(pics[i], &d, &s);
            int w = 0, h = 0;
            if (i < 2){
                w = 720, h = 576;
            }else{
                w = 1920, h = 1080;
            }
            struct stimg* img = pack_image(w, h, d, s);
            image2shm(shm, img);
            free_stimg(img);
            free(d);
            char file[128] = {0};
            sprintf(file, "../npic/%dx%d.%d", w, h, i);
            print_image(shm, file);
        }
    else
        for(int i = sizeof(pics)/sizeof(pics[0]) - 1; i >= 0; i--){
            readfile(pics[i], &d, &s);
            int w = 0, h = 0;
            if (i < 2){
                w = 720, h = 576;
            }else{
                w = 1920, h = 1080;
            }
            struct stimg* img = pack_image(w, h, d, s);
            image2shm(shm, img);
            free_stimg(img);
            free(d);
            char file[128] = {0};
            sprintf(file, "../npic/%dx%d.%d", w, h, i);
            print_image(shm, file);
        }
}
 
static void test_rule(void* shm);
 
static int test_once(void* shm, const int count){
 
    struct timeval s, e;
    gettimeofday(&s, NULL);
 
    for(int i = 0; i < count; i++)
        test_rule(shm);
 
    for(int i = 0; i < count; i++){
        test_image(shm, 0);
        // test_image(shm, 1);
    }
    for(int i = 0; i < count; i++){
        // test_image(shm, 0);
        test_image(shm, 1);
    }
    for(int i = 0; i < count; i++){
        test_image(shm, 0);
        test_image(shm, 1);
    }
    for(int i = 0; i < count; i++){
        test_image(shm, 1);
        test_image(shm, 0);
    }
 
    for(int i = 0; i < count; i++)
        test_rule(shm);
 
    gettimeofday(&e, NULL);
 
    return (e.tv_sec- s.tv_sec)*1000 + (e.tv_usec - s.tv_usec)/1000;
}
 
int main(int argc, char const *argv[])
{
    char* shm = (char*)malloc(10*1024*1024);
 
    int cntPerTest = 1000;
    int cntRun = 1;
    int avg = 0;
    for(int i = 1; i <= cntRun; i++){
        int tm = test_once(shm, cntPerTest);
        if (avg == 0) avg = tm;
        else avg = (double)avg / (double) i * (double)(i-1) + (double)tm / (double)i;
    }
 
    printf("run %d time test, each %d use %d ms\n", cntPerTest*cntRun, cntPerTest, avg);
 
    return 0;
}
 
static Target* pack_target(const int size){
    Target* tgts = (Target*)calloc(size, sizeof(Target));
    for(int i = 0; i < size; i++){
        tgts[i].id = i + 1002;
        tgts[i].confidence = 99-i;
        TRect rc = {0,0,1920,1080};
        memcpy(&tgts[i].rect, &rc, sizeof(TRect));
        memcpy(tgts[i].type, "pack_target", 11);
        tgts[i].feature_size = 123;
        tgts[i].feature = (char*)calloc(tgts[i].feature_size, 1);
        memcpy(tgts[i].feature, "feature and feature_size", 24);
        tgts[i].attribute_size = 235;
        tgts[i].attribute = (char*)calloc(tgts[i].attribute_size, 1);
        memcpy(tgts[i].attribute, "attribute and attribute_size", 28);
    }
    return tgts;
}
static void print_targets(struct sttgt* tgts, const int count){
    for(int i = 0; i < count; i++){
        struct sttgt* tgt = &tgts[i];
        printf("\t\t<target> [%d] id [%lu] confidence [%d] rect [%dx%dx%dx%d] type [%s] \
            feature_size [%d] feature [%s] attribute_size [%d] attribute [%s]\n",
            i, tgt->id, tgt->confidence,
            tgt->rect.left, tgt->rect.top, tgt->rect.right, tgt->rect.bottom,
            tgt->type,
            tgt->feature_size, tgt->feature, tgt->attribute_size, tgt->attribute);
    }
}
 
const char* sdktype[] = { "face", "yolo", "bike", "fire", "car" };
static void pack_result(void* shm, const int s, const int e){
 
    int sdkcnt = e - s;
 
    TResult* resarray = (TResult*)calloc(sdkcnt, sizeof(TResult));
    for(size_t i = 0; i < sdkcnt; i++){
        resarray[i].count = 2+i;
        resarray[i].targets = pack_target(resarray[i].count);
    }
 
    for(size_t i = 0; i < sdkcnt; i++){
        add_result2rule_in_shm(shm, &resarray[i],
            sdktype[s+i], strlen(sdktype[i+s]), timestamp, strlen(timestamp));
    }
 
    for(size_t i = 0; i < sdkcnt; i++){
        for(int j = 0; j < resarray[i].count; j++){
            free(resarray[i].targets[j].feature);
            free(resarray[i].targets[j].attribute);
        }
        free(resarray[i].targets);
    }
    free(resarray);
}
 
static char* handletrack = "camera1";
static void pack_rule(void* shm){
 
    rule2shm(shm, handletrack, strlen(handletrack));
    pack_result(shm, 0, 3);
}
static void print_rule(void* shm){
    struct strule* rule = shm2rule(shm);
 
    if (printlog)
        printf("<rule> handletrack [%s] datatype [%s]\n", rule->handletrack, rule->datatype);
 
    for(int i = 0; i < rule->sdk_count; i++){
 
        struct stsdk* sdk = &rule->sdk[i];
        if (printlog){
            printf("\t<sdk> [%d] type [%s] tgts [%d] id [%s] name [%s] timestamp [%s]\n",
                i, sdk->sdktype, sdk->tgt_count, sdk->sdkid, sdk->sdkname, sdk->timestamp);
 
            print_targets(sdk->tgt, sdk->tgt_count);
        }
 
    }
    free_strule(rule);
}
 
static void test_rule(void* shm){
    pack_rule(shm);
    pack_result(shm, 2, 5);
 
    pack_result(shm, 0, 5);
 
    // 测试时发现如果取 6 造成 rule 中的 handletrack 字符串的长度溢出
    // 取 5 时大约有 35 个 sdk 运行,目前一张图片不会运行 35 个 sdk
    // 暂时保留 handletrack 的 256 个字符限制
    // for(int i = 0; i < 6; i++){
    for(int i = 0; i < 5; i++){
        pack_result(shm, 0, 5);
    }
 
    print_rule(shm);
}