Bimba.git

ref: c0cdb1186a8135521b2f75fdfdd6fd8d712a25ff

converter/server/vendor/rybakit/msgpack/tests/Perf/Writer/TableWriter.php


  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
<?php

/*
 * This file is part of the rybakit/msgpack.php package.
 *
 * (c) Eugene Leonovich <gen.work@gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace MessagePack\Tests\Perf\Writer;

use MessagePack\Tests\Perf\Test;
use MessagePack\Tests\Perf\TestSkippedException;

class TableWriter implements Writer
{
    private const COLUMN_WIDTH_MIN = 9;
    private const COLUMN_WIDTH_FIRST = 20;

    private const STATUS_SKIPPED = 'S';
    private const STATUS_FAILED = 'F';
    private const STATUS_IGNORED = 'I';

    private $ignoreIncomplete;
    private $width;
    private $widths = [];

    private $total = [];
    private $skipped = [];
    private $failed = [];
    private $ignored = [];

    public function __construct(bool $ignoreIncomplete = null)
    {
        $this->ignoreIncomplete = $ignoreIncomplete ?? true;
    }

    public function open(array $benchmarkInfo, array $targets) : void
    {
        $this->widths = [self::COLUMN_WIDTH_FIRST];

        $cells = ['Test/Target'];
        foreach ($targets as $target) {
            $targetName = $target->getName();
            $this->widths[] = max(strlen($targetName) + 2, self::COLUMN_WIDTH_MIN);
            $cells[] = $targetName;

            $this->total[$targetName] = 0;
            $this->skipped[$targetName] = 0;
            $this->failed[$targetName] = 0;
            $this->ignored[$targetName] = 0;
        }

        $this->width = array_sum($this->widths);

        foreach ($benchmarkInfo as $title => $value) {
            echo "$title: $value\n";
        }
        echo "\n";

        echo str_repeat('=', $this->width)."\n";
        $this->writeRow($cells);
        echo str_repeat('-', $this->width)."\n";
    }

    public function write(Test $test, array $stats) : void
    {
        $cells = [$test];
        $isIncomplete = false;

        foreach ($stats as $targetName => $result) {
            if (!$result instanceof \Exception) {
                $this->total[$targetName] += $result;
                $cells[$targetName] = sprintf('%.4f', $result);
                continue;
            }

            $isIncomplete = true;

            if ($result instanceof TestSkippedException) {
                ++$this->skipped[$targetName];
                $cells[$targetName] = self::STATUS_SKIPPED;
            } else {
                ++$this->failed[$targetName];
                $cells[$targetName] = self::STATUS_FAILED;
            }
        }

        if ($this->ignoreIncomplete && $isIncomplete) {
            foreach ($stats as $targetName => $result) {
                $value = $cells[$targetName];

                if (self::STATUS_SKIPPED === $value || self::STATUS_FAILED === $value) {
                    continue;
                }

                $cells[$targetName] = self::STATUS_IGNORED;
                $this->total[$targetName] -= $result;
                ++$this->ignored[$targetName];
            }
        }

        $this->writeRow($cells, '.');
    }

    public function close() : void
    {
        echo str_repeat('=', $this->width)."\n";

        $this->writeSummary('Total', $this->total, function ($value) {
            return sprintf('%.4f', $value);
        });

        $this->writeSummary('Skipped', $this->skipped);
        $this->writeSummary('Failed', $this->failed);
        $this->writeSummary('Ignored', $this->ignored);
    }

    private function writeSummary(string $title, array $values, \Closure $formatter = null) : void
    {
        $cells = [$title];

        foreach ($values as $value) {
            $cells[] = $formatter ? $formatter($value) : $value;
        }

        $this->writeRow($cells);
    }

    private function writeRow(array $cells, string $padChar = ' ') : void
    {
        $title = array_shift($cells);

        echo $title;
        $paddingLen = $this->widths[0] - strlen($title);

        if ($this->widths[0] > 1) {
            echo ' '.str_repeat($padChar, $paddingLen - 1);
        }

        $i = 1;
        foreach ($cells as $name => $value) {
            $multiplier = $this->widths[$i] - strlen($value) - 2;
            echo (1 === $i) ? $padChar : ' ';
            echo str_repeat($padChar, $multiplier > 0 ? $multiplier : 0).' ';
            echo $value;
            ++$i;
        }

        echo "\n";
    }
}