Skip to content

Commit

Permalink
usage: count parsed files including TS files
Browse files Browse the repository at this point in the history
This both allows us to figure out if users are using k6 with a lot of
files and if ts support is actually used.
  • Loading branch information
mstoykov committed Sep 13, 2024
1 parent f53d1c6 commit f6efeb9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
1 change: 1 addition & 0 deletions js/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ func (bi *BundleInstance) manipulateOptions(options lib.Options) error {

func (b *Bundle) newCompiler(logger logrus.FieldLogger) *compiler.Compiler {
c := compiler.New(logger)
c.WithUsage(b.preInitState.Usage)
c.Options = compiler.Options{
CompatibilityMode: b.CompatibilityMode,
SourceMapLoader: generateSourceMapLoader(logger, b.filesystems),
Expand Down
26 changes: 25 additions & 1 deletion js/compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,30 @@ import (
"github.com/sirupsen/logrus"

"go.k6.io/k6/lib"
"go.k6.io/k6/usage"
)

// A Compiler compiles JavaScript or TypeScript source code into a sobek.Program
type Compiler struct {
logger logrus.FieldLogger
Options Options
usage *usage.Usage
}

// New returns a new Compiler
func New(logger logrus.FieldLogger) *Compiler {
return &Compiler{logger: logger}
return &Compiler{
logger: logger,
// TODO(@mstoykov): unfortunately otherwise we need to do much bigger rewrite around experimental extensions
// and likely more extensions tests. This way tests don't need to care about this and the compiler code
// doesn't need to check if it has usage set or not.
usage: usage.New(),
}
}

// WithUsage allows the compiler to be given [usage.Usage] to use
func (c *Compiler) WithUsage(u *usage.Usage) {
c.usage = u
}

// Options are options to the compiler
Expand Down Expand Up @@ -64,7 +77,15 @@ func (c *Compiler) Parse(
return state.parseImpl(src, filename, commonJSWrap)
}

const (
usageParsedFilesKey = "usage/parsedFiles"
usageParsedTSFilesKey = "usage/parsedTSFiles"
)

func (ps *parsingState) parseImpl(src, filename string, commonJSWrap bool) (*ast.Program, string, error) {
if err := ps.compiler.usage.Uint64(usageParsedFilesKey, 1); err != nil {
ps.compiler.logger.WithError(err).Warn("couldn't report usage for " + usageParsedFilesKey)
}
code := src
if commonJSWrap { // the lines in the sourcemap (if available) will be fixed by increaseMappingsByOne
code = ps.wrap(code, filename)
Expand Down Expand Up @@ -97,6 +118,9 @@ func (ps *parsingState) parseImpl(src, filename string, commonJSWrap bool) (*ast
}

if ps.compatibilityMode == lib.CompatibilityModeExperimentalEnhanced {
if err := ps.compiler.usage.Uint64(usageParsedTSFilesKey, 1); err != nil {
ps.compiler.logger.WithError(err).Warn("couldn't report usage for " + usageParsedTSFilesKey)
}
code, ps.srcMap, err = esbuildTransform(src, filename)
if err != nil {
return nil, "", err
Expand Down

0 comments on commit f6efeb9

Please sign in to comment.