<commit_before>package l1instruction

import (
	"fmt"
	"path"

	"github.com/intelsdi-x/swan/pkg/conf"
	"github.com/intelsdi-x/swan/pkg/executor"
	"github.com/intelsdi-x/swan/pkg/utils/fs"
	"github.com/pkg/errors"
)

const (
	// ID is used for specifying which aggressors should be used via parameters.
	ID                = "l1i"
	name              = "L1 Instruction"
	defaultIntensity  = 19

	// {min,max}Intensity are hardcoded values in l1i binary
	// For further information look inside l1i.c which can be found in github.com/intelsdi-x/swan
	// repository.
	minIntensity = 0
	maxIntensity = 20

	// -1 (or absence of iteration argument) means infinite iterations in l1i.
	infiniteIterations = -1
	defaultIterations = infiniteIterations
)

// PathFlag represents l1i path flag.
var PathFlag = conf.NewFileFlag(
	"l1i_path",
	"Path to L1 instruction binary",
	path.Join(fs.GetSwanWorkloadsPath(), "low-level-aggressors/l1i"),
)

// Config is a struct for l1i aggressor configuration.
type Config struct {
	Path string
	// Intensity means level(in range <1;20>) of L1 load.
	Intensity int
	// Iteration means how many L1 load should be executed.
	Iterations int
}

// DefaultL1iConfig is a constructor for l1i aggressor Config with default parameters.
func DefaultL1iConfig() Config {
	return Config{
		Path:       PathFlag.Value(),
		Intensity:  defaultIntensity,
		Iterations: defaultIterations,
	}
}

// l1i is a launcher for l1i aggressor.
type l1i struct {
	exec executor.Executor
	conf Config
}

// New is a constructor for l1i aggressor.
func New(exec executor.Executor, config Config) executor.Launcher {
	return l1i{
		exec: exec,
		conf: config,
	}
}

func (l l1i) buildCommand() string {
	return fmt.Sprintf("%s %d %d", l.conf.Path, l.conf.Intensity, l.conf.Iterations)
}

func (l l1i) verifyConfiguration() error {
	if l.conf.Intensity > maxIntensity || l.conf.Intensity < minIntensity {
		return errors.Errorf("intensivity value(%d) is out of range <%d;%d>",
			l.conf.Intensity,
			minIntensity,
			maxIntensity)
	}
	if l.conf.Iterations <= 0 && l.conf.Iterations != infiniteIterations {
		return errors.Errorf("iterations value(%d) should be greater than 0", l.conf.Iterations)
	}
	return nil
}

// Launch starts a workload.
// It returns a workload represented as a Task instance.
// Error is returned when Launcher is unable to start a job or when configuration is invalid.
func (l l1i) Launch() (executor.TaskHandle, error) {
	if err := l.verifyConfiguration(); err != nil {
		return nil, err
	}
	return l.exec.Execute(l.buildCommand())
}

// Name returns human readable name for job.
func (l l1i) Name() string {
	return name
}
<commit_msg>SCE-602: kubernetes test refactor - stop & clean task handle (#312)<commit_after>package l1instruction

import (
	"fmt"
	"path"

	"github.com/intelsdi-x/swan/pkg/conf"
	"github.com/intelsdi-x/swan/pkg/executor"
	"github.com/intelsdi-x/swan/pkg/utils/fs"
	"github.com/pkg/errors"
)

const (
	// ID is used for specifying which aggressors should be used via parameters.
	ID               = "l1i"
	name             = "L1 Instruction"
	defaultIntensity = 19

	// {min,max}Intensity are hardcoded values in l1i binary
	// For further information look inside l1i.c which can be found in github.com/intelsdi-x/swan
	// repository.
	minIntensity = 0
	maxIntensity = 20

	// -1 (or absence of iteration argument) means infinite iterations in l1i.
	infiniteIterations = -1
	defaultIterations  = infiniteIterations
)

// PathFlag represents l1i path flag.
var PathFlag = conf.NewFileFlag(
	"l1i_path",
	"Path to L1 instruction binary",
	path.Join(fs.GetSwanWorkloadsPath(), "low-level-aggressors/l1i"),
)

// Config is a struct for l1i aggressor configuration.
type Config struct {
	Path string
	// Intensity means level(in range <1;20>) of L1 load.
	Intensity int
	// Iteration means how many L1 load should be executed.
	Iterations int
}

// DefaultL1iConfig is a constructor for l1i aggressor Config with default parameters.
func DefaultL1iConfig() Config {
	return Config{
		Path:       PathFlag.Value(),
		Intensity:  defaultIntensity,
		Iterations: defaultIterations,
	}
}

// l1i is a launcher for l1i aggressor.
type l1i struct {
	exec executor.Executor
	conf Config
}

// New is a constructor for l1i aggressor.
func New(exec executor.Executor, config Config) executor.Launcher {
	return l1i{
		exec: exec,
		conf: config,
	}
}

func (l l1i) buildCommand() string {
	return fmt.Sprintf("%s %d %d", l.conf.Path, l.conf.Intensity, l.conf.Iterations)
}

func (l l1i) verifyConfiguration() error {
	if l.conf.Intensity > maxIntensity || l.conf.Intensity < minIntensity {
		return errors.Errorf("intensivity value(%d) is out of range <%d;%d>",
			l.conf.Intensity,
			minIntensity,
			maxIntensity)
	}
	if l.conf.Iterations <= 0 && l.conf.Iterations != infiniteIterations {
		return errors.Errorf("iterations value(%d) should be greater than 0", l.conf.Iterations)
	}
	return nil
}

// Launch starts a workload.
// It returns a workload represented as a Task instance.
// Error is returned when Launcher is unable to start a job or when configuration is invalid.
func (l l1i) Launch() (executor.TaskHandle, error) {
	if err := l.verifyConfiguration(); err != nil {
		return nil, err
	}
	return l.exec.Execute(l.buildCommand())
}

// Name returns human readable name for job.
func (l l1i) Name() string {
	return name
}

{'fraction_non_alphanumeric': 0.10740878629932986, 'fraction_numerical': 0.014892032762472078, 'mean_word_length': 3.668114682884448, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6983154', 'n_tokens_mistral': 1881, 'n_tokens_neox': 1801, 'n_words': 570}