为什么这个ColdFusion 8应用程序导致CPU飙升?

时间:2014-08-08 18:08:43

标签: iis coldfusion windows-server-2003 uploadify cfimage

感谢您抽出宝贵时间来研究这个问题....请耐心等待....

前几天,我们的某个Web服务器停止提供网页。 Web服务器是运行带有IIS 6的Windows Server 2003 R2标准版Service Pack 2的物理服务器。它使用Java 1.6.0_24运行ColdFusion 8.0.1 Standard。这个服务器有3个面向公众的网站,实际上没有太大的用处。所有三个网站上的页面都超时或返回500个错误。

当我登录服务器以查看问题所在时,我希望看到使用大量内存的JRUN服务。内存很好,但是,我注意到CPU运行在或接近100%。约有50%被JRUN使用,另外50%被一个失控的备份过程使用,我杀了它。但是,然后JRUN迅速吃掉CPU并使用接近100%。

我查看了ColdFusion日志,发现发生了几个 Java堆空间错误。

我查看了IIS日志,发现有一堆应用程序请求允许我们的客户使用uploadify为其产品上传多个图像文件。该应用程序使用ColdFusion编写,并使用jQuery调用处理上载的Web服务,并使用<CFIMAGE>调整上传图像的大小。

在日志中看到这些信息之后,我认为这个应用程序的某些部分必定是罪魁祸首。

我似乎无法找到究竟是什么导致了Java堆空间错误和CPU峰值。有什么想法吗?

WebService CFC方法:

<cffunction
    name="uploadFile"
    access="remote"
    returntype="Struct"
    output="false"
    returnformat="JSON">
    <cfargument name="FileName" type="String" default="" />
    <cfargument name="FileData" type="String" default="" />

    <cfscript>
        var _response = NewAPIResponse();
        var _tempFilePath = APPLICATION.TempDir & "\" & ARGUMENTS.FileName;
        var _qItem = QueryNew("");
        var _product = CreateObject("component", "cfc.Product");
        var _result = {};
        var _sku = "";

        /*
            Each file must be named [Part Number].[file extension], so, \
            parse the file name to get everything before the file extension
        */
        _sku =
            Trim(
                REQUEST.UDFLib.File.getFileNameWithoutExtension(
                    ARGUMENTS.FileName
                    )
                );
    </cfscript>

    <cfif Len(_sku) GT 20>
        <cfthrow
            message="#ARGUMENTS.FileName#: File Name does not correspond to an existing Part Number." />
    </cfif>

    <cfset _qItem = _product.readSKU(_sku) />

    <cfif NOT _qItem.RECORDCOUNT>
        <cfthrow
            message="#ARGUMENTS.FileName#: File Name does not correspond to an existing Part Number." />
    </cfif>

    <cfscript>
        FileCopy(ARGUMENTS.FileData, _tempFilePath);

        _aMessages =
            _product.setId(
                _qItem.SKU
                ).updateThumbnailImages(uploadFilePath = _tempFilePath);
    </cfscript>

    <cfif ArrayLen(_aMessages)>
        <cfthrow
            message="#ARGUMENTS.FileName#: #_aMessages[1].getText()#" />
    </cfif>

    <cfscript>
        _result["SKU"] = _product.getSKU();
        _result["SMALLIMAGESRC"] = _product.getSmallImageSrc();
        _result["LARGEIMAGESRC"] = _product.getLargeImageSrc();

        ArrayAppend(_response.data, _result);
    </cfscript>

    <cfreturn _response />
</cffunction>

图像调整功能:

<cffunction name="updateThumbnailImages" returntype="Array" output="false">
    <cfargument name="uploadFilePath" type="String" required="true" />

    <cfset var _image = {} />

    <cfif FileExists(ARGUMENTS.uploadFilePath)>
        <cfset _image =
            REQUEST.UDFLib.Image.scale(
                imagePath = ARGUMENTS.uploadFilePath,
                maxHeight = 500,
                maxWidth = 700
                ) />

        <cfimage
            action="write"
            source="#_image#"
            overwrite="true"
            destination="#getLargeImagePath()#" />

        <cfset _image =
            REQUEST.UDFLib.Image.scale(
                imagePath = ARGUMENTS.uploadFilePath,
                maxHeight = 300,
                maxWidth = 300
                ) />

        <cfimage
            action="write"
            source="#_image#"
            overwrite="true"
            destination="#getMediumImagePath()#" />

        <cfset _image =
            REQUEST.UDFLib.Image.scale(
                imagePath = ARGUMENTS.uploadFilePath,
                maxHeight = 50,
                maxWidth = 50
                ) />

        <cfimage
            action="write"
            source="#_image#"
            overwrite="true"
            destination="#getSmallImagePath()#" />
    </cfif>
