Parially, a patch from Scott McKellar:
[OpenSRF.git] / src / c-apps / timejson.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <stdarg.h>
5 #include <time.h>
6 #include <sys/time.h>
7 #include <sys/resource.h>
8 #include <malloc.h>
9 #include "opensrf/utils.h"
10 #include "opensrf/osrf_json.h"
11
12 struct timeval diff_timeval( const struct timeval * begin,
13         const struct timeval * end );
14
15 static const char sample_json[] =
16         "{\"menu\": {\"id\": \"file\", \"value\": \"File\","
17         "\"popup\": { \"menuitem\": [ {\"value\": \"New\", "
18         "\"onclick\": \"CreateNewDoc()\"},"
19         "{\"value\": \"Open\", \"onclick\": \"OpenDoc()\"}, "
20         "{\"value\": \"Close\", \"onclick\": \"CloseDoc()\"}]}}}";
21
22 int main( void ) {
23         int rc = 0;
24
25         struct timezone tz = { 240, 1 };
26         struct timeval begin_timeval;
27         struct timeval end_timeval;
28
29         gettimeofday( &begin_timeval, &tz );
30
31         long i;
32         jsonObject * pObj = NULL;
33
34         for( i = 10000000; i; --i )
35         {
36 //              pObj = jsonParseString( sample_json );
37                 pObj = jsonNewObject( NULL );
38                 jsonObject * p1 = jsonNewObject( NULL );
39                 jsonObject * p2 = jsonNewObject( NULL );
40                 jsonObjectFree( p1 );
41                 jsonObjectFree( p2 );
42                 jsonObjectFree( pObj );
43         }
44
45         jsonObjectFreeUnused();
46         
47         gettimeofday( &end_timeval, &tz );
48
49         struct timeval elapsed = diff_timeval( &begin_timeval, &end_timeval );
50
51         printf( "Elapsed time: %ld seconds, %ld microseconds\n",
52                         (long) elapsed.tv_sec, (long) elapsed.tv_usec );
53
54         struct rlimit rlim;
55         if( getrlimit( RLIMIT_DATA, &rlim ) )
56                 printf( "Error calling getrlimit\n" );
57         else
58                 printf( "Address space: %lu\n", (unsigned long) rlim.rlim_cur );
59
60         malloc_stats();
61
62         return rc;
63 }
64
65 struct timeval diff_timeval( const struct timeval * begin, const struct timeval * end )
66 {
67         struct timeval diff;
68
69         diff.tv_sec = end->tv_sec - begin->tv_sec;
70         diff.tv_usec = end->tv_usec - begin->tv_usec;
71
72         if( diff.tv_usec < 0 )
73         {
74                 diff.tv_usec += 1000000;
75                 --diff.tv_sec;
76         }
77
78         return diff;
79
80 }