~ubuntu-branches/ubuntu/saucy/libpthread-workqueue/saucy

« back to all changes in this revision

Viewing changes to testing/api/test.c

  • Committer: Bazaar Package Importer
  • Author(s): Mark Heily
  • Date: 2011-03-13 16:22:30 UTC
  • Revision ID: james.westby@ubuntu.com-20110313162230-yaiyoa7g3dk8xmww
Tags: upstream-0.4.1
ImportĀ upstreamĀ versionĀ 0.4.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <limits.h>
 
2
#include <stdlib.h>
 
3
#include <stdio.h>
 
4
#include <string.h>
 
5
#include <unistd.h>
 
6
 
 
7
#include "config.h"
 
8
#include "src/private.h"
 
9
 
 
10
#if HAVE_ERR_H
 
11
# include <err.h>
 
12
#else
 
13
# define err(rc,msg,...) do { perror(msg); exit(rc); } while (0)
 
14
# define errx(rc,msg,...) do { puts(msg); exit(rc); } while (0)
 
15
#endif
 
16
 
 
17
#include "pthread_workqueue.h"
 
18
 
 
19
static int work_cnt;
 
20
 
 
21
pthread_workqueue_t wq;
 
22
 
 
23
void additem(void (*func)(void *), 
 
24
             void * arg)
 
25
{
 
26
    int rv;
 
27
    
 
28
    rv = pthread_workqueue_additem_np(wq, *func, arg, NULL, NULL);
 
29
    if (rv != 0)
 
30
        errx(1, "unable to add item: %s", strerror(rv));
 
31
    puts("added item\n");
 
32
}
 
33
 
 
34
void
 
35
compute(void *arg)
 
36
{
 
37
    int *count = (int *) arg;
 
38
    static const int nval = 5000;
 
39
    int val[nval];
 
40
    int i,j;
 
41
 
 
42
    /* Do some useless computation */
 
43
    for (i = 0; i < nval; i++) {
 
44
        val[i] = INT_MAX;
 
45
    }
 
46
    for (j = 0; j < nval; j++) {
 
47
        for (i = 0; i < nval; i++) {
 
48
            val[i] /= 3;
 
49
            val[i] *= 2;
 
50
            val[i] /= 4;
 
51
            val[i] *= 5;
 
52
        }
 
53
    }
 
54
 
 
55
    if (count != NULL) 
 
56
        (*count)--;
 
57
}
 
58
 
 
59
 
 
60
void
 
61
sleepy(void *msg)
 
62
{
 
63
    printf("%s\n", (char *) msg);
 
64
    if (strcmp(msg, "done") == 0)
 
65
        exit(0);
 
66
    sleep(random() % 6);
 
67
}
 
68
 
 
69
void
 
70
lazy(void *arg)
 
71
{
 
72
    sleep(3);
 
73
    printf("item %lu complete\n", (unsigned long) arg);
 
74
        work_cnt--;
 
75
}
 
76
 
 
77
void
 
78
run_blocking_test(void)
 
79
{
 
80
        const int rounds = 50;
 
81
        work_cnt = rounds;
 
82
    for (unsigned long i = 0; i < rounds; i++) {
 
83
        additem(lazy, (void *) i);
 
84
    }
 
85
        while (work_cnt > 0)
 
86
                sleep(1);
 
87
}
 
88
 
 
89
void
 
90
run_cond_wait_test(void)
 
91
{
 
92
        const int rounds = 10;
 
93
 
 
94
        sleep(3);       /* Allow time for the workers to enter pthread_cond_wait() */
 
95
        work_cnt = rounds;
 
96
    for (unsigned long i = 0; i < rounds; i++) {
 
97
        additem(lazy, (void *) i);
 
98
                sleep(1);
 
99
    }
 
100
        while (work_cnt > 0)
 
101
                sleep(1);
 
102
}
 
103
 
 
104
void
 
105
run_load_test(void)
 
106
{
 
107
    char buf[16];
 
108
    for (int i = 0; i < 1024; i++) {
 
109
        sprintf(buf, "%d", i);
 
110
        additem(sleepy, strdup(buf));
 
111
        additem(compute, NULL);
 
112
    }
 
113
    additem(sleepy, "done");
 
114
}
 
115
 
 
116
/* Try to overwhelm the CPU with computation requests */
 
117
void
 
118
run_stress_test(int rounds)
 
119
{
 
120
        work_cnt = rounds;
 
121
    for (int i = 0; i < rounds; i++) {
 
122
        additem(compute, &work_cnt);
 
123
    }
 
124
        while (work_cnt > 0)
 
125
                sleep(1);
 
126
    puts("====== stress test complete =======");
 
127
}
 
128
 
 
129
 
 
130
int main() {
 
131
    int rv;
 
132
 
 
133
    printf("pthread_workqueue_create_np().. ");
 
134
    rv = pthread_workqueue_create_np(&wq, NULL);
 
135
    if (rv != 0)
 
136
        err(1, "failed");
 
137
    printf("ok\n");
 
138
 
 
139
    printf("stress test.. ");
 
140
    run_stress_test(25);
 
141
    printf("ok\n");
 
142
 
 
143
    //run_deadlock_test();
 
144
//    run_cond_wait_test();
 
145
//    run_blocking_test();
 
146
    //run_load_test();
 
147
 
 
148
 
 
149
        puts("All tests completed.\n");
 
150
    exit(0);
 
151
}