summaryrefslogtreecommitdiff
path: root/src/support/MESA_timer/sample/sample.c
blob: 0b8eeebfc7b2c966d25a60d1a6d63f924a4799ec (plain)
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
#include<stdio.h>
#include<stdlib.h>
#include<sys/time.h>
#include<unistd.h>
#include<time.h>
#include"MESA_timer.h"


typedef struct event_t{
    int id;
}event_t;

void event_cb(void * event)
{
    event_t * _event = (event_t *)event;
    struct timeval now;
    gettimeofday(&now, NULL);
    long curtime = now.tv_sec * 1000000 + now.tv_usec;
    printf("event %d timeout, current time is %ld\n", _event->id, curtime / 1000);
    return;
}

void unit_test_for_queue()
{
    MESA_timer_t * timer = MESA_timer_create(0, TM_TYPE_QUEUE);

    struct timeval current_time;
    long timeout = 5000;
    long curtime;
    MESA_timer_index_t * indexs[10000];
    event_t events[10000];
    int i = 0;
    while(i < 10000)
    {
        events[i].id = i;
        gettimeofday(&current_time, NULL);
        curtime = current_time.tv_sec * 1000000 + current_time.tv_usec;

        MESA_timer_check(timer, curtime, 10000);
        if(MESA_timer_add(timer, curtime, timeout, event_cb, &events[i], &indexs[i]) < 0)
        {
            printf("add event %d failed\n", i);
        }
        else
        {
            printf("add event %d success, it will occur at %ld\n", i, (curtime + timeout) / 1000);
        }

        usleep(100);
        i++;
    }
    printf("%ld\n", MESA_timer_count(timer));
    printf("%ld\n", MESA_timer_memsize(timer));
    MESA_timer_destroy(timer, NULL, NULL);
}


void unit_test_for_wheel()
{
    MESA_timer_t * timer = MESA_timer_create(3000, TM_TYPE_WHEEL);

    srand((int)time(NULL));

    struct timeval current_time;
    long curtime;

    MESA_timer_index_t * indexs[10000];
    event_t events[10000];
    int i = 0;

    while(i < 10000)
    {
        long timeout = (rand() % 10 + 1) * 1000;
        events[i].id = i;
        gettimeofday(&current_time, NULL);
        curtime = current_time.tv_sec * 1000000 + current_time.tv_usec;

        MESA_timer_check(timer, curtime, 10000);
        if(MESA_timer_add(timer, curtime, timeout, event_cb, &events[i], &indexs[i]) < 0)
        {
            printf("add event %d failed\n", i);
        }
        else
        {
            printf("add event %d at time %ld, timeout %ld, it will occur at %ld\n", \
                    i, curtime / 1000, timeout,  (curtime + timeout) / 1000);
        }

        usleep(100);
        i++;
    }
    MESA_timer_destroy(timer, NULL, NULL);
}


void wheel_test()
{
    MESA_timer_t * timer = MESA_timer_create(6, TM_TYPE_WHEEL);

    srand((int)time(NULL));

    struct timeval current_time;
    long curtime;

    MESA_timer_index_t * indexs[20];
    event_t events[20];
    int i = 0;

    while(i < 20)
    {
        long timeout = rand() % 10;
        events[i].id = i;
        curtime = i;
        MESA_timer_check(timer, curtime, 10000);
        if(MESA_timer_add(timer, curtime, timeout, event_cb, &events[i], &indexs[i]) < 0)
        {
            printf("add event %d failed\n", i - 10);
        }
        else
        {
            printf("add event %d at time %ld, timeout %ld, it will occur at %ld\n", \
                    i, curtime, timeout,  curtime + timeout);
        }

        sleep(1);
        i++;
    }
    MESA_timer_destroy(timer, NULL, NULL);
}



int main()
{
    /* unit_test_for_wheel(); */
    unit_test_for_queue();
    /* wheel_test(); */
    return 0;
}