</cffunction>

图像缩放UDF:

<cffunction name="getDimensionsToEnlarge" returntype="Struct" output="false">
    <cfargument name="imageWidth" type="Numeric" required="true" />
    <cfargument name="imageHeight" type="Numeric" required="true" />
    <cfargument name="minWidth" type="Numeric" required="true" />
    <cfargument name="minHeight" type="Numeric" required="true" />

    <cfscript>
        var dimensions = {
            width = -1,
            height = -1
            };

        if  (
                ARGUMENTS.minHeight > 0
            &&  ARGUMENTS.minWidth > 0
            &&  imageHeight < ARGUMENTS.minHeight
            &&  imageWidth < ARGUMENTS.minWidth
            ) {
            dimensions.width = ARGUMENTS.minWidth;
            dimensions.height = ARGUMENTS.minHeight;
        }

        return dimensions;
    </cfscript>
</cffunction>

<cffunction name="getDimensionsToShrink" returntype="Struct" output="false">
    <cfargument name="imageWidth" type="Numeric" required="true" />
    <cfargument name="imageHeight" type="Numeric" required="true" />
    <cfargument name="maxWidth" type="Numeric" required="true" />
    <cfargument name="maxHeight" type="Numeric" required="true" />

    <cfscript>
        var dimensions = {
            width = -1,
            height = -1
            };

        if  (
                ARGUMENTS.maxHeight > 0
            &&  ARGUMENTS.maxWidth > 0
            &&  (
                    imageHeight > ARGUMENTS.maxHeight
                ||  imageWidth > ARGUMENTS.maxWidth
                )
            ) {
            dimensions.width = ARGUMENTS.maxWidth;
            dimensions.height = ARGUMENTS.maxHeight;
        }

        return dimensions;
    </cfscript>
</cffunction>

<cffunction name="getDimensionsToFit" returntype="Struct" output="false">
    <cfargument name="imageWidth" type="Numeric" required="true" />
    <cfargument name="imageHeight" type="Numeric" required="true" />
    <cfargument name="minWidth" type="Numeric" required="true" />
    <cfargument name="minHeight" type="Numeric" required="true" />
    <cfargument name="maxWidth" type="Numeric" required="true" />
    <cfargument name="maxHeight" type="Numeric" required="true" />

    <cfscript>
        var dimensions = {
            width = -1,
            height = -1
            };

        dimensions =
            getDimensionsToEnlarge(
                imageHeight = ARGUMENTS.imageHeight,
                imageWidth = ARGUMENTS.imageWidth,
                minWidth = ARGUMENTS.minWidth,
                minHeight = ARGUMENTS.minHeight
                );

        if (dimensions.width < 0 && dimensions.height < 0)
            dimensions =
                getDimensionsToShrink(
                    imageHeight = ARGUMENTS.imageHeight,
                    imageWidth = ARGUMENTS.imageWidth,
                    maxWidth = ARGUMENTS.maxWidth,
                    maxHeight = ARGUMENTS.maxHeight
                    );

        return dimensions;
    </cfscript>
</cffunction>

