<commit_before>/*
 * Copyright (C) 2019 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package uk.co.reecedunn.intellij.plugin.saxon.profiler

import com.intellij.openapi.vfs.VirtualFile
import com.intellij.util.text.nullize
import uk.co.reecedunn.intellij.plugin.processor.database.DatabaseModule
import uk.co.reecedunn.intellij.plugin.processor.debug.StackFrame
import uk.co.reecedunn.intellij.plugin.processor.profile.ProfileEntry
import uk.co.reecedunn.intellij.plugin.processor.profile.ProfileReport
import uk.co.reecedunn.intellij.plugin.saxon.query.s9api.binding.trace.InstructionInfo
import uk.co.reecedunn.intellij.plugin.saxon.query.s9api.proxy.TraceListener
import uk.co.reecedunn.intellij.plugin.xpath.model.XsDecimal
import uk.co.reecedunn.intellij.plugin.xpath.model.XsDuration
import uk.co.reecedunn.intellij.plugin.xpath.model.XsInteger
import java.math.BigDecimal
import java.math.BigInteger
import java.text.DateFormat
import java.text.SimpleDateFormat
import java.util.*
import kotlin.collections.HashMap

private val XMLSCHEMA_DATETIME_FORMAT: DateFormat by lazy {
    val format = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
    format.timeZone = TimeZone.getTimeZone("UTC")
    format
}

class SaxonProfileInstruction(
    val instruction: InstructionInfo,
    var deepTime: Long,
    var count: Int = 1
)

class SaxonProfileTraceListener(val version: String, val query: VirtualFile) : TraceListener {
    var elapsed: Long = 0
    var created: Date? = null

    val instructions: Stack<SaxonProfileInstruction> = Stack()
    val results: HashMap<InstructionInfo, SaxonProfileInstruction> = HashMap()

    override fun setOutputDestination(logger: Any) {
    }

    override fun open(controller: Any) {
        elapsed = System.nanoTime()
        created = Date()
    }

    override fun close() {
        elapsed = System.nanoTime() - elapsed
    }

    override fun enter(instruction: InstructionInfo, context: Any) {
        // Some instructions (e.g. ClauseInfo) don't provide column information,
        // and generate different instances for each iteration. Ignore these to
        // prevent noise in the profile report.
        if (instruction.getColumnNumber() == -1) return

        instructions.push(SaxonProfileInstruction(instruction, System.nanoTime()))
    }

    override fun leave(instruction: InstructionInfo) {
        // Some instructions (e.g. ClauseInfo) don't provide column information,
        // and generate different instances for each iteration. Ignore these to
        // prevent noise in the profile report.
        if (instruction.getColumnNumber() == -1) return

        val current = instructions.pop()
        current.deepTime = System.nanoTime() - current.deepTime

        val result = results[instruction]
        if (result == null) {
            results[instruction] = current
        } else {<commit_msg>Saxon < 9.7 call TraceListener.open/close twice.<commit_after>/*
 * Copyright (C) 2019 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package uk.co.reecedunn.intellij.plugin.saxon.profiler

import com.intellij.openapi.vfs.VirtualFile
import com.intellij.util.text.nullize
import uk.co.reecedunn.intellij.plugin.processor.database.DatabaseModule
import uk.co.reecedunn.intellij.plugin.processor.debug.StackFrame
import uk.co.reecedunn.intellij.plugin.processor.profile.ProfileEntry
import uk.co.reecedunn.intellij.plugin.processor.profile.ProfileReport
import uk.co.reecedunn.intellij.plugin.saxon.query.s9api.binding.trace.InstructionInfo
import uk.co.reecedunn.intellij.plugin.saxon.query.s9api.proxy.TraceListener
import uk.co.reecedunn.intellij.plugin.xpath.model.XsDecimal
import uk.co.reecedunn.intellij.plugin.xpath.model.XsDuration
import uk.co.reecedunn.intellij.plugin.xpath.model.XsInteger
import java.math.BigDecimal
import java.math.BigInteger
import java.text.DateFormat
import java.text.SimpleDateFormat
import java.util.*
import kotlin.collections.HashMap

private val XMLSCHEMA_DATETIME_FORMAT: DateFormat by lazy {
    val format = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
    format.timeZone = TimeZone.getTimeZone("UTC")
    format
}

class SaxonProfileInstruction(
    val instruction: InstructionInfo,
    var deepTime: Long,
    var count: Int = 1
)

class SaxonProfileTraceListener(val version: String, val query: VirtualFile) : TraceListener {
    var elapsed: Long = 0
    var created: Date? = null
    var started: Boolean = false

    val instructions: Stack<SaxonProfileInstruction> = Stack()
    val results: HashMap<InstructionInfo, SaxonProfileInstruction> = HashMap()

    override fun setOutputDestination(logger: Any) {
    }

    override fun open(controller: Any) {
        // Saxon <= 9.6 call open twice.
        if (started) return

        started = true
        elapsed = System.nanoTime()
        created = Date()
    }

    override fun close() {
        // Saxon <= 9.6 call close twice.
        if (!started) return

        elapsed = System.nanoTime() - elapsed
        started = false
    }

    override fun enter(instruction: InstructionInfo, context: Any) {
        // Some instructions (e.g. ClauseInfo) don't provide column information,
        // and generate different instances for each iteration. Ignore these to
        // prevent noise in the profile report.
        if (instruction.getColumnNumber() == -1) return

        instructions.push(SaxonProfileInstruction(instruction, System.nanoTime()))
    }

    override fun leave(instruction: InstructionInfo) {
        // Some instructions (e.g. ClauseInfo) don't provide column information,
        // and generate different instances for each iteration. Ignore these to
        // prevent noise in the profile report.
        if (instruction.getColumnNumber() == -1) return

        val current = instructions.pop()
        current.deepTime = System.nanoTime() - current.deepTime

        val result = results[instruction]
        if (result == null) {
            results[instruction] = current
        } else {
{'fraction_non_alphanumeric': 0.09291092484727945, 'fraction_numerical': 0.004830231566983947, 'mean_word_length': 3.550743374272786, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 9, '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': '6127247', 'n_tokens_mistral': 2048, 'n_tokens_neox': 1983, 'n_words': 566}