|
| 1 | +package benchmarks |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "flag" |
| 6 | + "fmt" |
| 7 | + "net" |
| 8 | + |
| 9 | + "github.com/ProtonMail/gluon/benchmarks/gluon_bench/flags" |
| 10 | + "github.com/ProtonMail/gluon/benchmarks/gluon_bench/utils" |
| 11 | + "github.com/emersion/go-imap" |
| 12 | + "github.com/emersion/go-imap/client" |
| 13 | + "github.com/google/uuid" |
| 14 | +) |
| 15 | + |
| 16 | +var copyCountFlag = flag.Uint("copy-count", 0, "Total number of messages to copy during copy benchmarks.") |
| 17 | +var copyListFlag = flag.String("copy-list", "", "Use a list of predefined sequences to copy rather than random generated.") |
| 18 | +var copyRandomRangesFlag = flag.Bool("copy-random-ranges", false, "If not using a copy list, use a random range rather than a random sequence.") |
| 19 | +var copyAllFlag = flag.Bool("copy-all", false, "If set, perform a copy of the all messages.") |
| 20 | + |
| 21 | +type Copy struct { |
| 22 | + messageCount uint32 |
| 23 | + copyCount uint32 |
| 24 | + copyLists [][]*imap.SeqSet |
| 25 | + dstMailbox string |
| 26 | +} |
| 27 | + |
| 28 | +func NewCopy() *Copy { |
| 29 | + return &Copy{ |
| 30 | + dstMailbox: uuid.NewString(), |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +func (*Copy) Name() string { |
| 35 | + return "copy" |
| 36 | +} |
| 37 | + |
| 38 | +func (c *Copy) Setup(ctx context.Context, addr net.Addr) error { |
| 39 | + cl, err := utils.NewClient(addr.String()) |
| 40 | + if err != nil { |
| 41 | + return err |
| 42 | + } |
| 43 | + |
| 44 | + defer utils.CloseClient(cl) |
| 45 | + |
| 46 | + //Delete mailbox if it exists |
| 47 | + if err := cl.Delete(c.dstMailbox); err != nil { |
| 48 | + // ignore error |
| 49 | + } |
| 50 | + |
| 51 | + if err := cl.Create(c.dstMailbox); err != nil { |
| 52 | + return err |
| 53 | + } |
| 54 | + |
| 55 | + status, err := cl.Status(*flags.MailboxFlag, []imap.StatusItem{imap.StatusMessages}) |
| 56 | + if err != nil { |
| 57 | + return err |
| 58 | + } |
| 59 | + |
| 60 | + c.messageCount = status.Messages |
| 61 | + |
| 62 | + if c.messageCount == 0 { |
| 63 | + return fmt.Errorf("mailbox '%v' has no messages", *flags.MailboxFlag) |
| 64 | + } |
| 65 | + |
| 66 | + if *copyCountFlag == 0 { |
| 67 | + c.copyCount = c.messageCount / 2 |
| 68 | + } else { |
| 69 | + c.copyCount = uint32(*copyCountFlag) |
| 70 | + } |
| 71 | + |
| 72 | + if len(*copyListFlag) != 0 { |
| 73 | + list, err := utils.SequenceListFromFile(*copyListFlag) |
| 74 | + if err != nil { |
| 75 | + return err |
| 76 | + } |
| 77 | + |
| 78 | + c.copyLists = make([][]*imap.SeqSet, *flags.ParallelClientsFlag) |
| 79 | + for i := uint(0); i < *flags.ParallelClientsFlag; i++ { |
| 80 | + c.copyLists[i] = list |
| 81 | + } |
| 82 | + } else if *copyAllFlag { |
| 83 | + c.copyLists = make([][]*imap.SeqSet, *flags.ParallelClientsFlag) |
| 84 | + for i := uint(0); i < *flags.ParallelClientsFlag; i++ { |
| 85 | + c.copyLists[i] = []*imap.SeqSet{utils.NewSequenceSetAll()} |
| 86 | + } |
| 87 | + } else { |
| 88 | + c.copyLists = make([][]*imap.SeqSet, *flags.ParallelClientsFlag) |
| 89 | + for i := uint(0); i < *flags.ParallelClientsFlag; i++ { |
| 90 | + list := make([]*imap.SeqSet, 0, c.copyCount) |
| 91 | + for r := uint32(0); r < c.copyCount; r++ { |
| 92 | + var seqSet *imap.SeqSet |
| 93 | + if !*copyRandomRangesFlag { |
| 94 | + seqSet = utils.RandomSequenceSetNum(c.copyCount) |
| 95 | + } else { |
| 96 | + seqSet = utils.RandomSequenceSetRange(c.copyCount) |
| 97 | + } |
| 98 | + list = append(list, seqSet) |
| 99 | + } |
| 100 | + c.copyLists[i] = list |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + return nil |
| 105 | +} |
| 106 | + |
| 107 | +func (c *Copy) TearDown(ctx context.Context, addr net.Addr) error { |
| 108 | + cl, err := utils.NewClient(addr.String()) |
| 109 | + if err != nil { |
| 110 | + return err |
| 111 | + } |
| 112 | + |
| 113 | + defer utils.CloseClient(cl) |
| 114 | + |
| 115 | + if err := cl.Delete(c.dstMailbox); err != nil { |
| 116 | + return err |
| 117 | + } |
| 118 | + |
| 119 | + return nil |
| 120 | +} |
| 121 | + |
| 122 | +func (c *Copy) Run(ctx context.Context, addr net.Addr) error { |
| 123 | + utils.RunParallelClients(addr, func(cl *client.Client, index uint) { |
| 124 | + for _, v := range c.copyLists[index] { |
| 125 | + if err := cl.Copy(v, c.dstMailbox); err != nil { |
| 126 | + panic(err) |
| 127 | + } |
| 128 | + } |
| 129 | + }) |
| 130 | + |
| 131 | + return nil |
| 132 | +} |
0 commit comments