@@ -13,6 +13,7 @@ import (
13
13
"math/rand"
14
14
"os"
15
15
"path/filepath"
16
+ "regexp"
16
17
"strings"
17
18
"testing"
18
19
"time"
@@ -35,8 +36,10 @@ import (
35
36
"github.com/cockroachdb/cockroach/pkg/util/humanizeutil"
36
37
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
37
38
"github.com/cockroachdb/cockroach/pkg/util/log"
39
+ "github.com/cockroachdb/cockroach/pkg/util/tracing/tracingpb"
38
40
"github.com/cockroachdb/datadriven"
39
41
"github.com/cockroachdb/logtags"
42
+ "github.com/stretchr/testify/assert"
40
43
"github.com/stretchr/testify/require"
41
44
)
42
45
@@ -165,6 +168,12 @@ var runAsimTests = envutil.EnvOrDefaultBool("COCKROACH_RUN_ASIM_TESTS", false)
165
168
// random number generator that creates the seed used to generate each
166
169
// simulation sample. The default values are: duration=30m (30 minutes)
167
170
// samples=1 seed=random.
171
+ //
172
+ // To run all tests and rewrite the testdata files as well as generate the
173
+ // artifacts in `testdata/generated`, you can use:
174
+ /*
175
+ ./dev test pkg/kv/kvserver/asim/tests --ignore-cache --rewrite -v -f TestDataDriven -- --test_env COCKROACH_RUN_ASIM_TESTS=true --test_env COCKROACH_ALWAYS_KEEP_TEST_LOGS=true
176
+ */
168
177
func TestDataDriven (t * testing.T ) {
169
178
skip .UnderDuressWithIssue (t , 149875 )
170
179
leakTestAfter := leaktest .AfterTest (t )
@@ -531,7 +540,34 @@ func TestDataDriven(t *testing.T) {
531
540
require .NotNil (t , set , "unknown mode value: %s" , mv )
532
541
set (& eventGen )
533
542
543
+ // TODO(tbg): need to decide whether multiple evals in a single file
544
+ // is a feature or an anti-pattern. If it's a feature, we should let
545
+ // the `name` part below be adjustable (but not the plotDir) via a
546
+ // parameter to the `eval` command.
547
+ testName := name + "_" + mv
548
+
534
549
for sample := 0 ; sample < samples ; sample ++ {
550
+ recIdx := map [int64 ]int {}
551
+ settingsGen .Settings .OnRecording = func (storeID int64 , rec tracingpb.Recording ) {
552
+ if ! rewrite || len (rec [0 ].Logs ) == 0 {
553
+ return
554
+ }
555
+ traceDir := filepath .Join (plotDir , "traces" , fmt .Sprintf ("s%d" , storeID ))
556
+ if recIdx [storeID ] == 0 {
557
+ require .NoError (t , os .MkdirAll (traceDir , 0755 ))
558
+ }
559
+ re := regexp .MustCompile (`[^a-zA-Z0-9]+` )
560
+ outName := fmt .Sprintf ("%s_%s_s%d" , mv , re .ReplaceAllString (rec [0 ].Operation , "_" ), storeID )
561
+ if sample > 0 {
562
+ outName += fmt .Sprintf ("_sample%d" , sample + 1 )
563
+ }
564
+ outName += "_" + fmt .Sprintf ("%03d.txt" , recIdx [storeID ])
565
+ assert .NoError (t , os .WriteFile (
566
+ filepath .Join (traceDir , outName ),
567
+ []byte (rec .String ()), 0644 ))
568
+ recIdx [storeID ] += 1
569
+ }
570
+
535
571
assertionFailures := []string {}
536
572
var tmpStrB * strings.Builder = nil
537
573
if stateStrForOnce == "" {
@@ -561,11 +597,7 @@ func TestDataDriven(t *testing.T) {
561
597
// Generate artifacts. Hash artifact input data to ensure they are
562
598
// up to date.
563
599
hasher := fnv .New64a ()
564
- // TODO(tbg): need to decide whether multiple evals in a single file
565
- // is a feature or an anti-pattern. If it's a feature, we should let
566
- // the `name` part below be adjustable (but not the plotDir) via a
567
- // parameter to the `eval` command.
568
- testName := name + "_" + mv
600
+
569
601
for sample , h := range run .hs {
570
602
printStatsAndGenerateJSON (t , & buf , h , testName , sample + 1 , plotDir , hasher , rewrite ,
571
603
settingsGen .Settings .TickInterval , metricsMap )
0 commit comments