All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.goldin.spock.extensions.profiler.ProfilerExtension.groovy Maven / Gradle / Ivy

The newest version!
package com.github.goldin.spock.extensions.profiler

import org.gcontracts.annotations.Requires
import org.spockframework.runtime.AbstractRunListener
import org.spockframework.runtime.extension.IGlobalExtension
import org.spockframework.runtime.extension.IMethodInterceptor
import org.spockframework.runtime.extension.IMethodInvocation
import org.spockframework.runtime.model.SpecInfo

import java.util.concurrent.ConcurrentLinkedQueue
import org.spockframework.runtime.model.ErrorInfo


/**
 * Global extension profiling features execution time.
 */
class ProfilerExtension implements IGlobalExtension
{
    private final Collection data = new ConcurrentLinkedQueue()

    /**
     * Writes {@link #data} to the file.
     */
    private void writeFile ()
    {
        final padSize = data*.description()*.size().max()

        new File( 'profiler.txt' ).write(
            data.sort().
                 collect { "${ it.description().padRight( padSize ) } : ${ it.executionTime } ms" }.
                 join( '\n' ), 'UTF-8' )
    }


    @Requires({ specInfo })
    void visitSpec ( SpecInfo specInfo )
    {
        /**
         * Feature interceptors updating {@link #data} with execution time.
         */
        specInfo.features*.addInterceptor({
            IMethodInvocation invocation ->

            final long t = System.currentTimeMillis()

            invocation.proceed()

            data.add( new NodeData( method        : invocation.feature,
                                    executionTime : System.currentTimeMillis() - t ))
        } as IMethodInterceptor )

        /**
         * Spec listener writing the {@link #data} to the file.
         */
        specInfo.addListener( new AbstractRunListener(){

            @Override
            void afterSpec ( SpecInfo  spec  ){ writeFile() }

            @Override
            void error     ( ErrorInfo error ){ writeFile() }
        })
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy