コード例 #1
0
ファイル: main_ipc.cpp プロジェクト: YangChunQuan/Code
int Wcache()
{
    Cache cache;

    int nRet = cache.Attach(g_sKeyFile, WRITER);
    if (nRet < 0)
    {
        printf("%s\n", cache.Strerr());
        return -1;
    }

    printf("Attach success %d\n", nRet);
    nRet = cache.Register();
    if (nRet < 0)
    {
        printf("%s\n", cache.Strerr());
        return -1;
    }
    printf("Register  OK %d\n", nRet);

    gettimeofday(&time_c, NULL);
    while (true)
    {
        if (_HandleSignal() < 0)
            break;

        gettimeofday(&time_a, NULL);
        memcpy(wbuff, &time_a, sizeof(time_a));
        nRet = cache.Write(wbuff, g_len);
        gettimeofday(&time_b, NULL);

        if (nRet != g_len)
        {
            printf("write fialed %s\n", cache.Strerr());
            return -1;
        }
        g_count0++;

        unsigned long long use_time0 = (time_b.tv_sec - time_a.tv_sec) * 1000000 + (time_b.tv_usec - time_a.tv_usec);
        total_time0 += use_time0;

        if(g_usleep > 0)
            usleep(g_usleep);
    }
    double time = (time_b.tv_sec-time_c.tv_sec)*1000000 + (time_b.tv_usec - time_c.tv_usec);
    double speed = (double)g_count0/time;
    printf("speed:%lf unit/s \n", speed*1000000);

    return 0;
}
コード例 #2
0
ファイル: main_ipc.cpp プロジェクト: YangChunQuan/Code
int Cache_()
{
    Cache cachew;
    Cache cacher;

    int nRet = cachew.Attach(g_sKeyFile, WRITER);
    if (nRet < 0)
    {
        printf("%s\n", cachew.Strerr());
        return -1;
    }
    nRet = cacher.Attach(g_sKeyFile, READER);
    if (nRet < 0)
    {
        printf("%s\n", cacher.Strerr());
        return -1;
    }

    printf("Attach success %d\n", nRet);
    nRet = cachew.Register();
    if (nRet < 0)
    {
        printf("%s\n", cachew.Strerr());
        return -1;
    }
    nRet = cacher.Register();
    if (nRet < 0)
    {
        printf("%s\n", cacher.Strerr());
        return -1;
    }
    printf("Register  OK %d\n", nRet);

    while (true)
    {
        if (_HandleSignal() < 0)
            break;

        gettimeofday(&time_a, NULL);
        nRet = cachew.Write(wbuff, g_len);
        gettimeofday(&time_b, NULL);

        if (nRet != g_len)
        {
            printf("fialed %s\n", cachew.Strerr());
            break;
        }

        if(g_usleep > 0)
            usleep(g_usleep);

        gettimeofday(&time_c, NULL);
        nRet = cacher.Read(rbuff, sizeof(rbuff), g_nSig);
        gettimeofday(&time_d, NULL);

        if (nRet != g_len)
        {
            printf("fialed %s\n", cacher.Strerr());
            break;
        }

        g_count1++;
        g_count2++;

        unsigned long long use_time1 = (time_b.tv_sec - time_a.tv_sec) * 1000000 + (time_b.tv_usec - time_a.tv_usec);
        total_time1 += use_time1;
        unsigned long long use_time2 = (time_d.tv_sec - time_c.tv_sec) * 1000000 + (time_d.tv_usec - time_c.tv_usec);
        total_time2 += use_time2;
    }

    return 0;
}