# Path: utils/time.py
# def time_took(function):
#     before = time.time()
#     try:
#         success = function()
#     except Exception as e:
#         print('Error: %s' %e)
#         return 0
#     after = time.time()
#     took = (after - before) if success else 0
#     return took
# 
# Path: utils/process.py
# def run_gst_cmd(cmd):
#     try:
#         rc, stdout, stderr = run_cmd(cmd, shell=False)
#         took = parse_gst_execution_time(stdout)
#     except Exception as e:
#         print(e)
#         took = 0
#     return took
# 
# Path: utils/colors.py
# def print_red(text):
#     print("%s%s%s" %(Colors.RED, text, Colors.DEFAULT))


import os
import utils.video as video
import utils.hardware as hw
from utils.time import time_took
from utils.process import run_gst_cmd 
from utils.colors import print_red 
    from utils.run import run_test
    info = "glimagesink benchmark, GPU: %s CPU: %s" %(hw.gpu(), hw.cpu())
    info += "\nTest\tFramerate"
    return info

def run():
    # Ensure that we are not limited by vblank
    os.environ['vblank_mode'] = '0'

    info = get_test_banner()
    for gl_platform in GST_GL_PLATFORMS:
        os.environ['GST_GL_PLATFORM'] = gl_platform
        gl_windows = GST_GL_WINDOW[gl_platform]
        for gl_window in gl_windows:
            os.environ['GST_GL_WINDOW'] = gl_window 
            for gl_api in GST_GL_API:
                os.environ['GST_GL_API'] = gl_api
                for colorspace in CSP:
                    for resolution in RESOLUTIONS:
                        w, h = resolution[0], resolution[1]
                        testname = "%s %s %s %sx%s %s" %(gl_platform, gl_window, gl_api, w, h, colorspace)
                        num_buffers, bufsize, caps = video.generate_buffers_from_pattern(colorspace, w, h, RAW_BUF_FILE, max_buffers=MAX_BUFFERS)

                        cmd = cmd_pattern %(bufsize, caps)

                        took = run_gst_cmd(cmd) 
                        if took <= 0:
                            print_red('test %s failed' %testname)
                        else:
                            # Run it twice to ensure that file was in cache
                            print('Running %s test' %testname)

# Path: utils/time.py
# def time_took(function):
#     before = time.time()
#     try:
#         success = function()
#     except Exception as e:
#         print('Error: %s' %e)
#         return 0
#     after = time.time()
#     took = (after - before) if success else 0
#     return took
# 
# Path: utils/process.py
# def run_gst_cmd(cmd):
#     try:
#         rc, stdout, stderr = run_cmd(cmd, shell=False)
#         took = parse_gst_execution_time(stdout)
#     except Exception as e:
#         print(e)
#         took = 0
#     return took
# 
# Path: utils/colors.py
# def print_red(text):
#     print("%s%s%s" %(Colors.RED, text, Colors.DEFAULT))


# Path: tests/glimagesink_bench.py
import os
import utils.video as video
import utils.hardware as hw
from utils.time import time_took
from utils.process import run_gst_cmd 
from utils.colors import print_red 
    from utils.run import run_test

    info = "glimagesink benchmark, GPU: %s CPU: %s" %(hw.gpu(), hw.cpu())
    info += "\nTest\tFramerate"
    return info

def run():
    # Ensure that we are not limited by vblank
    os.environ['vblank_mode'] = '0'

    info = get_test_banner()
    for gl_platform in GST_GL_PLATFORMS:
        os.environ['GST_GL_PLATFORM'] = gl_platform
        gl_windows = GST_GL_WINDOW[gl_platform]
        for gl_window in gl_windows:
            os.environ['GST_GL_WINDOW'] = gl_window 
            for gl_api in GST_GL_API:
                os.environ['GST_GL_API'] = gl_api
                for colorspace in CSP:
                    for resolution in RESOLUTIONS:
                        w, h = resolution[0], resolution[1]
                        testname = "%s %s %s %sx%s %s" %(gl_platform, gl_window, gl_api, w, h, colorspace)
                        num_buffers, bufsize, caps = video.generate_buffers_from_pattern(colorspace, w, h, RAW_BUF_FILE, max_buffers=MAX_BUFFERS)

                        cmd = cmd_pattern %(bufsize, caps)

                        took = run_gst_cmd(cmd) 
                        if took <= 0:
                            print_red('test %s failed' %testname)
                        else:
                            # Run it twice to ensure that file was in cache
                            print('Running %s test' %testname)

                            took = run_gst_cmd(cmd)