Home | History | Annotate | Download | only in examples
      1 /*
      2  * compress_functions.c
      3  * Copyright  : Kyle Harper
      4  * License    : Follows same licensing as the lz4.c/lz4.h program at any given time.  Currently, BSD 2.
      5  * Description: A program to demonstrate the various compression functions involved in when using LZ4_compress_default().  The idea
      6  *              is to show how each step in the call stack can be used directly, if desired.  There is also some benchmarking for
      7  *              each function to demonstrate the (probably lack of) performance difference when jumping the stack.
      8  *              (If you're new to lz4, please read simple_buffer.c to understand the fundamentals)
      9  *
     10  *              The call stack (before theoretical compiler optimizations) for LZ4_compress_default is as follows:
     11  *                LZ4_compress_default
     12  *                  LZ4_compress_fast
     13  *                    LZ4_compress_fast_extState
     14  *                      LZ4_compress_generic
     15  *
     16  *              LZ4_compress_default()
     17  *                This is the recommended function for compressing data.  It will serve as the baseline for comparison.
     18  *              LZ4_compress_fast()
     19  *                Despite its name, it's not a "fast" version of compression.  It simply decides if HEAPMODE is set and either
     20  *                allocates memory on the heap for a struct or creates the struct directly on the stack.  Stack access is generally
     21  *                faster but this function itself isn't giving that advantage, it's just some logic for compile time.
     22  *              LZ4_compress_fast_extState()
     23  *                This simply accepts all the pointers and values collected thus far and adds logic to determine how
     24  *                LZ4_compress_generic should be invoked; specifically: can the source fit into a single pass as determined by
     25  *                LZ4_64Klimit.
     26  *              LZ4_compress_generic()
     27  *                As the name suggests, this is the generic function that ultimately does most of the heavy lifting.  Calling this
     28  *                directly can help avoid some test cases and branching which might be useful in some implementation-specific
     29  *                situations, but you really need to know what you're doing AND what you're asking lz4 to do!  You also need a
     30  *                wrapper function because this function isn't exposed with lz4.h.
     31  *
     32  *              The call stack for decompression functions is shallow.  There are 2 options:
     33  *                LZ4_decompress_safe  ||  LZ4_decompress_fast
     34  *                  LZ4_decompress_generic
     35  *
     36  *               LZ4_decompress_safe
     37  *                 This is the recommended function for decompressing data.  It is considered safe because the caller specifies
     38  *                 both the size of the compresssed buffer to read as well as the maximum size of the output (decompressed) buffer
     39  *                 instead of just the latter.
     40  *               LZ4_decompress_fast
     41  *                 Again, despite its name it's not a "fast" version of decompression.  It simply frees the caller of sending the
     42  *                 size of the compressed buffer (it will simply be read-to-end, hence it's non-safety).
     43  *               LZ4_decompress_generic
     44  *                 This is the generic function that both of the LZ4_decompress_* functions above end up calling.  Calling this
     45  *                 directly is not advised, period.  Furthermore, it is a static inline function in lz4.c, so there isn't a symbol
     46  *                 exposed for anyone using lz4.h to utilize.
     47  *
     48  *               Special Note About Decompression:
     49  *               Using the LZ4_decompress_safe() function protects against malicious (user) input.  If you are using data from a
     50  *               trusted source, or if your program is the producer (P) as well as its consumer (C) in a PC or MPMC setup, you can
     51  *               safely use the LZ4_decompress_fast function
     52  */
     53 
     54 /* Since lz4 compiles with c99 and not gnu/std99 we need to enable POSIX linking for time.h structs and functions. */
     55 #if __STDC_VERSION__ >= 199901L
     56 #define _XOPEN_SOURCE 600
     57 #else
     58 #define _XOPEN_SOURCE 500
     59 #endif
     60 #define _POSIX_C_SOURCE 199309L
     61 
     62 /* Includes, for Power! */
     63 #include "lz4.h"
     64 #include <stdio.h>    /* for printf() */
     65 #include <stdlib.h>   /* for exit() */
     66 #include <string.h>   /* for atoi() memcmp() */
     67 #include <stdint.h>   /* for uint_types */
     68 #include <inttypes.h> /* for PRIu64 */
     69 #include <time.h>     /* for clock_gettime() */
     70 #include <locale.h>   /* for setlocale() */
     71 
     72 /* We need to know what one billion is for clock timing. */
     73 #define BILLION 1000000000L
     74 
     75 /* Create a crude set of test IDs so we can switch on them later  (Can't switch() on a char[] or char*). */
     76 #define ID__LZ4_COMPRESS_DEFAULT        1
     77 #define ID__LZ4_COMPRESS_FAST           2
     78 #define ID__LZ4_COMPRESS_FAST_EXTSTATE  3
     79 #define ID__LZ4_COMPRESS_GENERIC        4
     80 #define ID__LZ4_DECOMPRESS_SAFE         5
     81 #define ID__LZ4_DECOMPRESS_FAST         6
     82 
     83 
     84 
     85 /*
     86  * Easy show-error-and-bail function.
     87  */
     88 void run_screaming(const char *message, const int code) {
     89   printf("%s\n", message);
     90   exit(code);
     91   return;
     92 }
     93 
     94 
     95 /*
     96  * Centralize the usage function to keep main cleaner.
     97  */
     98 void usage(const char *message) {
     99   printf("Usage: ./argPerformanceTesting <iterations>\n");
    100   run_screaming(message, 1);
    101   return;
    102 }
    103 
    104 
    105 
    106 /*
    107  * Runs the benchmark for LZ4_compress_* based on function_id.
    108  */
    109 uint64_t bench(
    110     const char *known_good_dst,
    111     const int function_id,
    112     const int iterations,
    113     const char *src,
    114     char *dst,
    115     const size_t src_size,
    116     const size_t max_dst_size,
    117     const size_t comp_size
    118   ) {
    119   uint64_t time_taken = 0;
    120   int rv = 0;
    121   const int warm_up = 5000;
    122   struct timespec start, end;
    123   const int acceleration = 1;
    124   LZ4_stream_t state;
    125 
    126   // Select the right function to perform the benchmark on.  We perform 5000 initial loops to warm the cache and ensure that dst
    127   // remains matching to known_good_dst between successive calls.
    128   switch(function_id) {
    129     case ID__LZ4_COMPRESS_DEFAULT:
    130       printf("Starting benchmark for function: LZ4_compress_default()\n");
    131       for(int junk=0; junk<warm_up; junk++)
    132         rv = LZ4_compress_default(src, dst, src_size, max_dst_size);
    133       if (rv < 1)
    134         run_screaming("Couldn't run LZ4_compress_default()... error code received is in exit code.", rv);
    135       if (memcmp(known_good_dst, dst, max_dst_size) != 0)
    136         run_screaming("According to memcmp(), the compressed dst we got doesn't match the known_good_dst... ruh roh.", 1);
    137       clock_gettime(CLOCK_MONOTONIC, &start);
    138       for (int i=1; i<=iterations; i++)
    139         LZ4_compress_default(src, dst, src_size, max_dst_size);
    140       break;
    141 
    142     case ID__LZ4_COMPRESS_FAST:
    143       printf("Starting benchmark for function: LZ4_compress_fast()\n");
    144       for(int junk=0; junk<warm_up; junk++)
    145         rv = LZ4_compress_fast(src, dst, src_size, max_dst_size, acceleration);
    146       if (rv < 1)
    147         run_screaming("Couldn't run LZ4_compress_fast()... error code received is in exit code.", rv);
    148       if (memcmp(known_good_dst, dst, max_dst_size) != 0)
    149         run_screaming("According to memcmp(), the compressed dst we got doesn't match the known_good_dst... ruh roh.", 1);
    150       clock_gettime(CLOCK_MONOTONIC, &start);
    151       for (int i=1; i<=iterations; i++)
    152         LZ4_compress_fast(src, dst, src_size, max_dst_size, acceleration);
    153       break;
    154 
    155     case ID__LZ4_COMPRESS_FAST_EXTSTATE:
    156       printf("Starting benchmark for function: LZ4_compress_fast_extState()\n");
    157       for(int junk=0; junk<warm_up; junk++)
    158         rv = LZ4_compress_fast_extState(&state, src, dst, src_size, max_dst_size, acceleration);
    159       if (rv < 1)
    160         run_screaming("Couldn't run LZ4_compress_fast_extState()... error code received is in exit code.", rv);
    161       if (memcmp(known_good_dst, dst, max_dst_size) != 0)
    162         run_screaming("According to memcmp(), the compressed dst we got doesn't match the known_good_dst... ruh roh.", 1);
    163       clock_gettime(CLOCK_MONOTONIC, &start);
    164       for (int i=1; i<=iterations; i++)
    165         LZ4_compress_fast_extState(&state, src, dst, src_size, max_dst_size, acceleration);
    166       break;
    167 
    168 //    Disabled until LZ4_compress_generic() is exposed in the header.
    169 //    case ID__LZ4_COMPRESS_GENERIC:
    170 //      printf("Starting benchmark for function: LZ4_compress_generic()\n");
    171 //      LZ4_resetStream((LZ4_stream_t*)&state);
    172 //      for(int junk=0; junk<warm_up; junk++) {
    173 //        LZ4_resetStream((LZ4_stream_t*)&state);
    174 //        //rv = LZ4_compress_generic_wrapper(&state, src, dst, src_size, max_dst_size, notLimited, byU16, noDict, noDictIssue, acceleration);
    175 //        LZ4_compress_generic_wrapper(&state, src, dst, src_size, max_dst_size, acceleration);
    176 //      }
    177 //      if (rv < 1)
    178 //        run_screaming("Couldn't run LZ4_compress_generic()... error code received is in exit code.", rv);
    179 //      if (memcmp(known_good_dst, dst, max_dst_size) != 0)
    180 //        run_screaming("According to memcmp(), the compressed dst we got doesn't match the known_good_dst... ruh roh.", 1);
    181 //      for (int i=1; i<=iterations; i++) {
    182 //        LZ4_resetStream((LZ4_stream_t*)&state);
    183 //        //LZ4_compress_generic_wrapper(&state, src, dst, src_size, max_dst_size, notLimited, byU16, noDict, noDictIssue, acceleration);
    184 //        LZ4_compress_generic_wrapper(&state, src, dst, src_size, max_dst_size, acceleration);
    185 //      }
    186 //      break;
    187 
    188     case ID__LZ4_DECOMPRESS_SAFE:
    189       printf("Starting benchmark for function: LZ4_decompress_safe()\n");
    190       for(int junk=0; junk<warm_up; junk++)
    191         rv = LZ4_decompress_safe(src, dst, comp_size, src_size);
    192       if (rv < 1)
    193         run_screaming("Couldn't run LZ4_decompress_safe()... error code received is in exit code.", rv);
    194       if (memcmp(known_good_dst, dst, src_size) != 0)
    195         run_screaming("According to memcmp(), the compressed dst we got doesn't match the known_good_dst... ruh roh.", 1);
    196       clock_gettime(CLOCK_MONOTONIC, &start);
    197       for (int i=1; i<=iterations; i++)
    198         LZ4_decompress_safe(src, dst, comp_size, src_size);
    199       break;
    200 
    201     case ID__LZ4_DECOMPRESS_FAST:
    202       printf("Starting benchmark for function: LZ4_decompress_fast()\n");
    203       for(int junk=0; junk<warm_up; junk++)
    204         rv = LZ4_decompress_fast(src, dst, src_size);
    205       if (rv < 1)
    206         run_screaming("Couldn't run LZ4_decompress_fast()... error code received is in exit code.", rv);
    207       if (memcmp(known_good_dst, dst, src_size) != 0)
    208         run_screaming("According to memcmp(), the compressed dst we got doesn't match the known_good_dst... ruh roh.", 1);
    209       clock_gettime(CLOCK_MONOTONIC, &start);
    210       for (int i=1; i<=iterations; i++)
    211         LZ4_decompress_fast(src, dst, src_size);
    212       break;
    213 
    214     default:
    215       run_screaming("The test specified isn't valid.  Please check your code.", 1);
    216       break;
    217   }
    218 
    219   // Stop timer and return time taken.
    220   clock_gettime(CLOCK_MONOTONIC, &end);
    221   time_taken = BILLION *(end.tv_sec - start.tv_sec) + end.tv_nsec - start.tv_nsec;
    222 
    223   return time_taken;
    224 }
    225 
    226 
    227 
    228 /*
    229  * main()
    230  * We will demonstrate the use of each function for simplicity sake.  Then we will run 2 suites of benchmarking:
    231  * Test suite A)  Uses generic Lorem Ipsum text which should be generally compressible insomuch as basic human text is
    232  *                compressible for such a small src_size
    233  * Test Suite B)  For the sake of testing, see what results we get if the data is drastically easier to compress.  IF there are
    234  *                indeed losses and IF more compressible data is faster to process, this will exacerbate the findings.
    235  */
    236 int main(int argc, char **argv) {
    237   // Get and verify options.  There's really only 1:  How many iterations to run.
    238   int iterations = 1000000;
    239   if (argc > 1)
    240     iterations = atoi(argv[1]);
    241   if (iterations < 1)
    242     usage("Argument 1 (iterations) must be > 0.");
    243 
    244   // First we will create 2 sources (char *) of 2000 bytes each.  One normal text, the other highly-compressible text.
    245   const char *src    = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed luctus purus et risus vulputate, et mollis orci ullamcorper. Nulla facilisi. Fusce in ligula sed purus varius aliquet interdum vitae justo. Proin quis diam velit. Nulla varius iaculis auctor. Cras volutpat, justo eu dictum pulvinar, elit sem porttitor metus, et imperdiet metus sapien et ante. Nullam nisi nulla, ornare eu tristique eu, dignissim vitae diam. Nulla sagittis porta libero, a accumsan felis sagittis scelerisque.  Integer laoreet eleifend congue. Etiam rhoncus leo vel dolor fermentum, quis luctus nisl iaculis. Praesent a erat sapien. Aliquam semper mi in lorem ultrices ultricies. Lorem ipsum dolor sit amet, consectetur adipiscing elit. In feugiat risus sed enim ultrices, at sodales nulla tristique. Maecenas eget pellentesque justo, sed pellentesque lectus. Fusce sagittis sit amet elit vel varius. Donec sed ligula nec ligula vulputate rutrum sed ut lectus. Etiam congue pharetra leo vitae cursus. Morbi enim ante, porttitor ut varius vel, tincidunt quis justo. Nunc iaculis, risus id ultrices semper, metus est efficitur ligula, vel posuere risus nunc eget purus. Ut lorem turpis, condimentum at sem sed, porta aliquam turpis. In ut sapien a nulla dictum tincidunt quis sit amet lorem. Fusce at est egestas, luctus neque eu, consectetur tortor. Phasellus eleifend ultricies nulla ac lobortis.  Morbi maximus quam cursus vehicula iaculis. Maecenas cursus vel justo ut rutrum. Curabitur magna orci, dignissim eget dapibus vitae, finibus id lacus. Praesent rhoncus mattis augue vitae bibendum. Praesent porta mauris non ultrices fermentum. Quisque vulputate ipsum in sodales pulvinar. Aliquam nec mollis felis. Donec vitae augue pulvinar, congue nisl sed, pretium purus. Fusce lobortis mi ac neque scelerisque semper. Pellentesque vel est vitae magna aliquet aliquet. Nam non dolor. Nulla facilisi. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Morbi ac lacinia felis metus.";
    246   const char *hc_src = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
    247   // Set and derive sizes.  Since we're using strings, use strlen() + 1 for \0.
    248   const size_t src_size = strlen(src) + 1;
    249   const size_t max_dst_size = LZ4_compressBound(src_size);
    250   int bytes_returned = 0;
    251   // Now build allocations for the data we'll be playing with.
    252   char *dst               = calloc(1, max_dst_size);
    253   char *known_good_dst    = calloc(1, max_dst_size);
    254   char *known_good_hc_dst = calloc(1, max_dst_size);
    255   if (dst == NULL || known_good_dst == NULL || known_good_hc_dst == NULL)
    256     run_screaming("Couldn't allocate memory for the destination buffers.  Sad :(", 1);
    257 
    258   // Create known-good buffers to verify our tests with other functions will produce the same results.
    259   bytes_returned = LZ4_compress_default(src, known_good_dst, src_size, max_dst_size);
    260   if (bytes_returned < 1)
    261     run_screaming("Couldn't create a known-good destination buffer for comparison... this is bad.", 1);
    262   const size_t src_comp_size = bytes_returned;
    263   bytes_returned = LZ4_compress_default(hc_src, known_good_hc_dst, src_size, max_dst_size);
    264   if (bytes_returned < 1)
    265     run_screaming("Couldn't create a known-good (highly compressible) destination buffer for comparison... this is bad.", 1);
    266   const size_t hc_src_comp_size = bytes_returned;
    267 
    268 
    269   /* LZ4_compress_default() */
    270   // This is the default function so we don't need to demonstrate how to use it.  See basics.c if you need more basal information.
    271 
    272   /* LZ4_compress_fast() */
    273   // Using this function is identical to LZ4_compress_default except we need to specify an "acceleration" value.  Defaults to 1.
    274   memset(dst, 0, max_dst_size);
    275   bytes_returned = LZ4_compress_fast(src, dst, src_size, max_dst_size, 1);
    276   if (bytes_returned < 1)
    277     run_screaming("Failed to compress src using LZ4_compress_fast.  echo $? for return code.", bytes_returned);
    278   if (memcmp(dst, known_good_dst, bytes_returned) != 0)
    279     run_screaming("According to memcmp(), the value we got in dst from LZ4_compress_fast doesn't match the known-good value.  This is bad.", 1);
    280 
    281   /* LZ4_compress_fast_extState() */
    282   // Using this function directly requires that we build an LZ4_stream_t struct ourselves.  We do NOT have to reset it ourselves.
    283   memset(dst, 0, max_dst_size);
    284   LZ4_stream_t state;
    285   bytes_returned = LZ4_compress_fast_extState(&state, src, dst, src_size, max_dst_size, 1);
    286   if (bytes_returned < 1)
    287     run_screaming("Failed to compress src using LZ4_compress_fast_extState.  echo $? for return code.", bytes_returned);
    288   if (memcmp(dst, known_good_dst, bytes_returned) != 0)
    289     run_screaming("According to memcmp(), the value we got in dst from LZ4_compress_fast_extState doesn't match the known-good value.  This is bad.", 1);
    290 
    291   /* LZ4_compress_generic */
    292   // When you can exactly control the inputs and options of your LZ4 needs, you can use LZ4_compress_generic and fixed (const)
    293   // values for the enum types such as dictionary and limitations.  Any other direct-use is probably a bad idea.
    294   //
    295   // That said, the LZ4_compress_generic() function is 'static inline' and does not have a prototype in lz4.h to expose a symbol
    296   // for it.  In other words: we can't access it directly.  I don't want to submit a PR that modifies lz4.c/h.  Yann and others can
    297   // do that if they feel it's worth expanding this example.
    298   //
    299   // I will, however, leave a skeleton of what would be required to use it directly:
    300   /*
    301     memset(dst, 0, max_dst_size);
    302     // LZ4_stream_t state:  is already declared above.  We can reuse it BUT we have to reset the stream ourselves between each call.
    303     LZ4_resetStream((LZ4_stream_t *)&state);
    304     // Since src size is small we know the following enums will be used:  notLimited (0), byU16 (2), noDict (0), noDictIssue (0).
    305     bytes_returned = LZ4_compress_generic(&state, src, dst, src_size, max_dst_size, notLimited, byU16, noDict, noDictIssue, 1);
    306     if (bytes_returned < 1)
    307       run_screaming("Failed to compress src using LZ4_compress_generic.  echo $? for return code.", bytes_returned);
    308     if (memcmp(dst, known_good_dst, bytes_returned) != 0)
    309       run_screaming("According to memcmp(), the value we got in dst from LZ4_compress_generic doesn't match the known-good value.  This is bad.", 1);
    310   */
    311 
    312 
    313   /* Benchmarking */
    314   /* Now we'll run a few rudimentary benchmarks with each function to demonstrate differences in speed based on the function used.
    315    * Remember, we cannot call LZ4_compress_generic() directly (yet) so it's disabled.
    316    */
    317   // Suite A - Normal Compressibility
    318   char *dst_d = calloc(1, src_size);
    319   memset(dst, 0, max_dst_size);
    320   printf("\nStarting suite A:  Normal compressible text.\n");
    321   uint64_t time_taken__default       = bench(known_good_dst, ID__LZ4_COMPRESS_DEFAULT,       iterations, src,            dst,   src_size, max_dst_size, src_comp_size);
    322   uint64_t time_taken__fast          = bench(known_good_dst, ID__LZ4_COMPRESS_FAST,          iterations, src,            dst,   src_size, max_dst_size, src_comp_size);
    323   uint64_t time_taken__fast_extstate = bench(known_good_dst, ID__LZ4_COMPRESS_FAST_EXTSTATE, iterations, src,            dst,   src_size, max_dst_size, src_comp_size);
    324   //uint64_t time_taken__generic       = bench(known_good_dst, ID__LZ4_COMPRESS_GENERIC,       iterations, src,            dst,   src_size, max_dst_size, src_comp_size);
    325   uint64_t time_taken__decomp_safe   = bench(src,            ID__LZ4_DECOMPRESS_SAFE,        iterations, known_good_dst, dst_d, src_size, max_dst_size, src_comp_size);
    326   uint64_t time_taken__decomp_fast   = bench(src,            ID__LZ4_DECOMPRESS_FAST,        iterations, known_good_dst, dst_d, src_size, max_dst_size, src_comp_size);
    327   // Suite B - Highly Compressible
    328   memset(dst, 0, max_dst_size);
    329   printf("\nStarting suite B:  Highly compressible text.\n");
    330   uint64_t time_taken_hc__default       = bench(known_good_hc_dst, ID__LZ4_COMPRESS_DEFAULT,       iterations, hc_src,            dst,   src_size, max_dst_size, hc_src_comp_size);
    331   uint64_t time_taken_hc__fast          = bench(known_good_hc_dst, ID__LZ4_COMPRESS_FAST,          iterations, hc_src,            dst,   src_size, max_dst_size, hc_src_comp_size);
    332   uint64_t time_taken_hc__fast_extstate = bench(known_good_hc_dst, ID__LZ4_COMPRESS_FAST_EXTSTATE, iterations, hc_src,            dst,   src_size, max_dst_size, hc_src_comp_size);
    333   //uint64_t time_taken_hc__generic       = bench(known_good_hc_dst, ID__LZ4_COMPRESS_GENERIC,       iterations, hc_src,            dst,   src_size, max_dst_size, hc_src_comp_size);
    334   uint64_t time_taken_hc__decomp_safe   = bench(hc_src,            ID__LZ4_DECOMPRESS_SAFE,        iterations, known_good_hc_dst, dst_d, src_size, max_dst_size, hc_src_comp_size);
    335   uint64_t time_taken_hc__decomp_fast   = bench(hc_src,            ID__LZ4_DECOMPRESS_FAST,        iterations, known_good_hc_dst, dst_d, src_size, max_dst_size, hc_src_comp_size);
    336 
    337   // Report and leave.
    338   setlocale(LC_ALL, "");
    339   const char *format        = "|%-14s|%-30s|%'14.9f|%'16d|%'14d|%'13.2f%%|\n";
    340   const char *header_format = "|%-14s|%-30s|%14s|%16s|%14s|%14s|\n";
    341   const char *separator     = "+--------------+------------------------------+--------------+----------------+--------------+--------------+\n";
    342   printf("\n");
    343   printf("%s", separator);
    344   printf(header_format, "Source", "Function Benchmarked", "Total Seconds", "Iterations/sec", "ns/Iteration", "% of default");
    345   printf("%s", separator);
    346   printf(format, "Normal Text", "LZ4_compress_default()",       (double)time_taken__default       / BILLION, (int)(iterations / ((double)time_taken__default       /BILLION)), time_taken__default       / iterations, (double)time_taken__default       * 100 / time_taken__default);
    347   printf(format, "Normal Text", "LZ4_compress_fast()",          (double)time_taken__fast          / BILLION, (int)(iterations / ((double)time_taken__fast          /BILLION)), time_taken__fast          / iterations, (double)time_taken__fast          * 100 / time_taken__default);
    348   printf(format, "Normal Text", "LZ4_compress_fast_extState()", (double)time_taken__fast_extstate / BILLION, (int)(iterations / ((double)time_taken__fast_extstate /BILLION)), time_taken__fast_extstate / iterations, (double)time_taken__fast_extstate * 100 / time_taken__default);
    349   //printf(format, "Normal Text", "LZ4_compress_generic()",       (double)time_taken__generic       / BILLION, (int)(iterations / ((double)time_taken__generic       /BILLION)), time_taken__generic       / iterations, (double)time_taken__generic       * 100 / time_taken__default);
    350   printf(format, "Normal Text", "LZ4_decompress_safe()",        (double)time_taken__decomp_safe   / BILLION, (int)(iterations / ((double)time_taken__decomp_safe   /BILLION)), time_taken__decomp_safe   / iterations, (double)time_taken__decomp_safe   * 100 / time_taken__default);
    351   printf(format, "Normal Text", "LZ4_decompress_fast()",        (double)time_taken__decomp_fast   / BILLION, (int)(iterations / ((double)time_taken__decomp_fast   /BILLION)), time_taken__decomp_fast   / iterations, (double)time_taken__decomp_fast   * 100 / time_taken__default);
    352   printf(header_format, "", "", "", "", "", "");
    353   printf(format, "Compressible", "LZ4_compress_default()",       (double)time_taken_hc__default       / BILLION, (int)(iterations / ((double)time_taken_hc__default       /BILLION)), time_taken_hc__default       / iterations, (double)time_taken_hc__default       * 100 / time_taken_hc__default);
    354   printf(format, "Compressible", "LZ4_compress_fast()",          (double)time_taken_hc__fast          / BILLION, (int)(iterations / ((double)time_taken_hc__fast          /BILLION)), time_taken_hc__fast          / iterations, (double)time_taken_hc__fast          * 100 / time_taken_hc__default);
    355   printf(format, "Compressible", "LZ4_compress_fast_extState()", (double)time_taken_hc__fast_extstate / BILLION, (int)(iterations / ((double)time_taken_hc__fast_extstate /BILLION)), time_taken_hc__fast_extstate / iterations, (double)time_taken_hc__fast_extstate * 100 / time_taken_hc__default);
    356   //printf(format, "Compressible", "LZ4_compress_generic()",       (double)time_taken_hc__generic       / BILLION, (int)(iterations / ((double)time_taken_hc__generic       /BILLION)), time_taken_hc__generic       / iterations, (double)time_taken_hc__generic       * 100 / time_taken_hc__default);
    357   printf(format, "Compressible", "LZ4_decompress_safe()",        (double)time_taken_hc__decomp_safe   / BILLION, (int)(iterations / ((double)time_taken_hc__decomp_safe   /BILLION)), time_taken_hc__decomp_safe   / iterations, (double)time_taken_hc__decomp_safe   * 100 / time_taken_hc__default);
    358   printf(format, "Compressible", "LZ4_decompress_fast()",        (double)time_taken_hc__decomp_fast   / BILLION, (int)(iterations / ((double)time_taken_hc__decomp_fast   /BILLION)), time_taken_hc__decomp_fast   / iterations, (double)time_taken_hc__decomp_fast   * 100 / time_taken_hc__default);
    359   printf("%s", separator);
    360   printf("\n");
    361   printf("All done, ran %d iterations per test.\n", iterations);
    362   return 0;
    363 }
    364