summaryrefslogtreecommitdiff
path: root/src/merchant-tools/taler-merchant-benchmark.c
blob: 238b9f033a1926335f0cc079284b4b335bd29818 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
/*
  This file is part of TALER
  (C) 2014--2023 Taler Systems SA

  TALER is free software; you can redistribute it and/or modify it
  under the terms of the GNU Affero General Public License as
  published by the Free Software Foundation; either version 3, or
  (at your option) any later version.

  TALER is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with TALER; see the file COPYING.  If not,
  see <http://www.gnu.org/licenses/>
*/
/**
 * @file taler-merchant-benchmark.c
 * @brief benchmark the backend to evaluate performance
 * @author Marcello Stanisci
 * @author Christian Grothoff
 */
#include "platform.h"
#include <taler/taler_util.h>
#include <taler/taler_testing_lib.h>
#include "taler_merchant_testing_lib.h"


/**
 * Maximum length of an amount (value plus currency string) needed by the test.
 * We have a 32-bit and a 64-bit value (~48 characters), plus the currency, plus
 * some punctuation.
 */
#define MAX_AMOUNT_LEN (TALER_CURRENCY_LEN + 64)

/**
 * Maximum length of an order JSON.  Generously allocated.
 */
#define MAX_ORDER_LEN (MAX_AMOUNT_LEN * 4 + 2048)


/**
 * ID to use for the 'alternative' instance.
 */
static const char *alt_instance_id = "alt";

/**
 * What API key should we send in the HTTP 'Authorization' header?
 */
static char *apikey;

/**
 * Witnesses if the ordinary cases payment suite should be run.
 */
static bool ordinary;

/**
 * Witnesses if the corner cases payment suite should be run.
 */
static bool corner;

/**
 * Base URL of the alternative non default instance.
 */
static char *alt_instance_url;

/**
 * How many unaggregated payments we want to generate.
 */
static unsigned int unaggregated_number = 1;

/**
 * How many payments that use two coins we want to generate.
 */
static unsigned int twocoins_number = 1;

/**
 * How many payments we want to generate.
 */
static unsigned int payments_number = 1;

/**
 * Config filename to give to commands (like wirewatch).
 */
static char *cfg_filename;

/**
 * Merchant base URL.
 */
static char *merchant_url;

/**
 * Currency used.
 */
static char *currency;

/**
 * Set to 1 if `-f` command line option given.
 */
static int use_fakebank;

/**
 * Configuration section with details about the exchange
 * bank account to use.
 */
static char *exchange_bank_section;

/**
 * Credentials to use for the benchmark.
 */
static struct TALER_TESTING_Credentials cred;


/**
 * Actual commands collection.
 */
static void
run (void *cls,
     struct TALER_TESTING_Interpreter *is)
{
  char CURRENCY_10_02[MAX_AMOUNT_LEN];
  char CURRENCY_10[MAX_AMOUNT_LEN];
  char CURRENCY_9_98[MAX_AMOUNT_LEN];
  char CURRENCY_5_01[MAX_AMOUNT_LEN];
  char CURRENCY_5[MAX_AMOUNT_LEN];
  char CURRENCY_4_99[MAX_AMOUNT_LEN];
  char CURRENCY_4_98[MAX_AMOUNT_LEN];
  char CURRENCY_0_02[MAX_AMOUNT_LEN];
  char CURRENCY_0_01[MAX_AMOUNT_LEN];

