exchange

Base system with REST service to issue digital coins, run by the payment service provider
Log | Files | Refs | Submodules | README | LICENSE

test_exchangedb_by_j.c (6998B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2014-2022 Taler Systems SA
      4 
      5   TALER is free software; you can redistribute it and/or modify it under the
      6   terms of the GNU General Public License as published by the Free Software
      7   Foundation; either version 3, or (at your option) any later version.
      8 
      9   TALER is distributed in the hope that it will be useful, but WITHOUT ANY
     10   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
     11   A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
     12 
     13   You should have received a copy of the GNU General Public License along with
     14   TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
     15 */
     16 /**
     17  * @file exchangedb/test_exchangedb_by_j.c
     18  * @brief test cases for DB interaction functions
     19  * @author Joseph Xu
     20  */
     21 #include "taler/platform.h"
     22 #include "taler/taler_exchangedb_lib.h"
     23 #include "taler/taler_json_lib.h"
     24 #include "taler/taler_exchangedb_plugin.h"
     25 #include "math.h"
     26 #define ROUNDS 10
     27 
     28 /**
     29  * Global result from the testcase.
     30  */
     31 static int result;
     32 
     33 /**
     34  * Report line of error if @a cond is true, and jump to label "drop".
     35  */
     36 #define FAILIF(cond)                            \
     37         do {                                          \
     38           if (! (cond)) {break;}                    \
     39           GNUNET_break (0);                           \
     40           goto drop;                                  \
     41         } while (0)
     42 
     43 
     44 /**
     45  * Initializes @a ptr with random data.
     46  */
     47 #define RND_BLK(ptr)                                                    \
     48         GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK, ptr, sizeof (* \
     49                                                                              ptr))
     50 
     51 /**
     52  * Initializes @a ptr with zeros.
     53  */
     54 #define ZR_BLK(ptr) \
     55         memset (ptr, 0, sizeof (*ptr))
     56 
     57 
     58 /**
     59  * Currency we use.  Must match test-exchange-db-*.conf.
     60  */
     61 #define CURRENCY "EUR"
     62 
     63 /**
     64  * Database plugin under test.
     65  */
     66 static struct TALER_EXCHANGEDB_Plugin *plugin;
     67 
     68 
     69 /**
     70  * Main function that will be run by the scheduler.
     71  *
     72  * @param cls closure with config
     73  */
     74 static void
     75 run (void *cls)
     76 {
     77   static const unsigned int batches[] = {1, 2, 3, 4, 8, 16 };
     78   struct GNUNET_TIME_Relative times[sizeof (batches) / sizeof(*batches)];
     79   unsigned long long sqrs[sizeof (batches) / sizeof(*batches)];
     80   struct GNUNET_CONFIGURATION_Handle *cfg = cls;
     81   const uint32_t num_partitions = 10;
     82 
     83   if (NULL ==
     84       (plugin = TALER_EXCHANGEDB_plugin_load (cfg)))
     85   {
     86     GNUNET_break (0);
     87     result = 77;
     88     return;
     89   }
     90 
     91 
     92   if (GNUNET_OK !=
     93       plugin->create_tables (plugin->cls,
     94                              true,
     95                              num_partitions))
     96   {
     97     GNUNET_break (0);
     98     result = 77;
     99     goto cleanup;
    100   }
    101 
    102   memset (times, 0, sizeof (times));
    103   memset (sqrs, 0, sizeof (sqrs));
    104   for (unsigned int r = 0; r < ROUNDS; r++)
    105   {
    106     for (unsigned int i = 0; i< 6; i++)
    107     {
    108       const char *sndr = "payto://x-taler-bank/localhost:8080/1";
    109       struct TALER_Amount value;
    110       unsigned int batch_size = batches[i];
    111       unsigned int iterations = 16;  // 1024*10;
    112       struct TALER_ReservePublicKeyP reserve_pubs[iterations];
    113       struct GNUNET_TIME_Absolute now;
    114       struct GNUNET_TIME_Timestamp ts;
    115       struct GNUNET_TIME_Relative duration;
    116       struct TALER_EXCHANGEDB_ReserveInInfo reserves[iterations];
    117       enum GNUNET_DB_QueryStatus results[iterations];
    118       unsigned long long duration_sq;
    119 
    120       GNUNET_assert (GNUNET_OK ==
    121                      TALER_string_to_amount (CURRENCY ":1.000010",
    122                                              &value));
    123       now = GNUNET_TIME_absolute_get ();
    124       ts = GNUNET_TIME_timestamp_get ();
    125       for (unsigned int r = 0; r<iterations; r++)
    126       {
    127         RND_BLK (&reserve_pubs[r]);
    128         reserves[r].reserve_pub = &reserve_pubs[r];
    129         reserves[r].balance = &value;
    130         reserves[r].execution_time = ts;
    131         reserves[r].sender_account_details = sndr;
    132         reserves[r].exchange_account_name = "name";
    133         reserves[r].wire_reference = r;
    134       }
    135       FAILIF (iterations !=
    136               plugin->batch2_reserves_in_insert (plugin->cls,
    137                                                  reserves,
    138                                                  iterations,
    139                                                  batch_size,
    140                                                  results));
    141       duration = GNUNET_TIME_absolute_get_duration (now);
    142       times[i] = GNUNET_TIME_relative_add (times[i],
    143                                            duration);
    144       duration_sq = duration.rel_value_us * duration.rel_value_us;
    145       GNUNET_assert (duration_sq / duration.rel_value_us ==
    146                      duration.rel_value_us);
    147       GNUNET_assert (sqrs[i] + duration_sq >= sqrs[i]);
    148       sqrs[i] += duration_sq;
    149       fprintf (stdout,
    150                "for a batchsize equal to %d it took %s\n",
    151                batch_size,
    152                GNUNET_STRINGS_relative_time_to_string (duration,
    153                                                        GNUNET_NO) );
    154 
    155       system ("./test.sh");   // DELETE AFTER TIMER
    156     }
    157   }
    158   for (unsigned int i = 0; i< 6; i++)
    159   {
    160     struct GNUNET_TIME_Relative avg;
    161     double avg_dbl;
    162     double variance;
    163 
    164     avg = GNUNET_TIME_relative_divide (times[i],
    165                                        ROUNDS);
    166     avg_dbl = avg.rel_value_us;
    167     variance = sqrs[i] - (avg_dbl * avg_dbl * ROUNDS);
    168     fprintf (stdout,
    169              "Batch[%2u]: %8llu ± %6.0f\n",
    170              batches[i],
    171              (unsigned long long) avg.rel_value_us,
    172              sqrt (variance / (ROUNDS - 1)));
    173   }
    174 
    175   result = 0;
    176 drop:
    177   GNUNET_break (GNUNET_OK ==
    178                 plugin->drop_tables (plugin->cls));
    179 cleanup:
    180   TALER_EXCHANGEDB_plugin_unload (plugin);
    181   plugin = NULL;
    182 }
    183 
    184 
    185 int
    186 main (int argc,
    187       char *const argv[])
    188 {
    189   const char *plugin_name;
    190   char *config_filename;
    191   char *testname;
    192   struct GNUNET_CONFIGURATION_Handle *cfg;
    193   (void) argc;
    194   result = -1;
    195   if (NULL == (plugin_name = strrchr (argv[0], (int) '-')))
    196   {
    197     GNUNET_break (0);
    198     return -1;
    199   }
    200 
    201   GNUNET_log_setup (argv[0],
    202                     "WARNING",
    203                     NULL);
    204   plugin_name++;
    205   (void) GNUNET_asprintf (&testname,
    206                           "test-exchange-db-%s",
    207                           plugin_name);
    208   (void) GNUNET_asprintf (&config_filename,
    209                           "%s.conf",
    210                           testname);
    211   fprintf (stdout,
    212            "Using config: %s\n",
    213            config_filename);
    214   cfg = GNUNET_CONFIGURATION_create ();
    215   if (GNUNET_OK !=
    216       GNUNET_CONFIGURATION_parse (cfg,
    217                                   config_filename))
    218   {
    219     GNUNET_break (0);
    220     GNUNET_free (config_filename);
    221     GNUNET_free (testname);
    222     return 2;
    223   }
    224   GNUNET_SCHEDULER_run (&run,
    225                         cfg);
    226   GNUNET_CONFIGURATION_destroy (cfg);
    227   GNUNET_free (config_filename);
    228   GNUNET_free (testname);
    229   return result;
    230 }
    231 
    232 
    233 /* end of test_exchangedb_by_j.c */