<commit_before>/* Copyright (C) 2013 CompleteDB LLC.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with PubSubSQL.  If not, see <http://www.gnu.org/licenses/>.
 */

package pubsubsql

import "testing"
import "fmt"

// prints consumed tokens on a separate line
type printlnTokenConsumer struct {
}

func (c *printlnTokenConsumer) Consume(t token) {
	fmt.Println(t)
}

// sends consumed tokens to the channel
type chanTokenConsumer struct {
	channel chan token
}

func (consumer *chanTokenConsumer) Consume(t token) {
	consumer.channel <- t
	if t.typ == tokenTypeEOF {
		close(consumer.channel)
	}
}

func validateTokens(t *testing.T, expected []token, tokens chan token) {
	breakLoop := false
	for _, e := range expected {
		g := <-tokens
		if e.typ != g.typ {
			t.Errorf("expected type " + e.typ.String() + " but got " + g.typ.String() + " value: " + g.val)
			breakLoop = true
		}
		if e.val != g.val {
			t.Errorf("expected value " + e.val + " but got " + g.val)
			breakLoop = true
		}
		if breakLoop {
			break
		}
	}
}

// INSERT 
func TestSqlInsertStatement(t *testing.T) {
	consumer := chanTokenConsumer{channel: make(chan token)}
	go lex("insert into stocks (	ticker,bid, ask		 ) values (IBM, '34.43', 465.123)", &consumer)
	expected := []token{
		{tokenTypeSqlInsert, "insert"},
		{tokenTypeSqlInto, "into"},
		{tokenTypeSqlTable, "stocks"},
		{tokenTypeSqlLeftParenthesis, "("},<commit_msg>lexer add benchmarks for sql update<commit_after>/* Copyright (C) 2013 CompleteDB LLC.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with PubSubSQL.  If not, see <http://www.gnu.org/licenses/>.
 */

package pubsubsql

import "testing"
import "fmt"

// ignores consumed tokens usifull in benchmark code
type ignoreTokenConsumer struct {
}

func (c *ignoreTokenConsumer) Consume(t token) {
}

// prints consumed tokens on a separate line
type printlnTokenConsumer struct {
}

func (c *printlnTokenConsumer) Consume(t token) {
	fmt.Println(t)
}

// sends consumed tokens to the channel
type chanTokenConsumer struct {
	channel chan token
}

func (consumer *chanTokenConsumer) Consume(t token) {
	consumer.channel <- t
	if t.typ == tokenTypeEOF {
		close(consumer.channel)
	}
}

func validateTokens(t *testing.T, expected []token, tokens chan token) {
	breakLoop := false
	for _, e := range expected {
		g := <-tokens
		if e.typ != g.typ {
			t.Errorf("expected type " + e.typ.String() + " but got " + g.typ.String() + " value: " + g.val)
			breakLoop = true
		}
		if e.val != g.val {
			t.Errorf("expected value " + e.val + " but got " + g.val)
			breakLoop = true
		}
		if breakLoop {
			break
		}
	}
}

// BENCHMARKS

// UPDATE
// update 1 column
func BenchmarkUpdate1(b *testing.B) {
	for i := 0; i < b.N; i++ {
		var consumer ignoreTokenConsumer 
		lex("update table1 set col1 = 'value1' where key = '1234567890'", &consumer)
	}
}

// update 2 columns
func BenchmarkUpdate2(b *testing.B) {
	for i := 0; i < b.N; i++ {
		var consumer ignoreTokenConsumer 
		lex("update table1 set col1 = 'value1', col2 = 'value2 where key = '1234567890'", &consumer)
	}
}

// update 4 columns
func BenchmarkUpdate4(b *testing.B) {
	for i := 0; i < b.N; i++ {
		var consumer ignoreTokenConsumer 
		lex("update table1 set col1 = 'value1', col2 = 'value2, col3 = 'value3' col4 = value4 where key = '1234567890'", &consumer)
	}
}

// END BENCHMARKS

// INSERT 
func TestSqlInsertStatement(t *testing.T) {
	consumer := chanTokenConsumer{channel: make(chan token)}
	go lex("insert into stocks (	ticker,bid, ask		 ) values (IBM, '34.43', 465.123)", &consumer)
	expected := []token{
		{tokenTypeSqlInsert, "insert"},
		{tokenTypeSqlInto, "into"},
		{tokenTypeSqlTable, "stocks"},
		{tokenTypeSqlLeftParenthesis, "("},
{'fraction_non_alphanumeric': 0.1163515478911282, 'fraction_numerical': 0.017868273426137546, 'mean_word_length': 3.7381889763779528, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5157648', 'n_tokens_mistral': 1655, 'n_tokens_neox': 1508, 'n_words': 550}