首页 > 其他分享 >fasthttp + `page partial gziped cache`: 页面输出服务性能提升20%

fasthttp + `page partial gziped cache`: 页面输出服务性能提升20%

时间:2023-10-10 18:22:48浏览次数:38  
标签:github partial cache ctx gziped html gzip fasthttp com

作者:张富春(ahfuzhang),转载时请注明作者和引用链接,谢谢!


接上一篇:http 中使用 gzip 输出内容时,如何预先压缩前一半页面?

经过实测,对线上一个输出 html 的服务进行了改造,通过预先压缩页面前半部分的方法,此接口的性能提升了 20%.

对比项 无 gzip 压缩 gzip 压缩+前半部分预压缩
输出字节数 4399 2246
每核 qps 14052.63 16924.75

具体的写法如下:
1.获取改造后的库

go get github.com/ahfuzhang/[email protected]

2.在 go.mod 中修改:

replace (
	github.com/klauspost/compress => github.com/ahfuzhang/compress v1.17.2
)

require (
	github.com/klauspost/compress v1.16.3
    github.com/valyala/bytebufferpool v1.0.0
	github.com/valyala/fasthttp v1.50.0
)

3.代码:

package main

import (
	"bytes"
	_ "embed"
	"fmt"
	"log"
	"os"

	"github.com/klauspost/compress/gzip"
	"github.com/valyala/bytebufferpool"
	"github.com/valyala/fasthttp"
)

//go:embed raw.html
var html string

//go:embed raw.js
var js string


func testGzipedHttp() {
    topHalf, digest := gzip.GetGzipedData([]byte(html))  // cache 页面的前一半, digest 是这些内容的 crc32 的校验和
	requestHandler := func(ctx *fasthttp.RequestCtx) {
		ctx.Response.Header.Add("Content-Type", "text/plain")
		ctx.Response.Header.Add("Content-Encoding", "gzip")

		switch string(ctx.Request.URI().RequestURI()) {
		case "/1":   // direct output
			w, _ := gzip.NewWriterLevel(ctx, gzip.BestCompression)
			w.Write([]byte(html))
			w.Write([]byte(js))
			w.Close()
		case "/2":
			w := gzip.GetWriter(ctx)  // 使用对象池
		    w.WriteHeader()  // 写 gzip 的头部信息,10 字节
            w.WriteGzipedData([]byte(html), topHalf, digest)  
            // 当这个缓存是第一次输出的时候,可以传入 digest 值,这样可以少算一次  crc32
            // 当不是第一次输出的时候, 第三个参数 digest 填  0

            w.Write([]byte(js))
            gzip.PutWriter(w)  // 必须调用,写入尾部信息,并放回对象池
		}
	}

	s := &fasthttp.Server{
		Handler: requestHandler,
	}
	if err := s.ListenAndServe(":8080"); err != nil {
		log.Fatalf("error in ListenAndServe: %v", err)
	}
}

func main() {
	testGzipedHttp()
}

希望对你有用

标签:github,partial,cache,ctx,gziped,html,gzip,fasthttp,com
From: https://www.cnblogs.com/ahfuzhang/p/17755400.html

相关文章

  • Direct Component/SEDA Component/VM Component/Timer Component/Log Component/Cache
    http://camel.apache.org/direct.htmlDirectComponentThedirect:componentprovidesdirect,synchronousinvocationofanyconsumerswhenaproducersendsamessageexchange.Thisendpointcanbeusedtoconnectexistingroutesinthesamecamelcontext.Asy......
  • Application Cache HTML
    主要是加速离线存储,Web开发者可借助微信提供的资源存储能力,直接从地加载Web资源而不需要再从服务端拉取,从而减少网页加载时间,为微信用户提供更优质的网页浏览体验使用方式example.appcacheCACHEMANIFEST#版本号或注释CACHE:index.htmlstyles.cssapp.jsNETWO......
  • MMU和cache详解(TLB机制)
    MMU和cache详解(TLB机制)南方铁匠于2017-07-2110:53:59发布10013收藏50分类专栏:计算机体系结构版权计算机体系结构专栏收录该内容53篇文章12订阅订阅专栏1.MMUMMU:memorymanagementunit,称为内存管理单元,或者是存储器管理单元,MMU是硬件设备,它被保存在主存(mainm......
  • NetCore Ocelot 之 Cache
    OcelotsupportssomeveryrudimentarycachingatthemomentproviderbytheCacheManagerproject.Thissanamazingprojectthatissolvingalotofcachingproblems.IwouldrecommendusingthispackagetocachewithOcelot.Thefollowingexampleshowsh......
  • laravel8对接阿里云sdk刷新cdn缓存接口RefreshObjectCaches
    <?phpnamespaceApp\Admin\Forms;useEncore\Admin\Widgets\Form;useIlluminate\Http\Request;useAlibabaCloud\Client\AlibabaCloud;useAlibabaCloud\Client\Exception\ClientException;useAlibabaCloud\Client\Exception\ServerException;......
  • 简述memcached的工作原理
     Memcached只支持能序列化的数据类型,不支持持久化,基于Key-Value的内存缓存系统1.内存分配机制 应用程序运行需要使用内存存储数据,但对于一个缓存系统来说,申请内存、释放内存将十分频繁,非常容易导致大量内存碎片,最后导致无连续可用内存可用。 Memcached采用了Slab......
  • django-celery-results - 使用 Django ORM/Cache 作为结果后端
    https://docs.celeryq.dev/en/stable/django/first-steps-with-django.html#django-celery-results-using-the-django-orm-cache-as-a-result-backend这个一般自己设置一下result_backend也行,要用django-celery-results也是一个选择。......
  • Go每日一库之180:fastcache(协程安全且支持大量数据存储的高性能缓存库)
    fastcache是一个线程安全并且支持大量数据存储的高性能缓存组件库。这是官方Github主页上的项目介绍,和fasthttp名字一样以fast打头,作者对项目代码的自信程度可见一斑。此外该库的核心代码非常轻量,笔者本着学习的目的分析下内部的代码实现。基准测试官方给出了fastca......
  • gitlab--在 k8s 里通过 helm 部署 runner、使用缓存 cache、使用制品 artifacts
    安装helm链接:https://www.cnblogs.com/zouzou-busy/p/16134885.html配置chart存储库#添加chart存储库[root@master1~]#helmrepoaddgitlabhttps://charts.gitlab.io"gitlab"hasbeenaddedtoyourrepositories#查看存储库[root@master1~]#helmrepolist......
  • cache
    title:关于缓存tags:[缓存,Cache]categories:湿垃圾keywords:缓存,cache,缓存击穿,缓存雪崩,缓存穿透description:关于缓存的知识repost:truedate:2020-10-0715:14:13{%notewarningno-icon%}缓存的一些知识收集介绍前端缓存一般是指一个资源(如html、css......