123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283 |
- // Copyright (c) 2016 Uber Technologies, Inc.
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- package zapcore_test
- import (
- "fmt"
- "testing"
- "time"
- "github.com/stretchr/testify/assert"
- "go.uber.org/atomic"
- "go.uber.org/zap/internal/ztest"
- . "go.uber.org/zap/zapcore"
- )
- var counterTestCases = [][]string{
- // some stuff I made up
- {
- "foo",
- "bar",
- "baz",
- "alpha",
- "bravo",
- "charlie",
- "delta",
- },
- // shuf -n50 /usr/share/dict/words
- {
- "unbracing",
- "stereotomy",
- "supranervian",
- "moaning",
- "exchangeability",
- "gunyang",
- "sulcation",
- "dariole",
- "archheresy",
- "synchronistically",
- "clips",
- "unsanctioned",
- "Argoan",
- "liparomphalus",
- "layship",
- "Fregatae",
- "microzoology",
- "glaciaria",
- "Frugivora",
- "patterist",
- "Grossulariaceae",
- "lithotint",
- "bargander",
- "opisthographical",
- "cacography",
- "chalkstone",
- "nonsubstantialism",
- "sardonicism",
- "calamiform",
- "lodginghouse",
- "predisposedly",
- "topotypic",
- "broideress",
- "outrange",
- "gingivolabial",
- "monoazo",
- "sparlike",
- "concameration",
- "untoothed",
- "Camorrism",
- "reissuer",
- "soap",
- "palaiotype",
- "countercharm",
- "yellowbird",
- "palterly",
- "writinger",
- "boatfalls",
- "tuglike",
- "underbitten",
- },
- // shuf -n100 /usr/share/dict/words
- {
- "rooty",
- "malcultivation",
- "degrade",
- "pseudoindependent",
- "stillatory",
- "antiseptize",
- "protoamphibian",
- "antiar",
- "Esther",
- "pseudelminth",
- "superfluitance",
- "teallite",
- "disunity",
- "spirignathous",
- "vergency",
- "myliobatid",
- "inosic",
- "overabstemious",
- "patriarchally",
- "foreimagine",
- "coetaneity",
- "hemimellitene",
- "hyperspatial",
- "aulophyte",
- "electropoion",
- "antitrope",
- "Amarantus",
- "smaltine",
- "lighthead",
- "syntonically",
- "incubous",
- "versation",
- "cirsophthalmia",
- "Ulidian",
- "homoeography",
- "Velella",
- "Hecatean",
- "serfage",
- "Spermaphyta",
- "palatoplasty",
- "electroextraction",
- "aconite",
- "avirulence",
- "initiator",
- "besmear",
- "unrecognizably",
- "euphoniousness",
- "balbuties",
- "pascuage",
- "quebracho",
- "Yakala",
- "auriform",
- "sevenbark",
- "superorganism",
- "telesterion",
- "ensand",
- "nagaika",
- "anisuria",
- "etching",
- "soundingly",
- "grumpish",
- "drillmaster",
- "perfumed",
- "dealkylate",
- "anthracitiferous",
- "predefiance",
- "sulphoxylate",
- "freeness",
- "untucking",
- "misworshiper",
- "Nestorianize",
- "nonegoistical",
- "construe",
- "upstroke",
- "teated",
- "nasolachrymal",
- "Mastodontidae",
- "gallows",
- "radioluminescent",
- "uncourtierlike",
- "phasmatrope",
- "Clunisian",
- "drainage",
- "sootless",
- "brachyfacial",
- "antiheroism",
- "irreligionize",
- "ked",
- "unfact",
- "nonprofessed",
- "milady",
- "conjecture",
- "Arctomys",
- "guapilla",
- "Sassenach",
- "emmetrope",
- "rosewort",
- "raphidiferous",
- "pooh",
- "Tyndallize",
- },
- }
- func BenchmarkSampler_Check(b *testing.B) {
- for _, keys := range counterTestCases {
- b.Run(fmt.Sprintf("%v keys", len(keys)), func(b *testing.B) {
- fac := NewSamplerWithOptions(
- NewCore(
- NewJSONEncoder(testEncoderConfig()),
- &ztest.Discarder{},
- DebugLevel,
- ),
- time.Millisecond, 1, 1000)
- b.ResetTimer()
- b.RunParallel(func(pb *testing.PB) {
- i := 0
- for pb.Next() {
- ent := Entry{
- Level: DebugLevel + Level(i%4),
- Message: keys[i],
- }
- _ = fac.Check(ent, nil)
- i++
- if n := len(keys); i >= n {
- i -= n
- }
- }
- })
- })
- }
- }
- func makeSamplerCountingHook() (func(_ Entry, dec SamplingDecision), *atomic.Int64, *atomic.Int64) {
- droppedCount := new(atomic.Int64)
- sampledCount := new(atomic.Int64)
- h := func(_ Entry, dec SamplingDecision) {
- if dec&LogDropped > 0 {
- droppedCount.Inc()
- } else if dec&LogSampled > 0 {
- sampledCount.Inc()
- }
- }
- return h, droppedCount, sampledCount
- }
- func BenchmarkSampler_CheckWithHook(b *testing.B) {
- hook, dropped, sampled := makeSamplerCountingHook()
- for _, keys := range counterTestCases {
- b.Run(fmt.Sprintf("%v keys", len(keys)), func(b *testing.B) {
- fac := NewSamplerWithOptions(
- NewCore(
- NewJSONEncoder(testEncoderConfig()),
- &ztest.Discarder{},
- DebugLevel,
- ),
- time.Millisecond,
- 1,
- 1000,
- SamplerHook(hook),
- )
- b.ResetTimer()
- b.RunParallel(func(pb *testing.PB) {
- i := 0
- for pb.Next() {
- ent := Entry{
- Level: DebugLevel + Level(i%4),
- Message: keys[i],
- }
- _ = fac.Check(ent, nil)
- i++
- if n := len(keys); i >= n {
- i -= n
- }
- }
- })
- })
- }
- // We expect to see 1000 dropped messages for every sampled per settings,
- // with a delta due to less 1000 messages getting dropped after initial one
- // is sampled.
- assert.Greater(b, dropped.Load()/1000, sampled.Load()-1000)
- }
|