{"id":18262901,"url":"https://github.com/midwayjs/pgo","last_synced_at":"2025-04-04T20:30:58.435Z","repository":{"id":42238512,"uuid":"459880281","full_name":"midwayjs/pgo","owner":"midwayjs","description":"PGO（Profile Guided Optimization），是一种根据运行时 Profiling Data 来进行优化的技术。","archived":false,"fork":false,"pushed_at":"2022-12-02T09:57:45.000Z","size":41,"stargazers_count":7,"open_issues_count":1,"forks_count":4,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-20T18:09:45.337Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/midwayjs.png","metadata":{"files":{"readme":"readme-java.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-02-16T06:29:01.000Z","updated_at":"2023-06-21T07:20:44.000Z","dependencies_parsed_at":"2023-01-23T02:01:13.474Z","dependency_job_id":null,"html_url":"https://github.com/midwayjs/pgo","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/midwayjs%2Fpgo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/midwayjs%2Fpgo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/midwayjs%2Fpgo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/midwayjs%2Fpgo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/midwayjs","download_url":"https://codeload.github.com/midwayjs/pgo/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247246233,"owners_count":20907754,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-05T11:09:15.178Z","updated_at":"2025-04-04T20:30:57.902Z","avatar_url":"https://github.com/midwayjs.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"## 介绍\nAlibaba Dragonwell 11提供多种可以加速应用启动的技术，为了让函数计算的客户能方便的使用Alibaba Dragonwell的启动加速技术，我们开发了此工具，封装了相关的流程和细节，并提供简单的使用方式，目前已经支持AppCDS和EagerAppCDS技术。需要特别说明的是，启动加速的作用是提升应用的启动速度，不会对您的代码逻辑做任何更改，您可以放心使用本工具。\n\n## 启动加速技术简介\n下面对AppCDS和EagerAppCDS启动加速技术做简单的说明。\n\n### CDS\nCDS全称是Class-Data Sharing，其原理是让类可以被预处理放到一个归档文件中，后续Java程序启动时，JVM将这个归档文件映射到内存中，类加载器从内存获取对应的Class数据，避免从文件中读取，以节约应用启动的时间。CDS只能作用于Boot Class Loader加载的类。\n\n### AppCDS\n在JDK 10中，CDS扩展为AppCDS，AppCDS不仅能够作用于Boot Class Loader，也能够对App Class Loader和Custom Class Loader（客户自定义类加载器）起作用，大大增加CDS技术的适用范围。\n\n### EagerAppCDS\nAppCDS技术对于Custom Class Loader加载的类优化效果并不明显。为此阿里JVM团队在Alibaba Dragonwell 11中研发了EagerAppCDS技术，针对特定的Custom Class Loader加载类的过程进行优化，可以进一步降低应用启动时间。\n\n## 本工具原理说明\n当您执行s deploy时，s工具会自动首先执行您配置的pre-deploy，进而调用到本工具。本工具会自动在您的云账号中创建一个辅助函数，这个辅助函数会被调用并生成加速相关的文件，然后会下载加速相关的文件到您的本地，这些文件会随同您的代码一起被部署到云上创建正式函数。  \n\n辅助函数和您定义在s.yaml中的函数相比，initializer完全相同，handler不同。您定义的函数的handler是您的业务逻辑，而辅助函数的handler则是用于生成和下载加速文件相关的逻辑，和您的业务逻辑完全无关。在辅助函数的handler执行之前，您的业务函数的initializer会先执行。为了提升启动加速的效果，建议您把类加载和初始化工作放到initializer中。更具体的概念可以参考函数计算 [官方文档](https://help.aliyun.com/document_detail/157704.htm) \n\n当您调用调用正式函数时，我们会检测您的代码目录中是否存在加速相关文件，如果存在则自动打开启动加速特性；如果代码目录中不存在启动加速相关相关文件，则会以常规方式正常执行，不会对影响您的业务逻辑。\n\n需要说明的是，本工具自动创建辅助函数并调用，会导致额外的费用，价格和正式函数相同。因为仅调用1次或者数次，可以认为费用很低。\n\n流程图如下：  \n![](https://img.alicdn.com/imgextra/i4/O1CN0129uvNW1dDYyZ6Pypc_!!6000000003702-0-tps-691-829.jpg)\n\n## 如何使用？\n目前 本工具 与 [Serverless Devs](https://www.serverless-devs.com/zh-cn) 实现了集成，可以通过 Serverless Devs 的 `s cli` 直接使用，具体步骤如下：\n\n1. 在 `s.yaml` 中的 service actions 中添加 `pre-deploy` ，配置 run 命令为 `s cli pgo gen --lang=java --module=helloworld`。\n![](https://img.alicdn.com/imgextra/i2/O1CN01Mly0DB1p4CH0ESbMz_!!6000000005306-0-tps-1155-816.jpg)\n\n2. 在 `s.yaml` 中的 service actions 中添加 `post-deploy` ，配置 run 命令为 `s cli pgo gen --lang=java --module=helloworld --enable`。\n   ![](https://img.alicdn.com/imgextra/i1/O1CN019ppCa3203hcSwjVSl_!!6000000006794-0-tps-1141-1050.jpg)\n\n3. 将 `s.yaml` 中的 runtime 改为 `java11`，并且修改codeUri为固定值target/artifact\n![](https://img.alicdn.com/imgextra/i1/O1CN0188jlpL21EWajOK0e2_!!6000000006953-0-tps-945-1167.jpg)\n\n4. 在 `s.yaml` 中为`service`配置logConfig和role，便于把函数产生的日志发送到您的SLS Logstore中\n   ![](https://img.alicdn.com/imgextra/i2/O1CN018orbW21GA8r623ARX_!!6000000000581-0-tps-942-1176.jpg)\n\n5. 部署函数  \n部署时（s deploy）会优先使用s build的产物，这些产物存放在项目根目录下的.s目录。由于我们在上文中配置了codeUri为target/artifact，所以为了让s deploy读取到target/artifact目录中的文件，必须删除掉.s目录中的文件和文件夹。如果您没有执行过s build，则无需执行下面的删除命令。\n```shell\nrm -rf .s/*\n```\n执行部署命令\n```shell\ns deploy\n```\n\n6. 调用函数\n- http trigger调用方式\n```shell\ncurl 'curl http://135******1392103.cn-shanghai.fc.aliyuncs.com/2016-08-15/proxy/hello-world-service/http-trigger-java11-springboot/'\n```\n- event trigger调用方式\n```shell\ns cli fc-api invokeFunction --serviceName hello-world-service --functionName http-trigger-java11-springboot --event '{}'\n```\n\n7. 附带一个完整的yaml文件，您可以用此文件快速开始\n```yaml\nedition: 1.0.0\nname: hello-world-app\naccess: \"default\"\n\nvars:\n   service:\n\nservices:\n   helloworld:\n      component: fc\n      actions:\n         pre-deploy:\n            - run: s cli pgo gen --lang=java --module=helloworld --downloader=oss\n         post-deploy:\n            - run: s cli pgo gen --lang=java --module=helloworld --enable\n      props:\n         region: cn-hongkong\n         service:\n            name: springboot-hello-world-service\n            logConfig:\n               project: fc-project-yibo-hongkong\n               logstore: fc-logstore-hongkong\n               enableRequestMetrics: true\n               enableInstanceMetrics: true\n               logBeginRule: DefaultRegex\n            role: acs:ram::1351XXXXXX39XXX3:role/AliyunFCDefaultRole\n         function:\n            name: http-trigger-springboot-demo\n            instanceType: c1\n            description: 'hello world by serverless devs'\n            runtime: java11\n            codeUri: target/artifact\n            handler: com.example.demo.DemoApplication::handleRequest\n            memorySize: 8192\n            timeout: 60\n            initializationTimeout: 60\n            initializer: com.example.demo.DemoApplication::initialize\n            environmentVariables:\n\n         triggers:\n            - name: httpTrigger\n              type: http\n              config:\n                 authType: anonymous\n                 methods:\n                    - GET\n                    - POST\n```\n\n---\n\nAlibaba JVM 团队\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmidwayjs%2Fpgo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmidwayjs%2Fpgo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmidwayjs%2Fpgo/lists"}