<cffunction name="scale" returntype="Any" output="false">
    <cfargument name="imagePath" type="String" required="true" />
    <cfargument name="action" type="String" default="fit" hint="shrink, enlarge, or fit"/>
    <cfargument name="minWidth" type="Numeric" default="-1" />
    <cfargument name="minHeight" type="Numeric" default="-1" />
    <cfargument name="maxWidth" type="Numeric" default="-1" />
    <cfargument name="maxHeight" type="Numeric" default="-1" />

    <cfscript>
        var scaledDimensions = {
                width = -1,
                height = -1
            };
        var scaledImage = ImageNew();

        scaledImage = ImageNew(ARGUMENTS.imagePath);

        switch (ARGUMENTS.action) {
            case "shrink":
                scaledDimensions =
                    getDimensionsToShrink(
                        imageHeight = scaledImage.getHeight(),
                        imageWidth = scaledImage.getWidth(),
                        maxWidth = ARGUMENTS.maxWidth,
                        maxHeight = ARGUMENTS.maxHeight
                    );

                break;
            case "enlarge":
                scaledDimensions =
                    getDimensionsToEnlarge(
                        imageHeight = scaledImage.getHeight(),
                        imageWidth = scaledImage.getWidth(),
                        minWidth = ARGUMENTS.minWidth,
                        minHeight = ARGUMENTS.minHeight
                    );

                break;
            default:
                scaledDimensions =
                    getDimensionsToFit(
                        imageHeight = scaledImage.getHeight(),
                        imageWidth = scaledImage.getWidth(),
                        minWidth = ARGUMENTS.minWidth,
                        minHeight = ARGUMENTS.minHeight,
                        maxWidth = ARGUMENTS.maxWidth,
                        maxHeight = ARGUMENTS.maxHeight
                    );

                break;
        }

        if (scaledDimensions.width > 0 && scaledDimensions.height > 0) {
            // This helps the image quality
            ImageSetAntialiasing(scaledImage, "on");

            ImageScaleToFit(
                scaledImage,
                scaledDimensions.width,
                scaledDimensions.height
                );
        }

        return scaledImage;
    </cfscript>
</cffunction>

1 个答案:

答案 0 :(得分:0)

以下是我在评论部分中提到的示例代码。这提高了我们的可靠性,但在极端负载下,我们发现问题仍然存在,这就是我们迁移到Java 1.7的原因。然而1.7有自己的问题,因为性能急剧下降here。希望它有所帮助。

<cffunction name="init" access="private" output="false" returntype="void">
    <cfset variables.instance = StructNew() />
    <!--- create object and set to variables.instance --->
    <cfset setProductObject()>
</cffunction>

<cffunction name="setProductObject" access="private" returntype="void" output="false">
    <!--- create object once and set it to the variables.instance scope --->
    <cfset var product = createObject("component","cfc.Product")>
    <cfset variables.instance.product = product/>
    <cfset product = javacast('null', '')>
</cffunction>

<cffunction name="getProductObject" access="private" returntype="cfc.Product" output="false">
    <!--- instead of creating object each time use the one already in memory and duplicate it --->
    <cfset var productObj = duplicate(variables.instance.product)>
    <cfreturn productObj />
</cffunction>

<cffunction name="uploadFile" access="remote" returntype="struct" returnformat="JSON" output="false">
    <cfargument name="FileName" type="String" default="" />
    <cfargument name="FileData" type="String" default="" />
        <cfscript>
            var _response = NewAPIResponse();
            var _tempFilePath = APPLICATION.TempDir & "\" & ARGUMENTS.FileName;
            var _qItem = QueryNew("");
            var _product = "";
            var _result = {};
            var _sku = "";
            //check init() function has been run if not run it
            if NOT structKeyExists(variables,'instance') {
                init();         
            }

            _product = getProductObject();
            /*
                Each file must be named [Part Number].[file extension], so, \
                parse the file name to get everything before the file extension
            */
            _sku =
                Trim(
                    REQUEST.UDFLib.File.getFileNameWithoutExtension(
                        ARGUMENTS.FileName
                        )
                    );
        </cfscript>

        <cfif Len(_sku) GT 20>
            <cfthrow
                message="#ARGUMENTS.FileName#: File Name does not correspond to an existing Part Number." />
        </cfif>

        <cfset _qItem = _product.readSKU(_sku) />

        <cfif NOT _qItem.RECORDCOUNT>
            <cfthrow
                message="#ARGUMENTS.FileName#: File Name does not correspond to an existing Part Number." />
        </cfif>

        <cfscript>
            FileCopy(ARGUMENTS.FileData, _tempFilePath);

            _aMessages =
                _product.setId(
                    _qItem.SKU
                    ).updateThumbnailImages(uploadFilePath = _tempFilePath);
        </cfscript>

        <cfif ArrayLen(_aMessages)>
            <cfthrow
                message="#ARGUMENTS.FileName#: #_aMessages[1].getText()#" />
        </cfif>

        <cfscript>
            _result["SKU"] = _product.getSKU();
            _result["SMALLIMAGESRC"] = _product.getSmallImageSrc();
            _result["LARGEIMAGESRC"] = _product.getLargeImageSrc();

            ArrayAppend(_response.data, _result);
        </cfscript>

        <cfreturn _response />
</cffunction>