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 (6986B)


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