|
| 1 | +// Kiebitz - Privacy-Friendly Appointment Scheduling |
| 2 | +// Copyright (C) 2021-2021 The Kiebitz Authors |
| 3 | +// |
| 4 | +// This program is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU Affero General Public License as |
| 6 | +// published by the Free Software Foundation, either version 3 of the |
| 7 | +// License, or (at your option) any later version. |
| 8 | +// |
| 9 | +// This program is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU Affero General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU Affero General Public License |
| 15 | +// along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +package helpers |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "github.com/kiebitz-oss/services" |
| 22 | + at "github.com/kiebitz-oss/services/testing" |
| 23 | + af "github.com/kiebitz-oss/services/testing/fixtures" |
| 24 | + "github.com/urfave/cli" |
| 25 | +) |
| 26 | + |
| 27 | +type SettingsFixture struct { |
| 28 | + settings *services.Settings |
| 29 | +} |
| 30 | + |
| 31 | +func (s SettingsFixture) Setup(fixtures map[string]interface{}) (interface{}, error) { |
| 32 | + return s.settings, nil |
| 33 | +} |
| 34 | + |
| 35 | +func (s SettingsFixture) Teardown(fixture interface{}) error { |
| 36 | + return nil |
| 37 | +} |
| 38 | + |
| 39 | +func benchmark(settings *services.Settings) func(c *cli.Context) error { |
| 40 | + return func(c *cli.Context) error { |
| 41 | + |
| 42 | + safetyOff := c.Bool("safetyOff") |
| 43 | + appointments := c.Int("appointments") |
| 44 | + providers := c.Int("providers") |
| 45 | + slots := c.Int("slots") |
| 46 | + |
| 47 | + if !settings.Test && !safetyOff { |
| 48 | + return fmt.Errorf("Non-test system detected, aborting! Override this by setting --safetyOff.") |
| 49 | + } |
| 50 | + |
| 51 | + var fixturesConfig = []at.FC{ |
| 52 | + |
| 53 | + // we create the settings |
| 54 | + at.FC{SettingsFixture{settings: settings}, "settings"}, |
| 55 | + |
| 56 | + // we create a client (without a key) |
| 57 | + at.FC{af.Client{}, "client"}, |
| 58 | + |
| 59 | + // we create a mediator |
| 60 | + at.FC{af.Mediator{}, "mediator"}, |
| 61 | + |
| 62 | + at.FC{af.ProvidersAndAppointments{ |
| 63 | + Providers: int64(providers), |
| 64 | + BaseProvider: af.Provider{ |
| 65 | + ZipCode: "10707", |
| 66 | + Name: "Dr. Maier Müller", |
| 67 | + Street: "Mühlenstr. 55", |
| 68 | + City: "Berlin", |
| 69 | + StoreData: true, |
| 70 | + Confirm: true, |
| 71 | + }, |
| 72 | + BaseAppointments: af.Appointments{ |
| 73 | + N: int64(appointments), |
| 74 | + Start: af.TS("2022-10-01T12:00:00Z"), |
| 75 | + Duration: 30, |
| 76 | + Slots: int64(slots), |
| 77 | + Properties: map[string]interface{}{ |
| 78 | + "vaccine": "moderna", |
| 79 | + }, |
| 80 | + }, |
| 81 | + }, "providersAndAppointments"}, |
| 82 | + } |
| 83 | + |
| 84 | + fixtures, err := at.SetupFixtures(fixturesConfig) |
| 85 | + |
| 86 | + if err != nil { |
| 87 | + return err |
| 88 | + } |
| 89 | + |
| 90 | + if err := at.TeardownFixtures(fixturesConfig, fixtures); err != nil { |
| 91 | + return err |
| 92 | + } |
| 93 | + |
| 94 | + return nil |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +func Testing(settings *services.Settings) ([]cli.Command, error) { |
| 99 | + |
| 100 | + return []cli.Command{ |
| 101 | + { |
| 102 | + Name: "testing", |
| 103 | + Aliases: []string{"a"}, |
| 104 | + Flags: []cli.Flag{}, |
| 105 | + Usage: "Test functionality.", |
| 106 | + Subcommands: []cli.Command{ |
| 107 | + { |
| 108 | + Name: "benchmark", |
| 109 | + Flags: []cli.Flag{ |
| 110 | + &cli.IntFlag{ |
| 111 | + Name: "providers", |
| 112 | + Value: 1000, |
| 113 | + Usage: "number of providers to create", |
| 114 | + }, |
| 115 | + &cli.IntFlag{ |
| 116 | + Name: "appointments", |
| 117 | + Value: 1000, |
| 118 | + Usage: "number of appointments to create per provider", |
| 119 | + }, |
| 120 | + &cli.IntFlag{ |
| 121 | + Name: "slots", |
| 122 | + Value: 20, |
| 123 | + Usage: "number of slots per appointments to create", |
| 124 | + }, |
| 125 | + &cli.BoolFlag{ |
| 126 | + Name: "safetyOff", |
| 127 | + Usage: "set if you want to run this test on a non-test system", |
| 128 | + }, |
| 129 | + }, |
| 130 | + Usage: "Run a comprehensive benchmark.", |
| 131 | + Action: benchmark(settings), |
| 132 | + }, |
| 133 | + }, |
| 134 | + }, |
| 135 | + }, nil |
| 136 | +} |
0 commit comments