Skip to content

Instantly share code, notes, and snippets.

@bbengfort
Last active April 3, 2023 17:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bbengfort/fed9f92142b31b0261fc71fdb0d168a5 to your computer and use it in GitHub Desktop.
Save bbengfort/fed9f92142b31b0261fc71fdb0d168a5 to your computer and use it in GitHub Desktop.
Murmur3 comparison and test fixtures
#!/usr/bin/env python3
import mmh3
import json
import base64
def topic_hash(name):
hash = mmh3.hash128(name.encode('utf-8'), signed=False).to_bytes(16, byteorder='big', signed=False)
hash = hash[8:] + hash[:8]
return base64.urlsafe_b64encode(hash).decode('utf-8').strip("=")
def main():
with open("topichash.json", "r") as f:
fixture = json.load(f)
for name, expected in fixture.items():
actual = topic_hash(name)
if actual == expected:
print(name, expected, actual)
if __name__ == "__main__":
main()
package main
import (
"encoding/base64"
"encoding/json"
"fmt"
"log"
"os"
"github.com/spaolacci/murmur3"
)
var topicNames = []string{
"testing.topics.topica",
"testing.topics.topicb",
"testing.topics.topicc",
"noaa-alerts",
"chocolate-covered-espresso-beans",
}
func topicHash(topicName string) string {
hash := murmur3.New128()
hash.Write([]byte(topicName))
return base64.RawURLEncoding.EncodeToString(hash.Sum(nil))
}
func checkTopicHash(topicHash string) error {
sum, err := base64.RawURLEncoding.DecodeString(topicHash)
if err != nil {
return err
}
if len(sum) != 16 {
return fmt.Errorf("expected length 16 sum not length %d", len(sum))
}
return nil
}
func main() {
fixture := make(map[string]string)
for _, name := range topicNames {
fixture[name] = topicHash(name)
if err := checkTopicHash(fixture[name]); err != nil {
log.Fatal(err)
}
}
// Write fixture to disk
f, err := os.Create("topichash.json")
if err != nil {
log.Fatal(err)
}
defer f.Close()
encoder := json.NewEncoder(f)
encoder.SetIndent("", " ")
if err := encoder.Encode(fixture); err != nil {
log.Fatal(err)
}
}
{
"chocolate-covered-espresso-beans": "cLHSo2nCBxyOezviLM5gwg",
"noaa-alerts": "MRpILJErB6vKZROR6H9JiA",
"testing.topics.topica": "IdTcxHgZmlLM8oBj_YytLw",
"testing.topics.topicb": "F7x4fhbO4EhHVNDmBjMRIQ",
"testing.topics.topicc": "YuxuzM--ndLwlQX0kqnOBw"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment