| 1 | /* |
|---|
| 2 | * Copyright 2008 Digital Bazaar, Inc. |
|---|
| 3 | * |
|---|
| 4 | * This file is part of librdfa. |
|---|
| 5 | * |
|---|
| 6 | * librdfa is Free Software, and can be licensed under any of the |
|---|
| 7 | * following three licenses: |
|---|
| 8 | * |
|---|
| 9 | * 1. GNU Lesser General Public License (LGPL) V2.1 or any |
|---|
| 10 | * newer version |
|---|
| 11 | * 2. GNU General Public License (GPL) V2 or any newer version |
|---|
| 12 | * 3. Apache License, V2.0 or any newer version |
|---|
| 13 | * |
|---|
| 14 | * You may not use this file except in compliance with at least one of |
|---|
| 15 | * the above three licenses. |
|---|
| 16 | * |
|---|
| 17 | * See LICENSE-* at the top of this software distribution for more |
|---|
| 18 | * information regarding the details of each license. |
|---|
| 19 | * |
|---|
| 20 | * This test checks to see how quickly we can process triples and is a |
|---|
| 21 | * very basic performance test for the librdfa library. |
|---|
| 22 | */ |
|---|
| 23 | #include <stdio.h> |
|---|
| 24 | #include <string.h> |
|---|
| 25 | #include <time.h> |
|---|
| 26 | #include <rdfa.h> |
|---|
| 27 | #include <rdfa_utils.h> |
|---|
| 28 | |
|---|
| 29 | #define MAX_ITERATIONS 20000 |
|---|
| 30 | int g_iteration = 0; |
|---|
| 31 | rdfacontext* g_context = NULL; |
|---|
| 32 | unsigned long long g_bytes_processed = 0; |
|---|
| 33 | |
|---|
| 34 | void process_triple(rdftriple* triple, void* callback_data) |
|---|
| 35 | { |
|---|
| 36 | rdfa_free_triple(triple); |
|---|
| 37 | } |
|---|
| 38 | |
|---|
| 39 | size_t fill_buffer(char* buffer, size_t buffer_length, void* callback_data) |
|---|
| 40 | { |
|---|
| 41 | // short-circuit last iteration |
|---|
| 42 | if(g_iteration == MAX_ITERATIONS + 1) |
|---|
| 43 | { |
|---|
| 44 | return 0; |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | char* data = NULL; |
|---|
| 48 | size_t data_length = buffer_length; |
|---|
| 49 | memset(buffer, ' ', buffer_length); |
|---|
| 50 | |
|---|
| 51 | // Note: code assumes data length < buffer length |
|---|
| 52 | if(g_iteration == 0) |
|---|
| 53 | { |
|---|
| 54 | data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
|---|
| 55 | "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML+RDFa 1.0//EN\" " |
|---|
| 56 | "\"http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd\">\n" |
|---|
| 57 | "<html xmlns=\"http://www.w3.org/1999/xhtml\"\n" |
|---|
| 58 | " xmlns:dc=\"http://purl.org/dc/elements/1.1/\">\n" |
|---|
| 59 | "<head><title>Speed Test</title></head>\n" |
|---|
| 60 | "<body><p>\n"; |
|---|
| 61 | memcpy(buffer, data, strlen(data)); |
|---|
| 62 | } |
|---|
| 63 | else if(g_iteration < MAX_ITERATIONS) |
|---|
| 64 | { |
|---|
| 65 | data = "<span about=\"#foo\" rel=\"dc:title\" resource=\"#you\" />"; |
|---|
| 66 | memcpy(buffer, data, strlen(data)); |
|---|
| 67 | } |
|---|
| 68 | else |
|---|
| 69 | { |
|---|
| 70 | data = "</p></body></html>"; |
|---|
| 71 | |
|---|
| 72 | // update data_length because this is the end of the stream, no |
|---|
| 73 | // whitespace after it |
|---|
| 74 | data_length = strlen(data); |
|---|
| 75 | memcpy(buffer, data, data_length); |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | g_iteration++; |
|---|
| 79 | g_bytes_processed += data_length; |
|---|
| 80 | //buffer[buffer_length - 1] = 0; |
|---|
| 81 | |
|---|
| 82 | //printf("%s", buffer); |
|---|
| 83 | |
|---|
| 84 | return data_length; |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | int main(int argc, char** argv) |
|---|
| 88 | { |
|---|
| 89 | printf("Speed test...\n"); |
|---|
| 90 | |
|---|
| 91 | clock_t stime = clock(); |
|---|
| 92 | |
|---|
| 93 | rdfacontext* g_context = rdfa_create_context("http://example.org/speed"); |
|---|
| 94 | rdfa_set_default_graph_triple_handler(g_context, &process_triple); |
|---|
| 95 | rdfa_set_buffer_filler(g_context, &fill_buffer); |
|---|
| 96 | rdfa_parse(g_context); |
|---|
| 97 | rdfa_free_context(g_context); |
|---|
| 98 | |
|---|
| 99 | clock_t etime = clock(); |
|---|
| 100 | |
|---|
| 101 | float delta = etime - stime; |
|---|
| 102 | printf("Processed %1.2f triples per second from %lli bytes of data.\n", |
|---|
| 103 | (MAX_ITERATIONS / (delta / CLOCKS_PER_SEC)), g_bytes_processed); |
|---|
| 104 | |
|---|
| 105 | return 0; |
|---|
| 106 | } |
|---|