<commit_before>package bench

import (
	"crypto/rand"
	"fmt"
	"os"
	"time"

	"github.com/chzyer/flow"
	"github.com/chzyer/madq/go/fs"
	"github.com/chzyer/madq/go/ptrace"
)

type FsFile struct {
	BenchCnt  int    `name:"count" desc:"bench size" default:"200"`
	BlockSize int    `name:"bs" desc:"block size" default:"200"`
	Dir       string `desc:"test directory path" default:"/tmp/madq/bench/fsfile"`
}

func (f *FsFile) FlaglyDesc() string {
	return "benchmark file Read/Write"
}

func (cfg *FsFile) FlaglyHandle(f *flow.Flow) error {
	defer f.Close()

	now := time.Now()

	vs, err := fs.NewVolumeSource(cfg.Dir)
	if err != nil {
		return err
	}
	defer vs.Close()

	vol, err := fs.NewVolume(f, &fs.VolumeConfig{
		Delegate: vs,
	})
	if err != nil {
		return err
	}
	defer vol.Close()

	fd, err := vol.Open("/hello", os.O_CREATE)
	if err != nil {
		return fmt.Errorf("error in openfile: %v", err)
	}
	defer fd.Close()

	buf := make([]byte, cfg.BlockSize)
	rand.Read(buf)
	var size ptrace.Size

	size.AddInt(len(buf) * cfg.BenchCnt)

	for i := 0; i < cfg.BenchCnt; i++ {
		fd.Write(buf)
	}
	fd.Sync()
	duration := time.Now().Sub(now)

	println(fs.Stat.String())
	println(size.Rate(duration).String())
	return nil
}<commit_msg>[bench] fsfile: add mem mode<commit_after>package bench

import (
	"crypto/rand"
	"fmt"
	"os"
	"time"

	"github.com/chzyer/flow"
	"github.com/chzyer/madq/go/bio"
	"github.com/chzyer/madq/go/fs"
	"github.com/chzyer/madq/go/ptrace"
	"github.com/chzyer/test"
)

type FsFile struct {
	Mem       bool
	BenchCnt  int    `name:"count" desc:"bench size" default:"200"`
	BlockSize int    `name:"bs" desc:"block size" default:"200"`
	Dir       string `desc:"test directory path" default:"/tmp/madq/bench/fsfile"`
}

func (f *FsFile) FlaglyDesc() string {
	return "benchmark file Read/Write"
}

func (cfg *FsFile) FlaglyHandle(f *flow.Flow) error {
	defer f.Close()

	now := time.Now()

	volcfg := &fs.VolumeConfig{}

	if cfg.Mem {
		volcfg.Delegate = bio.NewHybrid(test.NewMemDisk())
	} else {
		vs, err := fs.NewVolumeSource(cfg.Dir)
		if err != nil {
			return err
		}
		defer vs.Close()
		volcfg.Delegate = vs
	}

	vol, err := fs.NewVolume(f, volcfg)
	if err != nil {
		return err
	}
	defer vol.Close()

	fd, err := vol.Open("/hello", os.O_CREATE)
	if err != nil {
		return fmt.Errorf("error in openfile: %v", err)
	}
	defer fd.Close()

	buf := make([]byte, cfg.BlockSize)
	rand.Read(buf)
	var size ptrace.Size

	size.AddInt(len(buf) * cfg.BenchCnt)

	for i := 0; i < cfg.BenchCnt; i++ {
		fd.Write(buf)
	}
	fd.Sync()
	duration := time.Now().Sub(now)

	println(fs.Stat.String())
	println(size.Rate(duration).String())
	return nil
}
{'fraction_non_alphanumeric': 0.17214043035107587, 'fraction_numerical': 0.005285013212533031, 'mean_word_length': 3.6247818499127398, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '1426661', 'n_tokens_mistral': 1119, 'n_tokens_neox': 1067, 'n_words': 237}