  GNUNET_snprintf (CURRENCY_10_02,
                   sizeof (CURRENCY_10_02),
                   "%s:10.02",
                   currency);
  GNUNET_snprintf (CURRENCY_10,
                   sizeof (CURRENCY_10),
                   "%s:10",
                   currency);
  GNUNET_snprintf (CURRENCY_9_98,
                   sizeof (CURRENCY_9_98),
                   "%s:9.98",
                   currency);
  GNUNET_snprintf (CURRENCY_5_01,
                   sizeof (CURRENCY_5_01),
                   "%s:5.01",
                   currency);
  GNUNET_snprintf (CURRENCY_5,
                   sizeof (CURRENCY_5),
                   "%s:5",
                   currency);
  GNUNET_snprintf (CURRENCY_4_99,
                   sizeof (CURRENCY_4_99),
                   "%s:4.99",
                   currency);
  GNUNET_snprintf (CURRENCY_4_98,
                   sizeof (CURRENCY_4_98),
                   "%s:4.98",
                   currency);
  GNUNET_snprintf (CURRENCY_0_02,
                   sizeof (CURRENCY_0_02),
                   "%s:0.02",
                   currency);
  GNUNET_snprintf (CURRENCY_0_01,
                   sizeof (CURRENCY_0_01),
                   "%s:0.01",
                   currency);
  if (ordinary)
  {
    struct TALER_TESTING_Command ordinary_commands[] = {
      TALER_TESTING_cmd_get_exchange (
        "get-exchange",
        cred.cfg,
        NULL,
        true,
        true),
      TALER_TESTING_cmd_set_authorization (
        "set-auth-valid",
        apikey),
      TALER_TESTING_cmd_merchant_post_instances (
        "instance-create-default",
        merchant_url,
        "default",
        MHD_HTTP_NO_CONTENT),
      TALER_TESTING_cmd_merchant_post_account (
        "instance-create-default-account",
        merchant_url,
        cred.user42_payto,
        NULL, NULL,
        MHD_HTTP_OK),
      TALER_TESTING_cmd_admin_add_incoming (
        "create-reserve-1",
        CURRENCY_10_02,
        &cred.ba,
        cred.user43_payto),
      TALER_TESTING_cmd_exec_wirewatch2 (
        "wirewatch-1",
        cfg_filename,
        exchange_bank_section),
      TALER_TESTING_cmd_withdraw_amount (
        "withdraw-coin-1",
        "create-reserve-1",
        CURRENCY_5,
        0,
        MHD_HTTP_OK),
      TALER_TESTING_cmd_withdraw_amount (
        "withdraw-coin-2",
        "create-reserve-1",
        CURRENCY_5,
        0,
        MHD_HTTP_OK),
      TALER_TESTING_cmd_merchant_post_orders (
        "create-proposal-1",
        cred.cfg,
        merchant_url,
        MHD_HTTP_OK,
        NULL,  /* random order ID please */
        GNUNET_TIME_UNIT_ZERO_TS,
        GNUNET_TIME_UNIT_FOREVER_TS,
        CURRENCY_5),
      TALER_TESTING_cmd_merchant_pay_order (
        "deposit-simple",
        merchant_url,
        MHD_HTTP_OK,
        "create-proposal-1",
        "withdraw-coin-1",
        CURRENCY_5,
        CURRENCY_4_99,
        NULL),
      TALER_TESTING_cmd_rewind_ip (
        "rewind-payments",
        "create-reserve-1",
        payments_number),
      TALER_TESTING_cmd_exec_aggregator (
        "aggregate-1x",
        cfg_filename),
      TALER_TESTING_cmd_exec_transfer (
        "transfer-1",
        cfg_filename),

      TALER_TESTING_cmd_end ()
    };

    TALER_TESTING_run (is,
                       ordinary_commands);
    return;
  }

