github.com/Lephar/snapd@v0.0.0-20210825215435-c7fba9cef4d2/strutil/matchcounter_benchmark_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2018 Canonical Ltd
     5   *
     6   * This program is free software: you can redistribute it and/or modify
     7   * it under the terms of the GNU General Public License version 3 as
     8   * published by the Free Software Foundation.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   *
    18   */
    19  
    20  package strutil_test
    21  
    22  import (
    23  	"regexp"
    24  	"testing"
    25  
    26  	"github.com/snapcore/snapd/strutil"
    27  )
    28  
    29  func benchmarkMatchCounter(b *testing.B, wrx *regexp.Regexp, wn int) {
    30  	buf := []byte(out)
    31  	for n := 0; n < b.N; n++ {
    32  		for step := 1; step < 100; step++ {
    33  			w := &strutil.MatchCounter{Regexp: wrx, N: wn}
    34  			var i int
    35  			for i = 0; i+step < len(buf); i += step {
    36  				_, err := w.Write(buf[i : i+step])
    37  				if err != nil {
    38  					b.Fatalf("step:%d i:%d: %v", step, i, err)
    39  				}
    40  			}
    41  			_, err := w.Write(buf[i:])
    42  			if err != nil {
    43  				b.Fatalf("step:%d tail: %v", step, err)
    44  			}
    45  		}
    46  	}
    47  }
    48  
    49  func BenchmarkNil(b *testing.B)         { benchmarkMatchCounter(b, nil, 3) }
    50  func BenchmarkNilAll(b *testing.B)      { benchmarkMatchCounter(b, nil, -1) }
    51  func BenchmarkNilEquiv(b *testing.B)    { benchmarkMatchCounter(b, nilRegexpEquiv, 3) }
    52  func BenchmarkNilEquivAll(b *testing.B) { benchmarkMatchCounter(b, nilRegexpEquiv, -1) }