<commit_before>
# Copyright (C) 2010, Parrot Foundation.
# $Id$

class ProfTest::PIRProfile is Hash;

method new($pir_code, $canonical? = 1) {
    self<canonical> := $canonical;
    self<pir_code>  := $pir_code;
    self.build_pir_profile;
    self.build_profile_array;
    self;
}

method profile_array() {
    self<profile_array>;
}

method build_profile_array() {

    my @pprof_lines := pir::split("\n", self<profile>);
    self<profile_array> := ();

    grammar pprof_line {
        rule TOP { ^^ [ <variable_line> | <fixed_line> ] $$ }

        rule line_type { [ 'VERSION' | 'CLI' | 'END_OF_RUNLOOP' | 'CS' | 'OP' ] }

        rule fixed_line { <line_type> ':' <fixed_data> }
        rule fixed_data { \N* }

        rule variable_line { <line_type> ':' <variable_data>* }
        rule variable_data { '{x{' <field_name> ':' <field_data> '}x}' }
        rule field_name    { <.ident> }
        #XXX: really need to find something better 
        rule field_data    { <[a..zA..Z0..9_\-;\/.]>* }
    }

    for @pprof_lines -> $line {
        my $line_match := pprof_line.parse($line);
        #pir::say($line);
        #_dumper($line_match);
        self<profile_array>.push($line_match);
    }
}

method build_pir_profile() {

    my $tmp_pir := '/tmp/test.pir';
    my $tmp_pprof := '/tmp/test.pprof';
    my $fh := pir::new__p_sc('FileHandle');
    $fh.open($tmp_pir, "w");
    $fh.puts(self<pir_code>);
    $fh.close();

    my %config := self.get_config();
    my $parrot_exe := %config<prefix> ~ %config<slash> ~ %config<test_prog>;
    my $hash_seed_opt := '';

    my %env := pir::new__p_sc('Env');
    %env{'PARROT_PROFILING_FILENAME'} := $tmp_pprof;
    if self<canonical> {
        %env{'PARROT_PROFILING_CANONICAL_OUTPUT'} := 1;
        $hash_seed_opt := '--hash-seed=1234';
    }

    my $cli := "$parrot_exe $hash_seed_opt --runcore profiling $tmp_pir";<commit_msg>[proftest] use more generic, if predictable, temp files<commit_after>
# Copyright (C) 2010, Parrot Foundation.
# $Id$

class ProfTest::PIRProfile is Hash;

method new($pir_code, $canonical? = 1) {
    self<canonical> := $canonical;
    self<pir_code>  := $pir_code;
    self.build_pir_profile;
    self.build_profile_array;
    self;
}

method profile_array() {
    self<profile_array>;
}

method build_profile_array() {

    my @pprof_lines := pir::split("\n", self<profile>);
    self<profile_array> := ();

    grammar pprof_line {
        rule TOP { ^^ [ <variable_line> | <fixed_line> ] $$ }

        rule line_type { [ 'VERSION' | 'CLI' | 'END_OF_RUNLOOP' | 'CS' | 'OP' ] }

        rule fixed_line { <line_type> ':' <fixed_data> }
        rule fixed_data { \N* }

        rule variable_line { <line_type> ':' <variable_data>* }
        rule variable_data { '{x{' <field_name> ':' <field_data> '}x}' }
        rule field_name    { <.ident> }
        #XXX(cotto): really need to find something better 
        rule field_data    { <[a..zA..Z0..9_\-;\/.]>* }
    }

    for @pprof_lines -> $line {
        my $line_match := pprof_line.parse($line);
        #pir::say($line);
        #_dumper($line_match);
        self<profile_array>.push($line_match);
    }
}

method build_pir_profile() {

    my %config    := self.get_config();
    #XXX(cotto) use a random filename (requires randomness from pir)
    my $tmp_pir   := %config<tempdir> ~ %config<slash> ~ 'test.pir';
    my $tmp_pprof := %config<tempdir> ~ %config<slash> ~ 'test.pprof';

    my $fh := pir::new__p_sc('FileHandle');
    $fh.open($tmp_pir, "w");
    $fh.puts(self<pir_code>);
    $fh.close();

    my $parrot_exe := %config<prefix> ~ %config<slash> ~ %config<test_prog>;
    my $hash_seed_opt := '';

    my %env := pir::new__p_sc('Env');
    %env{'PARROT_PROFILING_FILENAME'} := $tmp_pprof;
    if self<canonical> {
        %env{'PARROT_PROFILING_CANONICAL_OUTPUT'} := 1;
        $hash_seed_opt := '--hash-seed=1234';
    }

    my $cli := "$parrot_exe $hash_seed_opt --runcore profiling $tmp_pir";
{'fraction_non_alphanumeric': 0.17559373420919655, 'fraction_numerical': 0.00606366851945427, 'mean_word_length': 3.035677879714577, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 51, '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': '245428', 'n_tokens_mistral': 1548, 'n_tokens_neox': 1452, 'n_words': 273}