  if (corner) /* should never be 'false' here */
  {
    struct TALER_TESTING_Command corner_commands[] = {
      TALER_TESTING_cmd_get_exchange (
        "get-exchange",
        cred.cfg,
        NULL,
        true,
        true),
      TALER_TESTING_cmd_set_authorization (
        "set-auth-valid",
        apikey),
      TALER_TESTING_cmd_merchant_post_instances (
        "instance-create-default",
        merchant_url,
        "default",
        MHD_HTTP_NO_CONTENT),
      TALER_TESTING_cmd_merchant_post_account (
        "instance-create-default-account",
        merchant_url,
        cred.user42_payto,
        NULL, NULL,
        MHD_HTTP_OK),
      TALER_TESTING_cmd_merchant_post_instances (
        "instance-create-alt",
        merchant_url,
        alt_instance_id,
        MHD_HTTP_NO_CONTENT),
      TALER_TESTING_cmd_merchant_post_account (
        "instance-create-alt-account",
        alt_instance_url,
        cred.user42_payto,
        NULL, NULL,
        MHD_HTTP_OK),
      TALER_TESTING_cmd_admin_add_incoming (
        "create-reserve-1",
        CURRENCY_5_01,
        &cred.ba,
        cred.user43_payto),
      TALER_TESTING_cmd_exec_wirewatch2 (
        "wirewatch-1",
        cfg_filename,
        exchange_bank_section),
      TALER_TESTING_cmd_withdraw_amount (
        "withdraw-coin-1",
        "create-reserve-1",
        CURRENCY_5,
        0,
        MHD_HTTP_OK),
      TALER_TESTING_cmd_merchant_post_orders (
        "create-unaggregated-proposal",
        cred.cfg,
        alt_instance_url,
        MHD_HTTP_OK,
        NULL,  /* use random order ID */
        GNUNET_TIME_UNIT_ZERO_TS,
        GNUNET_TIME_UNIT_FOREVER_TS,
        CURRENCY_5),
      TALER_TESTING_cmd_merchant_pay_order (
        "deposit-unaggregated",
        alt_instance_url,
        MHD_HTTP_OK,
        "create-unaggregated-proposal",
        "withdraw-coin-1",
        CURRENCY_5,
        CURRENCY_4_99,
        NULL),
      TALER_TESTING_cmd_rewind_ip (
        "rewind-unaggregated",
        "create-reserve-1",
        unaggregated_number),
      TALER_TESTING_cmd_admin_add_incoming (
        "create-reserve-2",
        CURRENCY_10_02,
        &cred.ba,
        cred.user43_payto),
      TALER_TESTING_cmd_exec_wirewatch2 (
        "wirewatch-2",
        cfg_filename,
        exchange_bank_section),
      TALER_TESTING_cmd_withdraw_amount (
        "withdraw-coin-2",
        "create-reserve-2",
        CURRENCY_5,
        0,
        MHD_HTTP_OK),
      TALER_TESTING_cmd_withdraw_amount (
        "withdraw-coin-3",
        "create-reserve-2",
        CURRENCY_5,
        0,
        MHD_HTTP_OK),
      TALER_TESTING_cmd_merchant_post_orders (
        "create-twocoins-proposal",
        cred.cfg,
        merchant_url,
        MHD_HTTP_OK,
        NULL, /* use random order ID */
        GNUNET_TIME_UNIT_ZERO_TS,
        GNUNET_TIME_UNIT_FOREVER_TS,
        CURRENCY_10),
      TALER_TESTING_cmd_merchant_pay_order (
        "deposit-twocoins",
        merchant_url,
        MHD_HTTP_OK,
        "create-twocoins-proposal",
        "withdraw-coin-2;withdraw-coin-3",
        CURRENCY_10,
        CURRENCY_9_98,
        NULL),
      TALER_TESTING_cmd_exec_aggregator (
        "aggregate-twocoins",
        cfg_filename),
      TALER_TESTING_cmd_exec_transfer (
        "transfer-twocoins",
        cfg_filename),
      TALER_TESTING_cmd_rewind_ip (
        "rewind-twocoins",
        "create-reserve-2",
        twocoins_number),
      TALER_TESTING_cmd_end ()
    };

    TALER_TESTING_run (is,
                       corner_commands);
    return;
  }
}


/**
 * The main function of the serve tool
 *
 * @param argc number of arguments from the command line
 * @param argv command line arguments
 * @return 0 ok, or `enum PaymentGeneratorError` on error
 */
