Go experiments

This commit is contained in:
Eduard Urbach 2016-10-29 01:11:25 +09:00
parent 6805afaa61
commit 4ed5d14bb5
11 changed files with 100 additions and 52 deletions

5
.gitignore vendored
View File

@ -31,4 +31,7 @@ vendor/
node_modules/
# debugger
debug
debug
# pixy
❖.go

1
layout.css Normal file

File diff suppressed because one or more lines are too long

7
layout/layout.pixy Normal file
View File

@ -0,0 +1,7 @@
component Layout(content string, css string)
html
head
title Test
style!= css
body
main#content

View File

@ -1,6 +0,0 @@
doctype html
html
head
title Test
body
{{ yield }}

View File

@ -1 +0,0 @@
{{ yield }}

51
main.go
View File

@ -6,36 +6,65 @@ import (
"github.com/aerojs/aero"
"github.com/blitzprog/arn"
"github.com/valyala/fasthttp"
)
func main() {
app := aero.New()
example, _ := ioutil.ReadFile("security/frontpage.html")
cssBytes, _ := ioutil.ReadFile("layout.css")
css := string(cssBytes)
css = css
app.Get("/", func(ctx *aero.Context) {
ctx.RespondBytes(example)
ctx.Respond(Stream.Dashboard())
})
app.Get("/hello", func(ctx *aero.Context) {
ctx.Respond("Hello World")
})
template := aero.NewTemplate("pages/anime/anime.pug")
app.Get("/anime/:id", func(ctx *aero.Context) {
id, _ := strconv.Atoi(ctx.Params.ByName("id"))
anime, err := arn.GetAnime(id)
anime = anime
if err != nil {
ctx.Respond("Anime not found")
return
}
templateParams := make(map[string]interface{})
templateParams["anime"] = anime
ctx.Respond(template.Render(templateParams))
stream := fasthttp.AcquireByteBuffer()
Stream.Layout(stream, anime, css)
ctx.RespondBytes(stream.Bytes())
Stream.Release(stream)
// ctx.Respond(Render.Layout(Render.Anime(anime), css))
})
// layout := aero.NewTemplate("layout.pug")
// template := aero.NewTemplate("anime.pug")
// app.Get("/anime/:id", func(ctx *aero.Context) {
// id, _ := strconv.Atoi(ctx.Params.ByName("id"))
// anime, err := arn.GetAnime(id)
// if err != nil {
// ctx.Respond("Anime not found")
// return
// }
// content := template.Render(map[string]interface{}{
// "anime": anime,
// })
// final := layout.Render(map[string]interface{}{
// "content": content,
// })
// final = strings.Replace(final, cssSearch, cssReplace, 1)
// ctx.Respond(final)
// })
// app.Get("/t", func(ctx *aero.Context) {
// ctx.Respond(templates.Hello("abc"))
// })
app.Run()
}

View File

@ -1,33 +1,58 @@
package main
import (
"bytes"
"fmt"
"strings"
"testing"
"unsafe"
)
var code = "Hello World"
var slice = []byte("Hello World 2")
// func BenchmarkRender(b *testing.B) {
// b.ReportAllocs()
// layout := aero.NewTemplate("layout/layout.pug")
// template := aero.NewTemplate("pages/anime/anime.pug")
// cssBytes, _ := ioutil.ReadFile("layout.css")
// css := string(cssBytes)
// cssSearch := "</head><body"
// cssReplace := "<style>" + css + "</style></head><body"
func BenchmarkStringToBytesSafe(b *testing.B) {
// for i := 0; i < b.N; i++ {
// anime, _ := arn.GetAnime(1000001)
// content := template.Render(map[string]interface{}{
// "anime": anime,
// })
// final := layout.Render(map[string]interface{}{
// "content": content,
// })
// final = strings.Replace(final, cssSearch, cssReplace, 1)
// }
// }
func Benchmark1(b *testing.B) {
code := []byte(strings.Repeat("a", 10000))
b.ReportAllocs()
for i := 0; i < b.N; i++ {
slice = []byte(code)
var buf bytes.Buffer
buf.Write(code)
buf.Write(code)
buf.Write(code)
c := buf.String()
fmt.Println(len(c))
}
}
func BenchmarkStringToBytesUnsafe(b *testing.B) {
for i := 0; i < b.N; i++ {
slice = *(*[]byte)(unsafe.Pointer(&code))
}
}
func Benchmark2(b *testing.B) {
code := strings.Repeat("a", 10000)
func BenchmarkBytesToStringSafe(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
code = string(slice)
}
}
func BenchmarkBytesToStringUnsafe(b *testing.B) {
for i := 0; i < b.N; i++ {
code = *(*string)(unsafe.Pointer(&slice))
c := code
c += code
c += code
fmt.Println(len(c))
}
}

3
pages/anime/anime.pixy Normal file
View File

@ -0,0 +1,3 @@
component Anime(anime *arn.Anime)
h2= anime.Title.Romaji
p= anime.Description

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,2 @@
component Dashboard
h1 notify.moe

View File

@ -1 +0,0 @@
h1 notify.moe