zhangmeng
2023-02-03 35a85de7b7495878ae4dcb73449f18c28131496c
ctest/main.c
@@ -2,13 +2,17 @@
#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 struct stimg pack_image(const int w, const int h,
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;
@@ -21,22 +25,27 @@
static void print_image(void* shm, const char* file){
    struct stimg* img = shm2image(shm);
    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);
    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");
        printf(fmt, img->id, img->data_size, img->width, img->height, img->timestamp, img->camera_id, img->camera_name);
        printf("\n");
    }
    // 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);
    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);
}
@@ -64,7 +73,7 @@
    fclose(fp);
}
static void test_image(char* shm, const int order){
static void test_image(void* shm, const int order){
    const char* pics[] = {
        "../opic/720x576.jpg",
@@ -85,8 +94,9 @@
            }else{
                w = 1920, h = 1080;
            }
            struct stimg img = pack_image(w, h, d, s);
            image2shm(shm, &img);
            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);
@@ -101,8 +111,9 @@
            }else{
                w = 1920, h = 1080;
            }
            struct stimg img = pack_image(w, h, d, s);
            image2shm(shm, &img);
            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);
@@ -112,35 +123,53 @@
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);
    char* orig = shm;
    for(int i = 0; i < 1000; i++)
        test_rule(shm);
    for(int i = 0; i < 1000; i++){
        // test_image(shm, 0);
        test_image(shm, 1);
    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;
    }
    for(int i = 0; i < 1000; i++){
        // test_image(shm, 0);
        test_image(shm, 1);
    }
    for(int i = 0; i < 1000; i++){
        test_image(shm, 0);
        test_image(shm, 1);
    }
    for(int i = 0; i < 1000; i++){
        test_image(shm, 1);
        test_image(shm, 0);
    }
    printf("shm %p next address %p\n", orig, shm);
    // char* shm2 = (char*)malloc(10*1024*1024);
    char* shm2 = shm;
    for(int i = 0; i < 1000; i++)
        test_rule(shm2);
    printf("run %d time test, each %d use %d ms\n", cntPerTest*cntRun, cntPerTest, avg);
    return 0;
}
@@ -149,7 +178,7 @@
    Target* tgts = (Target*)calloc(size, sizeof(Target));
    for(int i = 0; i < size; i++){
        tgts[i].id = i + 1002;
        tgts[i].confidence = 99;
        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);
@@ -191,6 +220,10 @@
    }
    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);
@@ -205,18 +238,21 @@
static void print_rule(void* shm){
    struct strule* rule = shm2rule(shm);
    printf("<rule> handletrack [%s] datatype [%s]\n", rule->handletrack, rule->datatype);
    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];
        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);
        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);
            print_targets(sdk->tgt, sdk->tgt_count);
        }
    }
    free(rule);
    free_strule(rule);
}
static void test_rule(void* shm){