1/* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements.  See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License.  You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17
18#include "apr_strings.h"
19#include "apr_pools.h"
20#include "apr_general.h"
21#include "apr_hash.h"
22#include "apr_lib.h"
23#include "apr_time.h"
24#include <regex.h>
25#include <stdio.h>
26#include <stdlib.h>
27
28int main( int argc, char** argv) {
29    apr_pool_t *context;
30    regex_t regex;
31    int rc;
32    int i;
33    int iters;
34    apr_time_t now;
35    apr_time_t end;
36    apr_hash_t *h;
37
38
39    if (argc !=4 ) {
40            fprintf(stderr, "Usage %s match string #iterations\n",argv[0]);
41            return -1;
42    }
43    iters = atoi( argv[3]);
44
45    apr_initialize() ;
46    atexit(apr_terminate);
47    if (apr_pool_create(&context, NULL) != APR_SUCCESS) {
48        fprintf(stderr, "Something went wrong\n");
49        exit(-1);
50    }
51    rc = regcomp( &regex, argv[1], REG_EXTENDED|REG_NOSUB);
52
53
54    if (rc) {
55        char errbuf[2000];
56        regerror(rc, &regex,errbuf,2000);
57        fprintf(stderr,"Couldn't compile regex ;(\n%s\n ",errbuf);
58        return -1;
59    }
60    if ( regexec( &regex, argv[2], 0, NULL,0) == 0 ) {
61        fprintf(stderr,"Match\n");
62    }
63    else {
64        fprintf(stderr,"No Match\n");
65    }
66    now = apr_time_now();
67    for (i=0;i<iters;i++) {
68        regexec( &regex, argv[2], 0, NULL,0) ;
69    }
70    end=apr_time_now();
71    puts(apr_psprintf( context, "Time to run %d regex's          %8lld\n",iters,end-now));
72    h = apr_hash_make( context);
73    for (i=0;i<70;i++) {
74            apr_hash_set(h,apr_psprintf(context, "%dkey",i),APR_HASH_KEY_STRING,"1");
75    }
76    now = apr_time_now();
77    for (i=0;i<iters;i++) {
78        apr_hash_get( h, argv[2], APR_HASH_KEY_STRING);
79    }
80    end=apr_time_now();
81    puts(apr_psprintf( context, "Time to run %d hash (no find)'s %8lld\n",iters,end-now));
82    apr_hash_set(h, argv[2],APR_HASH_KEY_STRING,"1");
83    now = apr_time_now();
84    for (i=0;i<iters;i++) {
85        apr_hash_get( h, argv[2], APR_HASH_KEY_STRING);
86    }
87    end=apr_time_now();
88    puts(apr_psprintf( context, "Time to run %d hash (find)'s    %8lld\n",iters,end-now));
89
90    return 0;
91}
92