int
main (int argc,
      char *const *argv)
{
  char *loglev = NULL;
  char *logfile = NULL;
  char *exchange_account = NULL;
  struct GNUNET_GETOPT_CommandLineOption *options;
  struct GNUNET_GETOPT_CommandLineOption root_options[] = {
    GNUNET_GETOPT_option_cfgfile (&cfg_filename),
    GNUNET_GETOPT_option_string (
      'u',
      "exchange-account-section",
      "SECTION",
      "use exchange bank account configuration from the given SECTION",
      &exchange_bank_section),
    GNUNET_GETOPT_option_flag (
      'f',
      "fakebank",
      "use fakebank for the banking system",
      &use_fakebank),
    GNUNET_GETOPT_option_version (PACKAGE_VERSION " " VCS_VERSION),
    GNUNET_GETOPT_option_help (
      "Runs benchmark logic against merchant backend. "
      "Must be used with either 'ordinary' or 'corner' sub-commands."),
    GNUNET_GETOPT_option_string (
      'l',
      "logfile",
      "LF",
      "will log to file LF",
      &logfile),
    GNUNET_GETOPT_option_loglevel (&loglev),
    GNUNET_GETOPT_OPTION_END
  };
  struct GNUNET_GETOPT_CommandLineOption corner_options[] = {
    GNUNET_GETOPT_option_string (
      'a',
      "apikey",
      "APIKEY",
      "HTTP 'Authorization' header to send to the merchant",
      &apikey),
    GNUNET_GETOPT_option_cfgfile (&cfg_filename),
    GNUNET_GETOPT_option_flag (
      'f',
      "fakebank",
      "use fakebank for the banking system",
      &use_fakebank),
    GNUNET_GETOPT_option_help ("Populate databases with corner case payments"),
    GNUNET_GETOPT_option_string (
      'l',
      "logfile",
      "LF",
      "will log to file LF",
      &logfile),
    GNUNET_GETOPT_option_loglevel (&loglev),
    GNUNET_GETOPT_option_uint (
      't',
      "two-coins",
      "TC",
      "will perform TC 2-coins payments, defaults to 1",
      &twocoins_number),
    GNUNET_GETOPT_option_uint (
      'U',
      "unaggregated-number",
      "UN",
      "will generate UN unaggregated payments, defaults to 1",
      &unaggregated_number),
    GNUNET_GETOPT_option_string (
      'u',
      "exchange-account-section",
      "SECTION",
      "use exchange bank account configuration from the given SECTION",
      &exchange_bank_section),
    GNUNET_GETOPT_OPTION_END
  };
  struct GNUNET_GETOPT_CommandLineOption ordinary_options[] = {
    GNUNET_GETOPT_option_string (
      'a',
      "apikey",
      "APIKEY",
      "HTTP 'Authorization' header to send to the merchant",
      &apikey),
    GNUNET_GETOPT_option_cfgfile (&cfg_filename),
    GNUNET_GETOPT_option_mandatory (
      GNUNET_GETOPT_option_string (
        'e',
        "exchange-account",
        "SECTION",
        "configuration section specifying the exchange account to use, mandatory",
        &exchange_account)),
    GNUNET_GETOPT_option_flag (
      'f',
      "fakebank",
      "use fakebank for the banking system",
      &use_fakebank),
    GNUNET_GETOPT_option_help (
      "Generate Taler ordinary payments"
      " to populate the databases"),
    GNUNET_GETOPT_option_string (
      'l',
      "logfile",
      "LF",
      "will log to file LF",
      &logfile),
    GNUNET_GETOPT_option_loglevel (&loglev),
    GNUNET_GETOPT_option_uint (
      'p',
      "payments-number",
      "PN",
      "will generate PN payments, defaults to 1",
      &payments_number),
    GNUNET_GETOPT_option_version (PACKAGE_VERSION "-" VCS_VERSION),
    GNUNET_GETOPT_OPTION_END
  };
  const char *default_config_file;

  default_config_file = GNUNET_OS_project_data_get ()->user_config_file;
  options = root_options;
  if (NULL != argv[1])
  {
    if (0 == strcmp ("ordinary", argv[1]))
    {
      ordinary = true;
      options = ordinary_options;
    }
    if (0 == strcmp ("corner", argv[1]))
    {
      corner = true;
      options = corner_options;
    }
  }

  {
    enum GNUNET_GenericReturnValue result;

    result = GNUNET_GETOPT_run ("taler-merchant-benchmark",
                                options,
                                argc,
                                argv);
    switch (result)
    {
    case GNUNET_SYSERR:
      return EXIT_INVALIDARGUMENT;
    case GNUNET_NO:
      return EXIT_SUCCESS;
    case GNUNET_OK:
      break;
    }
  }
  if (NULL == exchange_bank_section)
    exchange_bank_section = "exchange-account-1";
  if (NULL == loglev)
    loglev = "INFO";
  GNUNET_log_setup ("taler-merchant-benchmark",
                    loglev,
                    logfile);
  if ( (! ordinary) &&
       (! corner) )
  {
    TALER_LOG_ERROR ("Please use 'ordinary' or 'corner' subcommands.\n");
    return EXIT_INVALIDARGUMENT;
  }
  if (NULL == cfg_filename)
    cfg_filename = (char *) default_config_file;
  /* load currency from configuration */
  {
    struct GNUNET_CONFIGURATION_Handle *cfg;

    cfg = GNUNET_CONFIGURATION_create ();
    if (GNUNET_OK !=
        GNUNET_CONFIGURATION_load (cfg,
                                   cfg_filename))
    {
      TALER_LOG_ERROR ("Could not parse configuration\n");
      return EXIT_NOTCONFIGURED;
    }
    if (GNUNET_OK !=
        TALER_config_get_currency (cfg,
                                   &currency))
    {
      TALER_LOG_ERROR ("Failed to read currency from configuration\n");
      GNUNET_CONFIGURATION_destroy (cfg);
      return EXIT_NOTCONFIGURED;
    }
    if (GNUNET_OK !=
        GNUNET_CONFIGURATION_get_value_string (cfg,
                                               "merchant-benchmark",
                                               "MERCHANT_URL",
                                               &merchant_url))
    {
      GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
                                 "merchant-benchmark",
                                 "MERCHANT_URL");
      GNUNET_CONFIGURATION_destroy (cfg);
      return EXIT_NOTCONFIGURED;
    }
    if ( (0 == strlen (merchant_url)) ||
         (merchant_url[strlen (merchant_url) - 1] != '/') )
    {
      GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
                                 "merchant-benchmark",
                                 "MERCHANT_URL",
                                 "Not a valid URL");
      GNUNET_CONFIGURATION_destroy (cfg);
      return EXIT_NOTCONFIGURED;
    }

    if (GNUNET_OK !=
        TALER_TESTING_get_credentials (
          cfg_filename,
          exchange_bank_section,
          use_fakebank
          ? TALER_TESTING_BS_FAKEBANK
          : TALER_TESTING_BS_IBAN,
          &cred))
    {
      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                  "Required bank credentials not given in configuration\n");
      GNUNET_free (cfg_filename);
      return EXIT_NOTCONFIGURED;
    }

    GNUNET_CONFIGURATION_destroy (cfg);
  }
  GNUNET_asprintf (&alt_instance_url,
                   "%sinstances/%s/",
                   merchant_url,
                   alt_instance_id);
  {
    enum GNUNET_GenericReturnValue result;

    result = TALER_TESTING_loop (&run,
                                 NULL);
    return (GNUNET_OK == result)
      ? 0
      : EXIT_FAILURE;
  }
}